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

add new version for DecenterAds #5711

Merged
merged 5 commits into from
Sep 8, 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
90 changes: 90 additions & 0 deletions modules/decenteradsBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { registerBidder } from '../src/adapters/bidderFactory.js'
import { BANNER, NATIVE, VIDEO } from '../src/mediaTypes.js'
import * as utils from '../src/utils.js'

const BIDDER_CODE = 'decenterads'
const URL = 'https://supply.decenterads.com/?c=o&m=multi'
const URL_SYNC = 'https://supply.decenterads.com/?c=o&m=cookie'

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

isBidRequestValid: function (opts) {
return Boolean(opts.bidId && opts.params && !isNaN(opts.params.placementId))
},

buildRequests: function (validBidRequests) {
validBidRequests = validBidRequests || []
let winTop = window
try {
window.top.location.toString()
winTop = window.top
} catch (e) { utils.logMessage(e) }

const location = utils.getWindowLocation()
const placements = []

for (let i = 0; i < validBidRequests.length; i++) {
const p = validBidRequests[i]

placements.push({
placementId: p.params.placementId,
bidId: p.bidId,
traffic: p.params.traffic || BANNER
})
}

return {
method: 'POST',
url: URL,
data: {
deviceWidth: winTop.screen.width,
deviceHeight: winTop.screen.height,
language: (navigator && navigator.language) ? navigator.language : '',
secure: +(location.protocol === 'https:'),
host: location.hostname,
page: location.pathname,
placements: placements
}
}
},

interpretResponse: function (opts) {
const body = opts.body
const response = []

for (let i = 0; i < body.length; i++) {
const item = body[i]
if (isBidResponseValid(item)) {
delete item.mediaType
response.push(item)
}
}

return response
},

getUserSyncs: function (syncOptions, serverResponses) {
return [{ type: 'image', url: URL_SYNC }]
}
}

registerBidder(spec)

function isBidResponseValid (bid) {
if (!bid.requestId || !bid.cpm || !bid.creativeId ||
!bid.ttl || !bid.currency) {
return false
}
switch (bid['mediaType']) {
case BANNER:
return Boolean(bid.width && bid.height && bid.ad)
case VIDEO:
return Boolean(bid.vastUrl)
case NATIVE:
return Boolean(bid.title && bid.image && bid.impressionTrackers)
default:
return false
}
}
207 changes: 207 additions & 0 deletions test/spec/modules/decenteradsBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
import { expect } from 'chai'
import { spec } from '../../../modules/decenteradsBidAdapter.js'
import { deepStrictEqual, notEqual, ok, strictEqual } from 'assert'

describe('DecenteradsAdapter', () => {
const bid = {
bidId: '9ec5b177515ee2e5',
bidder: 'decenterads',
params: {
placementId: 0,
traffic: 'banner'
}
}

describe('isBidRequestValid', () => {
it('Should return true if there are bidId, params and placementId parameters present', () => {
strictEqual(true, spec.isBidRequestValid(bid))
})

it('Should return false if at least one of parameters is not present', () => {
const b = { ...bid }
delete b.params.placementId
strictEqual(false, spec.isBidRequestValid(b))
})
})

describe('buildRequests', () => {
const serverRequest = spec.buildRequests([bid])

it('Creates a ServerRequest object with method, URL and data', () => {
ok(serverRequest)
ok(serverRequest.method)
ok(serverRequest.url)
ok(serverRequest.data)
})

it('Returns POST method', () => {
strictEqual('POST', serverRequest.method)
})

it('Returns valid URL', () => {
strictEqual('https://supply.decenterads.com/?c=o&m=multi', serverRequest.url)
})

it('Returns valid data if array of bids is valid', () => {
const { data } = serverRequest
strictEqual('object', typeof data)
deepStrictEqual(['deviceWidth', 'deviceHeight', 'language', 'secure', 'host', 'page', 'placements'], Object.keys(data))
strictEqual('number', typeof data.deviceWidth)
strictEqual('number', typeof data.deviceHeight)
strictEqual('string', typeof data.language)
strictEqual('string', typeof data.host)
strictEqual('string', typeof data.page)
notEqual(-1, [0, 1].indexOf(data.secure))

const placement = data.placements[0]
deepStrictEqual(['placementId', 'bidId', 'traffic'], Object.keys(placement))
strictEqual(0, placement.placementId)
strictEqual('9ec5b177515ee2e5', placement.bidId)
strictEqual('banner', placement.traffic)
})

it('Returns empty data if no valid requests are passed', () => {
const { placements } = spec.buildRequests([]).data

expect(spec.buildRequests([]).data.placements).to.be.an('array')
strictEqual(0, placements.length)
})
})

describe('interpretResponse', () => {
const validData = [
{
body: [{
mediaType: 'banner',
width: 300,
height: 250,
cpm: 0.4,
ad: 'Test',
requestId: '9ec5b177515ee2e5',
ttl: 120,
creativeId: '2',
netRevenue: true,
currency: 'USD',
dealId: '1'
}]
},
{
body: [{
vastUrl: 'decenterads.com',
mediaType: 'video',
cpm: 0.5,
requestId: '9ec5b177515ee2e5',
ttl: 120,
creativeId: '2',
netRevenue: true,
currency: 'USD',
dealId: '1'
}]
},
{
body: [{
mediaType: 'native',
clickUrl: 'decenterads.com',
title: 'Test',
image: 'decenterads.com',
creativeId: '2',
impressionTrackers: ['decenterads.com'],
ttl: 120,
cpm: 0.4,
requestId: '9ec5b177515ee2e5',
netRevenue: true,
currency: 'USD',
}]
}
]

for (const obj of validData) {
const { mediaType } = obj.body[0]

it(`Should interpret ${mediaType} response`, () => {
const response = spec.interpretResponse(obj)

expect(response).to.be.an('array')
strictEqual(1, response.length)

const copy = { ...obj.body[0] }
delete copy.mediaType
deepStrictEqual(copy, response[0])
})
}

const invalidData = [
{
body: [{
width: 300,
cpm: 0.4,
ad: 'Test',
requestId: '9ec5b177515ee2e5',
ttl: 120,
creativeId: '2',
netRevenue: true,
currency: 'USD',
dealId: '1'
}]
},
{
body: [{
mediaType: 'video',
cpm: 0.5,
requestId: '9ec5b177515ee2e5',
ttl: 120,
creativeId: '2',
netRevenue: true,
currency: 'USD',
dealId: '1'
}]
},
{
body: [{
mediaType: 'native',
clickUrl: 'decenterads.com',
title: 'Test',
impressionTrackers: ['decenterads.com'],
ttl: 120,
requestId: '9ec5b177515ee2e5',
creativeId: '2',
netRevenue: true,
currency: 'USD',
}]
}
]

for (const obj of invalidData) {
const { mediaType } = obj.body[0]

it(`Should return an empty array if invalid ${mediaType} response is passed `, () => {
const response = spec.interpretResponse(obj)

expect(response).to.be.an('array')
strictEqual(0, response.length)
})
}

it('Should return an empty array if invalid response is passed', () => {
const response = spec.interpretResponse({
body: [{
ttl: 120,
creativeId: '2',
netRevenue: true,
currency: 'USD',
dealId: '1'
}]
})

expect(response).to.be.an('array')
strictEqual(0, response.length)
})
})

describe('getUserSyncs', () => {
it('Returns valid URL and type', () => {
const expectedResult = [{ type: 'image', url: 'https://supply.decenterads.com/?c=o&m=cookie' }]
deepStrictEqual(expectedResult, spec.getUserSyncs())
})
})
})