Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VISX: currency validation & fix double escape of referer #4299

Merged
merged 1 commit into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 48 additions & 35 deletions modules/visxBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ const LOG_ERROR_MESS = {
emptySeatbid: 'Seatbid array from response has an empty item',
emptyResponse: 'Response is empty',
hasEmptySeatbidArray: 'Response has empty seatbid array',
hasNoArrayOfBids: 'Seatbid from response has no array of bid objects - '
hasNoArrayOfBids: 'Seatbid from response has no array of bid objects - ',
notAllowedCurrency: 'Currency is not supported - ',
currencyMismatch: 'Currency from the request is not match currency from the response - '
};
const currencyWhiteList = ['EUR', 'USD', 'GBP', 'PLN'];
export const spec = {
code: BIDDER_CODE,
isBidRequestValid: function(bid) {
Expand All @@ -34,6 +37,11 @@ export const spec = {
DEFAULT_CUR;
let reqId;

if (currencyWhiteList.indexOf(currency) === -1) {
utils.logError(LOG_ERROR_MESS.notAllowedCurrency + currency);
return;
}

bids.forEach(bid => {
reqId = bid.bidderRequestId;
const {params: {uid}, adUnitCode} = bid;
Expand Down Expand Up @@ -78,7 +86,7 @@ export const spec = {

if (bidderRequest) {
if (bidderRequest.refererInfo && bidderRequest.refererInfo.referer) {
payload.u = encodeURIComponent(bidderRequest.refererInfo.referer);
payload.u = bidderRequest.refererInfo.referer;
}
if (bidderRequest.gdprConsent) {
if (bidderRequest.gdprConsent.consentString) {
Expand Down Expand Up @@ -158,43 +166,48 @@ function _addBidResponse(serverBid, bidsMap, currency, bidResponses, bidsWithout
if (!serverBid.auid) errorMessage = LOG_ERROR_MESS.noAuid + JSON.stringify(serverBid);
if (!serverBid.adm) errorMessage = LOG_ERROR_MESS.noAdm + JSON.stringify(serverBid);
else {
const reqCurrency = currency || DEFAULT_CUR;
const awaitingBids = bidsMap[serverBid.auid];
if (awaitingBids) {
const sizeId = bidsWithoutSizeMatching ? `${serverBid.w}x${serverBid.h}` : Object.keys(awaitingBids)[0];
if (awaitingBids[sizeId]) {
const slot = awaitingBids[sizeId][0];

const bid = slot.bids.shift();
bidResponses.push({
requestId: bid.bidId,
bidderCode: spec.code,
cpm: serverBid.price,
width: serverBid.w,
height: serverBid.h,
creativeId: serverBid.auid,
currency: currency || DEFAULT_CUR,
netRevenue: true,
ttl: TIME_TO_LIVE,
ad: serverBid.adm,
dealId: serverBid.dealid
});

if (!slot.bids.length) {
slot.parents.forEach(({parent, key, uid}) => {
const index = parent[key].indexOf(slot);
if (index > -1) {
parent[key].splice(index, 1);
}
if (!parent[key].length) {
delete parent[key];
if (!utils.getKeys(parent).length) {
delete bidsMap[uid];
}
}
if (serverBid.cur && serverBid.cur !== reqCurrency) {
errorMessage = LOG_ERROR_MESS.currencyMismatch + reqCurrency + ' - ' + serverBid.cur;
} else {
const sizeId = bidsWithoutSizeMatching ? `${serverBid.w}x${serverBid.h}` : Object.keys(awaitingBids)[0];
if (awaitingBids[sizeId]) {
const slot = awaitingBids[sizeId][0];

const bid = slot.bids.shift();
bidResponses.push({
requestId: bid.bidId,
bidderCode: spec.code,
cpm: serverBid.price,
width: serverBid.w,
height: serverBid.h,
creativeId: serverBid.auid,
currency: reqCurrency,
netRevenue: true,
ttl: TIME_TO_LIVE,
ad: serverBid.adm,
dealId: serverBid.dealid
});

if (!slot.bids.length) {
slot.parents.forEach(({parent, key, uid}) => {
const index = parent[key].indexOf(slot);
if (index > -1) {
parent[key].splice(index, 1);
}
if (!parent[key].length) {
delete parent[key];
if (!utils.getKeys(parent).length) {
delete bidsMap[uid];
}
}
});
}
} else {
bidsWithoutSizeMatching && bidsWithoutSizeMatching.push(serverBid);
}
} else {
bidsWithoutSizeMatching && bidsWithoutSizeMatching.push(serverBid);
}
} else {
errorMessage = LOG_ERROR_MESS.noPlacementCode + serverBid.auid;
Expand Down
52 changes: 27 additions & 25 deletions test/spec/modules/visxBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('VisxAdapter', function () {
referer: 'http://example.com'
}
};
const encodedReferrer = encodeURIComponent(bidderRequest.refererInfo.referer);
const referrer = bidderRequest.refererInfo.referer;
let bidRequests = [
{
'bidder': 'visx',
Expand Down Expand Up @@ -86,7 +86,7 @@ describe('VisxAdapter', function () {
const request = spec.buildRequests([bidRequests[0]], bidderRequest);
const payload = request.data;
expect(payload).to.be.an('object');
expect(payload).to.have.property('u', encodedReferrer);
expect(payload).to.have.property('u', referrer);
expect(payload).to.have.property('pt', 'net');
expect(payload).to.have.property('auids', '903535');
expect(payload).to.have.property('sizes', '300x250,300x600');
Expand All @@ -98,7 +98,7 @@ describe('VisxAdapter', function () {
const request = spec.buildRequests(bidRequests, bidderRequest);
const payload = request.data;
expect(payload).to.be.an('object');
expect(payload).to.have.property('u', encodedReferrer);
expect(payload).to.have.property('u', referrer);
expect(payload).to.have.property('pt', 'net');
expect(payload).to.have.property('auids', '903535,903535,903536');
expect(payload).to.have.property('sizes', '300x250,300x600,728x90');
Expand All @@ -111,7 +111,7 @@ describe('VisxAdapter', function () {
const request = spec.buildRequests(bidRequests, bidderRequest);
const payload = request.data;
expect(payload).to.be.an('object');
expect(payload).to.have.property('u', encodedReferrer);
expect(payload).to.have.property('u', referrer);
expect(payload).to.have.property('pt', 'net');
expect(payload).to.have.property('auids', '903535,903535,903536');
expect(payload).to.have.property('sizes', '300x250,300x600,728x90');
Expand All @@ -124,7 +124,7 @@ describe('VisxAdapter', function () {
const request = spec.buildRequests(bidRequests, bidderRequest);
const payload = request.data;
expect(payload).to.be.an('object');
expect(payload).to.have.property('u', encodedReferrer);
expect(payload).to.have.property('u', referrer);
expect(payload).to.have.property('pt', 'net');
expect(payload).to.have.property('auids', '903535,903535,903536');
expect(payload).to.have.property('sizes', '300x250,300x600,728x90');
Expand All @@ -138,7 +138,7 @@ describe('VisxAdapter', function () {
const request = spec.buildRequests(bidRequests, bidderRequest);
const payload = request.data;
expect(payload).to.be.an('object');
expect(payload).to.have.property('u', encodedReferrer);
expect(payload).to.have.property('u', referrer);
expect(payload).to.have.property('pt', 'net');
expect(payload).to.have.property('auids', '903535,903535,903536');
expect(payload).to.have.property('sizes', '300x250,300x600,728x90');
Expand All @@ -149,16 +149,16 @@ describe('VisxAdapter', function () {

it('should add currency from currency.bidderCurrencyDefault', function () {
const getConfigStub = sinon.stub(config, 'getConfig').callsFake(
arg => arg === 'currency.bidderCurrencyDefault.visx' ? 'JPY' : 'USD');
arg => arg === 'currency.bidderCurrencyDefault.visx' ? 'GBP' : 'USD');
const request = spec.buildRequests(bidRequests, bidderRequest);
const payload = request.data;
expect(payload).to.be.an('object');
expect(payload).to.have.property('u', encodedReferrer);
expect(payload).to.have.property('u', referrer);
expect(payload).to.have.property('pt', 'net');
expect(payload).to.have.property('auids', '903535,903535,903536');
expect(payload).to.have.property('sizes', '300x250,300x600,728x90');
expect(payload).to.have.property('r', '22edbae2733bf6');
expect(payload).to.have.property('cur', 'JPY');
expect(payload).to.have.property('cur', 'GBP');
getConfigStub.restore();
});

Expand All @@ -168,7 +168,7 @@ describe('VisxAdapter', function () {
const request = spec.buildRequests(bidRequests, bidderRequest);
const payload = request.data;
expect(payload).to.be.an('object');
expect(payload).to.have.property('u', encodedReferrer);
expect(payload).to.have.property('u', referrer);
expect(payload).to.have.property('pt', 'net');
expect(payload).to.have.property('auids', '903535,903535,903536');
expect(payload).to.have.property('sizes', '300x250,300x600,728x90');
Expand Down Expand Up @@ -204,11 +204,11 @@ describe('VisxAdapter', function () {

describe('interpretResponse', function () {
const responses = [
{'bid': [{'price': 1.15, 'adm': '<div>test content 1</div>', 'auid': 903535, 'h': 250, 'w': 300}], 'seat': '1'},
{'bid': [{'price': 0.5, 'adm': '<div>test content 2</div>', 'auid': 903536, 'h': 600, 'w': 300}], 'seat': '1'},
{'bid': [{'price': 0.15, 'adm': '<div>test content 3</div>', 'auid': 903535, 'h': 90, 'w': 728}], 'seat': '1'},
{'bid': [{'price': 0, 'auid': 903537, 'h': 250, 'w': 300}], 'seat': '1'},
{'bid': [{'price': 0, 'adm': '<div>test content 5</div>', 'h': 250, 'w': 300}], 'seat': '1'},
{'bid': [{'price': 1.15, 'adm': '<div>test content 1</div>', 'auid': 903535, 'h': 250, 'w': 300, 'cur': 'EUR'}], 'seat': '1'},
{'bid': [{'price': 0.5, 'adm': '<div>test content 2</div>', 'auid': 903536, 'h': 600, 'w': 300, 'cur': 'EUR'}], 'seat': '1'},
{'bid': [{'price': 0.15, 'adm': '<div>test content 3</div>', 'auid': 903535, 'h': 90, 'w': 728, 'cur': 'EUR'}], 'seat': '1'},
{'bid': [{'price': 0, 'auid': 903537, 'h': 250, 'w': 300, 'cur': 'EUR'}], 'seat': '1'},
{'bid': [{'price': 0, 'adm': '<div>test content 5</div>', 'h': 250, 'w': 300, 'cur': 'EUR'}], 'seat': '1'},
undefined,
{'bid': [], 'seat': '1'},
{'seat': '1'},
Expand Down Expand Up @@ -346,7 +346,7 @@ describe('VisxAdapter', function () {
'auctionId': '1cbd2feafe5e8b',
}
];
const getConfigStub = sinon.stub(config, 'getConfig').returns('JPY');
const getConfigStub = sinon.stub(config, 'getConfig').returns('PLN');
const request = spec.buildRequests(bidRequests);
const expectedResponse = [
{
Expand All @@ -358,13 +358,15 @@ describe('VisxAdapter', function () {
'height': 250,
'ad': '<div>test content 1</div>',
'bidderCode': 'visx',
'currency': 'JPY',
'currency': 'PLN',
'netRevenue': true,
'ttl': 360,
}
];

const result = spec.interpretResponse({'body': {'seatbid': [responses[0]]}}, request);
const response = Object.assign({}, responses[0]);
Object.assign(response.bid[0], {'cur': 'PLN'});
const result = spec.interpretResponse({'body': {'seatbid': [response]}}, request);
expect(result).to.deep.equal(expectedResponse);
getConfigStub.restore();
});
Expand Down Expand Up @@ -412,11 +414,11 @@ describe('VisxAdapter', function () {

it('complicated case', function () {
const fullResponse = [
{'bid': [{'price': 1.15, 'adm': '<div>test content 1</div>', 'auid': 903535, 'h': 250, 'w': 300}], 'seat': '1'},
{'bid': [{'price': 0.5, 'adm': '<div>test content 2</div>', 'auid': 903536, 'h': 600, 'w': 300}], 'seat': '1'},
{'bid': [{'price': 0.15, 'adm': '<div>test content 3</div>', 'auid': 903535, 'h': 90, 'w': 728}], 'seat': '1'},
{'bid': [{'price': 0.15, 'adm': '<div>test content 4</div>', 'auid': 903535, 'h': 600, 'w': 300}], 'seat': '1'},
{'bid': [{'price': 0.5, 'adm': '<div>test content 5</div>', 'auid': 903536, 'h': 600, 'w': 350}], 'seat': '1'},
{'bid': [{'price': 1.15, 'adm': '<div>test content 1</div>', 'auid': 903535, 'h': 250, 'w': 300, 'cur': 'EUR'}], 'seat': '1'},
{'bid': [{'price': 0.5, 'adm': '<div>test content 2</div>', 'auid': 903536, 'h': 600, 'w': 300, 'cur': 'EUR'}], 'seat': '1'},
{'bid': [{'price': 0.15, 'adm': '<div>test content 3</div>', 'auid': 903535, 'h': 90, 'w': 728, 'cur': 'EUR'}], 'seat': '1'},
{'bid': [{'price': 0.15, 'adm': '<div>test content 4</div>', 'auid': 903535, 'h': 600, 'w': 300, 'cur': 'EUR'}], 'seat': '1'},
{'bid': [{'price': 0.5, 'adm': '<div>test content 5</div>', 'auid': 903536, 'h': 600, 'w': 350, 'cur': 'EUR'}], 'seat': '1'},
];
const bidRequests = [
{
Expand Down Expand Up @@ -550,8 +552,8 @@ describe('VisxAdapter', function () {

it('dublicate uids and sizes in one slot', function () {
const fullResponse = [
{'bid': [{'price': 1.15, 'adm': '<div>test content 1</div>', 'auid': 903535, 'h': 250, 'w': 300}], 'seat': '1'},
{'bid': [{'price': 0.5, 'adm': '<div>test content 2</div>', 'auid': 903535, 'h': 250, 'w': 300}], 'seat': '1'},
{'bid': [{'price': 1.15, 'adm': '<div>test content 1</div>', 'auid': 903535, 'h': 250, 'w': 300, 'cur': 'EUR'}], 'seat': '1'},
{'bid': [{'price': 0.5, 'adm': '<div>test content 2</div>', 'auid': 903535, 'h': 250, 'w': 300, 'cur': 'EUR'}], 'seat': '1'},
];
const bidRequests = [
{
Expand Down