Skip to content

Commit

Permalink
Updates to meet 1.0 callBids/config changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcswart committed Dec 14, 2017
1 parent fb9cdc1 commit 15fc703
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 46 deletions.
58 changes: 41 additions & 17 deletions modules/serverbidServerBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import Adapter from 'src/adapter';
import bidfactory from 'src/bidfactory';
import bidmanager from 'src/bidmanager';
import * as utils from 'src/utils';
import { ajax } from 'src/ajax';
import adaptermanager from 'src/adaptermanager';
import { STATUS, S2S } from 'src/constants';
import { config } from 'src/config';

const TYPE = S2S.SRC;
const getConfig = config.getConfig;
const REQUIRED_S2S_CONFIG_KEYS = ['siteId', 'networkId', 'bidders', 'endpoint'];

let _s2sConfig;
config.setDefaults({
's2sConfig': {
enabled: false,
timeout: 1000,
adapter: 'serverbidServer'
}
});

var ServerBidServerAdapter;
ServerBidServerAdapter = function ServerBidServerAdapter() {
Expand Down Expand Up @@ -50,14 +61,25 @@ ServerBidServerAdapter = function ServerBidServerAdapter() {
sizeMap[123] = '970x250';
sizeMap[43] = '300x600';

let s2sConfig;
baseAdapter.setConfig = function(s2sconfig) {
s2sConfig = s2sconfig || {};
};
function getLocalConfig() {
return (_s2sConfig || {});
}

let _s2sConfig;
function setS2sConfig(options) {
let contains = (xs, x) => xs.indexOf(x) > -1;
let userConfig = Object.keys(options);

function getConfig() {
return (s2sConfig || {});
REQUIRED_S2S_CONFIG_KEYS.forEach(key => {
if (!contains(userConfig, key)) {
utils.logError(key + ' missing in server to server config');
return;
}
})

_s2sConfig = options;
}
getConfig('s2sConfig', ({s2sConfig}) => setS2sConfig(s2sConfig));

function _convertFields(bid) {
let safeBid = bid || {};
Expand All @@ -67,24 +89,27 @@ ServerBidServerAdapter = function ServerBidServerAdapter() {
return converted;
}

baseAdapter.callBids = function(params) {
baseAdapter.callBids = function(s2sBidRequest, bidRequests, addBidResponse, done, ajax) {
let params = s2sBidRequest;
let shouldDoWorkFn = function(bidRequest) {
return bidRequest &&
bidRequest.ad_units &&
utils.isArray(bidRequest.ad_units) &&
bidRequest.ad_units.length;
}
if (shouldDoWorkFn(params)) {
_callBids(params);
_callBids(s2sBidRequest, bidRequests, addBidResponse, done, ajax);
}
};

function _callBids(bidRequest) {
function _callBids(s2sBidRequest, bidRequests, addBidResponse, done, ajax) {
let bidRequest = s2sBidRequest;

// one request per ad unit
for (let i = 0; i < bidRequest.ad_units.length; i++) {
let adunit = bidRequest.ad_units[i];
let siteId = getConfig().siteId;
let networkId = getConfig().networkId;
let siteId = getLocalConfig().siteId;
let networkId = getLocalConfig().networkId;
let sizes = adunit.sizes;

const data = {
Expand All @@ -105,7 +130,7 @@ ServerBidServerAdapter = function ServerBidServerAdapter() {
const bid = bids[i];
bid.code = adunit.code;

const placement = Object.assign({
const placement = Object.assign({},{
divName: bid.bid_id,
networkId: networkId,
siteId: siteId,
Expand All @@ -120,12 +145,12 @@ ServerBidServerAdapter = function ServerBidServerAdapter() {
}

if (data.placements.length) {
ajax(BASE_URI, _responseCallback(bids), JSON.stringify(data), { method: 'POST', withCredentials: true, contentType: 'application/json' });
ajax(BASE_URI, _responseCallback(addBidResponse, bids), JSON.stringify(data), { method: 'POST', withCredentials: true, contentType: 'application/json' });
}
}
}

function _responseCallback(bids) {
function _responseCallback(addBidResponse, bids) {
return function (resp) {
let bid;
let bidId;
Expand Down Expand Up @@ -172,7 +197,7 @@ ServerBidServerAdapter = function ServerBidServerAdapter() {
} else {
bid = noBid(bidObj);
}
bidmanager.addBidResponse(placementCode, bid);
addBidResponse(placementCode, bid);
}
}
};
Expand All @@ -198,7 +223,6 @@ ServerBidServerAdapter = function ServerBidServerAdapter() {
// this function when the page asks to send out bid requests.
return Object.assign(this, {
queueSync: baseAdapter.queueSync,
setConfig: baseAdapter.setConfig,
callBids: baseAdapter.callBids,
setBidderCode: baseAdapter.setBidderCode,
type: TYPE
Expand Down
106 changes: 77 additions & 29 deletions test/spec/modules/serverbidServerBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { expect } from 'chai';
import Adapter from 'modules/serverbidServerBidAdapter';
import bidmanager from 'src/bidmanager';
import * as utils from 'src/utils';
import { config } from 'src/config';
import { ajax } from 'src/ajax';

const ENDPOINT = 'https://e.serverbid.com/api/v2';

let CONFIG = {
accountId: '1',
enabled: true,
bidders: ['appnexus'],
timeout: 1000,
Expand All @@ -16,6 +16,10 @@ let CONFIG = {
endpoint: ENDPOINT
};

let CONFIG_ARG = {
s2sConfig: CONFIG
}

const REQUEST = {
'account_id': '1',
'tid': '437fbbf5-33f5-487a-8e16-a7112903cfe5',
Expand Down Expand Up @@ -51,6 +55,40 @@ const REQUEST = {
]
};

const BID_REQUESTS = [
{
'bidderCode': 'appnexus',
'auctionId': '173afb6d132ba3',
'bidderRequestId': '3d1063078dfcc8',
'tid': '437fbbf5-33f5-487a-8e16-a7112903cfe5',
'bids': [
{
'bidder': 'appnexus',
'params': {
'placementId': '10433394',
'member': 123
},
'bid_id': '123',
'adUnitCode': 'div-gpt-ad-1460505748561-0',
'transactionId': '4ef956ad-fd83-406d-bd35-e4bb786ab86c',
'sizes': [
{
'w': 300,
'h': 250
}
],
'bidId': '259fb43aaa06c1',
'bidderRequestId': '3d1063078dfcc8',
'auctionId': '173afb6d132ba3'
}
],
'auctionStart': 1510852447530,
'timeout': 5000,
'src': 's2s',
'doneCbCallCount': 0
}
];

const RESPONSE = {
'user': { 'key': 'ue1-2d33e91b71e74929b4aeecc23f4376f1' },
'decisions': {
Expand Down Expand Up @@ -147,10 +185,17 @@ const REQUEST_TWO_UNITS = {
};

describe('ServerBid S2S Adapter', () => {
let adapter;
let adapter,
addBidResponse = sinon.spy(),
done = sinon.spy();

beforeEach(() => adapter = new Adapter());

afterEach(() => {
addBidResponse.reset();
done.reset();
});

describe('request function', () => {
let xhr;
let requests;
Expand All @@ -173,23 +218,25 @@ describe('ServerBid S2S Adapter', () => {

beforeEach(() => {
server = sinon.fakeServer.create();
sinon.stub(bidmanager, 'addBidResponse');
sinon.stub(utils, 'getBidRequest').returns({
bidId: '123'
});
});

afterEach(() => {
server.restore();
bidmanager.addBidResponse.restore();
utils.getBidRequest.restore();
});

it('registers bids', () => {
server.respondWith(JSON.stringify(RESPONSE));

adapter.setConfig(CONFIG);
adapter.callBids(REQUEST);
config.setConfig(CONFIG_ARG);
adapter.callBids(REQUEST, BID_REQUESTS, addBidResponse, done, ajax);
server.respond();
sinon.assert.calledOnce(bidmanager.addBidResponse);
sinon.assert.calledOnce(addBidResponse);

const response = bidmanager.addBidResponse.firstCall.args[1];
const response = addBidResponse.firstCall.args[1];
expect(response).to.have.property('statusMessage', 'Bid available');
expect(response).to.have.property('cpm', 0.5);
expect(response).to.have.property('adId', '123');
Expand All @@ -198,55 +245,56 @@ describe('ServerBid S2S Adapter', () => {
it('registers no-bid response when ad unit not set', () => {
server.respondWith(JSON.stringify(RESPONSE_NO_BID_NO_UNIT));

adapter.setConfig(CONFIG);
adapter.callBids(REQUEST);
config.setConfig(CONFIG_ARG);
adapter.callBids(REQUEST, BID_REQUESTS, addBidResponse, done, ajax);
server.respond();
sinon.assert.calledOnce(bidmanager.addBidResponse);
sinon.assert.calledOnce(addBidResponse);

const ad_unit_code = bidmanager.addBidResponse.firstCall.args[0];
const ad_unit_code = addBidResponse.firstCall.args[0];
expect(ad_unit_code).to.equal('div-gpt-ad-1460505748561-0');

const response = bidmanager.addBidResponse.firstCall.args[1];
const response = addBidResponse.firstCall.args[1];
expect(response).to.have.property('statusMessage', 'Bid returned empty or error response');

const bid_request_passed = bidmanager.addBidResponse.firstCall.args[1];
const bid_request_passed = addBidResponse.firstCall.args[1];
expect(bid_request_passed).to.have.property('adId', '123');
});

it('registers no-bid response when ad unit is set', () => {
server.respondWith(JSON.stringify(RESPONSE_NO_BID_NO_UNIT));

adapter.setConfig(CONFIG);
adapter.callBids(REQUEST);
config.setConfig(CONFIG_ARG);
adapter.callBids(REQUEST, BID_REQUESTS, addBidResponse, done, ajax);
server.respond();
sinon.assert.calledOnce(bidmanager.addBidResponse);
sinon.assert.calledOnce(addBidResponse);

const ad_unit_code = bidmanager.addBidResponse.firstCall.args[0];
const ad_unit_code = addBidResponse.firstCall.args[0];
expect(ad_unit_code).to.equal('div-gpt-ad-1460505748561-0');

const response = bidmanager.addBidResponse.firstCall.args[1];
const response = addBidResponse.firstCall.args[1];
expect(response).to.have.property('statusMessage', 'Bid returned empty or error response');
});

it('registers no-bid response when there are less bids than requests', () => {
server.respondWith(JSON.stringify(RESPONSE));

adapter.setConfig(CONFIG);
adapter.callBids(REQUEST_TWO_UNITS);
config.setConfig(CONFIG_ARG);
adapter.callBids(REQUEST_TWO_UNITS, BID_REQUESTS, addBidResponse, done, ajax);
server.respond();

sinon.assert.calledTwice(bidmanager.addBidResponse);
sinon.assert.calledTwice(addBidResponse);

expect(bidmanager.addBidResponse.firstCall.args[0]).to.equal('div-gpt-ad-1460505748561-0');
expect(bidmanager.addBidResponse.secondCall.args[0]).to.equal('div-gpt-ad-1460505748561-1');
expect(addBidResponse.firstCall.args[0]).to.equal('div-gpt-ad-1460505748561-0');
expect(addBidResponse.secondCall.args[0]).to.equal('div-gpt-ad-1460505748561-1');

expect(bidmanager.addBidResponse.firstCall.args[1]).to.have.property('adId', '123');
expect(bidmanager.addBidResponse.secondCall.args[1]).to.have.property('adId', '101111');
expect(addBidResponse.firstCall.args[1]).to.have.property('adId', '123');
expect(addBidResponse.secondCall.args[1]).to.have.property('adId', '101111');

expect(bidmanager.addBidResponse.firstCall.args[1])
expect(addBidResponse.firstCall.args[1])
.to.have.property('statusMessage', 'Bid available');
expect(bidmanager.addBidResponse.secondCall.args[1])
expect(addBidResponse.secondCall.args[1])
.to.have.property('statusMessage', 'Bid returned empty or error response');
});

});
});

0 comments on commit 15fc703

Please sign in to comment.