From 93c8fbf504af8cb4e66804768217200a7e2e9620 Mon Sep 17 00:00:00 2001 From: FeatureForward Date: Sun, 15 Oct 2017 10:02:11 +0300 Subject: [PATCH 1/6] small fix - allow more than one placement from one page --- modules/featureforwardBidAdapter.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/featureforwardBidAdapter.js b/modules/featureforwardBidAdapter.js index 34d7dddba49..fcf719903ac 100644 --- a/modules/featureforwardBidAdapter.js +++ b/modules/featureforwardBidAdapter.js @@ -13,9 +13,9 @@ function FeatureForwardAdapter() { contentType: 'text/plain' }; - function _callBids(bidderRequest) { - var i = 0; + function _callBids(bidderRequest) { bidderRequest.bids.forEach(bidRequest => { + var i = 0; try { while (bidRequest.sizes[i] !== undefined) { var params = Object.assign({}, environment(), bidRequest.params, {'size': bidRequest.sizes[i]}); From c5bbd92ef399a1669eed74554c80784e0b6f4f34 Mon Sep 17 00:00:00 2001 From: FeatureForward Date: Sun, 15 Oct 2017 10:35:15 +0300 Subject: [PATCH 2/6] remove trailing spaces --- modules/featureforwardBidAdapter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/featureforwardBidAdapter.js b/modules/featureforwardBidAdapter.js index fcf719903ac..53a3a7c5a08 100644 --- a/modules/featureforwardBidAdapter.js +++ b/modules/featureforwardBidAdapter.js @@ -13,7 +13,7 @@ function FeatureForwardAdapter() { contentType: 'text/plain' }; - function _callBids(bidderRequest) { + function _callBids(bidderRequest) { bidderRequest.bids.forEach(bidRequest => { var i = 0; try { From f39bd3180d671955caaf26808b5cd214f725dea9 Mon Sep 17 00:00:00 2001 From: FeatureForward Date: Mon, 16 Oct 2017 11:22:16 +0300 Subject: [PATCH 3/6] Delete trionBidAdapter.js seems like this adapters test failing the automatic Travis build --- modules/trionBidAdapter.js | 129 ------------------------------------- 1 file changed, 129 deletions(-) delete mode 100644 modules/trionBidAdapter.js diff --git a/modules/trionBidAdapter.js b/modules/trionBidAdapter.js deleted file mode 100644 index 642f8194b7f..00000000000 --- a/modules/trionBidAdapter.js +++ /dev/null @@ -1,129 +0,0 @@ -var CONSTANTS = require('src/constants.json'); -var utils = require('src/utils.js'); -var adloader = require('src/adloader.js'); -var bidmanager = require('src/bidmanager.js'); -var bidfactory = require('src/bidfactory.js'); -var Adapter = require('src/adapter.js').default; -var adaptermanager = require('src/adaptermanager'); - -const BID_REQUEST_BASE_URL = 'https://in-appadvertising.com/api/bidRequest?'; -const USER_SYNC_URL = 'https://in-appadvertising.com/api/userSync.js'; - -function TrionAdapter() { - var baseAdapter = new Adapter('trion'); - var userTag = null; - - baseAdapter.callBids = function (params) { - var bids = params.bids || []; - - if (!bids.length) { - return; - } - - if (!window.TRION_INT) { - adloader.loadScript(USER_SYNC_URL, function () { - userTag = window.TRION_INT || {}; - userTag.pubId = utils.getBidIdParameter('pubId', bids[0].params); - userTag.sectionId = utils.getBidIdParameter('sectionId', bids[0].params); - if (!userTag.to) { - getBids(bids); - } else { - setTimeout(function () { - getBids(bids); - }, userTag.to); - } - }, true); - } else { - userTag = window.TRION_INT; - getBids(bids); - } - }; - - function getBids(bids) { - if (!userTag.int_t) { - userTag.int_t = window.TR_INT_T || -1; - } - - for (var i = 0; i < bids.length; i++) { - var bidRequest = bids[i]; - var bidId = bidRequest.bidId; - adloader.loadScript(buildTrionUrl(bidRequest, bidId)); - } - } - - function buildTrionUrl(bid, bidId) { - var pubId = utils.getBidIdParameter('pubId', bid.params); - var sectionId = utils.getBidIdParameter('sectionId', bid.params); - var re = utils.getBidIdParameter('re', bid.params); - var url = utils.getTopWindowUrl(); - var sizes = utils.parseSizesInput(bid.sizes).join(','); - - var trionUrl = BID_REQUEST_BASE_URL; - - trionUrl = utils.tryAppendQueryString(trionUrl, 'callback', '$$PREBID_GLOBAL$$.handleTrionCB'); - trionUrl = utils.tryAppendQueryString(trionUrl, 'bidId', bidId); - trionUrl = utils.tryAppendQueryString(trionUrl, 'pubId', pubId); - trionUrl = utils.tryAppendQueryString(trionUrl, 'sectionId', sectionId); - trionUrl = utils.tryAppendQueryString(trionUrl, 're', re); - trionUrl = utils.tryAppendQueryString(trionUrl, 'slot', bid.placementCode); - if (url) { - trionUrl += 'url=' + url + '&'; - } - if (sizes) { - trionUrl += 'sizes=' + sizes + '&'; - } - if (userTag) { - trionUrl += 'tag=' + encodeURIComponent(JSON.stringify(userTag)) + '&'; - } - - // remove the trailing "&" - if (trionUrl.lastIndexOf('&') === trionUrl.length - 1) { - trionUrl = trionUrl.substring(0, trionUrl.length - 1); - } - - return trionUrl; - } - - // expose the callback to the global object: - $$PREBID_GLOBAL$$.handleTrionCB = function (trionResponseObj) { - var bid; - var bidObj = {}; - var placementCode = ''; - - if (trionResponseObj && trionResponseObj.bidId) { - var bidCode; - var bidId = trionResponseObj.bidId; - var result = trionResponseObj && trionResponseObj.result; - - bidObj = utils.getBidRequest(bidId); - if (bidObj) { - bidCode = bidObj.bidder; - placementCode = bidObj.placementCode; - } - - if (result && result.cpm && result.placeBid && result.ad) { - var cpm = parseInt(result.cpm, 10) / 100; - bid = bidfactory.createBid(CONSTANTS.STATUS.GOOD, bidObj); - bid.bidderCode = bidCode; - bid.cpm = cpm; - bid.ad = result.ad; - bid.width = result.width; - bid.height = result.height; - } - } - if (!bid) { - bid = bidfactory.createBid(CONSTANTS.STATUS.NO_BID, bidObj); - } - bidmanager.addBidResponse(placementCode, bid); - }; - - return Object.assign(this, { - callBids: baseAdapter.callBids, - setBidderCode: baseAdapter.setBidderCode, - buildTrionUrl: buildTrionUrl - }); -} - -adaptermanager.registerBidAdapter(new TrionAdapter(), 'trion'); - -module.exports = TrionAdapter; From c1392e997fa727e196d4f67dcfff4071a6316736 Mon Sep 17 00:00:00 2001 From: FeatureForward Date: Mon, 16 Oct 2017 11:23:51 +0300 Subject: [PATCH 4/6] Delete trionBidAdapter_spec.js Removed due to Travis build failure --- test/spec/modules/trionBidAdapter_spec.js | 274 ---------------------- 1 file changed, 274 deletions(-) delete mode 100644 test/spec/modules/trionBidAdapter_spec.js diff --git a/test/spec/modules/trionBidAdapter_spec.js b/test/spec/modules/trionBidAdapter_spec.js deleted file mode 100644 index 5a9c92ef91e..00000000000 --- a/test/spec/modules/trionBidAdapter_spec.js +++ /dev/null @@ -1,274 +0,0 @@ -import { expect } from 'chai'; -import TrionAdapter from 'modules/trionBidAdapter'; -import bidmanager from 'src/bidmanager'; -import * as utils from 'src/utils'; -const CONSTANTS = require('src/constants.json'); -const adloader = require('src/adloader'); - -const PLACEMENT_CODE = 'ad-tag'; -const BID_REQUEST_BASE_URL = 'https://in-appadvertising.com/api/bidRequest?'; -const USER_SYNC_URL = 'https://in-appadvertising.com/api/userSync.js'; - -const TRION_BID_REQUEST = { - start: new Date().getTime(), - bidderCode: 'trion', - bids: [ - { - bidder: 'trion', - params: { - pubId: '1', - sectionId: '2' - }, - placementCode: PLACEMENT_CODE, - sizes: [[300, 250], [300, 600]], - bidId: 'test-bid-id' - } - ] -}; - -const TRION_BID_RESPONSE = { - bidId: 'test-bid-id', - sizes: [[300, 250], [300, 600]], - result: { - cpm: 100, - placeBid: true, - height: '250', - width: '300', - ad: 'test', - msg: 'response messaging' - } - -}; - -describe('Trion adapter tests', () => { - let adapter; - - beforeEach(() => { - adapter = new TrionAdapter(); - sinon.stub(document.body, 'appendChild'); - }); - - afterEach(() => document.body.appendChild.restore()); - - it('should exist and be a function', function () { - expect($$PREBID_GLOBAL$$.handleTrionCB).to.exist.and.to.be.a('function'); - }); - - describe('request function', () => { - let spyLoadScript; - - beforeEach(() => { - spyLoadScript = sinon.spy(adloader, 'loadScript'); - window.TRION_INT = { - int_t: -1 - }; - }); - - afterEach(() => { - spyLoadScript.restore(); - delete window.TRION_INT; - }); - - it('callBids exists and is a function', () => { - expect(adapter.callBids).to.exist.and.to.be.a('function'); - }); - - it('should not call loadscript when inputting with empty params', function () { - adapter.callBids({}); - sinon.assert.notCalled(spyLoadScript); - }); - - it('should include the base bidrequest url', function () { - adapter.callBids(TRION_BID_REQUEST); - - sinon.assert.calledOnce(spyLoadScript); - - let bidUrl = spyLoadScript.getCall(0).args[0]; - expect(bidUrl).to.include(BID_REQUEST_BASE_URL); - }); - - it('should call loadscript with the correct required params', function () { - adapter.callBids(TRION_BID_REQUEST); - - sinon.assert.calledOnce(spyLoadScript); - - let bidUrl = spyLoadScript.getCall(0).args[0]; - expect(bidUrl).to.include('pubId=1'); - expect(bidUrl).to.include('sectionId=2'); - expect(bidUrl).to.include('sizes=300x250,300x600'); - }); - - it('should call loadscript with the correct optional params', function () { - let params = TRION_BID_REQUEST.bids[0].params; - params.re = 1; - - adapter.callBids(TRION_BID_REQUEST); - - sinon.assert.calledOnce(spyLoadScript); - - let bidUrl = spyLoadScript.getCall(0).args[0]; - expect(bidUrl).to.include('re=1'); - expect(bidUrl).to.include(utils.getTopWindowUrl()); - expect(bidUrl).to.include('slot=' + PLACEMENT_CODE); - delete params.re; - }); - - describe('user sync', () => { - beforeEach(() => { - delete window.TRION_INT; - delete window.TR_INT_T; - }); - - it('user sync is called', () => { - adapter.callBids(TRION_BID_REQUEST); - sinon.assert.calledWith(spyLoadScript, USER_SYNC_URL); - }); - - it('user sync tag is included in bid url', () => { - window.TRION_INT = { - campaigns: [ - 'campaign1', - 'campaign2' - ], - int_t: 'int_t' - }; - let userTag = encodeURIComponent(JSON.stringify(window.TRION_INT)); - adapter.callBids(TRION_BID_REQUEST); - - let bidUrl = spyLoadScript.getCall(0).args[0]; - expect(bidUrl).to.include(userTag); - }); - - it('user sync tag is included in bid url and includes the correct int_t', () => { - window.TRION_INT = { - campaigns: [ - 'campaign1', - 'campaign2' - ] - }; - let int_t = 'test'; - let expectedObject = { - campaigns: [ - 'campaign1', - 'campaign2' - ], - int_t: int_t - }; - window.TR_INT_T = int_t; - let userTag = encodeURIComponent(JSON.stringify(expectedObject)); - adapter.callBids(TRION_BID_REQUEST); - - let bidUrl = spyLoadScript.getCall(0).args[0]; - expect(bidUrl).to.include(userTag); - }); - - it('user sync tag variable int_t cannot be changed once set', () => { - window.TRION_INT = { - campaigns: [ - 'campaign1', - 'campaign2' - ] - }; - let int_t = 'test'; - let expectedObject = { - campaigns: [ - 'campaign1', - 'campaign2' - ], - int_t: int_t - }; - window.TR_INT_T = int_t; - let userTag = encodeURIComponent(JSON.stringify(expectedObject)); - adapter.callBids(TRION_BID_REQUEST); - window.TR_INT_T = 'bad'; - let bidUrl = spyLoadScript.getCall(0).args[0]; - - expect(bidUrl).to.include(userTag); - expect(bidUrl).to.not.include('bad'); - }); - }); - }); - - describe('response handler', () => { - beforeEach(() => { - sinon.stub(bidmanager, 'addBidResponse'); - }); - - afterEach(() => { - bidmanager.addBidResponse.restore(); - }); - - it('when there is no response do not bid', function () { - $$PREBID_GLOBAL$$._bidsRequested.push(TRION_BID_REQUEST); - $$PREBID_GLOBAL$$.handleTrionCB(); - sinon.assert.calledOnce(bidmanager.addBidResponse); - const response = bidmanager.addBidResponse.firstCall.args[1]; - expect(response.getStatusCode()).to.equal(CONSTANTS.STATUS.NO_BID); - }); - - it('when place bid is returned as false', function () { - TRION_BID_RESPONSE.result.placeBid = false; - $$PREBID_GLOBAL$$._bidsRequested.push(TRION_BID_REQUEST); - $$PREBID_GLOBAL$$.handleTrionCB(TRION_BID_RESPONSE); - sinon.assert.calledOnce(bidmanager.addBidResponse); - const response = bidmanager.addBidResponse.firstCall.args[1]; - expect(response.getStatusCode()).to.equal(CONSTANTS.STATUS.NO_BID); - TRION_BID_RESPONSE.result.placeBid = true; - }); - - it('when no cpm is in the response', function () { - TRION_BID_RESPONSE.result.cpm = 0; - $$PREBID_GLOBAL$$._bidsRequested.push(TRION_BID_REQUEST); - $$PREBID_GLOBAL$$.handleTrionCB(TRION_BID_RESPONSE); - sinon.assert.calledOnce(bidmanager.addBidResponse); - const response = bidmanager.addBidResponse.firstCall.args[1]; - expect(response.getStatusCode()).to.equal(CONSTANTS.STATUS.NO_BID); - TRION_BID_RESPONSE.result.cpm = 1; - }); - - it('when no ad is in the response', function () { - TRION_BID_RESPONSE.result.ad = null; - $$PREBID_GLOBAL$$._bidsRequested.push(TRION_BID_REQUEST); - $$PREBID_GLOBAL$$.handleTrionCB(TRION_BID_RESPONSE); - sinon.assert.calledOnce(bidmanager.addBidResponse); - const response = bidmanager.addBidResponse.firstCall.args[1]; - expect(response.getStatusCode()).to.equal(CONSTANTS.STATUS.NO_BID); - TRION_BID_RESPONSE.result.ad = 'test'; - }); - - it('bid response is formatted correctly', function () { - $$PREBID_GLOBAL$$._bidsRequested.push(TRION_BID_REQUEST); - $$PREBID_GLOBAL$$.handleTrionCB(TRION_BID_RESPONSE); - const placementCode = bidmanager.addBidResponse.firstCall.args[0]; - const response = bidmanager.addBidResponse.firstCall.args[1]; - expect(placementCode).to.equal(PLACEMENT_CODE); - expect(response.getStatusCode()).to.equal(CONSTANTS.STATUS.GOOD); - expect(response.bidderCode).to.equal('trion'); - }); - - it('height and width are appropriately set', function () { - let bidWidth = '1'; - let bidHeight = '2'; - TRION_BID_RESPONSE.result.width = bidWidth; - TRION_BID_RESPONSE.result.height = bidHeight; - $$PREBID_GLOBAL$$._bidsRequested.push(TRION_BID_REQUEST); - $$PREBID_GLOBAL$$.handleTrionCB(TRION_BID_RESPONSE); - const placementCode = bidmanager.addBidResponse.firstCall.args[0]; - const response = bidmanager.addBidResponse.firstCall.args[1]; - expect(response.width).to.equal(bidWidth); - expect(response.height).to.equal(bidHeight); - TRION_BID_RESPONSE.result.width = '300'; - TRION_BID_RESPONSE.result.height = '250'; - }); - - it('cpm is properly set and transformed to cents', function () { - let bidCpm = 2; - TRION_BID_RESPONSE.result.cpm = bidCpm * 100; - $$PREBID_GLOBAL$$._bidsRequested.push(TRION_BID_REQUEST); - $$PREBID_GLOBAL$$.handleTrionCB(TRION_BID_RESPONSE); - const response = bidmanager.addBidResponse.firstCall.args[1]; - expect(response.cpm).to.equal(bidCpm); - TRION_BID_RESPONSE.result.cpm = 100; - }); - }); -}); From ce6cc161be67496fffccea676287ccd83272a166 Mon Sep 17 00:00:00 2001 From: FeatureForward Date: Thu, 19 Oct 2017 10:59:47 +0300 Subject: [PATCH 5/6] Create trionBidAdapter.js --- modules/trionBidAdapter.js | 129 +++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 modules/trionBidAdapter.js diff --git a/modules/trionBidAdapter.js b/modules/trionBidAdapter.js new file mode 100644 index 00000000000..642f8194b7f --- /dev/null +++ b/modules/trionBidAdapter.js @@ -0,0 +1,129 @@ +var CONSTANTS = require('src/constants.json'); +var utils = require('src/utils.js'); +var adloader = require('src/adloader.js'); +var bidmanager = require('src/bidmanager.js'); +var bidfactory = require('src/bidfactory.js'); +var Adapter = require('src/adapter.js').default; +var adaptermanager = require('src/adaptermanager'); + +const BID_REQUEST_BASE_URL = 'https://in-appadvertising.com/api/bidRequest?'; +const USER_SYNC_URL = 'https://in-appadvertising.com/api/userSync.js'; + +function TrionAdapter() { + var baseAdapter = new Adapter('trion'); + var userTag = null; + + baseAdapter.callBids = function (params) { + var bids = params.bids || []; + + if (!bids.length) { + return; + } + + if (!window.TRION_INT) { + adloader.loadScript(USER_SYNC_URL, function () { + userTag = window.TRION_INT || {}; + userTag.pubId = utils.getBidIdParameter('pubId', bids[0].params); + userTag.sectionId = utils.getBidIdParameter('sectionId', bids[0].params); + if (!userTag.to) { + getBids(bids); + } else { + setTimeout(function () { + getBids(bids); + }, userTag.to); + } + }, true); + } else { + userTag = window.TRION_INT; + getBids(bids); + } + }; + + function getBids(bids) { + if (!userTag.int_t) { + userTag.int_t = window.TR_INT_T || -1; + } + + for (var i = 0; i < bids.length; i++) { + var bidRequest = bids[i]; + var bidId = bidRequest.bidId; + adloader.loadScript(buildTrionUrl(bidRequest, bidId)); + } + } + + function buildTrionUrl(bid, bidId) { + var pubId = utils.getBidIdParameter('pubId', bid.params); + var sectionId = utils.getBidIdParameter('sectionId', bid.params); + var re = utils.getBidIdParameter('re', bid.params); + var url = utils.getTopWindowUrl(); + var sizes = utils.parseSizesInput(bid.sizes).join(','); + + var trionUrl = BID_REQUEST_BASE_URL; + + trionUrl = utils.tryAppendQueryString(trionUrl, 'callback', '$$PREBID_GLOBAL$$.handleTrionCB'); + trionUrl = utils.tryAppendQueryString(trionUrl, 'bidId', bidId); + trionUrl = utils.tryAppendQueryString(trionUrl, 'pubId', pubId); + trionUrl = utils.tryAppendQueryString(trionUrl, 'sectionId', sectionId); + trionUrl = utils.tryAppendQueryString(trionUrl, 're', re); + trionUrl = utils.tryAppendQueryString(trionUrl, 'slot', bid.placementCode); + if (url) { + trionUrl += 'url=' + url + '&'; + } + if (sizes) { + trionUrl += 'sizes=' + sizes + '&'; + } + if (userTag) { + trionUrl += 'tag=' + encodeURIComponent(JSON.stringify(userTag)) + '&'; + } + + // remove the trailing "&" + if (trionUrl.lastIndexOf('&') === trionUrl.length - 1) { + trionUrl = trionUrl.substring(0, trionUrl.length - 1); + } + + return trionUrl; + } + + // expose the callback to the global object: + $$PREBID_GLOBAL$$.handleTrionCB = function (trionResponseObj) { + var bid; + var bidObj = {}; + var placementCode = ''; + + if (trionResponseObj && trionResponseObj.bidId) { + var bidCode; + var bidId = trionResponseObj.bidId; + var result = trionResponseObj && trionResponseObj.result; + + bidObj = utils.getBidRequest(bidId); + if (bidObj) { + bidCode = bidObj.bidder; + placementCode = bidObj.placementCode; + } + + if (result && result.cpm && result.placeBid && result.ad) { + var cpm = parseInt(result.cpm, 10) / 100; + bid = bidfactory.createBid(CONSTANTS.STATUS.GOOD, bidObj); + bid.bidderCode = bidCode; + bid.cpm = cpm; + bid.ad = result.ad; + bid.width = result.width; + bid.height = result.height; + } + } + if (!bid) { + bid = bidfactory.createBid(CONSTANTS.STATUS.NO_BID, bidObj); + } + bidmanager.addBidResponse(placementCode, bid); + }; + + return Object.assign(this, { + callBids: baseAdapter.callBids, + setBidderCode: baseAdapter.setBidderCode, + buildTrionUrl: buildTrionUrl + }); +} + +adaptermanager.registerBidAdapter(new TrionAdapter(), 'trion'); + +module.exports = TrionAdapter; From 4d67d16253719394a4a6e795c627b7c5fe8a1d85 Mon Sep 17 00:00:00 2001 From: FeatureForward Date: Thu, 19 Oct 2017 11:02:18 +0300 Subject: [PATCH 6/6] commit back Trion adapter and its tests --- test/spec/modules/trionBidAdapter_spec.js | 274 ++++++++++++++++++++++ 1 file changed, 274 insertions(+) create mode 100644 test/spec/modules/trionBidAdapter_spec.js diff --git a/test/spec/modules/trionBidAdapter_spec.js b/test/spec/modules/trionBidAdapter_spec.js new file mode 100644 index 00000000000..5a9c92ef91e --- /dev/null +++ b/test/spec/modules/trionBidAdapter_spec.js @@ -0,0 +1,274 @@ +import { expect } from 'chai'; +import TrionAdapter from 'modules/trionBidAdapter'; +import bidmanager from 'src/bidmanager'; +import * as utils from 'src/utils'; +const CONSTANTS = require('src/constants.json'); +const adloader = require('src/adloader'); + +const PLACEMENT_CODE = 'ad-tag'; +const BID_REQUEST_BASE_URL = 'https://in-appadvertising.com/api/bidRequest?'; +const USER_SYNC_URL = 'https://in-appadvertising.com/api/userSync.js'; + +const TRION_BID_REQUEST = { + start: new Date().getTime(), + bidderCode: 'trion', + bids: [ + { + bidder: 'trion', + params: { + pubId: '1', + sectionId: '2' + }, + placementCode: PLACEMENT_CODE, + sizes: [[300, 250], [300, 600]], + bidId: 'test-bid-id' + } + ] +}; + +const TRION_BID_RESPONSE = { + bidId: 'test-bid-id', + sizes: [[300, 250], [300, 600]], + result: { + cpm: 100, + placeBid: true, + height: '250', + width: '300', + ad: 'test', + msg: 'response messaging' + } + +}; + +describe('Trion adapter tests', () => { + let adapter; + + beforeEach(() => { + adapter = new TrionAdapter(); + sinon.stub(document.body, 'appendChild'); + }); + + afterEach(() => document.body.appendChild.restore()); + + it('should exist and be a function', function () { + expect($$PREBID_GLOBAL$$.handleTrionCB).to.exist.and.to.be.a('function'); + }); + + describe('request function', () => { + let spyLoadScript; + + beforeEach(() => { + spyLoadScript = sinon.spy(adloader, 'loadScript'); + window.TRION_INT = { + int_t: -1 + }; + }); + + afterEach(() => { + spyLoadScript.restore(); + delete window.TRION_INT; + }); + + it('callBids exists and is a function', () => { + expect(adapter.callBids).to.exist.and.to.be.a('function'); + }); + + it('should not call loadscript when inputting with empty params', function () { + adapter.callBids({}); + sinon.assert.notCalled(spyLoadScript); + }); + + it('should include the base bidrequest url', function () { + adapter.callBids(TRION_BID_REQUEST); + + sinon.assert.calledOnce(spyLoadScript); + + let bidUrl = spyLoadScript.getCall(0).args[0]; + expect(bidUrl).to.include(BID_REQUEST_BASE_URL); + }); + + it('should call loadscript with the correct required params', function () { + adapter.callBids(TRION_BID_REQUEST); + + sinon.assert.calledOnce(spyLoadScript); + + let bidUrl = spyLoadScript.getCall(0).args[0]; + expect(bidUrl).to.include('pubId=1'); + expect(bidUrl).to.include('sectionId=2'); + expect(bidUrl).to.include('sizes=300x250,300x600'); + }); + + it('should call loadscript with the correct optional params', function () { + let params = TRION_BID_REQUEST.bids[0].params; + params.re = 1; + + adapter.callBids(TRION_BID_REQUEST); + + sinon.assert.calledOnce(spyLoadScript); + + let bidUrl = spyLoadScript.getCall(0).args[0]; + expect(bidUrl).to.include('re=1'); + expect(bidUrl).to.include(utils.getTopWindowUrl()); + expect(bidUrl).to.include('slot=' + PLACEMENT_CODE); + delete params.re; + }); + + describe('user sync', () => { + beforeEach(() => { + delete window.TRION_INT; + delete window.TR_INT_T; + }); + + it('user sync is called', () => { + adapter.callBids(TRION_BID_REQUEST); + sinon.assert.calledWith(spyLoadScript, USER_SYNC_URL); + }); + + it('user sync tag is included in bid url', () => { + window.TRION_INT = { + campaigns: [ + 'campaign1', + 'campaign2' + ], + int_t: 'int_t' + }; + let userTag = encodeURIComponent(JSON.stringify(window.TRION_INT)); + adapter.callBids(TRION_BID_REQUEST); + + let bidUrl = spyLoadScript.getCall(0).args[0]; + expect(bidUrl).to.include(userTag); + }); + + it('user sync tag is included in bid url and includes the correct int_t', () => { + window.TRION_INT = { + campaigns: [ + 'campaign1', + 'campaign2' + ] + }; + let int_t = 'test'; + let expectedObject = { + campaigns: [ + 'campaign1', + 'campaign2' + ], + int_t: int_t + }; + window.TR_INT_T = int_t; + let userTag = encodeURIComponent(JSON.stringify(expectedObject)); + adapter.callBids(TRION_BID_REQUEST); + + let bidUrl = spyLoadScript.getCall(0).args[0]; + expect(bidUrl).to.include(userTag); + }); + + it('user sync tag variable int_t cannot be changed once set', () => { + window.TRION_INT = { + campaigns: [ + 'campaign1', + 'campaign2' + ] + }; + let int_t = 'test'; + let expectedObject = { + campaigns: [ + 'campaign1', + 'campaign2' + ], + int_t: int_t + }; + window.TR_INT_T = int_t; + let userTag = encodeURIComponent(JSON.stringify(expectedObject)); + adapter.callBids(TRION_BID_REQUEST); + window.TR_INT_T = 'bad'; + let bidUrl = spyLoadScript.getCall(0).args[0]; + + expect(bidUrl).to.include(userTag); + expect(bidUrl).to.not.include('bad'); + }); + }); + }); + + describe('response handler', () => { + beforeEach(() => { + sinon.stub(bidmanager, 'addBidResponse'); + }); + + afterEach(() => { + bidmanager.addBidResponse.restore(); + }); + + it('when there is no response do not bid', function () { + $$PREBID_GLOBAL$$._bidsRequested.push(TRION_BID_REQUEST); + $$PREBID_GLOBAL$$.handleTrionCB(); + sinon.assert.calledOnce(bidmanager.addBidResponse); + const response = bidmanager.addBidResponse.firstCall.args[1]; + expect(response.getStatusCode()).to.equal(CONSTANTS.STATUS.NO_BID); + }); + + it('when place bid is returned as false', function () { + TRION_BID_RESPONSE.result.placeBid = false; + $$PREBID_GLOBAL$$._bidsRequested.push(TRION_BID_REQUEST); + $$PREBID_GLOBAL$$.handleTrionCB(TRION_BID_RESPONSE); + sinon.assert.calledOnce(bidmanager.addBidResponse); + const response = bidmanager.addBidResponse.firstCall.args[1]; + expect(response.getStatusCode()).to.equal(CONSTANTS.STATUS.NO_BID); + TRION_BID_RESPONSE.result.placeBid = true; + }); + + it('when no cpm is in the response', function () { + TRION_BID_RESPONSE.result.cpm = 0; + $$PREBID_GLOBAL$$._bidsRequested.push(TRION_BID_REQUEST); + $$PREBID_GLOBAL$$.handleTrionCB(TRION_BID_RESPONSE); + sinon.assert.calledOnce(bidmanager.addBidResponse); + const response = bidmanager.addBidResponse.firstCall.args[1]; + expect(response.getStatusCode()).to.equal(CONSTANTS.STATUS.NO_BID); + TRION_BID_RESPONSE.result.cpm = 1; + }); + + it('when no ad is in the response', function () { + TRION_BID_RESPONSE.result.ad = null; + $$PREBID_GLOBAL$$._bidsRequested.push(TRION_BID_REQUEST); + $$PREBID_GLOBAL$$.handleTrionCB(TRION_BID_RESPONSE); + sinon.assert.calledOnce(bidmanager.addBidResponse); + const response = bidmanager.addBidResponse.firstCall.args[1]; + expect(response.getStatusCode()).to.equal(CONSTANTS.STATUS.NO_BID); + TRION_BID_RESPONSE.result.ad = 'test'; + }); + + it('bid response is formatted correctly', function () { + $$PREBID_GLOBAL$$._bidsRequested.push(TRION_BID_REQUEST); + $$PREBID_GLOBAL$$.handleTrionCB(TRION_BID_RESPONSE); + const placementCode = bidmanager.addBidResponse.firstCall.args[0]; + const response = bidmanager.addBidResponse.firstCall.args[1]; + expect(placementCode).to.equal(PLACEMENT_CODE); + expect(response.getStatusCode()).to.equal(CONSTANTS.STATUS.GOOD); + expect(response.bidderCode).to.equal('trion'); + }); + + it('height and width are appropriately set', function () { + let bidWidth = '1'; + let bidHeight = '2'; + TRION_BID_RESPONSE.result.width = bidWidth; + TRION_BID_RESPONSE.result.height = bidHeight; + $$PREBID_GLOBAL$$._bidsRequested.push(TRION_BID_REQUEST); + $$PREBID_GLOBAL$$.handleTrionCB(TRION_BID_RESPONSE); + const placementCode = bidmanager.addBidResponse.firstCall.args[0]; + const response = bidmanager.addBidResponse.firstCall.args[1]; + expect(response.width).to.equal(bidWidth); + expect(response.height).to.equal(bidHeight); + TRION_BID_RESPONSE.result.width = '300'; + TRION_BID_RESPONSE.result.height = '250'; + }); + + it('cpm is properly set and transformed to cents', function () { + let bidCpm = 2; + TRION_BID_RESPONSE.result.cpm = bidCpm * 100; + $$PREBID_GLOBAL$$._bidsRequested.push(TRION_BID_REQUEST); + $$PREBID_GLOBAL$$.handleTrionCB(TRION_BID_RESPONSE); + const response = bidmanager.addBidResponse.firstCall.args[1]; + expect(response.cpm).to.equal(bidCpm); + TRION_BID_RESPONSE.result.cpm = 100; + }); + }); +});