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

Fixing the issue introduced in #3845 for rubi analytics adapter #3996

Merged
merged 2 commits into from
Jul 15, 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
10 changes: 5 additions & 5 deletions modules/rubiconAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,17 @@ function sendMessage(auctionId, bidWonId) {
);
}

function parseBidResponse(bid) {
export function parseBidResponse(bid) {
return _pick(bid, [
'getCpmInNewCurrency as bidPriceUSD', (fn) => {
'bidPriceUSD', () => {
if (typeof bid.currency === 'string' && bid.currency.toUpperCase() === 'USD') {
return Number(bid.cpm);
}
// use currency conversion function if present
if (typeof fn === 'function') {
return Number(fn('USD'));
if (typeof bid.getCpmInNewCurrency === 'function') {
return Number(bid.getCpmInNewCurrency('USD'));
}
// TODO: throw error or something if not USD and currency module wasn't present?
utils.logWarn('Rubicon Analytics Adapter: Could not determine the bidPriceUSD of the bid ', bid);
},
'dealId',
'status',
Expand Down
35 changes: 34 additions & 1 deletion test/spec/modules/rubiconAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import rubiconAnalyticsAdapter, { SEND_TIMEOUT } from 'modules/rubiconAnalyticsAdapter';
import rubiconAnalyticsAdapter, { SEND_TIMEOUT, parseBidResponse } from 'modules/rubiconAnalyticsAdapter';
import CONSTANTS from 'src/constants.json';
import { config } from 'src/config';

import {
setConfig,
addBidResponseHook,
} from 'modules/currency';

let Ajv = require('ajv');
let schema = require('./rubiconAnalyticsSchema.json');
let ajv = new Ajv({
Expand Down Expand Up @@ -694,5 +699,33 @@ describe('rubicon analytics adapter', function () {
expect(timedOutBid.error.code).to.equal('timeout-error');
expect(timedOutBid).to.not.have.property('bidResponse');
});

it('should successfully convert bid price to USD in parseBidResponse', function () {
// Set the rates
setConfig({
adServerCurrency: 'JPY',
rates: {
USD: {
JPY: 100
}
}
});

// set our bid response to JPY
const bidCopy = utils.deepClone(BID2);
bidCopy.currency = 'JPY';
bidCopy.cpm = 100;

// Now add the bidResponse hook which hooks on the currenct conversion function onto the bid response
let innerBid;
addBidResponseHook(function(adCodeId, bid) {
innerBid = bid;
}, 'elementId', bidCopy);

// Use the rubi analytics parseBidResponse Function to get the resulting cpm from the bid response!
const bidResponseObj = parseBidResponse(innerBid);
expect(bidResponseObj).to.have.property('bidPriceUSD');
expect(bidResponseObj.bidPriceUSD).to.equal(1.0);
});
});
});
56 changes: 26 additions & 30 deletions test/spec/modules/rubiconBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ describe('the rubicon adapter', function () {
});

it('should use rubicon sizes if present (including non-mappable sizes)', function () {
var sizesBidderRequest = clone(bidderRequest);
var sizesBidderRequest = utils.deepClone(bidderRequest);
sizesBidderRequest.bids[0].params.sizes = [55, 57, 59, 801];

let [request] = spec.buildRequests(sizesBidderRequest.bids, sizesBidderRequest);
Expand All @@ -481,7 +481,7 @@ describe('the rubicon adapter', function () {
});

it('should not validate bid request if no valid sizes', function () {
var sizesBidderRequest = clone(bidderRequest);
var sizesBidderRequest = utils.deepClone(bidderRequest);
sizesBidderRequest.bids[0].sizes = [[621, 250], [300, 251]];

let result = spec.isBidRequestValid(sizesBidderRequest.bids[0]);
Expand All @@ -490,7 +490,7 @@ describe('the rubicon adapter', function () {
});

it('should not validate bid request if no account id is present', function () {
var noAccountBidderRequest = clone(bidderRequest);
var noAccountBidderRequest = utils.deepClone(bidderRequest);
delete noAccountBidderRequest.bids[0].params.accountId;

let result = spec.isBidRequestValid(noAccountBidderRequest.bids[0]);
Expand All @@ -499,7 +499,7 @@ describe('the rubicon adapter', function () {
});

it('should allow a floor override', function () {
var floorBidderRequest = clone(bidderRequest);
var floorBidderRequest = utils.deepClone(bidderRequest);
floorBidderRequest.bids[0].params.floor = 2;

let [request] = spec.buildRequests(floorBidderRequest.bids, floorBidderRequest);
Expand Down Expand Up @@ -876,17 +876,17 @@ describe('the rubicon adapter', function () {
'rf': 'localhost'
};

const bidCopy = clone(bidderRequest.bids[0]);
const bidCopy = utils.deepClone(bidderRequest.bids[0]);
bidCopy.params.siteId = '70608';
bidCopy.params.zoneId = '1111';
bidderRequest.bids.push(bidCopy);

const bidCopy2 = clone(bidderRequest.bids[0]);
const bidCopy2 = utils.deepClone(bidderRequest.bids[0]);
bidCopy2.params.siteId = '99999';
bidCopy2.params.zoneId = '2222';
bidderRequest.bids.push(bidCopy2);

const bidCopy3 = clone(bidderRequest.bids[0]);
const bidCopy3 = utils.deepClone(bidderRequest.bids[0]);
bidCopy3.params.siteId = '99999';
bidCopy3.params.zoneId = '3333';
bidderRequest.bids.push(bidCopy3);
Expand Down Expand Up @@ -970,7 +970,7 @@ describe('the rubicon adapter', function () {

// TEST '10' BIDS, add 9 to 1 existing bid
for (let i = 0; i < 9; i++) {
let bidCopy = clone(bidderRequest.bids[0]);
let bidCopy = utils.deepClone(bidderRequest.bids[0]);
bidCopy.params.zoneId = `${i}0000`;
bidderRequest.bids.push(bidCopy);
}
Expand All @@ -989,7 +989,7 @@ describe('the rubicon adapter', function () {

// TEST '100' BIDS, add 90 to the previously added 10
for (let i = 0; i < 90; i++) {
let bidCopy = clone(bidderRequest.bids[0]);
let bidCopy = utils.deepClone(bidderRequest.bids[0]);
bidCopy.params.zoneId = `${(i + 10)}0000`;
bidderRequest.bids.push(bidCopy);
}
Expand All @@ -1013,14 +1013,14 @@ describe('the rubicon adapter', function () {
return config[key];
});

const bidCopy = clone(bidderRequest.bids[0]);
const bidCopy = utils.deepClone(bidderRequest.bids[0]);
bidderRequest.bids.push(bidCopy);

const bidCopy2 = clone(bidderRequest.bids[0]);
const bidCopy2 = utils.deepClone(bidderRequest.bids[0]);
bidCopy2.params.siteId = '32001';
bidderRequest.bids.push(bidCopy2);

const bidCopy3 = clone(bidderRequest.bids[0]);
const bidCopy3 = utils.deepClone(bidderRequest.bids[0]);
bidCopy3.params.siteId = '32001';
bidderRequest.bids.push(bidCopy3);

Expand All @@ -1036,18 +1036,18 @@ describe('the rubicon adapter', function () {
return config[key];
});

const bidCopy = clone(bidderRequest.bids[0]);
const bidCopy = utils.deepClone(bidderRequest.bids[0]);
bidderRequest.bids.push(bidCopy);

const bidCopy2 = clone(bidderRequest.bids[0]);
const bidCopy2 = utils.deepClone(bidderRequest.bids[0]);
bidCopy2.params.siteId = '32001';
bidderRequest.bids.push(bidCopy2);

const bidCopy3 = clone(bidderRequest.bids[0]);
const bidCopy3 = utils.deepClone(bidderRequest.bids[0]);
bidCopy3.params.siteId = '32001';
bidderRequest.bids.push(bidCopy3);

const bidCopy4 = clone(bidderRequest.bids[0]);
const bidCopy4 = utils.deepClone(bidderRequest.bids[0]);
bidCopy4.mediaTypes = {
video: {
context: 'instream',
Expand Down Expand Up @@ -1080,7 +1080,7 @@ describe('the rubicon adapter', function () {

describe('user id config', function() {
it('should send tpid_tdid when userId defines tdid', function () {
const clonedBid = clone(bidderRequest.bids[0]);
const clonedBid = utils.deepClone(bidderRequest.bids[0]);
clonedBid.userId = {
tdid: 'abcd-efgh-ijkl-mnop-1234'
};
Expand Down Expand Up @@ -1137,7 +1137,7 @@ describe('the rubicon adapter', function () {

it('should send request with proper ad position', function () {
createVideoBidderRequest();
let positionBidderRequest = clone(bidderRequest);
let positionBidderRequest = utils.deepClone(bidderRequest);
positionBidderRequest.bids[0].mediaTypes.video.pos = 1;
let [request] = spec.buildRequests(positionBidderRequest.bids, positionBidderRequest);
expect(request.data.imp[0].video.pos).to.equal(1);
Expand Down Expand Up @@ -1173,25 +1173,25 @@ describe('the rubicon adapter', function () {

it('should send request with proper ad position when mediaTypes.video.pos is not defined', function () {
createVideoBidderRequest();
let positionBidderRequest = clone(bidderRequest);
let positionBidderRequest = utils.deepClone(bidderRequest);
positionBidderRequest.bids[0].params.position = undefined;
positionBidderRequest.bids[0].mediaTypes.video.pos = undefined;
let [request] = spec.buildRequests(positionBidderRequest.bids, positionBidderRequest);
expect(request.data.imp[0].video.pos).to.equal(0);

positionBidderRequest = clone(bidderRequest);
positionBidderRequest = utils.deepClone(bidderRequest);
positionBidderRequest.bids[0].params.position = 'atf'
positionBidderRequest.bids[0].mediaTypes.video.pos = undefined;
[request] = spec.buildRequests(positionBidderRequest.bids, positionBidderRequest);
expect(request.data.imp[0].video.pos).to.equal(1);

positionBidderRequest = clone(bidderRequest);
positionBidderRequest = utils.deepClone(bidderRequest);
positionBidderRequest.bids[0].params.position = 'btf';
positionBidderRequest.bids[0].mediaTypes.video.pos = undefined;
[request] = spec.buildRequests(positionBidderRequest.bids, positionBidderRequest);
expect(request.data.imp[0].video.pos).to.equal(3);

positionBidderRequest = clone(bidderRequest);
positionBidderRequest = utils.deepClone(bidderRequest);
positionBidderRequest.bids[0].params.position = 'foobar';
positionBidderRequest.bids[0].mediaTypes.video.pos = undefined;
[request] = spec.buildRequests(positionBidderRequest.bids, positionBidderRequest);
Expand All @@ -1216,7 +1216,7 @@ describe('the rubicon adapter', function () {
bidderRequest.auctionStart + 100
);

const bidRequestCopy = clone(bidderRequest.bids[0]);
const bidRequestCopy = utils.deepClone(bidderRequest.bids[0]);
expect(spec.isBidRequestValid(bidRequestCopy)).to.equal(true);

// change context to outstream, still true
Expand Down Expand Up @@ -1302,7 +1302,7 @@ describe('the rubicon adapter', function () {
bidderRequest.auctionStart + 100
);

const bidRequestCopy = clone(bidderRequest);
const bidRequestCopy = utils.deepClone(bidderRequest);

let [request] = spec.buildRequests(bidRequestCopy.bids, bidRequestCopy);
expect(spec.isBidRequestValid(bidderRequest.bids[0])).to.equal(true);
Expand Down Expand Up @@ -1350,7 +1350,7 @@ describe('the rubicon adapter', function () {
bidderRequest.auctionStart + 100
);

const bidRequestCopy = clone(bidderRequest);
const bidRequestCopy = utils.deepClone(bidderRequest);

let requests = spec.buildRequests(bidRequestCopy.bids, bidRequestCopy);
expect(requests.length).to.equal(1);
Expand Down Expand Up @@ -1801,7 +1801,7 @@ describe('the rubicon adapter', function () {
};

let bids = spec.interpretResponse({ body: response }, {
bidRequest: [clone(bidderRequest.bids[0])]
bidRequest: [utils.deepClone(bidderRequest.bids[0])]
});

expect(bids).to.be.lengthOf(1);
Expand Down Expand Up @@ -2135,7 +2135,3 @@ describe('the rubicon adapter', function () {
});
});
});

function clone(obj) {
return JSON.parse(JSON.stringify(obj));
}