forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabtshieldIdSystem_spec.js
More file actions
312 lines (256 loc) · 11.3 KB
/
Copy pathabtshieldIdSystem_spec.js
File metadata and controls
312 lines (256 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import { abtshieldIdSubmodule, parseMcrResponse } from 'modules/abtshieldIdSystem.js';
import { server } from 'test/mocks/xhr.js';
import { createEidsArray } from '../../../modules/userId/eids.js';
import { attachIdSystem } from '../../../modules/userId/index.js';
import { config } from '../../../src/config.js';
import { expect } from 'chai';
import '../../../modules/allowActivities.js';
const MODULE_NAME = 'abtshieldId';
const STORAGE = { type: 'html5', name: 'abtshield_id', expires: 1 };
describe('abtshieldIdSystem', function () {
describe('name', function () {
it('should expose the module name', function () {
expect(abtshieldIdSubmodule.name).to.equal(MODULE_NAME);
});
});
describe('gvlid', function () {
it('should expose vendor id 825', function () {
expect(abtshieldIdSubmodule.gvlid).to.equal(825);
});
});
// parseMcrResponse
describe('parseMcrResponse', function () {
it('returns null for empty input', function () {
expect(parseMcrResponse(null)).to.be.null;
expect(parseMcrResponse('')).to.be.null;
expect(parseMcrResponse(undefined)).to.be.null;
});
it('returns null when uuid is missing', function () {
expect(parseMcrResponse('{"t":["seg1"]}')).to.be.null;
});
it('returns null when uuid is empty string', function () {
expect(parseMcrResponse('{"uuid":""}')).to.be.null;
});
it('returns null for malformed JSON', function () {
expect(parseMcrResponse('not json')).to.be.null;
});
it('returns object with uuid from iuid field (actual API response field)', function () {
const result = parseMcrResponse('{"scr":10000,"iuid":"abc-123","b":-1,"t":-1}');
expect(result).to.deep.equal({ uuid: 'abc-123' });
});
it('returns object with uuid from uuid field (fallback)', function () {
const result = parseMcrResponse('{"uuid":"abc-123"}');
expect(result).to.deep.equal({ uuid: 'abc-123' });
});
it('prefers iuid over uuid when both present', function () {
const result = parseMcrResponse('{"iuid":"from-iuid","uuid":"from-uuid"}');
expect(result).to.deep.equal({ uuid: 'from-iuid' });
});
it('returns object with uuid and segments when present', function () {
const result = parseMcrResponse('{"uuid":"abc-123","t":["seg1","seg2"]}');
expect(result).to.deep.equal({ uuid: 'abc-123', segments: ['seg1', 'seg2'] });
});
it('adds sivt segment when b is 1', function () {
const result = parseMcrResponse('{"uuid":"abc-123","b":1}');
expect(result).to.deep.equal({ uuid: 'abc-123', segments: ['sivt'] });
});
it('adds sivt segment to t segments when b is 1', function () {
const result = parseMcrResponse('{"uuid":"abc-123","b":1,"t":["foo"]}');
expect(result).to.deep.equal({ uuid: 'abc-123', segments: ['foo', 'sivt'] });
});
it('does not duplicate sivt when already present in t segments', function () {
const result = parseMcrResponse('{"uuid":"abc-123","b":1,"t":["foo","sivt"]}');
expect(result).to.deep.equal({ uuid: 'abc-123', segments: ['foo', 'sivt'] });
});
it('omits segments when the t array is empty', function () {
const result = parseMcrResponse('{"uuid":"abc-123","t":[]}');
expect(result).to.deep.equal({ uuid: 'abc-123' });
expect(result).to.not.have.property('segments');
});
it('filters non-string entries from the t array', function () {
const result = parseMcrResponse('{"uuid":"abc-123","t":[1,"seg1",null,"seg2"]}');
expect(result.segments).to.deep.equal(['seg1', 'seg2']);
});
it('accepts an already-parsed object', function () {
const result = parseMcrResponse({ uuid: 'abc-123', t: ['seg1'] });
expect(result).to.deep.equal({ uuid: 'abc-123', segments: ['seg1'] });
});
});
// getId
describe('getId', function () {
beforeEach(function () {
server.requests.length = 0;
});
afterEach(function () {
sinon.restore();
config.resetConfig();
});
it('returns undefined when storage.expires is below the 1-day floor', function () {
const result = abtshieldIdSubmodule.getId({
params: { sid: 'pb.publisher-x' },
storage: { type: 'html5', name: 'abtshield_id', expires: 0.5 }
});
expect(result).to.be.undefined;
expect(server.requests).to.have.length(0);
});
it('returns undefined when storage.expires is not a number', function () {
const result = abtshieldIdSubmodule.getId({
params: { sid: 'pb.publisher-x' },
storage: { type: 'html5', name: 'abtshield_id', expires: '1' }
});
expect(result).to.be.undefined;
expect(server.requests).to.have.length(0);
});
it('returns undefined when storage.refreshInSeconds is below the 1-day floor', function () {
const result = abtshieldIdSubmodule.getId({
params: { sid: 'pb.publisher-x' },
storage: { type: 'html5', name: 'abtshield_id', expires: 1, refreshInSeconds: 1 }
});
expect(result).to.be.undefined;
expect(server.requests).to.have.length(0);
});
it('returns undefined and skips the request when params.sid is absent', function () {
const result = abtshieldIdSubmodule.getId({ storage: STORAGE });
expect(result).to.be.undefined;
expect(server.requests).to.have.length(0);
});
it('returns undefined and skips the request when params.sid is blank', function () {
const result = abtshieldIdSubmodule.getId({ storage: STORAGE, params: { sid: ' ' } });
expect(result).to.be.undefined;
expect(server.requests).to.have.length(0);
});
it('calls the default MCR endpoint with the provided sid', function () {
const cb = sinon.spy();
const { callback } = abtshieldIdSubmodule.getId({ storage: STORAGE, params: { sid: 'pb.publisher-x' } });
callback(cb);
const [req] = server.requests;
expect(req.method).to.equal('GET');
expect(req.url).to.equal('https://d1.abtshield.com/mcr?sid=pb.publisher-x');
expect(req.withCredentials).to.be.true;
});
it('scopes credential access to the abtshieldId component', function () {
config.setConfig({
allowActivities: {
accessRequestCredentials: {
rules: [{
condition({ componentType, componentName }) {
return componentType === 'userId' && componentName === MODULE_NAME;
},
allow: false
}]
}
}
});
const cb = sinon.spy();
const { callback } = abtshieldIdSubmodule.getId({ storage: STORAGE, params: { sid: 'pb.publisher-x' } });
callback(cb);
const [req] = server.requests;
expect(req.withCredentials).to.be.false;
});
it('trims whitespace from sid before using it in the URL', function () {
const cb = sinon.spy();
const { callback } = abtshieldIdSubmodule.getId({ storage: STORAGE, params: { sid: ' pb.publisher-x ' } });
callback(cb);
const [req] = server.requests;
expect(req.url).to.equal('https://d1.abtshield.com/mcr?sid=pb.publisher-x');
});
it('invokes callback with the parsed value on success', function () {
const cb = sinon.spy();
const { callback } = abtshieldIdSubmodule.getId({ storage: STORAGE, params: { sid: 'pb.publisher-x' } });
callback(cb);
server.requests[0].respond(
200,
{ 'Content-Type': 'application/json' },
JSON.stringify({ scr: 10000, iuid: 'test-uuid', b: -1, t: -1 })
);
expect(cb.calledOnce).to.be.true;
expect(cb.firstCall.args[0]).to.deep.equal({ uuid: 'test-uuid' });
});
it('invokes callback with sivt segment when b is 1', function () {
const cb = sinon.spy();
const { callback } = abtshieldIdSubmodule.getId({ storage: STORAGE, params: { sid: 'pb.publisher-x' } });
callback(cb);
server.requests[0].respond(
200,
{ 'Content-Type': 'application/json' },
JSON.stringify({ scr: 8000, iuid: 'test-uuid', b: 1, t: ['foo'] })
);
expect(cb.calledOnce).to.be.true;
expect(cb.firstCall.args[0]).to.deep.equal({ uuid: 'test-uuid', segments: ['foo', 'sivt'] });
});
it('invokes callback with undefined when response has no uuid', function () {
const cb = sinon.spy();
const { callback } = abtshieldIdSubmodule.getId({ storage: STORAGE, params: { sid: 'pb.publisher-x' } });
callback(cb);
server.requests[0].respond(
200,
{ 'Content-Type': 'application/json' },
JSON.stringify({ foo: 'bar' })
);
expect(cb.calledOnce).to.be.true;
expect(cb.firstCall.args[0]).to.be.undefined;
});
it('invokes callback with undefined on request error', function () {
const cb = sinon.spy();
const { callback } = abtshieldIdSubmodule.getId({ storage: STORAGE, params: { sid: 'pb.publisher-x' } });
callback(cb);
server.requests[0].error();
expect(cb.calledOnce).to.be.true;
expect(cb.firstCall.args[0]).to.be.undefined;
});
});
// decode
describe('decode', function () {
it('returns undefined for falsy input', function () {
expect(abtshieldIdSubmodule.decode(null)).to.be.undefined;
expect(abtshieldIdSubmodule.decode(undefined)).to.be.undefined;
expect(abtshieldIdSubmodule.decode('')).to.be.undefined;
});
it('returns undefined when object has no uuid', function () {
expect(abtshieldIdSubmodule.decode({ segments: ['seg1'] })).to.be.undefined;
});
it('decodes a uuid-only object', function () {
const result = abtshieldIdSubmodule.decode({ uuid: 'abc-123' });
expect(result).to.deep.equal({ [MODULE_NAME]: { uuid: 'abc-123' } });
});
it('decodes an object with uuid and segments', function () {
const result = abtshieldIdSubmodule.decode({ uuid: 'abc-123', segments: ['s1', 's2'] });
expect(result).to.deep.equal({ [MODULE_NAME]: { uuid: 'abc-123', segments: ['s1', 's2'] } });
});
it('returns a deep clone so callers cannot mutate cached state', function () {
const value = { uuid: 'abc-123', segments: ['s1'] };
const result = abtshieldIdSubmodule.decode(value);
result[MODULE_NAME].segments.push('mutated');
expect(value.segments).to.deep.equal(['s1']);
});
});
// EID output
describe('eid', function () {
before(function () {
attachIdSystem(abtshieldIdSubmodule);
});
it('produces a valid EID without segments', function () {
const userId = { [MODULE_NAME]: { uuid: 'some-uuid' } };
const eids = createEidsArray(userId);
expect(eids).to.have.length(1);
expect(eids[0]).to.deep.equal({
source: 'abtshield.com',
uids: [{ id: 'some-uuid', atype: 1 }]
});
});
it('produces a valid EID with segments in uid ext', function () {
const userId = { [MODULE_NAME]: { uuid: 'some-uuid', segments: ['seg1', 'seg2'] } };
const eids = createEidsArray(userId);
expect(eids).to.have.length(1);
expect(eids[0]).to.deep.equal({
source: 'abtshield.com',
uids: [{ id: 'some-uuid', atype: 1, ext: { segments: ['seg1', 'seg2'] } }]
});
});
it('omits uid ext when segments array is empty', function () {
const userId = { [MODULE_NAME]: { uuid: 'some-uuid', segments: [] } };
const eids = createEidsArray(userId);
expect(eids[0].uids[0]).to.not.have.property('ext');
});
});
});