Repository navigation
Expand file tree
/
Copy pathad_spiroBidAdapter_spec.js
More file actions
479 lines (418 loc) · 20.6 KB
/
Copy pathad_spiroBidAdapter_spec.js
File metadata and controls
479 lines (418 loc) · 20.6 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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
import { expect } from 'chai';
import { spec } from '../../../modules/ad_spiroBidAdapter.ts';
import { setConfig as setCurrencyConfig } from '../../../modules/currency.js';
import '../../../modules/priceFloors.js';
import { dep } from '../../../src/ajax.js';
import * as utils from '../../../src/utils.js';
import { server } from '../../mocks/xhr.js';
const AUCTION_URL = 'https://rtb.adspiro.io/pbjs';
const IFRAME_SYNC_URL = 'https://rtb.adspiro.io/u/iframe';
const IMAGE_SYNC_URL = 'https://rtb.adspiro.io/u/sync';
const VIDEO = { context: 'instream', playerSize: [640, 480], mimes: ['video/mp4'], protocols: [2, 3, 5, 6] };
const AUDIO = { context: 'instream', mimes: ['audio/mp4', 'audio/mpeg'], minduration: 5, maxduration: 30 };
const NATIVE_REQUEST = {
ver: '1.2',
assets: [
{ id: 1, required: 1, title: { len: 90 } },
{ id: 2, required: 1, img: { type: 3, w: 1200, h: 627 } },
{ id: 3, required: 1, data: { type: 1 } },
],
};
function makeBid(overrides = {}) {
return {
bidder: 'ad_spiro',
bidId: 'bid-1',
adUnitCode: 'div-1',
auctionId: 'auction-1',
bidderRequestId: 'bidder-request-1',
params: { publisherId: 'pub-1' },
mediaTypes: { banner: { sizes: [[300, 250], [728, 90]] } },
...overrides,
};
}
function buildRequests(bids, bidderRequestOverrides = {}) {
return spec.buildRequests(bids, {
bidderCode: 'ad_spiro',
auctionId: 'auction-1',
bidderRequestId: 'bidder-request-1',
timeout: 1500,
bids,
ortb2: {},
...bidderRequestOverrides,
});
}
function ortbResponse(bids, extra = { cur: 'USD' }) {
return { body: { id: 'response-1', ...extra, seatbid: [{ seat: 'adspiro', bid: bids }] } };
}
describe('ad_spiroBidAdapter', function () {
let sandbox;
beforeEach(function () {
sandbox = sinon.createSandbox();
});
afterEach(function () {
sandbox.restore();
});
describe('spec', function () {
it('registers the ad_spiro bidder for every media type, without a GVL ID or aliases', function () {
expect(spec.code).to.equal('ad_spiro');
expect(spec.supportedMediaTypes).to.deep.equal(['banner', 'video', 'native', 'audio']);
expect(spec).to.not.have.property('gvlid');
expect(spec).to.not.have.property('aliases');
});
});
describe('isBidRequestValid', function () {
it('accepts a non-empty string publisherId', function () {
expect(spec.isBidRequestValid(makeBid())).to.equal(true);
expect(spec.isBidRequestValid(makeBid({ params: { publisherId: 'test' } }))).to.equal(true);
});
it('rejects a bid without params', function () {
expect(spec.isBidRequestValid(makeBid({ params: undefined }))).to.equal(false);
expect(spec.isBidRequestValid(undefined)).to.equal(false);
});
it('rejects a missing publisherId', function () {
expect(spec.isBidRequestValid(makeBid({ params: {} }))).to.equal(false);
});
it('rejects an empty publisherId', function () {
expect(spec.isBidRequestValid(makeBid({ params: { publisherId: '' } }))).to.equal(false);
});
it('rejects a publisherId that is not a string', function () {
[123, null, true, ['pub-1'], { id: 'pub-1' }].forEach((publisherId) => {
expect(spec.isBidRequestValid(makeBid({ params: { publisherId } }))).to.equal(false);
});
});
});
describe('buildRequests', function () {
it('sends one POST to the Prebid.js endpoint with the publisherId in the URL', function () {
const requests = buildRequests([makeBid()]);
expect(requests).to.have.lengthOf(1);
expect(requests[0].method).to.equal('POST');
expect(requests[0].url).to.equal(`${AUCTION_URL}?pid=pub-1`);
});
it('asks for gzip and keeps the default text/plain body and credentials', function () {
const [request] = buildRequests([makeBid()]);
expect(request.options).to.deep.equal({ endpointCompression: true });
});
it('URL-encodes the publisherId', function () {
const [request] = buildRequests([makeBid({ params: { publisherId: 'a b&c/d?e' } })]);
expect(request.url).to.equal(`${AUCTION_URL}?pid=a%20b%26c%2Fd%3Fe`);
});
it('sends one request per publisherId, in first-seen order', function () {
const requests = buildRequests([
makeBid({ bidId: 'bid-1', params: { publisherId: '407' } }),
makeBid({ bidId: 'bid-2', params: { publisherId: '92' } }),
makeBid({ bidId: 'bid-3', params: { publisherId: '407' } }),
]);
expect(requests.map((request) => request.url)).to.deep.equal([`${AUCTION_URL}?pid=407`, `${AUCTION_URL}?pid=92`]);
expect(requests[0].data.imp.map((imp) => imp.id)).to.deep.equal(['bid-1', 'bid-3']);
expect(requests[1].data.imp.map((imp) => imp.id)).to.deep.equal(['bid-2']);
});
it('builds a banner imp with every ad unit size', function () {
const [request] = buildRequests([makeBid()]);
expect(request.data.imp).to.have.lengthOf(1);
expect(request.data.imp[0].id).to.equal('bid-1');
expect(request.data.imp[0].secure).to.equal(1);
expect(request.data.imp[0].banner.format).to.deep.equal([{ w: 300, h: 250 }, { w: 728, h: 90 }]);
});
it('sets tmax from the bidder timeout', function () {
const [request] = buildRequests([makeBid()], { timeout: 800 });
expect(request.data.tmax).to.equal(800);
});
it('passes first-party data, user IDs, schain and privacy signals through', function () {
const ortb2 = {
site: { page: 'https://news.example/article', domain: 'news.example' },
user: { ext: { eids: [{ source: 'pubcid.org', uids: [{ id: 'uid-1', atype: 1 }] }] } },
source: { ext: { schain: { ver: '1.0', complete: 1, nodes: [{ asi: 'news.example', sid: 'pub-1', hp: 1 }] } } },
regs: { coppa: 1, gpp: 'DBABTA~1YNN', gpp_sid: [7], ext: { us_privacy: '1YNN', gpc: '1' } },
};
const [request] = buildRequests([makeBid()], { ortb2 });
expect(request.data.site).to.deep.include(ortb2.site);
expect(request.data.user.ext.eids).to.deep.equal(ortb2.user.ext.eids);
expect(request.data.source.ext.schain).to.deep.equal(ortb2.source.ext.schain);
expect(request.data.regs).to.deep.equal(ortb2.regs);
});
describe('currency', function () {
beforeEach(function () {
setCurrencyConfig({});
});
afterEach(function () {
setCurrencyConfig({});
});
it('asks for bids in USD without the currency module', function () {
const [request] = buildRequests([makeBid()]);
expect(request.data.cur).to.deep.equal(['USD']);
});
it('asks for bids in USD when the ad server currency is EUR', function () {
setCurrencyConfig({ adServerCurrency: 'EUR', rates: { USD: { EUR: 0.9 } } });
const [request] = buildRequests([makeBid()]);
expect(request.data.cur).to.deep.equal(['USD']);
});
it('asks for bids in USD when first-party data sets another currency', function () {
const [request] = buildRequests([makeBid()], { ortb2: { cur: ['EUR'] } });
expect(request.data.cur).to.deep.equal(['USD']);
});
});
describe('floors', function () {
it('requests the floor in USD and keeps a USD floor', function () {
const getFloor = sinon.stub().returns({ currency: 'USD', floor: 1.25 });
const [request] = buildRequests([makeBid({ getFloor })]);
expect(getFloor.calledWith(sinon.match({ currency: 'USD' }))).to.equal(true);
expect(request.data.imp[0]).to.include({ bidfloor: 1.25, bidfloorcur: 'USD' });
});
it('removes a floor in another currency', function () {
const [request] = buildRequests([makeBid({ getFloor: () => ({ currency: 'EUR', floor: 1.25 }) })]);
expect(request.data.imp[0]).to.not.have.any.keys('bidfloor', 'bidfloorcur');
});
it('removes per-media-type and per-size floors in another currency, keeps them in USD', function () {
const floorsIn = (currency) => ({ mediaType, size }) =>
({ currency, floor: Array.isArray(size) ? 2 : mediaType === 'banner' ? 1.5 : 1 });
const [usd] = buildRequests([makeBid({ getFloor: floorsIn('USD') })]);
expect(usd.data.imp[0].banner.ext).to.include({ bidfloor: 1.5, bidfloorcur: 'USD' });
usd.data.imp[0].banner.format.forEach((format) => expect(format.ext).to.include({ bidfloor: 2, bidfloorcur: 'USD' }));
const [eur] = buildRequests([makeBid({ getFloor: floorsIn('EUR') })]);
expect(eur.data.imp[0].banner.ext).to.not.have.any.keys('bidfloor', 'bidfloorcur');
eur.data.imp[0].banner.format.forEach((format) => expect(format.ext).to.not.have.any.keys('bidfloor', 'bidfloorcur'));
});
it('keeps a floor that has no currency', function () {
const [request] = buildRequests([makeBid({ ortb2Imp: { bidfloor: 0.5 } })]);
expect(request.data.imp[0].bidfloor).to.equal(0.5);
});
});
describe('dropped media types', function () {
let logWarn;
beforeEach(function () {
logWarn = sandbox.stub(utils, 'logWarn');
});
it('drops an imp that has no media type left', function () {
const [request] = buildRequests([
makeBid({ bidId: 'bid-1' }),
makeBid({ bidId: 'bid-2', mediaTypes: { video: { context: 'instream', playerSize: [640, 480] } } }),
]);
expect(request.data.imp.map((imp) => imp.id)).to.deep.equal(['bid-1']);
});
it('does not send a request whose imps were all dropped', function () {
const requests = buildRequests([
makeBid({ bidId: 'bid-1', params: { publisherId: 'pub-1' } }),
makeBid({ bidId: 'bid-2', params: { publisherId: 'pub-2' }, mediaTypes: { video: { playerSize: [640, 480] } } }),
]);
expect(requests.map((request) => request.url)).to.deep.equal([`${AUCTION_URL}?pid=pub-1`]);
expect(buildRequests([makeBid({ mediaTypes: { video: { playerSize: [640, 480] } } })])).to.deep.equal([]);
});
if (FEATURES.VIDEO) {
it('removes video without mimes and keeps the rest of the imp', function () {
[undefined, []].forEach((mimes) => {
const video = { context: 'instream', playerSize: [640, 480], mimes };
const [request] = buildRequests([makeBid({ mediaTypes: { banner: { sizes: [[300, 250]] }, video } })]);
expect(request.data.imp[0].banner).to.exist;
expect(request.data.imp[0]).to.not.have.property('video');
});
expect(logWarn.calledWith(sinon.match('video without mimes removed from ad unit div-1'))).to.equal(true);
});
}
if (FEATURES.AUDIO) {
it('removes audio without mimes and keeps the rest of the imp', function () {
const audio = { context: 'instream', maxduration: 30 };
const [request] = buildRequests([makeBid({ mediaTypes: { banner: { sizes: [[300, 250]] }, audio } })]);
expect(request.data.imp[0].banner).to.exist;
expect(request.data.imp[0]).to.not.have.property('audio');
expect(logWarn.calledWith(sinon.match('audio without mimes removed from ad unit div-1'))).to.equal(true);
});
}
});
if (FEATURES.VIDEO) {
it('builds a video imp', function () {
const [request] = buildRequests([makeBid({ mediaTypes: { video: VIDEO } })]);
expect(request.data.imp[0].video).to.deep.include({ mimes: ['video/mp4'], protocols: [2, 3, 5, 6], w: 640, h: 480 });
expect(request.data.imp[0]).to.not.have.property('banner');
});
}
if (FEATURES.AUDIO) {
it('builds an audio imp', function () {
const [request] = buildRequests([makeBid({ mediaTypes: { audio: AUDIO } })]);
expect(request.data.imp[0].audio).to.deep.include({ mimes: ['audio/mp4', 'audio/mpeg'], minduration: 5, maxduration: 30 });
expect(request.data.imp[0]).to.not.have.property('banner');
});
}
if (FEATURES.NATIVE) {
it('builds a native imp from the ORTB native request', function () {
const bid = makeBid({ mediaTypes: { native: { ortb: NATIVE_REQUEST } }, nativeOrtbRequest: NATIVE_REQUEST });
const [request] = buildRequests([bid]);
const { native } = request.data.imp[0];
expect(native.ver).to.equal('1.2');
expect(JSON.parse(native.request)).to.deep.equal(NATIVE_REQUEST);
});
}
});
describe('interpretResponse', function () {
it('maps a banner bid', function () {
const [request] = buildRequests([makeBid()]);
const bids = spec.interpretResponse(ortbResponse([{
id: 'seat-bid-1',
impid: 'bid-1',
price: 0.5,
adm: '<div>ad</div>',
crid: 'creative-1',
w: 300,
h: 250,
mtype: 1,
adomain: ['advertiser.example'],
dealid: 'deal-1',
burl: 'https://rtb.adspiro.io/billing?id=1',
exp: 120,
}]), request);
expect(bids).to.have.lengthOf(1);
expect(bids[0]).to.deep.include({
requestId: 'bid-1',
cpm: 0.5,
currency: 'USD',
netRevenue: true,
ttl: 120,
creativeId: 'creative-1',
dealId: 'deal-1',
burl: 'https://rtb.adspiro.io/billing?id=1',
width: 300,
height: 250,
mediaType: 'banner',
ad: '<div>ad</div>',
});
expect(bids[0].meta.advertiserDomains).to.deep.equal(['advertiser.example']);
});
it('defaults ttl to 300 seconds and currency to USD', function () {
const [request] = buildRequests([makeBid()]);
const response = ortbResponse([{ impid: 'bid-1', price: 0.5, adm: '<div>ad</div>', crid: 'creative-1', mtype: 1 }], {});
const [bid] = spec.interpretResponse(response, request);
expect(bid.ttl).to.equal(300);
expect(bid.currency).to.equal('USD');
});
it('skips a bid whose mtype is missing or unknown', function () {
const logError = sandbox.stub(utils, 'logError');
const [request] = buildRequests([makeBid()]);
const bids = spec.interpretResponse(ortbResponse([
{ impid: 'bid-1', price: 0.5, adm: '<div>1</div>', crid: 'creative-1', mtype: 1 },
{ impid: 'bid-1', price: 0.6, adm: '<div>2</div>', crid: 'creative-2' },
{ impid: 'bid-1', price: 0.7, adm: '<div>3</div>', crid: 'creative-3', mtype: 5 },
]), request);
expect(bids.map((bid) => bid.creativeId)).to.deep.equal(['creative-1']);
expect(logError.callCount).to.equal(2);
});
it('returns no bids for an empty or non-object body', function () {
const [request] = buildRequests([makeBid()]);
['', null, undefined, 'Bad Request', []].forEach((body) => {
expect(spec.interpretResponse({ body }, request)).to.deep.equal([]);
});
expect(spec.interpretResponse(undefined, request)).to.deep.equal([]);
});
if (FEATURES.VIDEO) {
it('maps a video bid to VAST XML', function () {
const [request] = buildRequests([makeBid({ mediaTypes: { video: VIDEO } })]);
const [bid] = spec.interpretResponse(ortbResponse([
{ impid: 'bid-1', price: 0.5, adm: '<VAST version="4.2"></VAST>', crid: 'creative-1', mtype: 2 },
]), request);
expect(bid).to.deep.include({
mediaType: 'video',
vastXml: '<VAST version="4.2"></VAST>',
playerWidth: 640,
playerHeight: 480,
});
});
}
if (FEATURES.NATIVE) {
it('maps a native bid', function () {
const bidRequest = makeBid({ mediaTypes: { native: { ortb: NATIVE_REQUEST } }, nativeOrtbRequest: NATIVE_REQUEST });
const [request] = buildRequests([bidRequest]);
const nativeResponse = {
ver: '1.2',
assets: [
{ id: 1, title: { text: 'Adspiro test ad' } },
{ id: 2, img: { url: 'https://adspiro.io/prebid/test-native-1200x627.png', w: 1200, h: 627 } },
{ id: 3, data: { value: 'Adspiro' } },
],
link: { url: 'https://adspiro.io' },
};
const [bid] = spec.interpretResponse(ortbResponse([
{ impid: 'bid-1', price: 0.5, adm: JSON.stringify(nativeResponse), crid: 'creative-1', mtype: 4 },
]), request);
expect(bid.mediaType).to.equal('native');
expect(bid.native.ortb).to.deep.equal(nativeResponse);
});
}
if (FEATURES.AUDIO) {
it('maps an audio bid (mtype 3) to VAST XML', function () {
const [request] = buildRequests([makeBid({ mediaTypes: { audio: AUDIO } })]);
const [bid] = spec.interpretResponse(ortbResponse([
{ impid: 'bid-1', price: 0.5, adm: '<VAST version="4.2"></VAST>', crid: 'creative-1', mtype: 3 },
]), request);
expect(bid).to.deep.include({ mediaType: 'audio', vastXml: '<VAST version="4.2"></VAST>' });
});
it('maps audio for one bid without changing the next bid on the same imp', function () {
const [request] = buildRequests([makeBid({ mediaTypes: { banner: { sizes: [[300, 250]] }, audio: AUDIO } })]);
const bids = spec.interpretResponse(ortbResponse([
{ impid: 'bid-1', price: 0.5, adm: '<VAST version="4.2"></VAST>', crid: 'creative-1', mtype: 3 },
{ impid: 'bid-1', price: 0.4, adm: '<div>ad</div>', crid: 'creative-2', w: 300, h: 250, mtype: 1 },
]), request);
expect(bids.map((bid) => bid.mediaType)).to.deep.equal(['audio', 'banner']);
});
}
});
describe('getUserSyncs', function () {
const ALL_SYNCS = { iframeEnabled: true, pixelEnabled: true };
it('prefers an iframe sync', function () {
expect(spec.getUserSyncs(ALL_SYNCS, [], null, null, null, false)).to.deep.equal([
{ type: 'iframe', url: IFRAME_SYNC_URL },
]);
});
it('falls back to an image sync', function () {
expect(spec.getUserSyncs({ iframeEnabled: false, pixelEnabled: true }, [], null, null, null, false)).to.deep.equal([
{ type: 'image', url: IMAGE_SYNC_URL },
]);
});
it('returns no sync when neither type is allowed', function () {
expect(spec.getUserSyncs({ iframeEnabled: false, pixelEnabled: false }, [], null, null, null, false)).to.deep.equal([]);
});
it('returns no sync under COPPA', function () {
expect(spec.getUserSyncs(ALL_SYNCS, [], null, '1YNN', null, true)).to.deep.equal([]);
});
it('adds every privacy signal that is present', function () {
const gdprConsent = { gdprApplies: true, consentString: 'CONSENT' };
const gppConsent = { gppString: 'DBABTA~1YNN', applicableSections: [7, 8] };
expect(spec.getUserSyncs({ pixelEnabled: true }, [], gdprConsent, '1YNN', gppConsent, false)).to.deep.equal([{
type: 'image',
url: `${IMAGE_SYNC_URL}?gdpr=1&gdpr_consent=CONSENT&us_privacy=1YNN&gpp=DBABTA%7E1YNN&gpp_sid=7%2C8`,
}]);
});
it('sends gdpr=0 when GDPR does not apply', function () {
const [sync] = spec.getUserSyncs(ALL_SYNCS, [], { gdprApplies: false }, null, null, false);
expect(sync.url).to.equal(`${IFRAME_SYNC_URL}?gdpr=0`);
});
it('leaves out gdpr when gdprApplies is not a boolean', function () {
const [sync] = spec.getUserSyncs(ALL_SYNCS, [], { consentString: 'CONSENT' }, null, null, false);
expect(sync.url).to.equal(`${IFRAME_SYNC_URL}?gdpr_consent=CONSENT`);
});
it('sends the GPP string and sections independently', function () {
const [withString] = spec.getUserSyncs(ALL_SYNCS, [], null, null, { gppString: 'DBABTA~1YNN' }, false);
expect(new URL(withString.url).search).to.equal('?gpp=DBABTA%7E1YNN');
const [withSections] = spec.getUserSyncs(ALL_SYNCS, [], null, null, { applicableSections: [7] }, false);
expect(new URL(withSections.url).search).to.equal('?gpp_sid=7');
});
it('leaves out empty privacy signals', function () {
const gdprConsent = { consentString: '' };
const gppConsent = { gppString: '', applicableSections: [] };
expect(spec.getUserSyncs(ALL_SYNCS, [], gdprConsent, '', gppConsent, false)).to.deep.equal([
{ type: 'iframe', url: IFRAME_SYNC_URL },
]);
});
});
describe('onDataDeletionRequest', function () {
const DELETION_URL = 'https://rtb.adspiro.io/u/delete';
it('posts an empty keepalive request with credentials to the deletion endpoint', function () {
spec.onDataDeletionRequest([], null);
const requests = server.requests.filter((request) => request.url === DELETION_URL);
expect(requests).to.have.lengthOf(1);
expect(requests[0].method).to.equal('POST');
expect(requests[0].requestBody).to.equal(undefined);
expect(requests[0].fetch.request.keepalive).to.equal(true);
// the sent request drops credentials until the release adds this module's metadata, so check what was asked for
const [built] = dep.makeRequest.getCalls().filter(({ args }) => args[0] === DELETION_URL);
expect(built.args[1].credentials).to.equal('include');
});
});
});