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

cointrafficBidAdapter: added support responding in different currencies #5800

Merged
merged 7 commits into from
Oct 1, 2020
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
20 changes: 18 additions & 2 deletions modules/cointrafficBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import * as utils from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER} from '../src/mediaTypes.js'
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER } from '../src/mediaTypes.js'
import { config } from '../src/config.js'

const BIDDER_CODE = 'cointraffic';
const ENDPOINT_URL = 'https://appspb.cointraffic.io/pb/tmp';
const DEFAULT_CURRENCY = 'EUR';
const ALLOWED_CURRENCIES = [
'EUR', 'USD', 'JPY', 'BGN', 'CZK', 'DKK', 'GBP', 'HUF', 'PLN', 'RON', 'SEK', 'CHF', 'ISK', 'NOK', 'HRK', 'RUB', 'TRY',
'AUD', 'BRL', 'CAD', 'CNY', 'HKD', 'IDR', 'ILS', 'INR', 'KRW', 'MXN', 'MYR', 'NZD', 'PHP', 'SGD', 'THB', 'ZAR',
];

export const spec = {
code: BIDDER_CODE,
Expand All @@ -29,9 +35,19 @@ export const spec = {
buildRequests: function (validBidRequests, bidderRequest) {
return validBidRequests.map(bidRequest => {
const sizes = utils.parseSizesInput(bidRequest.params.size || bidRequest.sizes);
const currency =
config.getConfig(`currency.bidderCurrencyDefault.${BIDDER_CODE}`) ||
config.getConfig('currency.adServerCurrency') ||
DEFAULT_CURRENCY;

if (ALLOWED_CURRENCIES.indexOf(currency) === -1) {
utils.logError('Currency is not supported - ' + currency);
return;
}

const payload = {
placementId: bidRequest.params.placementId,
currency: currency,
sizes: sizes,
bidId: bidRequest.bidId,
referer: bidderRequest.refererInfo.referer,
Expand Down
5 changes: 4 additions & 1 deletion modules/cointrafficBidAdapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ Maintainer: [email protected]
```

# Description
The Cointraffic client module makes it easy to implement Cointraffic directly into your website. To get started, simply replace the ``placementId`` with your assigned tracker key. This is dependent on the size required by your account dashboard. For additional information on this module, please contact us at ``[email protected]``.
The Cointraffic client module makes it easy to implement Cointraffic directly into your website. To get started, simply replace the ``placementId`` with your assigned tracker key. This is dependent on the size required by your account dashboard.
We support response in different currencies. Supported currencies listed [here](https://www.ecb.europa.eu/stats/policy_and_exchange_rates/euro_reference_exchange_rates/html/index.en.html).

For additional information on this module, please contact us at ``[email protected]``.

# Test Parameters
```
Expand Down
138 changes: 127 additions & 11 deletions test/spec/modules/cointrafficBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { expect } from 'chai';
import { spec } from 'modules/cointrafficBidAdapter.js';
import { config } from 'src/config.js'
import * as utils from 'src/utils.js'

const ENDPOINT_URL = 'https://appspb.cointraffic.io/pb/tmp';

Expand Down Expand Up @@ -37,7 +39,7 @@ describe('cointrafficBidAdapter', function () {
],
bidId: 'bidId12345',
bidderRequestId: 'bidderRequestId12345',
auctionId: 'auctionId12345',
auctionId: 'auctionId12345'
},
{
bidder: 'cointraffic',
Expand All @@ -50,7 +52,7 @@ describe('cointrafficBidAdapter', function () {
],
bidId: 'bidId67890"',
bidderRequestId: 'bidderRequestId67890',
auctionId: 'auctionId12345',
auctionId: 'auctionId12345'
}
];

Expand All @@ -65,34 +67,71 @@ describe('cointrafficBidAdapter', function () {
}
};

const request = spec.buildRequests(bidRequests, bidderRequests);
it('replaces currency with EUR if there is no currency provided', function () {
const request = spec.buildRequests(bidRequests, bidderRequests);

expect(request[0].data.currency).to.equal('EUR');
expect(request[1].data.currency).to.equal('EUR');
});

it('replaces currency with EUR if there is no currency provided', function () {
const getConfigStub = sinon.stub(config, 'getConfig').callsFake(
arg => arg === 'currency.bidderCurrencyDefault.cointraffic' ? 'USD' : 'EUR'
);

const request = spec.buildRequests(bidRequests, bidderRequests);

expect(request[0].data.currency).to.equal('USD');
expect(request[1].data.currency).to.equal('USD');

getConfigStub.restore();
});

it('throws an error if currency provided in params is not allowed', function () {
const utilsMock = sinon.mock(utils).expects('logError').twice()
const getConfigStub = sinon.stub(config, 'getConfig').callsFake(
arg => arg === 'currency.bidderCurrencyDefault.cointraffic' ? 'BTC' : 'EUR'
);

const request = spec.buildRequests(bidRequests, bidderRequests);

expect(request[0]).to.undefined;
expect(request[1]).to.undefined;

utilsMock.restore()
getConfigStub.restore();
});

it('sends bid request to our endpoint via POST', function () {
const request = spec.buildRequests(bidRequests, bidderRequests);

expect(request[0].method).to.equal('POST');
expect(request[1].method).to.equal('POST');
});

it('attaches source and version to endpoint URL as query params', function () {
const request = spec.buildRequests(bidRequests, bidderRequests);

expect(request[0].url).to.equal(ENDPOINT_URL);
expect(request[1].url).to.equal(ENDPOINT_URL);
});
});

describe('interpretResponse', function () {
let bidRequest = [
{
it('should get the correct bid response', function () {
let bidRequest = [{
method: 'POST',
url: ENDPOINT_URL,
data: {
placementId: 'testPlacementId',
device: 'desktop',
currency: 'EUR',
sizes: ['300x250'],
bidId: 'bidId12345',
referer: 'www.example.com'
}
}
];
}];

it('should get the correct bid response', function () {
let serverResponse = {
body: {
requestId: 'bidId12345',
Expand All @@ -103,7 +142,7 @@ describe('cointrafficBidAdapter', function () {
height: 250,
creativeId: 'creativeId12345',
ttl: 90,
ad: '<html><h3>I am an ad</h3></html> ',
ad: '<html><h3>I am an ad</h3></html> '
}
};

Expand All @@ -118,22 +157,99 @@ describe('cointrafficBidAdapter', function () {
ttl: 90,
ad: '<html><h3>I am an ad</h3></html>'
}];

let result = spec.interpretResponse(serverResponse, bidRequest[0]);
expect(Object.keys(result)).to.deep.equal(Object.keys(expectedResponse));
});

it('should get empty bid response if server response body is empty', function () {
it('should get the correct bid response with different currency', function () {
let bidRequest = [{
method: 'POST',
url: ENDPOINT_URL,
data: {
placementId: 'testPlacementId',
device: 'desktop',
currency: 'USD',
sizes: ['300x250'],
bidId: 'bidId12345',
referer: 'www.example.com'
}
}];

let serverResponse = {
body: {}
body: {
requestId: 'bidId12345',
cpm: 3.9,
currency: 'USD',
netRevenue: true,
width: 300,
height: 250,
creativeId: 'creativeId12345',
ttl: 90,
ad: '<html><h3>I am an ad</h3></html> '
}
};

let expectedResponse = [{
requestId: 'bidId12345',
cpm: 3.9,
currency: 'USD',
netRevenue: true,
width: 300,
height: 250,
creativeId: 'creativeId12345',
ttl: 90,
ad: '<html><h3>I am an ad</h3></html>'
}];

const getConfigStub = sinon.stub(config, 'getConfig').returns('USD');

let result = spec.interpretResponse(serverResponse, bidRequest[0]);
expect(Object.keys(result)).to.deep.equal(Object.keys(expectedResponse));

getConfigStub.restore();
});

it('should get empty bid response requested currency is not available', function () {
let bidRequest = [{
method: 'POST',
url: ENDPOINT_URL,
data: {
placementId: 'testPlacementId',
device: 'desktop',
currency: 'BTC',
sizes: ['300x250'],
bidId: 'bidId12345',
referer: 'www.example.com'
}
}];

let serverResponse = {};

let expectedResponse = [];

const getConfigStub = sinon.stub(config, 'getConfig').returns('BTC');

let result = spec.interpretResponse(serverResponse, bidRequest[0]);
expect(Object.keys(result)).to.deep.equal(Object.keys(expectedResponse));

getConfigStub.restore();
});

it('should get empty bid response if no server response', function () {
let bidRequest = [{
method: 'POST',
url: ENDPOINT_URL,
data: {
placementId: 'testPlacementId',
device: 'desktop',
currency: 'EUR',
sizes: ['300x250'],
bidId: 'bidId12345',
referer: 'www.example.com'
}
}];

let serverResponse = {};

let expectedResponse = [];
Expand Down