Skip to content

Commit

Permalink
adding support in pbsAdapter for getFloor (#6273)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertrmartinez authored Feb 18, 2021
1 parent 0939147 commit 302b788
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
17 changes: 17 additions & 0 deletions modules/prebidServerBidAdapter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,23 @@ const OPEN_RTB_PROTOCOL = {
utils.deepSetValue(imp, 'ext.prebid.storedauctionresponse.id', storedAuctionResponseBid.storedAuctionResponse.toString());
}

const getFloorBid = find(firstBidRequest.bids, bid => bid.adUnitCode === adUnit.code && typeof bid.getFloor === 'function');

if (getFloorBid) {
let floorInfo;
try {
floorInfo = getFloorBid.getFloor({
currency: config.getConfig('currency.adServerCurrency') || DEFAULT_S2S_CURRENCY,
});
} catch (e) {
utils.logError('PBS: getFloor threw an error: ', e);
}
if (floorInfo && floorInfo.currency && !isNaN(parseFloat(floorInfo.floor))) {
imp.bidfloor = parseFloat(floorInfo.floor);
imp.bidfloorcur = floorInfo.currency
}
}

if (imp.banner || imp.video || imp.native) {
imps.push(imp);
}
Expand Down
95 changes: 95 additions & 0 deletions test/spec/modules/prebidServerBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,101 @@ describe('S2S Adapter', function () {
expect(requestBid.imp[0].ext.prebid.storedauctionresponse.id).to.equal('11111');
});

describe('price floors module', function () {
function runTest(expectedFloor, expectedCur) {
adapter.callBids(REQUEST, BID_REQUESTS, addBidResponse, done, ajax);
const requestBid = JSON.parse(server.requests[requestCount].requestBody);
expect(requestBid.imp[0].bidfloor).to.equal(expectedFloor);
expect(requestBid.imp[0].bidfloorcur).to.equal(expectedCur);
requestCount += 1;
}

let getFloorResponse, requestCount;
beforeEach(function () {
getFloorResponse = {};
requestCount = 0;
});

it('should NOT pass bidfloor and bidfloorcur when getFloor not present or returns invalid response', function () {
const _config = {
s2sConfig: CONFIG,
};

config.setConfig(_config);

// if no get floor
runTest(undefined, undefined);

// if getFloor returns empty object
BID_REQUESTS[0].bids[0].getFloor = () => getFloorResponse;
sinon.spy(BID_REQUESTS[0].bids[0], 'getFloor');

runTest(undefined, undefined);
// make sure getFloor was called
expect(
BID_REQUESTS[0].bids[0].getFloor.calledWith({
currency: 'USD',
})
).to.be.true;

// if getFloor does not return number
getFloorResponse = {currency: 'EUR', floor: 'not a number'};
runTest(undefined, undefined);

// if getFloor does not return currency
getFloorResponse = {floor: 1.1};
runTest(undefined, undefined);
});

it('should correctly pass bidfloor and bidfloorcur', function () {
const _config = {
s2sConfig: CONFIG,
};

config.setConfig(_config);

BID_REQUESTS[0].bids[0].getFloor = () => getFloorResponse;
sinon.spy(BID_REQUESTS[0].bids[0], 'getFloor');

// returns USD and string floor
getFloorResponse = {currency: 'USD', floor: '1.23'};
runTest(1.23, 'USD');
// make sure getFloor was called
expect(
BID_REQUESTS[0].bids[0].getFloor.calledWith({
currency: 'USD',
})
).to.be.true;

// returns non USD and number floor
getFloorResponse = {currency: 'EUR', floor: 0.85};
runTest(0.85, 'EUR');
});

it('should correctly pass adServerCurrency when set to getFloor not default', function () {
config.setConfig({
s2sConfig: CONFIG,
currency: { adServerCurrency: 'JPY' },
});

// we have to start requestCount at 1 because a conversion rates fetch occurs when adServerCur is not USD!
requestCount = 1;

BID_REQUESTS[0].bids[0].getFloor = () => getFloorResponse;
sinon.spy(BID_REQUESTS[0].bids[0], 'getFloor');

// returns USD and string floor
getFloorResponse = {currency: 'JPY', floor: 97.2};
runTest(97.2, 'JPY');
// make sure getFloor was called with JPY
expect(
BID_REQUESTS[0].bids[0].getFloor.calledWith({
currency: 'JPY',
})
).to.be.true;
});
});

it('adds device.w and device.h even if the config lacks a device object', function () {
const _config = {
s2sConfig: CONFIG,
Expand Down

0 comments on commit 302b788

Please sign in to comment.