-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
pubCommonId_spec.js
370 lines (315 loc) · 11 KB
/
pubCommonId_spec.js
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import {
requestBidHook,
getCookie,
setCookie,
setConfig,
isPubcidEnabled,
getExpInterval,
initPubcid,
setStorageItem,
getStorageItem,
removeStorageItem,
getPubcidConfig } from 'modules/pubCommonId.js';
import { getAdUnits } from 'test/fixtures/fixtures.js';
import * as auctionModule from 'src/auction.js';
import { registerBidder } from 'src/adapters/bidderFactory.js';
import * as utils from 'src/utils.js';
let events = require('src/events');
let constants = require('src/constants.json');
var assert = require('chai').assert;
var expect = require('chai').expect;
const ID_NAME = '_pubcid';
const EXP = '_exp';
const TIMEOUT = 2000;
const uuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89a-f][0-9a-f]{3}-[0-9a-f]{12}$/;
function cleanUp() {
window.document.cookie = ID_NAME + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
localStorage.removeItem(ID_NAME);
localStorage.removeItem(ID_NAME + EXP);
}
describe('Publisher Common ID', function () {
afterEach(function () {
$$PREBID_GLOBAL$$.requestBids.removeAll();
});
describe('Decorate adUnits', function () {
beforeEach(function() {
cleanUp();
});
afterEach(function() {
cleanUp();
});
it('Check same cookie', function () {
let adUnits1 = getAdUnits();
let adUnits2 = getAdUnits();
let innerAdUnits1;
let innerAdUnits2;
let pubcid;
expect(getCookie(ID_NAME)).to.be.null; // there should be no cookie initially
expect(localStorage.getItem(ID_NAME)).to.be.null; // there should be no local storage item either
requestBidHook((config) => { innerAdUnits1 = config.adUnits }, {adUnits: adUnits1});
pubcid = localStorage.getItem(ID_NAME); // local storage item is created after requestbidHook
innerAdUnits1.forEach((unit) => {
unit.bids.forEach((bid) => {
expect(bid).to.have.deep.nested.property('crumbs.pubcid');
expect(bid.crumbs.pubcid).to.equal(pubcid);
});
});
// verify cookie is null
expect(getCookie(ID_NAME)).to.be.null;
// verify same pubcid is preserved
requestBidHook((config) => { innerAdUnits2 = config.adUnits }, {adUnits: adUnits2});
assert.deepEqual(innerAdUnits1, innerAdUnits2);
});
it('Check different cookies', function () {
let adUnits1 = getAdUnits();
let adUnits2 = getAdUnits();
let innerAdUnits1;
let innerAdUnits2;
let pubcid1;
let pubcid2;
requestBidHook((config) => { innerAdUnits1 = config.adUnits }, {adUnits: adUnits1});
pubcid1 = localStorage.getItem(ID_NAME); // get first pubcid
removeStorageItem(ID_NAME); // remove storage
expect(pubcid1).to.not.be.null;
innerAdUnits1.forEach((unit) => {
unit.bids.forEach((bid) => {
expect(bid).to.have.deep.nested.property('crumbs.pubcid');
expect(bid.crumbs.pubcid).to.equal(pubcid1);
});
});
requestBidHook((config) => { innerAdUnits2 = config.adUnits }, {adUnits: adUnits2});
pubcid2 = localStorage.getItem(ID_NAME); // get second pubcid
innerAdUnits2.forEach((unit) => {
unit.bids.forEach((bid) => {
expect(bid).to.have.deep.nested.property('crumbs.pubcid');
expect(bid.crumbs.pubcid).to.equal(pubcid2);
});
});
expect(pubcid2).to.not.be.null;
expect(pubcid1).to.not.equal(pubcid2);
});
it('Check new cookie', function () {
let adUnits = getAdUnits();
let innerAdUnits;
let pubcid = utils.generateUUID();
setCookie(ID_NAME, pubcid, 600);
requestBidHook((config) => { innerAdUnits = config.adUnits }, {adUnits});
innerAdUnits.forEach((unit) => {
unit.bids.forEach((bid) => {
expect(bid).to.have.deep.nested.property('crumbs.pubcid');
expect(bid.crumbs.pubcid).to.equal(pubcid);
});
});
});
it('Replicate cookie to storage', function() {
let adUnits = getAdUnits();
let innerAdUnits;
let pubcid = utils.generateUUID();
setCookie(ID_NAME, pubcid, 600);
requestBidHook((config) => { innerAdUnits = config.adUnits }, {adUnits});
expect(getStorageItem(ID_NAME)).to.equal(pubcid);
});
it('Does not replicate storage to cookie', function() {
let adUnits = getAdUnits();
let innerAdUnits;
let pubcid = utils.generateUUID();
setStorageItem(ID_NAME, pubcid, 600);
requestBidHook((config) => { innerAdUnits = config.adUnits }, {adUnits});
expect(getCookie(ID_NAME)).to.be.null;
});
it('Cookie only', function() {
setConfig({type: 'cookie'});
let adUnits = getAdUnits();
let innerAdUnits;
requestBidHook((config) => { innerAdUnits = config.adUnits }, {adUnits});
expect(getCookie(ID_NAME)).to.match(uuidPattern);
expect(getStorageItem(ID_NAME)).to.be.null;
});
it('Storage only', function() {
setConfig({type: 'html5'});
let adUnits = getAdUnits();
let innerAdUnits;
requestBidHook((config) => { innerAdUnits = config.adUnits }, {adUnits});
expect(getCookie(ID_NAME)).to.be.null;
expect(getStorageItem(ID_NAME)).to.match(uuidPattern);
});
it('Bad id recovery', function() {
let adUnits = getAdUnits();
let innerAdUnits;
setStorageItem(ID_NAME, 'undefined', 600);
requestBidHook((config) => { innerAdUnits = config.adUnits }, {adUnits});
expect(getStorageItem(ID_NAME)).to.match(uuidPattern);
});
});
describe('Configuration', function () {
beforeEach(() => {
setConfig();
cleanUp();
});
afterEach(() => {
setConfig();
cleanUp();
});
it('empty config', function () {
// this should work as usual
setConfig({});
let adUnits = getAdUnits();
let innerAdUnits;
requestBidHook((config) => { innerAdUnits = config.adUnits }, {adUnits});
let pubcid = localStorage.getItem(ID_NAME);
innerAdUnits.forEach((unit) => {
unit.bids.forEach((bid) => {
expect(bid).to.have.deep.nested.property('crumbs.pubcid');
expect(bid.crumbs.pubcid).to.equal(pubcid);
});
});
});
it('disable', function () {
setConfig({enable: false});
let adUnits = getAdUnits();
let unmodified = getAdUnits();
let innerAdUnits;
expect(isPubcidEnabled()).to.be.false;
requestBidHook((config) => { innerAdUnits = config.adUnits }, {adUnits});
expect(getCookie(ID_NAME)).to.be.null;
assert.deepEqual(innerAdUnits, unmodified);
setConfig({enable: true}); // reset
requestBidHook((config) => { innerAdUnits = config.adUnits }, {adUnits});
innerAdUnits.forEach((unit) => {
unit.bids.forEach((bid) => {
expect(bid).to.have.deep.nested.property('crumbs.pubcid');
});
});
});
it('change expiration time', function () {
setConfig({expInterval: 100});
expect(getExpInterval()).to.equal(100);
let adUnits = getAdUnits();
let innerAdUnits;
requestBidHook((config) => { innerAdUnits = config.adUnits }, {adUnits});
innerAdUnits.every((unit) => {
unit.bids.forEach((bid) => {
expect(bid).to.have.deep.nested.property('crumbs.pubcid');
});
});
});
it.skip('disable auto create', function() {
setConfig({
create: false
});
const config = getPubcidConfig();
expect(config.create).to.be.false;
expect(config.typeEnabled).to.equal('html5');
let adUnits = getAdUnits();
let innerAdUnits;
requestBidHook((config) => { innerAdUnits = config.adUnits }, {adUnits});
const pubcid = localStorage.getItem(ID_NAME);
expect(pubcid).to.be.null;
});
});
describe('Invoking requestBid', function () {
let createAuctionStub;
let adUnits;
let adUnitCodes;
let capturedReqs;
let sampleSpec = {
code: 'sampleBidder',
isBidRequestValid: () => {},
buildRequest: (reqs) => {},
interpretResponse: () => {},
getUserSyncs: () => {}
};
beforeEach(function () {
adUnits = [{
code: 'adUnit-code',
mediaTypes: {
banner: {},
native: {},
},
sizes: [[300, 200], [300, 600]],
bids: [
{bidder: 'sampleBidder', params: {placementId: 'banner-only-bidder'}}
]
}];
adUnitCodes = ['adUnit-code'];
let auction = auctionModule.newAuction({adUnits, adUnitCodes, callback: function() {}, cbTimeout: TIMEOUT});
createAuctionStub = sinon.stub(auctionModule, 'newAuction');
createAuctionStub.returns(auction);
initPubcid();
registerBidder(sampleSpec);
});
afterEach(function () {
auctionModule.newAuction.restore();
});
it('test hook', function() {
$$PREBID_GLOBAL$$.requestBids({adUnits});
adUnits.forEach((unit) => {
unit.bids.forEach((bid) => {
expect(bid).to.have.deep.nested.property('crumbs.pubcid');
});
});
});
});
describe('Storage item functions', () => {
beforeEach(() => { cleanUp(); });
afterEach(() => { cleanUp(); });
it('Test set', () => {
const key = ID_NAME;
const val = 'test-set-value';
// Set item in localStorage
const now = Date.now();
setStorageItem(key, val, 100);
// Check both item and expiry time are stored
const expVal = localStorage.getItem(key + EXP);
const storedVal = localStorage.getItem(key);
// Verify expiry
expect(expVal).to.not.be.null;
const expDate = new Date(expVal);
expect((expDate.getTime() - now) / 1000).to.be.closeTo(100 * 60, 5);
// Verify value
expect(storedVal).to.equal(val);
});
it('Test get and remove', () => {
const key = ID_NAME;
const val = 'test-get-remove';
setStorageItem(key, val, 10);
expect(getStorageItem(key)).to.equal(val);
removeStorageItem(key);
expect(getStorageItem(key)).to.be.null;
});
it('Test expiry', () => {
const key = ID_NAME;
const val = 'test-expiry';
setStorageItem(key, val, -1);
expect(localStorage.getItem(key)).to.equal(val);
expect(getStorageItem(key)).to.be.null;
expect(localStorage.getItem(key)).to.be.null;
});
});
describe('event callback', () => {
beforeEach(() => {
setConfig();
cleanUp();
sinon.stub(events, 'getEvents').returns([]);
sinon.stub(utils, 'triggerPixel');
});
afterEach(() => {
setConfig();
cleanUp();
events.getEvents.restore();
utils.triggerPixel.restore();
});
it('auction end trigger', () => {
setConfig({
pixelUrl: '/any/url'
});
let adUnits = getAdUnits();
let innerAdUnits;
requestBidHook((config) => { innerAdUnits = config.adUnits }, {adUnits});
expect(utils.triggerPixel.called).to.be.false;
events.emit(constants.EVENTS.AUCTION_END, {});
expect(utils.triggerPixel.called).to.be.true;
expect(utils.triggerPixel.getCall(0).args[0]).to.include('/any/url');
});
});
});