Skip to content

Commit

Permalink
Add code, test, and doc for Adikteev adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-yuxuan committed Oct 25, 2018
1 parent 5e27370 commit cbc27fe
Show file tree
Hide file tree
Showing 4 changed files with 357 additions and 2 deletions.
8 changes: 6 additions & 2 deletions integrationExamples/gpt/pbjs_example_gpt.html
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,12 @@
width: '300',
height: '250',
}
}

},
bidder: 'adikteev',
params: {
placementId: 1234 // REQUIRED
}
},
]
}, {
code: 'div-gpt-ad-12345678-1',
Expand Down
85 changes: 85 additions & 0 deletions modules/adikteevBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import {registerBidder} from 'src/adapters/bidderFactory';
import {BANNER} from 'src/mediaTypes';
import * as utils from '../src/utils';

export const BIDDER_CODE = 'adikteev';
export const ENDPOINT_URL = 'https://serve-adserver.adikteev.com/api/prebid/bid';
export const ENDPOINT_URL_STAGING = 'https://serve-adserver-staging.adikteev.com/api/prebid/bid';
export const USER_SYNC_IFRAME_URL = 'https://serve-adserver.adikteev.com/api/prebid/sync-iframe';
export const USER_SYNC_IFRAME_URL_STAGING = 'https://serve-adserver-staging.adikteev.com/api/prebid/sync-iframe';
export const USER_SYNC_IMAGE_URL = 'https://serve-adserver.adikteev.com/api/prebid/sync-image';
export const USER_SYNC_IMAGE_URL_STAGING = 'https://serve-adserver-staging.adikteev.com/api/prebid/sync-image';

export let stagingEnvironmentSwitch = false; // Don't use it. Allow us to make tests on staging

export function setstagingEnvironmentSwitch(value) {
stagingEnvironmentSwitch = value;
}

function validateSizes(sizes) {
if (!utils.isArray(sizes) || typeof sizes[0] === 'undefined') {
return false;
}
return sizes.every(size => utils.isArray(size) && size.length === 2);
}

export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER],

isBidRequestValid: (bid) => {
setstagingEnvironmentSwitch(stagingEnvironmentSwitch || !!bid.params.stagingEnvironment);
return !!(
bid &&
bid.params &&
bid.params.currency &&
bid.params.currency === 'EUR' &&
bid.params.bidFloorPrice &&
bid.params.placementId &&
bid.bidder === BIDDER_CODE &&
validateSizes(bid.mediaTypes.banner.sizes)
)
},

buildRequests: (validBidRequests, bidderRequest) => {
const payload = {
validBidRequests,
bidderRequest,
url: utils.getTopWindowUrl(),
referrer: utils.getTopWindowReferrer(),
userAgent: navigator.userAgent,
screen: {
width: window.screen.width,
height: window.screen.height
},
language: navigator.language,
cookies: document.cookie.split(';'),
};
const payloadString = JSON.stringify(payload);
return {
method: 'POST',
url: stagingEnvironmentSwitch ? ENDPOINT_URL_STAGING : ENDPOINT_URL,
data: payloadString,
};
},

interpretResponse: ({body}) => body,

getUserSyncs: (syncOptions, serverResponses) => {
const syncs = [];
if (syncOptions.iframeEnabled) {
syncs.push({
type: 'iframe',
url: stagingEnvironmentSwitch ? USER_SYNC_IFRAME_URL_STAGING : USER_SYNC_IFRAME_URL,
});
}
if (syncOptions.pixelEnabled && serverResponses.length > 0) {
syncs.push({
type: 'image',
url: stagingEnvironmentSwitch ? USER_SYNC_IMAGE_URL_STAGING : USER_SYNC_IMAGE_URL,
});
}
return syncs;
},
};
registerBidder(spec);
36 changes: 36 additions & 0 deletions modules/adikteevBidAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Overview

