forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
auctionmanager_spec.js
1914 lines (1691 loc) · 61 KB
/
auctionmanager_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
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
getKeyValueTargetingPairs,
auctionCallbacks,
AUCTION_COMPLETED,
adjustBids,
getMediaTypeGranularity,
getPriceByGranularity,
addBidResponse
} from 'src/auction.js';
import CONSTANTS from 'src/constants.json';
import * as auctionModule from 'src/auction.js';
import { registerBidder } from 'src/adapters/bidderFactory.js';
import { createBid } from 'src/bidfactory.js';
import { config } from 'src/config.js';
import * as store from 'src/videoCache.js';
import * as ajaxLib from 'src/ajax.js';
import {find} from 'src/polyfill.js';
import { server } from 'test/mocks/xhr.js';
import {hook} from '../../src/hook.js';
import {auctionManager} from '../../src/auctionManager.js';
import 'modules/debugging/index.js' // some tests look for debugging side effects
import {AuctionIndex} from '../../src/auctionIndex.js';
import {expect} from 'chai';
import {deepClone} from '../../src/utils.js';
import { IMAGE as ortbNativeRequest } from 'src/native.js';
var assert = require('assert');
/* use this method to test individual files instead of the whole prebid.js project */
// TODO refactor to use the spec files
var utils = require('../../src/utils');
var fixtures = require('../fixtures/fixtures');
var adapterManager = require('src/adapterManager').default;
var events = require('src/events');
const BIDDER_CODE = 'sampleBidder';
const BIDDER_CODE1 = 'sampleBidder1';
const ADUNIT_CODE = 'adUnit-code';
const ADUNIT_CODE1 = 'adUnit-code-1';
/**
* @param {Object} [opts]
* @returns {Bid}
*/
function mockBid(opts) {
let bidderCode = opts && opts.bidderCode;
return {
'ad': 'creative',
'cpm': '1.99',
'width': 300,
'height': 250,
'bidderCode': bidderCode || BIDDER_CODE,
'requestId': utils.getUniqueIdentifierStr(),
'transactionId': (opts && opts.transactionId) || ADUNIT_CODE,
'creativeId': 'id',
'currency': 'USD',
'netRevenue': true,
'ttl': 360,
getSize: () => '300x250',
getIdentifiers() {
return {
src: this.source,
bidder: this.bidderCode,
bidId: this.requestId,
transactionId: this.transactionId,
auctionId: this.auctionId
}
}
};
}
/**
* @param {Bid} bid
* @param {Object} [opts]
* @returns {BidRequest}
*/
function mockBidRequest(bid, opts) {
if (!bid) {
throw new Error('bid required');
}
let bidderCode = opts && opts.bidderCode;
let adUnitCode = opts && opts.adUnitCode;
let defaultMediaType = {
banner: {
sizes: [[300, 250], [300, 600]]
}
}
let mediaType = (opts && opts.mediaType) ? opts.mediaType : defaultMediaType;
let requestId = utils.getUniqueIdentifierStr();
return {
'bidderCode': bidderCode || bid.bidderCode,
'auctionId': opts && opts.auctionId,
'bidderRequestId': requestId,
'bids': [
{
'bidder': bidderCode || bid.bidderCode,
'params': {
'placementId': 'id'
},
'adUnitCode': adUnitCode || ADUNIT_CODE,
'transactionId': bid.transactionId,
'sizes': [[300, 250], [300, 600]],
'bidId': bid.requestId,
'bidderRequestId': requestId,
'auctionId': opts && opts.auctionId,
'mediaTypes': mediaType
}
],
'auctionStart': 1505250713622,
'timeout': 3000
};
}
function mockBidder(bidderCode, bids) {
let spec = {
code: bidderCode,
isBidRequestValid: sinon.stub(),
buildRequests: sinon.stub(),
interpretResponse: sinon.stub(),
getUserSyncs: sinon.stub()
};
spec.buildRequests.returns([{'id': 123, 'method': 'POST'}]);
spec.isBidRequestValid.returns(true);
spec.interpretResponse.returns(bids);
return spec;
}
const TEST_BIDS = [mockBid()];
const TEST_BID_REQS = TEST_BIDS.map(mockBidRequest);
function mockAjaxBuilder() {
return function(url, callback) {
const fakeResponse = sinon.stub();
fakeResponse.returns('headerContent');
callback.success('response body', { getResponseHeader: fakeResponse });
};
}
describe('auctionmanager.js', function () {
let indexAuctions, indexStub
before(() => {
// hooks are global and their side effects depend on what has been loaded
[
auctionModule.addBidResponse,
auctionModule.addBidderRequests,
auctionModule.bidsBackCallback
].forEach((h) => h.getHooks().remove())
hook.ready();
});
beforeEach(() => {
indexAuctions = [];
indexStub = sinon.stub(auctionManager, 'index');
indexStub.get(() => new AuctionIndex(() => indexAuctions));
});
afterEach(() => {
indexStub.restore();
});
describe('getKeyValueTargetingPairs', function () {
const DEFAULT_BID = {
cpm: 5.578,
pbLg: 5.50,
pbMg: 5.50,
pbHg: 5.57,
pbAg: 5.50,
height: 300,
width: 250,
getSize() {
return this.height + 'x' + this.width;
},
adUnitCode: '12345',
bidderCode: 'appnexus',
adId: '1adId',
source: 'client',
mediaType: 'banner',
meta: {
advertiserDomains: ['adomain'],
primaryCatId: 'IAB-test'
}
};
/* return the expected response for a given bid, filter by keys if given */
function getDefaultExpected(bid, keys) {
var expected = {};
expected[ CONSTANTS.TARGETING_KEYS.BIDDER ] = bid.bidderCode;
expected[ CONSTANTS.TARGETING_KEYS.AD_ID ] = bid.adId;
expected[ CONSTANTS.TARGETING_KEYS.PRICE_BUCKET ] = bid.pbMg;
expected[ CONSTANTS.TARGETING_KEYS.SIZE ] = bid.getSize();
expected[ CONSTANTS.TARGETING_KEYS.SOURCE ] = bid.source;
expected[ CONSTANTS.TARGETING_KEYS.FORMAT ] = bid.mediaType;
expected[ CONSTANTS.TARGETING_KEYS.ADOMAIN ] = bid.meta.advertiserDomains[0];
expected[ CONSTANTS.TARGETING_KEYS.ACAT ] = bid.meta.primaryCatId;
if (bid.mediaType === 'video') {
expected[ CONSTANTS.TARGETING_KEYS.UUID ] = bid.videoCacheKey;
expected[ CONSTANTS.TARGETING_KEYS.CACHE_ID ] = bid.videoCacheKey;
expected[ CONSTANTS.TARGETING_KEYS.CACHE_HOST ] = 'prebid.adnxs.com';
}
if (!keys) {
return expected;
}
return keys.reduce((map, key) => {
map[key] = expected[key];
return map;
}, {});
}
var bid = {};
before(function () {
bid = Object.assign({}, DEFAULT_BID);
});
it('No bidder level configuration defined - default', function () {
$$PREBID_GLOBAL$$.bidderSettings = {};
let expected = getDefaultExpected(bid);
// remove hb_cache_host from expected
delete expected.hb_cache_host;
let response = getKeyValueTargetingPairs(bid.bidderCode, bid);
assert.deepEqual(response, expected);
});
it('should suppress acat if undefined', function () {
const noAcatBid = deepClone(DEFAULT_BID);
noAcatBid.meta.primaryCatId = ''
let expected = getDefaultExpected(noAcatBid);
delete expected.hb_acat;
let response = getKeyValueTargetingPairs(noAcatBid.bidderCode, noAcatBid);
assert.deepEqual(response, expected);
});
it('No bidder level configuration defined - default for video', function () {
config.setConfig({
cache: {
url: 'https://prebid.adnxs.com/pbc/v1/cache'
}
});
$$PREBID_GLOBAL$$.bidderSettings = {};
let videoBid = utils.deepClone(bid);
videoBid.mediaType = 'video';
videoBid.videoCacheKey = 'abc123def';
let expected = getDefaultExpected(videoBid);
let response = getKeyValueTargetingPairs(videoBid.bidderCode, videoBid);
assert.deepEqual(response, expected);
});
it('Custom configuration for all bidders', function () {
$$PREBID_GLOBAL$$.bidderSettings =
{
standard: {
adserverTargeting: [
{
key: CONSTANTS.TARGETING_KEYS.BIDDER,
val: function (bidResponse) {
return bidResponse.bidderCode;
}
}, {
key: CONSTANTS.TARGETING_KEYS.AD_ID,
val: function (bidResponse) {
return bidResponse.adId;
}
}, {
key: CONSTANTS.TARGETING_KEYS.PRICE_BUCKET,
val: function (bidResponse) {
// change default here
return bidResponse.pbHg;
}
}, {
key: CONSTANTS.TARGETING_KEYS.SIZE,
val: function (bidResponse) {
return bidResponse.size;
}
},
{
key: CONSTANTS.TARGETING_KEYS.SOURCE,
val: function (bidResponse) {
return bidResponse.source;
}
},
{
key: CONSTANTS.TARGETING_KEYS.FORMAT,
val: function (bidResponse) {
return bidResponse.mediaType;
}
},
{
key: CONSTANTS.TARGETING_KEYS.ADOMAIN,
val: function (bidResponse) {
return bidResponse.meta.advertiserDomains[0];
}
},
{
key: CONSTANTS.TARGETING_KEYS.ACAT,
val: function (bidResponse) {
return bidResponse.meta.primaryCatId;
}
}
]
}
};
var expected = getDefaultExpected(bid);
expected[CONSTANTS.TARGETING_KEYS.PRICE_BUCKET] = bid.pbHg;
var response = getKeyValueTargetingPairs(bid.bidderCode, bid);
assert.deepEqual(response, expected);
});
it('Custom configuration for all bidders with video bid', function () {
config.setConfig({
cache: {
url: 'https://prebid.adnxs.com/pbc/v1/cache'
}
});
let videoBid = utils.deepClone(bid);
videoBid.mediaType = 'video';
videoBid.videoCacheKey = 'abc123def';
$$PREBID_GLOBAL$$.bidderSettings =
{
standard: {
adserverTargeting: [
{
key: CONSTANTS.TARGETING_KEYS.BIDDER,
val: function (bidResponse) {
return bidResponse.bidderCode;
}
}, {
key: CONSTANTS.TARGETING_KEYS.AD_ID,
val: function (bidResponse) {
return bidResponse.adId;
}
}, {
key: CONSTANTS.TARGETING_KEYS.PRICE_BUCKET,
val: function (bidResponse) {
return bidResponse.pbMg;
}
}, {
key: CONSTANTS.TARGETING_KEYS.SIZE,
val: function (bidResponse) {
return bidResponse.size;
}
},
{
key: CONSTANTS.TARGETING_KEYS.SOURCE,
val: function (bidResponse) {
return bidResponse.source;
}
},
{
key: CONSTANTS.TARGETING_KEYS.FORMAT,
val: function (bidResponse) {
return bidResponse.mediaType;
}
},
{
key: CONSTANTS.TARGETING_KEYS.UUID,
val: function (bidResponse) {
return bidResponse.videoCacheKey;
}
},
{
key: CONSTANTS.TARGETING_KEYS.CACHE_ID,
val: function (bidResponse) {
return bidResponse.videoCacheKey;
}
},
{
key: CONSTANTS.TARGETING_KEYS.ADOMAIN,
val: function (bidResponse) {
return bidResponse.meta.advertiserDomains[0];
}
},
{
key: CONSTANTS.TARGETING_KEYS.ACAT,
val: function (bidResponse) {
return bidResponse.meta.primaryCatId;
}
}
]
}
};
let expected = getDefaultExpected(videoBid);
let response = getKeyValueTargetingPairs(videoBid.bidderCode, videoBid);
assert.deepEqual(response, expected);
});
it('Custom configuration for one bidder', function () {
$$PREBID_GLOBAL$$.bidderSettings =
{
appnexus: {
adserverTargeting: [
{
key: CONSTANTS.TARGETING_KEYS.BIDDER,
val: function (bidResponse) {
return bidResponse.bidderCode;
}
}, {
key: CONSTANTS.TARGETING_KEYS.AD_ID,
val: function (bidResponse) {
return bidResponse.adId;
}
}, {
key: CONSTANTS.TARGETING_KEYS.PRICE_BUCKET,
val: function (bidResponse) {
// change default here
return bidResponse.pbHg;
}
}, {
key: CONSTANTS.TARGETING_KEYS.SIZE,
val: function (bidResponse) {
return bidResponse.size;
}
}
]
}
};
var expected = getDefaultExpected(bid);
expected[CONSTANTS.TARGETING_KEYS.PRICE_BUCKET] = bid.pbHg;
var response = getKeyValueTargetingPairs(bid.bidderCode, bid);
assert.deepEqual(response, expected);
});
it('Custom configuration for one bidder - not matched', function () {
$$PREBID_GLOBAL$$.bidderSettings =
{
nonExistentBidder: {
adserverTargeting: [
{
key: CONSTANTS.TARGETING_KEYS.BIDDER,
val: function (bidResponse) {
return bidResponse.bidderCode;
}
}, {
key: CONSTANTS.TARGETING_KEYS.AD_ID,
val: function (bidResponse) {
return bidResponse.adId;
}
}, {
key: CONSTANTS.TARGETING_KEYS.PRICE_BUCKET,
val: function (bidResponse) {
// change default here
return bidResponse.pbHg;
}
}, {
key: CONSTANTS.TARGETING_KEYS.SIZE,
val: function (bidResponse) {
return bidResponse.size;
}
}
]
}
};
var expected = getDefaultExpected(bid);
var response = getKeyValueTargetingPairs(bid.bidderCode, bid);
assert.deepEqual(response, expected);
});
it('Should set targeting as expecting when pbs is enabled', function () {
config.setConfig({
s2sConfig: {
accountId: '1',
enabled: true,
defaultVendor: 'appnexus',
bidders: ['appnexus'],
timeout: 1000,
adapter: 'prebidServer'
}
});
$$PREBID_GLOBAL$$.bidderSettings = {};
let expected = getDefaultExpected(bid);
let response = getKeyValueTargetingPairs(bid.bidderCode, bid);
assert.deepEqual(response, expected);
});
it('Custom bidCpmAdjustment for one bidder and inherit standard but doesn\'t use standard bidCpmAdjustment', function () {
$$PREBID_GLOBAL$$.bidderSettings =
{
appnexus: {
bidCpmAdjustment: function (bidCpm) {
return bidCpm * 0.7;
},
},
standard: {
bidCpmAdjustment: function (bidCpm) {
return 200;
},
adserverTargeting: [
{
key: CONSTANTS.TARGETING_KEYS.BIDDER,
val: function (bidResponse) {
return bidResponse.bidderCode;
}
}, {
key: CONSTANTS.TARGETING_KEYS.AD_ID,
val: function (bidResponse) {
return bidResponse.adId;
}
}, {
key: CONSTANTS.TARGETING_KEYS.PRICE_BUCKET,
val: function (bidResponse) {
// change default here
return 10.00;
}
}
]
}
};
var expected = getDefaultExpected(bid, [CONSTANTS.TARGETING_KEYS.BIDDER, CONSTANTS.TARGETING_KEYS.AD_ID]);
expected[CONSTANTS.TARGETING_KEYS.PRICE_BUCKET] = 10.0;
var response = getKeyValueTargetingPairs(bid.bidderCode, bid);
assert.deepEqual(response, expected);
});
it('Standard bidCpmAdjustment changes the bid of any bidder', function () {
const bid = Object.assign({},
createBid(2),
fixtures.getBidResponses()[5]
);
assert.equal(bid.cpm, 0.5);
$$PREBID_GLOBAL$$.bidderSettings =
{
standard: {
bidCpmAdjustment: function (bidCpm) {
return bidCpm * 0.5;
}
}
};
adjustBids(bid)
assert.equal(bid.cpm, 0.25);
});
it('Custom bidCpmAdjustment AND custom configuration for one bidder and inherit standard settings', function () {
$$PREBID_GLOBAL$$.bidderSettings =
{
appnexus: {
bidCpmAdjustment: function (bidCpm) {
return bidCpm * 0.7;
},
adserverTargeting: [
{
key: CONSTANTS.TARGETING_KEYS.BIDDER,
val: function (bidResponse) {
return bidResponse.bidderCode;
}
}, {
key: CONSTANTS.TARGETING_KEYS.AD_ID,
val: function (bidResponse) {
return bidResponse.adId;
}
}, {
key: CONSTANTS.TARGETING_KEYS.PRICE_BUCKET,
val: function (bidResponse) {
// change default here
return 15.00;
}
}
]
},
standard: {
adserverTargeting: [
{
key: CONSTANTS.TARGETING_KEYS.BIDDER,
val: function (bidResponse) {
return bidResponse.bidderCode;
}
}, {
key: CONSTANTS.TARGETING_KEYS.AD_ID,
val: function (bidResponse) {
return bidResponse.adId;
}
}, {
key: CONSTANTS.TARGETING_KEYS.PRICE_BUCKET,
val: function (bidResponse) {
// change default here
return 10.00;
},
},
{
key: CONSTANTS.TARGETING_KEYS.SIZE,
val: function (bidResponse) {
return bidResponse.size;
}
}
]
}
};
var expected = getDefaultExpected(bid, [CONSTANTS.TARGETING_KEYS.BIDDER, CONSTANTS.TARGETING_KEYS.AD_ID, CONSTANTS.TARGETING_KEYS.SIZE]);
expected[CONSTANTS.TARGETING_KEYS.PRICE_BUCKET] = 15.0;
var response = getKeyValueTargetingPairs(bid.bidderCode, bid);
assert.deepEqual(response, expected);
});
it('sendStandardTargeting=false, and inherit custom', function () {
$$PREBID_GLOBAL$$.bidderSettings =
{
appnexus: {
sendStandardTargeting: false,
adserverTargeting: [
{
key: CONSTANTS.TARGETING_KEYS.BIDDER,
val: function (bidResponse) {
return bidResponse.bidderCode;
}
}, {
key: CONSTANTS.TARGETING_KEYS.AD_ID,
val: function (bidResponse) {
return bidResponse.adId;
}
}, {
key: CONSTANTS.TARGETING_KEYS.PRICE_BUCKET,
val: function (bidResponse) {
return bidResponse.pbHg;
}
}
]
}
};
var expected = getDefaultExpected(bid);
expected[CONSTANTS.TARGETING_KEYS.PRICE_BUCKET] = 5.57;
var response = getKeyValueTargetingPairs(bid.bidderCode, bid);
assert.deepEqual(response, expected);
assert.equal(bid.sendStandardTargeting, false);
});
it('suppressEmptyKeys=true', function() {
$$PREBID_GLOBAL$$.bidderSettings =
{
standard: {
suppressEmptyKeys: true,
adserverTargeting: [
{
key: 'aKeyWithAValue',
val: 42
},
{
key: 'aKeyWithAnEmptyValue',
val: ''
}
]
}
};
var expected = {
'aKeyWithAValue': 42
};
var response = getKeyValueTargetingPairs(bid.bidderCode, bid);
assert.deepEqual(response, expected);
});
});
describe('adjustBids', function () {
it('should adjust bids if greater than zero and pass copy of bid object', function () {
const bid = Object.assign({},
createBid(2),
fixtures.getBidResponses()[5]
);
assert.equal(bid.cpm, 0.5);
$$PREBID_GLOBAL$$.bidderSettings =
{
brealtime: {
bidCpmAdjustment: function (bidCpm, bidObj) {
assert.deepEqual(bidObj, bid);
if (bidObj.adUnitCode === 'negative') {
return bidCpm * -0.5;
}
if (bidObj.adUnitCode === 'zero') {
return 0;
}
return bidCpm * 0.5;
},
},
standard: {
adserverTargeting: [
]
}
};
// negative
bid.adUnitCode = 'negative';
adjustBids(bid)
assert.equal(bid.cpm, 0.5);
// positive
bid.adUnitCode = 'normal';
adjustBids(bid)
assert.equal(bid.cpm, 0.25);
// zero
bid.adUnitCode = 'zero';
adjustBids(bid)
assert.equal(bid.cpm, 0);
// reset bidderSettings so we don't mess up further tests
$$PREBID_GLOBAL$$.bidderSettings = {};
});
});
describe('createAuction', () => {
let adUnits, stubMakeBidRequests, stubCallAdapters
beforeEach(() => {
stubMakeBidRequests = sinon.stub(adapterManager, 'makeBidRequests').returns([{
bidderCode: BIDDER_CODE,
bids: [{
bidder: BIDDER_CODE
}]
}]);
stubCallAdapters = sinon.stub(adapterManager, 'callBids').callsFake((au, reqs, addBid, done) => {
reqs.forEach(r => done.apply(r));
});
adUnits = [{
code: ADUNIT_CODE,
transactionId: ADUNIT_CODE,
bids: [
{bidder: BIDDER_CODE},
]
}];
});
afterEach(() => {
stubMakeBidRequests.restore();
stubCallAdapters.restore();
});
it('passes global and bidder ortb2 to the auction', () => {
const ortb2Fragments = {
global: {},
bidder: {}
}
const auction = auctionManager.createAuction({adUnits, ortb2Fragments});
auction.callBids();
const anyArgs = [...Array(7).keys()].map(() => sinon.match.any);
sinon.assert.calledWith(stubMakeBidRequests, ...anyArgs.slice(0, 5).concat([sinon.match.same(ortb2Fragments)]));
sinon.assert.calledWith(stubCallAdapters, ...anyArgs.slice(0, 7).concat([sinon.match.same(ortb2Fragments)]));
});
it('correctly adds nonbids when they are emitted', () => {
const ortb2Fragments = {
global: {},
bidder: {}
}
const auction = auctionManager.createAuction({adUnits, ortb2Fragments});
expect(auction.getNonBids()[0]).to.equal(undefined);
events.emit(CONSTANTS.EVENTS.SEAT_NON_BID, {
auctionId: auction.getAuctionId(),
seatnonbid: ['test']
});
expect(auction.getNonBids()[0]).to.equal('test');
});
});
describe('addBidResponse #1', function () {
let createAuctionStub;
let adUnits;
let adUnitCodes;
let spec;
let auction;
let ajaxStub;
let bids;
let bidderRequests;
let makeRequestsStub;
before(function () {
makeRequestsStub = sinon.stub(adapterManager, 'makeBidRequests');
});
after(function () {
adapterManager.makeBidRequests.restore();
});
describe('when auction timeout is 3000', function () {
beforeEach(function () {
ajaxStub = sinon.stub(ajaxLib, 'ajaxBuilder').callsFake(mockAjaxBuilder);
adUnits = [{
code: ADUNIT_CODE,
transactionId: ADUNIT_CODE,
bids: [
{bidder: BIDDER_CODE, params: {placementId: 'id'}},
]
}];
adUnitCodes = [ADUNIT_CODE];
auction = auctionModule.newAuction({adUnits, adUnitCodes, callback: function() {}, cbTimeout: 3000});
bids = TEST_BIDS.slice();
bidderRequests = bids.map(b => mockBidRequest(b, {auctionId: auction.getAuctionId()}));
makeRequestsStub.returns(bidderRequests);
indexAuctions = [auction];
createAuctionStub = sinon.stub(auctionModule, 'newAuction');
createAuctionStub.returns(auction);
spec = mockBidder(BIDDER_CODE, bids);
registerBidder(spec);
});
afterEach(function () {
ajaxStub.restore();
auctionModule.newAuction.restore();
});
function checkPbDg(cpm, expected, msg) {
return function() {
bids[0].cpm = cpm;
auction.callBids();
let registeredBid = auction.getBidsReceived().pop();
assert.equal(registeredBid.pbDg, expected, msg);
};
};
it('should return proper price bucket increments for dense mode when cpm is in range 0-3',
checkPbDg('1.99', '1.99', '0 - 3 hits at to 1 cent increment'));
it('should return proper price bucket increments for dense mode when cpm is in range 3-8',
checkPbDg('4.39', '4.35', '3 - 8 hits at 5 cent increment'));
it('should return proper price bucket increments for dense mode when cpm is in range 8-20',
checkPbDg('19.99', '19.50', '8 - 20 hits at 50 cent increment'));
it('should return proper price bucket increments for dense mode when cpm is 20+',
checkPbDg('73.07', '20.00', '20+ caps at 20.00'));
it('should place dealIds in adserver targeting', function () {
bids[0].dealId = 'test deal';
auction.callBids();
let registeredBid = auction.getBidsReceived().pop();
assert.equal(registeredBid.adserverTargeting[CONSTANTS.TARGETING_KEYS.DEAL], 'test deal', 'dealId placed in adserverTargeting');
});
it('should pass through default adserverTargeting sent from adapter', function () {
bids[0].adserverTargeting = {};
bids[0].adserverTargeting.extra = 'stuff';
auction.callBids();
let registeredBid = auction.getBidsReceived().pop();
assert.equal(registeredBid.adserverTargeting[CONSTANTS.TARGETING_KEYS.BIDDER], BIDDER_CODE);
assert.equal(registeredBid.adserverTargeting.extra, 'stuff');
});
describe('install publisher-defined renderers', () => {
Object.entries({
'on adUnit': () => adUnits[0],
'on bid': () => bidderRequests[0].bids[0],
}).forEach(([t, getObj]) => {
it(t, () => {
let renderer = {
url: 'renderer.js',
render: (bid) => bid
};
let bids1 = Object.assign({},
bids[0],
{
bidderCode: BIDDER_CODE,
mediaType: 'video-outstream',
}
);
Object.assign(getObj(), {renderer});
spec.interpretResponse.returns(bids1);
auction.callBids();
const addedBid = auction.getBidsReceived().pop();
assert.equal(addedBid.renderer.url, 'renderer.js');
})
})
})
it('installs publisher-defined backup renderers on bids', function () {
let renderer = {
url: 'renderer.js',
backupOnly: true,
render: (bid) => bid
};
Object.assign(adUnits[0], {renderer});
let bids1 = Object.assign({},
bids[0],
{
bidderCode: BIDDER_CODE,
mediaType: 'video-outstream',
}
);
spec.interpretResponse.returns(bids1);
auction.callBids();
const addedBid = auction.getBidsReceived().pop();
assert.equal(addedBid.renderer.url, 'renderer.js');
});
it('installs publisher-defined renderers for a media type', function () {
const renderer = {
url: 'videoRenderer.js',
render: (bid) => bid
};
let myBid = mockBid();
let bidRequest = mockBidRequest(myBid);
bidRequest.bids[0] = {
...bidRequest.bids[0],
mediaTypes: {
banner: {
sizes: [[300, 250], [300, 600]]
},
video: {
context: 'outstream',
renderer
}
}
};
makeRequestsStub.returns([bidRequest]);
myBid.mediaType = 'video';
spec.interpretResponse.returns(myBid);
auction.callBids();
const addedBid = auction.getBidsReceived().pop();
assert.equal(addedBid.renderer.url, renderer.url);
});
it('installs bidder-defined renderer when onlyBackup is true in mediaTypes.video options ', function () {
const renderer = {
url: 'videoRenderer.js',
backupOnly: true,
render: (bid) => bid
};
let myBid = mockBid();
let bidRequest = mockBidRequest(myBid);
bidRequest.bids[0] = {
...bidRequest.bids[0],
mediaTypes: {
video: {
context: 'outstream',
renderer
}
}
};
makeRequestsStub.returns([bidRequest]);
myBid.mediaType = 'video';
myBid.renderer = {
url: 'renderer.js',
render: sinon.spy()
};
spec.interpretResponse.returns(myBid);
auction.callBids();
const addedBid = auction.getBidsReceived().pop();
assert.strictEqual(addedBid.renderer.url, myBid.renderer.url);
});
it('bid for a regular unit and a video unit', function() {
let renderer = {
url: 'renderer.js',
render: (bid) => bid
};
Object.assign(adUnits[0], {renderer});
// make sure that if the renderer is only on the second ad unit, prebid
// still correctly uses it
let bid = mockBid();
let bidRequests = [mockBidRequest(bid, {auctionId: auction.getAuctionId()})];
bidRequests[0].bids[1] = Object.assign({
bidId: utils.getUniqueIdentifierStr()
}, bidRequests[0].bids[0]);
Object.assign(bidRequests[0].bids[0], {
adUnitCode: ADUNIT_CODE1,
transactionId: ADUNIT_CODE1,