Skip to content

Commit

Permalink
calling for currency file even when default rates are specified (#3470)
Browse files Browse the repository at this point in the history
* calling for currency file even when default rates are specified

* fixed broken unit tests

* added unit test to check that currency file is still requested even when default rates are set
  • Loading branch information
bretg authored and mkendall07 committed Jan 22, 2019
1 parent 55de081 commit f26003d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
7 changes: 6 additions & 1 deletion modules/currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const CURRENCY_RATE_PRECISION = 4;
var bidResponseQueue = [];
var conversionCache = {};
var currencyRatesLoaded = false;
var needToCallForCurrencyFile = true;
var adServerCurrency = 'USD';

export var currencySupportEnabled = false;
Expand Down Expand Up @@ -56,6 +57,7 @@ export function setConfig(config) {
if (typeof config.rates === 'object') {
currencyRates.conversions = config.rates;
currencyRatesLoaded = true;
needToCallForCurrencyFile = false; // don't call if rates are already specified
}

if (typeof config.defaultRates === 'object') {
Expand Down Expand Up @@ -122,7 +124,9 @@ function initCurrency(url) {

hooks['addBidResponse'].addHook(addBidResponseHook, 100);

if (!currencyRates.conversions) {
// call for the file if we haven't already
if (needToCallForCurrencyFile) {
needToCallForCurrencyFile = false;
ajax(url,
{
success: function (response) {
Expand Down Expand Up @@ -150,6 +154,7 @@ function resetCurrency() {
conversionCache = {};
currencySupportEnabled = false;
currencyRatesLoaded = false;
needToCallForCurrencyFile = true;
currencyRates = {};
bidderCurrencyDefault = {};
}
Expand Down
40 changes: 40 additions & 0 deletions test/spec/modules/currency_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,33 @@ describe('currency', function () {
expect(currencySupportEnabled).to.equal(true);
});

it('currency file is called even when default rates are specified', function() {
// RESET to request currency file (specifically url value for this test)
setConfig({ 'adServerCurrency': undefined });

// DO NOT SET DEFAULT RATES, currency file should be requested
setConfig({
'adServerCurrency': 'JPY'
});
fakeCurrencyFileServer.respond();
expect(fakeCurrencyFileServer.requests.length).to.equal(1);
expect(fakeCurrencyFileServer.requests[0].url).to.equal('https://cdn.jsdelivr.net/gh/prebid/currency-file@1/latest.json?date=20030306');

// RESET to request currency file (specifically url value for this test)
setConfig({ 'adServerCurrency': undefined });

// SET DEFAULT RATES, currency file should STILL be requested
setConfig({
'adServerCurrency': 'JPY',
'defaultRates': {
'GBP': { 'CNY': 66, 'JPY': 132, 'USD': 264 },
'USD': { 'CNY': 60, 'GBP': 120, 'JPY': 240 }
} });
fakeCurrencyFileServer.respond();
expect(fakeCurrencyFileServer.requests.length).to.equal(2);
expect(fakeCurrencyFileServer.requests[1].url).to.equal('https://cdn.jsdelivr.net/gh/prebid/currency-file@1/latest.json?date=20030306');
});

it('date macro token $$TODAY$$ is replaced by current date (formatted as yyyymmdd)', function () {
// RESET to request currency file (specifically url value for this test)
setConfig({ 'adServerCurrency': undefined });
Expand All @@ -64,6 +91,9 @@ describe('currency', function () {
fakeCurrencyFileServer.respond();
expect(fakeCurrencyFileServer.requests[0].url).to.equal('https://cdn.jsdelivr.net/gh/prebid/currency-file@1/latest.json?date=20030306');

// RESET to request currency file (specifically url value for this test)
setConfig({ 'adServerCurrency': undefined });

// date macro should not modify 'conversionRateFile' if TOKEN is not found
setConfig({
'adServerCurrency': 'JPY',
Expand All @@ -72,6 +102,9 @@ describe('currency', function () {
fakeCurrencyFileServer.respond();
expect(fakeCurrencyFileServer.requests[1].url).to.equal('http://test.net/currency.json?date=foobar');

// RESET to request currency file (specifically url value for this test)
setConfig({ 'adServerCurrency': undefined });

// date macro should replace $$TODAY$$ with date for 'conversionRateFile' is configured
setConfig({
'adServerCurrency': 'JPY',
Expand All @@ -80,6 +113,9 @@ describe('currency', function () {
fakeCurrencyFileServer.respond();
expect(fakeCurrencyFileServer.requests[2].url).to.equal('http://test.net/currency.json?date=20030306');

// RESET to request currency file (specifically url value for this test)
setConfig({ 'adServerCurrency': undefined });

// MULTIPLE TOKENS used in a url is not supported. Only the TOKEN at left-most position is REPLACED
setConfig({
'adServerCurrency': 'JPY',
Expand Down Expand Up @@ -220,6 +256,7 @@ describe('currency', function () {

it('should result in NO_BID when currency support is not enabled and fromCurrency is not USD', function () {
setConfig({});

var bid = { 'cpm': 1, 'currency': 'GBP' };
var innerBid;
addBidResponseHook('elementId', bid, function(adCodeId, bid) {
Expand All @@ -241,6 +278,9 @@ describe('currency', function () {
});

it('should result in NO_BID when fromCurrency is not supported in file', function () {
// RESET to request currency file
setConfig({ 'adServerCurrency': undefined });

fakeCurrencyFileServer.respondWith(JSON.stringify(getCurrencyRates()));
setConfig({ 'adServerCurrency': 'JPY' });
fakeCurrencyFileServer.respond();
Expand Down

0 comments on commit f26003d

Please sign in to comment.