```
Module Name: Adikteev Bidder Adapter
Module Type: Bidder Adapter
Maintainer: [email protected]
```

# Description

Module that connects to Adikteev's demand sources

# Test Parameters

``` javascript
var adUnits = [
{
code: 'test-div',
mediaTypes: {
banner: {
sizes: [[750, 200]], // a display size
}
},
bids: [
{
bidder: 'adikteev',
params: {
placementId: 12345,
currency: 'EUR',
bidFloorPrice: 0.1,
}
}
]
}
];
```
230 changes: 230 additions & 0 deletions test/spec/modules/adikteevBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
import {expect} from 'chai';
import {
ENDPOINT_URL,
ENDPOINT_URL_STAGING,
setstagingEnvironmentSwitch,
spec,
stagingEnvironmentSwitch,
USER_SYNC_IFRAME_URL,
USER_SYNC_IFRAME_URL_STAGING,
USER_SYNC_IMAGE_URL,
USER_SYNC_IMAGE_URL_STAGING,
} from 'modules/adikteevBidAdapter';
import {newBidder} from 'src/adapters/bidderFactory';
import * as utils from '../../../src/utils';

describe('adikteevBidAdapter', () => {
const adapter = newBidder(spec);

describe('inherited functions', () => {
it('exists and is a function', () => {
expect(adapter.callBids).to.exist.and.to.be.a('function');
});
it('exists and is a function', () => {
expect(setstagingEnvironmentSwitch).to.exist.and.to.be.a('function');
});
it('exists and is correctly set', () => {
expect(stagingEnvironmentSwitch).to.exist.and.to.equal(false);
});
});

describe('isBidRequestValid', () => {
it('should return true when required params found', () => {
const validBid = {
bidder: 'adikteev',
params: {
placementId: 12345,
currency: 'EUR',
bidFloorPrice: 0.1,
},
mediaTypes: {
banner: {
sizes: [[750, 200]]
}
},
};
expect(spec.isBidRequestValid(validBid)).to.equal(true);
});

it('should mutate stagingEnvironmentSwitch when required params found', () => {
const withstagingEnvironmentSwitch = {
params: {
stagingEnvironment: true,
},
};
spec.isBidRequestValid(withstagingEnvironmentSwitch);
expect(stagingEnvironmentSwitch).to.equal(true);
setstagingEnvironmentSwitch(false);
});

it('should return false when required params are invalid', () => {
expect(spec.isBidRequestValid({
bidder: '', // invalid bidder
params: {
placementId: 12345,
currency: 'EUR',
bidFloorPrice: 0.1,
},
mediaTypes: {
banner: {
sizes: [[750, 200]]
}
},
})).to.equal(false);
expect(spec.isBidRequestValid({
bidder: 'adikteev',
params: {
placementId: '', // invalid placementId
currency: 'EUR',
bidFloorPrice: 0.1,
},
mediaTypes: {
banner: {
sizes: [[750, 200]]
}
},
})).to.equal(false);
expect(spec.isBidRequestValid({
bidder: 'adikteev',
params: {
placementId: 12345,
currency: 'EUR',
bidFloorPrice: 0.1,
},
mediaTypes: {
banner: {
sizes: [[750]] // invalid size
}
},
})).to.equal(false);
});
});

describe('buildRequests', () => {
const validBidRequests = [];
const bidderRequest = {};
const serverRequest = spec.buildRequests(validBidRequests, bidderRequest);
it('creates a request object with correct method, url and data', () => {
expect(serverRequest).to.exist.and.have.all.keys(
'method',
'url',
'data',
);
expect(serverRequest.method).to.equal('POST');
expect(serverRequest.url).to.equal(ENDPOINT_URL);

let requestData = JSON.parse(serverRequest.data);
expect(requestData).to.exist.and.have.all.keys(
'validBidRequests',
'bidderRequest',
'url',
'referrer',
'userAgent',
'screen',
'language',
'cookies',
);
expect(requestData.validBidRequests).to.deep.equal(validBidRequests);
expect(requestData.bidderRequest).to.deep.equal(bidderRequest);
expect(requestData.url).to.deep.equal(utils.getTopWindowUrl());
expect(requestData.referrer).to.deep.equal(utils.getTopWindowReferrer());
expect(requestData.userAgent).to.deep.equal(navigator.userAgent);
expect(requestData.screen.width).to.deep.equal(window.screen.width);
expect(requestData.screen.height).to.deep.equal(window.screen.height);
expect(requestData.language).to.deep.equal(navigator.language);
});

describe('staging environment', () => {
setstagingEnvironmentSwitch(true);
const serverRequest = spec.buildRequests(validBidRequests, bidderRequest);
expect(serverRequest.url).to.equal(ENDPOINT_URL_STAGING);
setstagingEnvironmentSwitch(false);
});
});

describe('interpretResponse', () => {
it('bid objects from response', () => {
const serverResponse =
{
body: [
{
requestId: '1258a5aa-d08f-11e8-a8d5-f2801f1b9fd1',
cpm: 1,
width: 300,
height: 250,
ad: '<div><script>var AK_CLICK_URL=\"http://www.bosch.com\";</script><script src=\"https://cdn-ww.adikteev.com/creatives/b4d4164d8f804d0ca6a4afa9cb3048fb.js\"></script></div>',
ttl: 360,
creativeId: 123,
netRevenue: false,
currency: 'EUR',
}
]
};
const bidResponses = spec.interpretResponse(serverResponse);
expect(bidResponses).to.be.an('array').that.is.not.empty; // yes, syntax is correct
expect(bidResponses[0]).to.have.all.keys(
'requestId',
'cpm',
'width',
'height',
'ad',
'ttl',
'creativeId',
'netRevenue',
'currency',
);

expect(bidResponses[0].requestId).to.equal(serverResponse.body[0].requestId);
expect(bidResponses[0].cpm).to.equal(serverResponse.body[0].cpm);
expect(bidResponses[0].width).to.equal(serverResponse.body[0].width);
expect(bidResponses[0].height).to.equal(serverResponse.body[0].height);
expect(bidResponses[0].ad).to.equal(serverResponse.body[0].ad);
expect(bidResponses[0].ttl).to.equal(serverResponse.body[0].ttl);
expect(bidResponses[0].creativeId).to.equal(serverResponse.body[0].creativeId);
expect(bidResponses[0].netRevenue).to.equal(serverResponse.body[0].netRevenue);
expect(bidResponses[0].currency).to.equal(serverResponse.body[0].currency);
});
});

describe('getUserSyncs', () => {
expect(spec.getUserSyncs({
iframeEnabled: true
}, [{}])).to.deep.equal([{
type: 'iframe',
url: USER_SYNC_IFRAME_URL
}]);

expect(spec.getUserSyncs({
pixelEnabled: true
}, [{}])).to.deep.equal([{
type: 'image',
url: USER_SYNC_IMAGE_URL
}]);

expect(spec.getUserSyncs({
iframeEnabled: true,
pixelEnabled: true
}, [{}])).to.deep.equal([{
type: 'iframe',
url: USER_SYNC_IFRAME_URL
}, {
type: 'image',
url: USER_SYNC_IMAGE_URL
}]);

describe('staging environment', () => {
setstagingEnvironmentSwitch(true);
expect(spec.getUserSyncs({
iframeEnabled: true,
pixelEnabled: true
}, [{}])).to.deep.equal([{
type: 'iframe',
url: USER_SYNC_IFRAME_URL_STAGING
}, {
type: 'image',
url: USER_SYNC_IMAGE_URL_STAGING
}]);
setstagingEnvironmentSwitch(false);
});
});
});

0 comments on commit cbc27fe

Please sign in to comment.