From dc6b450f3e15b5414dd3beb4e156b0116733945a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bendeg=C3=BAz=20=C3=81cs?= <30595431+acsbendi@users.noreply.github.com> Date: Wed, 21 Apr 2021 16:36:02 +0200 Subject: [PATCH] Kobler Bid Adapter: add new bid adapter (#6479) * Implemented Kobler bidder adapter. * Added missing '' to dealId example in parameters table. * Added information on supporting the Floors module. * Implemented tests for isBidRequestValid. * Implemented tests for inherited functions. * Removed unnecessary conditions and quotation marks. * Added TODO about deciding what to do in debug mode. * Added TODO about checking which currencies are allowed. * Added information on parameters read from the first bid only. * Fixed missing indexing operator when checking if mainSize is 0x0. * Implemented tests for buildRequests. * Implemented tests for interpretResponse. * Implemented tests for onBidWon. * Implemented tests for onTimeout. * Added some missing semicolons. * Removed TODO about allowed currencies. * Removed setting test in debug mode and related TODOs. * Removed optional pos parameter. * Removed optional bidfloor parameter and use floorPrice instead of floorprice. * Added support for multiple deal ID parameters. * Fixed formatting. * Added more explanation about the value of position param. * Moved pos property into Kobler-specific banner extension. * Simplifications based on PR comments. * Use getRefererInfo to get page URL for timeout notifications. * Removed TODO about auction type. * Added information on how to generate a sample bid. * Removed reading currency from currency.adServerCurrency. --- modules/koblerBidAdapter.js | 231 +++++++ modules/koblerBidAdapter.md | 67 ++ test/spec/modules/koblerBidAdapter_spec.js | 694 +++++++++++++++++++++ 3 files changed, 992 insertions(+) create mode 100644 modules/koblerBidAdapter.js create mode 100644 modules/koblerBidAdapter.md create mode 100644 test/spec/modules/koblerBidAdapter_spec.js diff --git a/modules/koblerBidAdapter.js b/modules/koblerBidAdapter.js new file mode 100644 index 00000000000..cc5b374af95 --- /dev/null +++ b/modules/koblerBidAdapter.js @@ -0,0 +1,231 @@ +import * as utils from '../src/utils.js'; +import {config} from '../src/config.js'; +import {registerBidder} from '../src/adapters/bidderFactory.js'; +import {BANNER} from '../src/mediaTypes.js'; +import {getRefererInfo} from '../src/refererDetection.js'; + +const BIDDER_CODE = 'kobler'; +const BIDDER_ENDPOINT = 'https://bid.essrtb.com/bid/prebid_rtb_call'; +const TIMEOUT_NOTIFICATION_ENDPOINT = 'https://bid.essrtb.com/notify/prebid_timeout'; +const SUPPORTED_CURRENCY = 'USD'; +const DEFAULT_TIMEOUT = 1000; +const TIME_TO_LIVE_IN_SECONDS = 10 * 60; + +export const isBidRequestValid = function (bid) { + return !!(bid && bid.bidId && bid.params && bid.params.placementId); +}; + +export const buildRequests = function (validBidRequests, bidderRequest) { + return { + method: 'POST', + url: BIDDER_ENDPOINT, + data: buildOpenRtbBidRequestPayload(validBidRequests, bidderRequest), + options: { + contentType: 'application/json' + } + }; +}; + +export const interpretResponse = function (serverResponse) { + const res = serverResponse.body; + const bids = [] + if (res) { + res.seatbid.forEach(sb => { + sb.bid.forEach(b => { + bids.push({ + requestId: b.impid, + cpm: b.price, + currency: res.cur, + width: b.w, + height: b.h, + creativeId: b.crid, + dealId: b.dealid, + netRevenue: true, + ttl: TIME_TO_LIVE_IN_SECONDS, + ad: b.adm, + nurl: b.nurl, + meta: { + advertiserDomains: b.adomain + } + }) + }) + }); + } + return bids; +}; + +export const onBidWon = function (bid) { + const cpm = bid.cpm || 0; + const adServerPrice = utils.deepAccess(bid, 'adserverTargeting.hb_pb', 0); + if (utils.isStr(bid.nurl) && bid.nurl !== '') { + const winNotificationUrl = utils.replaceAuctionPrice(bid.nurl, cpm) + .replace(/\${AD_SERVER_PRICE}/g, adServerPrice); + utils.triggerPixel(winNotificationUrl); + } +}; + +export const onTimeout = function (timeoutDataArray) { + if (utils.isArray(timeoutDataArray)) { + const refererInfo = getRefererInfo(); + const pageUrl = (refererInfo && refererInfo.referer) + ? refererInfo.referer + : window.location.href; + timeoutDataArray.forEach(timeoutData => { + const query = utils.parseQueryStringParameters({ + ad_unit_code: timeoutData.adUnitCode, + auction_id: timeoutData.auctionId, + bid_id: timeoutData.bidId, + timeout: timeoutData.timeout, + placement_id: utils.deepAccess(timeoutData, 'params.0.placementId'), + page_url: pageUrl, + }); + const timeoutNotificationUrl = `${TIMEOUT_NOTIFICATION_ENDPOINT}?${query}`; + utils.triggerPixel(timeoutNotificationUrl); + }); + } +}; + +function buildOpenRtbBidRequestPayload(validBidRequests, bidderRequest) { + const imps = validBidRequests.map(buildOpenRtbImpObject); + const timeout = bidderRequest.timeout || config.getConfig('bidderTimeout') || DEFAULT_TIMEOUT; + const pageUrl = (bidderRequest.refererInfo && bidderRequest.refererInfo.referer) + ? bidderRequest.refererInfo.referer + : window.location.href; + + const request = { + id: bidderRequest.auctionId, + at: 1, + tmax: timeout, + cur: [SUPPORTED_CURRENCY], + imp: imps, + device: { + devicetype: getDevice(), + geo: getGeo(validBidRequests[0]) + }, + site: { + page: pageUrl, + }, + test: getTest(validBidRequests[0]) + }; + + return JSON.stringify(request); +} + +function buildOpenRtbImpObject(validBidRequest) { + const sizes = getSizes(validBidRequest); + const mainSize = sizes[0]; + const floorInfo = getFloorInfo(validBidRequest, mainSize); + + return { + id: validBidRequest.bidId, + banner: { + format: buildFormatArray(sizes), + w: mainSize[0], + h: mainSize[1], + ext: { + kobler: { + pos: getPosition(validBidRequest) + } + } + }, + tagid: validBidRequest.params.placementId, + bidfloor: floorInfo.floor, + bidfloorcur: floorInfo.currency, + pmp: buildPmpObject(validBidRequest) + }; +} + +function getDevice() { + const ws = utils.getWindowSelf(); + const ua = ws.navigator.userAgent; + + if (/(tablet|ipad|playbook|silk|android 3.0|xoom|sch-i800|kindle)|(android(?!.*mobi))/i + .test(ua.toLowerCase())) { + return 5; // tablet + } + if (/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series([46])0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i + .test(ua.toLowerCase())) { + return 4; // phone + } + return 2; // personal computers +} + +function getGeo(validBidRequest) { + if (validBidRequest.params.zip) { + return { + zip: validBidRequest.params.zip + }; + } + return {}; +} + +function getTest(validBidRequest) { + return validBidRequest.params.test ? 1 : 0; +} + +function getSizes(validBidRequest) { + const sizes = utils.deepAccess(validBidRequest, 'mediaTypes.banner.sizes', validBidRequest.sizes); + if (utils.isArray(sizes) && sizes.length > 0) { + return sizes; + } + + return [[0, 0]]; +} + +function buildFormatArray(sizes) { + return sizes.map(size => { + return { + w: size[0], + h: size[1] + }; + }); +} + +function getPosition(validBidRequest) { + return parseInt(validBidRequest.params.position) || 0; +} + +function getFloorInfo(validBidRequest, mainSize) { + if (typeof validBidRequest.getFloor === 'function') { + const sizeParam = mainSize[0] === 0 && mainSize[1] === 0 ? '*' : mainSize; + return validBidRequest.getFloor({ + currency: SUPPORTED_CURRENCY, + mediaType: BANNER, + size: sizeParam + }); + } else { + return { + currency: SUPPORTED_CURRENCY, + floor: getFloorPrice(validBidRequest) + }; + } +} + +function getFloorPrice(validBidRequest) { + return parseFloat(validBidRequest.params.floorPrice) || 0.0; +} + +function buildPmpObject(validBidRequest) { + if (validBidRequest.params.dealIds) { + return { + deals: validBidRequest.params.dealIds.map(dealId => { + return { + id: dealId + }; + }) + }; + } + return {}; +} + +export const spec = { + code: BIDDER_CODE, + supportedMediaTypes: [BANNER], + isBidRequestValid, + buildRequests, + interpretResponse, + onBidWon, + onTimeout +}; + +registerBidder(spec); diff --git a/modules/koblerBidAdapter.md b/modules/koblerBidAdapter.md new file mode 100644 index 00000000000..7a7b2388367 --- /dev/null +++ b/modules/koblerBidAdapter.md @@ -0,0 +1,67 @@ +# Overview + +**Module Name**: Kobler Bidder Adapter +**Module Type**: Bidder Adapter +**Maintainer**: bidding-support@kobler.no + +# Description + +Connects to Kobler's demand sources. + +This adapter currently only supports Banner Ads. + +# Parameters + +| Parameter (in `params`) | Scope | Type | Description | Example | +| --- | --- | --- | --- | --- | +| placementId | Required | String | The identifier of the placement, it has to be issued by Kobler. | `'xjer0ch8'` | +| zip | Optional | String | Zip code of the user or the medium. When multiple ad units are submitted together, it is enough to set this parameter on the first one. | `'102 22'` | +| test | Optional | Boolean | Whether the request is for testing only. When multiple ad units are submitted together, it is enough to set this parameter on the first one. Defaults to false. | `true` | +| floorPrice | Optional | Float | Floor price in CPM and USD. Can be used as an alternative to the [Floors module](https://docs.prebid.org/dev-docs/modules/floors.html), which is also supported by this adapter. Defaults to 0. | `5.0` | +| position | Optional | Integer | The position of the ad unit. Can be used to differentiate between ad units if the same placement ID is used across multiple ad units. The first ad unit should have a `position` of 0, the second one should have a `position` of 1 and so on. Defaults to 0. | `1` | +| dealIds | Optional | Array of Strings | Array of deal IDs. | `['abc328745', 'mxw243253']` | + +# Test Parameters +```javascript + const adUnits = [{ + code: 'div-gpt-ad-1460505748561-1', + mediaTypes: { + banner: { + sizes: [[320, 250], [300, 250]], + } + }, + bids: [{ + bidder: 'kobler', + params: { + placementId: 'k5H7et3R0' + } + }] + }]; +``` + +In order to see a sample bid from Kobler (without a proper setup), you have to also do the following: +- Change the [`refererInfo` function](https://github.com/prebid/Prebid.js/blob/master/src/refererDetection.js) to return `'https://www.tv2.no/a/11734615'` as a [`referer`](https://github.com/prebid/Prebid.js/blob/caead3ccccc448e4cd09d074fd9f8833f56fe9b3/src/refererDetection.js#L169). This is necessary because Kobler only bids on recognized articles. +- Change the adapter's [`BIDDER_ENDPOINT`](https://github.com/prebid/Prebid.js/blob/master/modules/koblerBidAdapter.js#L8) to `'https://bid-service.dev.essrtb.com/bid/prebid_rtb_call'`. This endpoint belongs to the development server that is set up to always return a bid for the correct `placementId` and page URL combination. + +# Test Optional Parameters +```javascript + const adUnits = [{ + code: 'div-gpt-ad-1460505748561-1', + mediaTypes: { + banner: { + sizes: [[320, 250], [300, 250]], + } + }, + bids: [{ + bidder: 'kobler', + params: { + placementId: 'k5H7et3R0', + zip: '102 22', + test: true, + floorPrice: 5.0, + position: 1, + dealIds: ['abc328745', 'mxw243253'] + } + }] + }]; +``` diff --git a/test/spec/modules/koblerBidAdapter_spec.js b/test/spec/modules/koblerBidAdapter_spec.js new file mode 100644 index 00000000000..725c9ece118 --- /dev/null +++ b/test/spec/modules/koblerBidAdapter_spec.js @@ -0,0 +1,694 @@ +import {expect} from 'chai'; +import {spec} from 'modules/koblerBidAdapter.js'; +import {newBidder} from 'src/adapters/bidderFactory.js'; +import {config} from 'src/config.js'; +import * as utils from 'src/utils.js'; +import {getRefererInfo} from 'src/refererDetection.js'; + +function createBidderRequest(auctionId, timeout, pageUrl) { + return { + auctionId: auctionId || 'c1243d83-0bed-4fdb-8c76-42b456be17d0', + timeout: timeout || 2000, + refererInfo: { + referer: pageUrl || 'example.com' + } + }; +} + +function createValidBidRequest(params, bidId, sizes) { + return { + adUnitCode: 'adunit-code', + bidId: bidId || '22c4871113f461', + bidder: 'kobler', + bidderRequestId: '15246a574e859f', + bidRequestsCount: 1, + bidderRequestsCount: 1, + mediaTypes: { + banner: { + sizes: sizes || [[300, 250], [320, 100]] + } + }, + params: params || { + placementId: 'tpw58278' + }, + transactionTd: '04314114-15bd-4638-8664-bdb8bdc60bff' + }; +} + +describe('KoblerAdapter', function () { + describe('inherited functions', function () { + it('exists and is a function', function () { + const adapter = newBidder(spec); + expect(adapter.callBids).to.exist.and.to.be.a('function'); + }); + }); + + describe('isBidRequestValid', function () { + it('should not accept a request without bidId as valid', function () { + const bid = { + params: { + someParam: 'abc' + } + }; + + const result = spec.isBidRequestValid(bid); + + expect(result).to.be.false; + }); + + it('should not accept a request without params as valid', function () { + const bid = { + bidId: 'e11768e8-3b71-4453-8698-0a2feb866589' + }; + + const result = spec.isBidRequestValid(bid); + + expect(result).to.be.false; + }); + + it('should not accept a request without placementId as valid', function () { + const bid = { + bidId: 'e11768e8-3b71-4453-8698-0a2feb866589', + params: { + someParam: 'abc' + } + }; + + const result = spec.isBidRequestValid(bid); + + expect(result).to.be.false; + }); + + it('should accept a request with bidId and placementId as valid', function () { + const bid = { + bidId: 'e11768e8-3b71-4453-8698-0a2feb866589', + params: { + someParam: 'abc', + placementId: '8bde0923-1409-4253-9594-495b58d931ba' + } + }; + + const result = spec.isBidRequestValid(bid); + + expect(result).to.be.true; + }); + }); + + describe('buildRequests', function () { + it('should read data from bidder request', function () { + const testUrl = 'kobler.no'; + const auctionId = '8319af54-9795-4642-ba3a-6f57d6ff9100'; + const timeout = 5000; + const validBidRequests = [createValidBidRequest()]; + const bidderRequest = createBidderRequest(auctionId, timeout, testUrl); + + const result = spec.buildRequests(validBidRequests, bidderRequest); + const openRtbRequest = JSON.parse(result.data); + + expect(openRtbRequest.tmax).to.be.equal(timeout); + expect(openRtbRequest.id).to.be.equal(auctionId); + expect(openRtbRequest.site.page).to.be.equal(testUrl); + }); + + it('should read data from valid bid requests', function () { + const firstSize = [400, 800]; + const secondSize = [450, 950]; + const sizes = [firstSize, secondSize]; + const placementId = 'tsjs86325'; + const bidId = '3a56a019-4835-4f75-811c-76fac6853a2c'; + const validBidRequests = [ + createValidBidRequest( + { + placementId: placementId + }, + bidId, + sizes + ) + ]; + const bidderRequest = createBidderRequest(); + + const result = spec.buildRequests(validBidRequests, bidderRequest); + const openRtbRequest = JSON.parse(result.data); + + expect(openRtbRequest.imp.length).to.be.equal(1); + expect(openRtbRequest.imp[0].id).to.be.equal(bidId); + expect(openRtbRequest.imp[0].tagid).to.be.equal(placementId); + expect(openRtbRequest.imp[0].banner.w).to.be.equal(firstSize[0]); + expect(openRtbRequest.imp[0].banner.h).to.be.equal(firstSize[1]); + expect(openRtbRequest.imp[0].banner.format.length).to.be.equal(2); + expect(openRtbRequest.imp[0].banner.format[0].w).to.be.equal(firstSize[0]); + expect(openRtbRequest.imp[0].banner.format[0].h).to.be.equal(firstSize[1]); + expect(openRtbRequest.imp[0].banner.format[1].w).to.be.equal(secondSize[0]); + expect(openRtbRequest.imp[0].banner.format[1].h).to.be.equal(secondSize[1]); + }); + + it('should use 0x0 as default size', function () { + const validBidRequests = [ + createValidBidRequest( + undefined, + undefined, + [] + ) + ]; + const bidderRequest = createBidderRequest(); + + const result = spec.buildRequests(validBidRequests, bidderRequest); + const openRtbRequest = JSON.parse(result.data); + + expect(openRtbRequest.imp.length).to.be.equal(1); + expect(openRtbRequest.imp[0].banner.w).to.be.equal(0); + expect(openRtbRequest.imp[0].banner.h).to.be.equal(0); + expect(openRtbRequest.imp[0].banner.format.length).to.be.equal(1); + expect(openRtbRequest.imp[0].banner.format[0].w).to.be.equal(0); + expect(openRtbRequest.imp[0].banner.format[0].h).to.be.equal(0); + }); + + it('should use 0 as default position', function () { + const validBidRequests = [createValidBidRequest()]; + const bidderRequest = createBidderRequest(); + + const result = spec.buildRequests(validBidRequests, bidderRequest); + const openRtbRequest = JSON.parse(result.data); + + expect(openRtbRequest.imp.length).to.be.equal(1); + expect(openRtbRequest.imp[0].banner.ext.kobler.pos).to.be.equal(0); + }); + + it('should read zip from valid bid requests', function () { + const zip = '700 02'; + const validBidRequests = [ + createValidBidRequest( + { + placementId: 'nmah8324234', + zip: zip + } + ) + ]; + const bidderRequest = createBidderRequest(); + + const result = spec.buildRequests(validBidRequests, bidderRequest); + const openRtbRequest = JSON.parse(result.data); + + expect(openRtbRequest.device.geo.zip).to.be.equal(zip); + }); + + it('should read test from valid bid requests', function () { + const validBidRequests = [ + createValidBidRequest( + { + placementId: 'zwop842799', + test: true + } + ) + ]; + const bidderRequest = createBidderRequest(); + + const result = spec.buildRequests(validBidRequests, bidderRequest); + const openRtbRequest = JSON.parse(result.data); + + expect(openRtbRequest.test).to.be.equal(1); + }); + + it('should read floorPrice from valid bid requests', function () { + const floorPrice = 4.343; + const validBidRequests = [ + createValidBidRequest( + { + placementId: 'oqr3224234', + floorPrice: floorPrice + } + ) + ]; + const bidderRequest = createBidderRequest(); + + const result = spec.buildRequests(validBidRequests, bidderRequest); + const openRtbRequest = JSON.parse(result.data); + + expect(openRtbRequest.imp.length).to.be.equal(1); + expect(openRtbRequest.imp[0].bidfloor).to.be.equal(floorPrice); + }); + + it('should read position from valid bid requests', function () { + const placementId = 'yzksf234592'; + const validBidRequests = [ + createValidBidRequest( + { + placementId: placementId, + position: 1 + } + ), + createValidBidRequest( + { + placementId: placementId, + position: 2 + } + ), + createValidBidRequest( + { + placementId: placementId, + position: 3 + } + ) + ]; + const bidderRequest = createBidderRequest(); + + const result = spec.buildRequests(validBidRequests, bidderRequest); + const openRtbRequest = JSON.parse(result.data); + + expect(openRtbRequest.imp.length).to.be.equal(3); + expect(openRtbRequest.imp[0].banner.ext.kobler.pos).to.be.equal(1); + expect(openRtbRequest.imp[0].tagid).to.be.equal(placementId); + expect(openRtbRequest.imp[1].banner.ext.kobler.pos).to.be.equal(2); + expect(openRtbRequest.imp[1].tagid).to.be.equal(placementId); + expect(openRtbRequest.imp[2].banner.ext.kobler.pos).to.be.equal(3); + expect(openRtbRequest.imp[2].tagid).to.be.equal(placementId); + }); + + it('should read dealIds from valid bid requests', function () { + const dealIds1 = ['78214682234823']; + const dealIds2 = ['89913861235234', '27368423545328640']; + const validBidRequests = [ + createValidBidRequest( + { + placementId: 'rsl1239823', + dealIds: dealIds1 + } + ), + createValidBidRequest( + { + placementId: 'pqw234232', + dealIds: dealIds2 + } + ) + ]; + const bidderRequest = createBidderRequest(); + + const result = spec.buildRequests(validBidRequests, bidderRequest); + const openRtbRequest = JSON.parse(result.data); + + expect(openRtbRequest.imp.length).to.be.equal(2); + expect(openRtbRequest.imp[0].pmp.deals.length).to.be.equal(1); + expect(openRtbRequest.imp[0].pmp.deals[0].id).to.be.equal(dealIds1[0]); + expect(openRtbRequest.imp[1].pmp.deals.length).to.be.equal(2); + expect(openRtbRequest.imp[1].pmp.deals[0].id).to.be.equal(dealIds2[0]); + expect(openRtbRequest.imp[1].pmp.deals[1].id).to.be.equal(dealIds2[1]); + }); + + it('should read timeout from config', function () { + const timeout = 4000; + const validBidRequests = [createValidBidRequest()]; + // No timeout field + const bidderRequest = { + auctionId: 'c1243d83-0bed-4fdb-8c76-42b456be17d0', + refererInfo: { + referer: 'example.com' + } + }; + config.setConfig({ + bidderTimeout: timeout + }); + + const result = spec.buildRequests(validBidRequests, bidderRequest); + const openRtbRequest = JSON.parse(result.data); + + expect(openRtbRequest.tmax).to.be.equal(timeout); + }); + + it('should read floor price using floors module', function () { + const floorPriceFor580x400 = 6.5148; + const floorPriceForAnySize = 4.2343; + const validBidRequests = [ + createValidBidRequest(undefined, '98efe127-f926-4dde-b988-db8e5dba5a76', [[580, 400]]), + createValidBidRequest(undefined, 'c7698d4a-94f4-4a6b-a928-7e1facfbf752', []) + ]; + validBidRequests.forEach(validBidRequest => { + validBidRequest.getFloor = function (params) { + let floorPrice; + if (utils.isArray(params.size) && params.size[0] === 580 && params.size[1] === 400) { + floorPrice = floorPriceFor580x400; + } else if (params.size === '*') { + floorPrice = floorPriceForAnySize + } else { + floorPrice = 0 + } + return { + currency: params.currency, + floor: floorPrice + } + } + }) + const bidderRequest = createBidderRequest(); + + const result = spec.buildRequests(validBidRequests, bidderRequest); + const openRtbRequest = JSON.parse(result.data); + + expect(openRtbRequest.imp.length).to.be.equal(2); + expect(openRtbRequest.imp[0].id).to.be.equal('98efe127-f926-4dde-b988-db8e5dba5a76'); + expect(openRtbRequest.imp[0].bidfloor).to.be.equal(floorPriceFor580x400); + expect(openRtbRequest.imp[1].id).to.be.equal('c7698d4a-94f4-4a6b-a928-7e1facfbf752'); + expect(openRtbRequest.imp[1].bidfloor).to.be.equal(floorPriceForAnySize); + }); + + it('should create whole OpenRTB request', function () { + const validBidRequests = [ + createValidBidRequest( + { + placementId: 'pcha322364', + zip: '0015', + floorPrice: 5.6234, + position: 1, + dealIds: ['623472534328234'] + }, + '953ee65d-d18a-484f-a840-d3056185a060', + [[400, 600]] + ), + createValidBidRequest( + { + placementId: 'sdfgoi32y4', + floorPrice: 3.2543, + position: 2, + dealIds: ['92368234753283', '263845832942'] + }, + '8320bf79-9d90-4a17-87c6-5d505706a921', + [[400, 500], [200, 250], [300, 350]] + ), + createValidBidRequest( + { + placementId: 'gwms2738647', + position: 3 + }, + 'd0de713b-32e3-4191-a2df-a007f08ffe72', + [[800, 900]] + ) + ]; + const bidderRequest = createBidderRequest( + '9ff580cf-e10e-4b66-add7-40ac0c804e21', + 4500, + 'bid.kobler.no' + ); + + const result = spec.buildRequests(validBidRequests, bidderRequest); + const openRtbRequest = JSON.parse(result.data); + + const expectedOpenRtbRequest = { + id: '9ff580cf-e10e-4b66-add7-40ac0c804e21', + at: 1, + tmax: 4500, + cur: ['USD'], + imp: [ + { + id: '953ee65d-d18a-484f-a840-d3056185a060', + banner: { + format: [ + { + w: 400, + h: 600 + } + ], + w: 400, + h: 600, + ext: { + kobler: { + pos: 1 + } + } + }, + tagid: 'pcha322364', + bidfloor: 5.6234, + bidfloorcur: 'USD', + pmp: { + deals: [ + { + id: '623472534328234' + } + ] + } + }, + { + id: '8320bf79-9d90-4a17-87c6-5d505706a921', + banner: { + format: [ + { + w: 400, + h: 500 + }, + { + w: 200, + h: 250 + }, + { + w: 300, + h: 350 + } + ], + w: 400, + h: 500, + ext: { + kobler: { + pos: 2 + } + } + }, + tagid: 'sdfgoi32y4', + bidfloor: 3.2543, + bidfloorcur: 'USD', + pmp: { + deals: [ + { + id: '92368234753283' + }, + { + id: '263845832942' + } + ] + } + }, + { + id: 'd0de713b-32e3-4191-a2df-a007f08ffe72', + banner: { + format: [ + { + w: 800, + h: 900 + } + ], + w: 800, + h: 900, + ext: { + kobler: { + pos: 3 + } + } + }, + tagid: 'gwms2738647', + bidfloor: 0, + bidfloorcur: 'USD', + pmp: {} + } + ], + device: { + devicetype: 2, + geo: { + zip: '0015' + } + }, + site: { + page: 'bid.kobler.no' + }, + test: 0 + }; + + expect(openRtbRequest).to.deep.equal(expectedOpenRtbRequest); + }); + }); + + describe('interpretResponse', function () { + it('should handle empty body', function () { + const responseWithEmptyBody = { + body: undefined + }; + const bids = spec.interpretResponse(responseWithEmptyBody) + + expect(bids.length).to.be.equal(0); + }); + + it('should generate bids from OpenRTB response', function () { + const responseWithTwoBids = { + body: { + seatbid: [ + { + bid: [ + { + impid: '6194ddef-89a4-404f-9efd-6b718fc23308', + price: 7.981, + nurl: 'https://atag.essrtb.com/serve/prebid_win_notification?payload=sdhfusdaobfadslf234324&sp=${AUCTION_PRICE}&asp=${AD_SERVER_PRICE}', + crid: 'edea9b03-3a57-41aa-9c00-abd673e22006', + dealid: '', + w: 320, + h: 250, + adm: '', + adomain: [ + 'https://kobler.no' + ] + }, + { + impid: '2ec0b40f-d3ca-4ba5-8ce3-48290565690f', + price: 6.71234, + nurl: 'https://atag.essrtb.com/serve/prebid_win_notification?payload=nbashgufvishdafjk23432&sp=${AUCTION_PRICE}&asp=${AD_SERVER_PRICE}', + crid: 'fa2d5af7-2678-4204-9023-44c526160742', + dealid: '2783483223432342', + w: 580, + h: 400, + adm: '', + adomain: [ + 'https://bid.kobler.no' + ] + } + ] + } + ], + cur: 'USD' + } + }; + const bids = spec.interpretResponse(responseWithTwoBids) + + const expectedBids = [ + { + requestId: '6194ddef-89a4-404f-9efd-6b718fc23308', + cpm: 7.981, + currency: 'USD', + width: 320, + height: 250, + creativeId: 'edea9b03-3a57-41aa-9c00-abd673e22006', + dealId: '', + netRevenue: true, + ttl: 600, + ad: '', + nurl: 'https://atag.essrtb.com/serve/prebid_win_notification?payload=sdhfusdaobfadslf234324&sp=${AUCTION_PRICE}&asp=${AD_SERVER_PRICE}', + meta: { + advertiserDomains: [ + 'https://kobler.no' + ] + } + }, + { + requestId: '2ec0b40f-d3ca-4ba5-8ce3-48290565690f', + cpm: 6.71234, + currency: 'USD', + width: 580, + height: 400, + creativeId: 'fa2d5af7-2678-4204-9023-44c526160742', + dealId: '2783483223432342', + netRevenue: true, + ttl: 600, + ad: '', + nurl: 'https://atag.essrtb.com/serve/prebid_win_notification?payload=nbashgufvishdafjk23432&sp=${AUCTION_PRICE}&asp=${AD_SERVER_PRICE}', + meta: { + advertiserDomains: [ + 'https://bid.kobler.no' + ] + } + } + ]; + expect(bids).to.deep.equal(expectedBids); + }); + }); + + describe('onBidWon', function () { + beforeEach(function () { + sinon.stub(utils, 'triggerPixel'); + }); + afterEach(function () { + utils.triggerPixel.restore(); + }); + + it('Should not trigger pixel if bid does not contain nurl', function () { + spec.onBidWon({}); + + expect(utils.triggerPixel.called).to.be.false; + }); + + it('Should not trigger pixel if nurl is empty', function () { + spec.onBidWon({ + nurl: '' + }); + + expect(utils.triggerPixel.called).to.be.false; + }); + + it('Should trigger pixel with replaced nurl if nurl is not empty', function () { + spec.onBidWon({ + cpm: 8.341, + nurl: 'https://atag.essrtb.com/serve/prebid_win_notification?payload=sdhfusdaobfadslf234324&sp=${AUCTION_PRICE}&asp=${AD_SERVER_PRICE}', + adserverTargeting: { + hb_pb: 8 + } + }); + + expect(utils.triggerPixel.callCount).to.be.equal(1); + expect(utils.triggerPixel.firstCall.args[0]).to.be.equal( + 'https://atag.essrtb.com/serve/prebid_win_notification?payload=sdhfusdaobfadslf234324&sp=8.341&asp=8' + ); + }); + }); + + describe('onTimeout', function () { + beforeEach(function () { + sinon.stub(utils, 'triggerPixel'); + }); + afterEach(function () { + utils.triggerPixel.restore(); + }); + + it('Should not trigger pixel if timeout data is not array', function () { + spec.onTimeout(null); + + expect(utils.triggerPixel.called).to.be.false; + }); + + it('Should not trigger pixel if timeout data is empty', function () { + spec.onTimeout([]); + + expect(utils.triggerPixel.called).to.be.false; + }); + + it('Should trigger pixel with query parameters if timeout data not empty', function () { + spec.onTimeout([ + { + adUnitCode: 'adunit-code', + auctionId: 'a1fba829-dd41-409f-acfb-b7b0ac5f30c6', + bidId: 'ef236c6c-e934-406b-a877-d7be8e8a839a', + timeout: 100, + params: [ + { + placementId: 'xrwg62731', + } + ], + }, + { + adUnitCode: 'adunit-code-2', + auctionId: 'a1fba829-dd41-409f-acfb-b7b0ac5f30c6', + bidId: 'ca4121c8-9a4a-46ba-a624-e9b64af206f2', + timeout: 100, + params: [ + { + placementId: 'bc482234', + } + ], + } + ]); + + expect(utils.triggerPixel.callCount).to.be.equal(2); + expect(utils.triggerPixel.getCall(0).args[0]).to.be.equal( + 'https://bid.essrtb.com/notify/prebid_timeout?ad_unit_code=adunit-code&' + + 'auction_id=a1fba829-dd41-409f-acfb-b7b0ac5f30c6&bid_id=ef236c6c-e934-406b-a877-d7be8e8a839a&timeout=100&' + + 'placement_id=xrwg62731&page_url=' + encodeURIComponent(getRefererInfo().referer) + ); + expect(utils.triggerPixel.getCall(1).args[0]).to.be.equal( + 'https://bid.essrtb.com/notify/prebid_timeout?ad_unit_code=adunit-code-2&' + + 'auction_id=a1fba829-dd41-409f-acfb-b7b0ac5f30c6&bid_id=ca4121c8-9a4a-46ba-a624-e9b64af206f2&timeout=100&' + + 'placement_id=bc482234&page_url=' + encodeURIComponent(getRefererInfo().referer) + ); + }); + }); +});