forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathallegroBidAdapter_spec.js
More file actions
216 lines (198 loc) · 8.93 KB
/
allegroBidAdapter_spec.js
File metadata and controls
216 lines (198 loc) · 8.93 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
import {expect} from 'chai';
import {spec} from 'modules/allegroBidAdapter.js';
import {config} from 'src/config.js';
import sinon from 'sinon';
import * as utils from 'src/utils.js';
function buildBidRequest({bidId = 'bid1', adUnitCode = 'div-1', sizes = [[300, 250]], params = {}, mediaTypes} = {}) {
return {
bidId,
adUnitCode,
bidder: 'allegro',
params,
mediaTypes: mediaTypes || {banner: {sizes}},
};
}
function buildBidderRequest(bidRequests, ortb2Overrides = {}) {
return {
bidderCode: 'allegro',
bids: bidRequests,
auctionId: 'auc-1',
timeout: 1000,
refererInfo: {page: 'https://example.com', domain: 'example.com', ref: '', stack: ['https://example.com']},
ortb2: Object.assign({
device: {
dnt: 0,
sua: {mobile: 0}
}
}, ortb2Overrides)
};
}
describe('Allegro Bid Adapter', () => {
let configStub;
afterEach(() => {
sinon.restore();
});
it('should export the expected code and media types', () => {
expect(spec.code).to.equal('allegro');
expect(spec.supportedMediaTypes).to.deep.equal(['banner', 'video', 'native']);
});
describe('isBidRequestValid', () => {
it('returns true for not undefined bidRequest', () => {
expect(spec.isBidRequestValid({})).to.equal(true);
expect(spec.isBidRequestValid(buildBidRequest({}))).to.equal(true);
});
it('returns false for undefined bidRequest', () => {
expect(spec.isBidRequestValid(undefined)).to.equal(false);
});
});
describe('buildRequests', () => {
it('builds a POST request to default endpoint with ORTB data', () => {
configStub = sinon.stub(config, 'getConfig').callsFake((key) => undefined);
const bidRequests = [buildBidRequest({})];
const bidderRequest = buildBidderRequest(bidRequests);
const req = spec.buildRequests(bidRequests, bidderRequest);
expect(req.method).to.equal('POST');
expect(req.url).to.equal('https://prebid.rtb.allegrogroup.com/v1/rtb/prebid/bid');
expect(req.options.contentType).to.equal('text/plain');
expect(req.data).to.exist;
expect(req.data.imp).to.be.an('array').with.lengthOf(1);
});
it('respects custom bidder URL from config', () => {
configStub = sinon.stub(config, 'getConfig').callsFake((key) => {
if (key === 'allegro.bidderUrl') return 'https://override.endpoint/prebid';
return undefined;
});
let bidRequest = [buildBidRequest({})];
const req = spec.buildRequests(bidRequest, buildBidderRequest(bidRequest));
expect(req.url).to.equal('https://override.endpoint/prebid');
});
it('converts extension fields by default', () => {
configStub = sinon.stub(config, 'getConfig').callsFake((key) => undefined);
const bidRequests = [buildBidRequest({})];
const ortb2 = {
site: {ext: {siteCustom: 'val'}, publisher: {ext: {pubCustom: 'pub'}}},
user: {ext: {userCustom: 'usr'}, data: [{ext: {dataCustom: 'd1'}}]},
device: {ext: {deviceCustom: 'dev'}, sua: {mobile: 1}, dnt: 1},
regs: {ext: {gdpr: 1, other: 'x'}},
source: {ext: {sourceCustom: 'src'}},
ext: {requestCustom: 'req'}
};
const bidderRequest = buildBidderRequest(bidRequests, ortb2);
const req = spec.buildRequests(bidRequests, bidderRequest);
const data = req.data;
expect(data.site.ext).to.equal(undefined);
expect(data.site['[com.google.doubleclick.site]'].siteCustom).to.equal('val');
expect(data.site.publisher['[com.google.doubleclick.publisher]'].pubCustom).to.equal('pub');
expect(data.user['[com.google.doubleclick.user]'].userCustom).to.equal('usr');
expect(data.user.data[0]['[com.google.doubleclick.data]'].dataCustom).to.equal('d1');
expect(data.device['[com.google.doubleclick.device]'].deviceCustom).to.equal('dev');
expect(data.device.dnt).to.be.a('boolean');
expect(data.device.sua.mobile).to.equal(true);
expect(data.regs['[com.google.doubleclick.regs]'].other).to.equal('x');
expect(data.regs['[com.google.doubleclick.regs]'].gdpr).to.equal(true);
expect(data['[com.google.doubleclick.bid_request]'].requestCustom).to.equal('req');
});
it('does not convert extension fields when allegro.convertExtensionFields = false', () => {
configStub = sinon.stub(config, 'getConfig').callsFake((key) => {
if (key === 'allegro.convertExtensionFields') return false;
return undefined;
});
const bidRequests = [buildBidRequest({})];
const ortb2 = {site: {ext: {siteCustom: 'val'}}};
const req = spec.buildRequests(bidRequests, buildBidderRequest(bidRequests, ortb2));
expect(req.data.site.ext.siteCustom).to.equal('val');
expect(req.data.site['[com.google.doubleclick.site]']).to.equal(undefined);
});
it('converts numeric flags to booleans (topframe, secure, test) when present', () => {
configStub = sinon.stub(config, 'getConfig').callsFake((key) => undefined);
const bidRequests = [buildBidRequest({mediaTypes: {banner: {sizes: [[300, 250]], topframe: 1}}, params: {secure: 1}})];
const bidderRequest = buildBidderRequest(bidRequests);
// add test flag via ortb2 without clobbering existing device object
bidderRequest.ortb2.test = 1;
const req = spec.buildRequests(bidRequests, bidderRequest);
const imp = req.data.imp[0];
// topframe may be absent depending on processors; if present it's converted to boolean
if (Object.prototype.hasOwnProperty.call(imp.banner, 'topframe')) {
expect(imp.banner.topframe).to.be.a('boolean');
}
expect(imp.secure).to.equal(true);
expect(req.data.test).to.equal(true);
});
});
describe('interpretResponse', () => {
it('returns undefined for empty body', () => {
const result = spec.interpretResponse({}, {data: {}});
expect(result).to.equal(undefined);
});
it('returns converted bids for a valid ORTB response', () => {
configStub = sinon.stub(config, 'getConfig').callsFake((key) => undefined);
const bidRequests = [buildBidRequest({bidId: 'imp-1'})];
const bidderRequest = buildBidderRequest(bidRequests);
const built = spec.buildRequests(bidRequests, bidderRequest);
const impId = built.data.imp[0].id; // use actual id from converter
const ortbResponse = {
id: 'resp1',
seatbid: [{
seat: 'seat1',
bid: [{impid: impId, price: 1.23, crid: 'creative1', w: 300, h: 250}]
}]
};
const result = spec.interpretResponse({body: ortbResponse}, built);
expect(result).to.be.an('array').with.lengthOf(1);
const bid = result[0];
expect(bid.cpm).to.equal(1.23);
expect(bid.mediaType).to.equal('banner');
expect(bid.ttl).to.equal(360); // default context ttl
expect(bid.netRevenue).to.equal(true);
});
it('ignores bids with impid not present in original request', () => {
configStub = sinon.stub(config, 'getConfig').callsFake((key) => undefined);
const bidRequests = [buildBidRequest({bidId: 'imp-1'})];
const bidderRequest = buildBidderRequest(bidRequests);
const built = spec.buildRequests(bidRequests, bidderRequest);
const ortbResponse = {
seatbid: [{seat: 'seat1', bid: [{impid: 'unknown', price: 0.5, crid: 'x'}]}]
};
const result = spec.interpretResponse({body: ortbResponse}, built);
expect(result).to.be.an('array').that.is.empty;
});
});
describe('onBidWon', () => {
it('does nothing if config flag disabled', () => {
configStub = sinon.stub(config, 'getConfig').callsFake((key) => {
if (key === 'allegro.triggerImpressionPixel') return false;
return undefined;
});
const bid = {burl: 'https://example.com/win?price=${AUCTION_PRICE}', cpm: 1.2};
expect(spec.onBidWon(bid)).to.equal(undefined);
});
it('does nothing when burl missing even if flag enabled', () => {
configStub = sinon.stub(config, 'getConfig').callsFake((key) => {
if (key === 'allegro.triggerImpressionPixel') return true;
return undefined;
});
expect(spec.onBidWon({})).to.equal(undefined);
});
it('fires impression pixel with provided burl when enabled', () => {
const pixelSpy = sinon.spy();
// stub config and utils.triggerPixel; need to stub imported triggerPixel via utils module
configStub = sinon.stub(config, 'getConfig').callsFake((key) => {
if (key === 'allegro.triggerImpressionPixel') return true;
return undefined;
});
sinon.stub(utils, 'triggerPixel').callsFake(pixelSpy);
const bid = {
burl: 'https://example.com/win?aid=auction_id&bid=bid_id&imp=imp_id&price=0.91&cur=USD',
auctionId: 'auc-1',
requestId: 'req-1',
impid: 'imp-1',
cpm: 0.91,
currency: 'USD'
};
spec.onBidWon(bid);
expect(pixelSpy.calledOnce).to.equal(true);
const calledWith = pixelSpy.getCall(0).args[0];
expect(calledWith).to.equal(bid.burl);
});
});
});