Skip to content

Commit

Permalink
Filter out invalid key:values in Prebid.js before sending to SSP (#9)
Browse files Browse the repository at this point in the history
Co-authored-by: adci <adam.cink@ibillboard.com>
  • Loading branch information
hidlos and hidlos authored Mar 18, 2021
1 parent 13393f6 commit 3349945
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 78 deletions.
13 changes: 11 additions & 2 deletions modules/stroeerCoreBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,19 +332,28 @@ export const spec = {

function getGlobalKeyValues() {
try {
return win.SDG.Publisher.getConfig().getFilteredKeyValues();
return getValidKeyValues(win.SDG.Publisher.getConfig().getFilteredKeyValues());
} catch (e) {
return undefined;
}
}

function getLocalKeyValues(position) {
try {
return win.SDG.getCN().getSlotByPosition(position).getFilteredKeyValues();
return getValidKeyValues(win.SDG.getCN().getSlotByPosition(position).getFilteredKeyValues());
} catch (e) {
return undefined;
}
}

function getValidKeyValues(allKeyValues) {
const validKeys = Object.keys(allKeyValues).filter((key) => isValidValuesForKeyValue(allKeyValues[key]))
return validKeys.reduce((keyValues, key) => ({...keyValues, [key]: allKeyValues[key]}), {});
}

function isValidValuesForKeyValue(values) {
return Array.isArray(values) && values.every((v) => typeof v === 'string' || typeof v === 'number');
}
},

interpretResponse: function (serverResponse) {
Expand Down
207 changes: 131 additions & 76 deletions test/spec/modules/stroeerCoreBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,63 +594,118 @@ describe('stroeerCore bid adapter', function () {
assert.deepEqual(actualJsonPayload, expectedJsonPayload);
});

it('should have expected global key values', () => {
win.SDG = buildFakeSDGForGlobalKeyValues({
adset: ['brsl'],
browserapp: ['chrome'],
describe('and metatag is available', () => {
it('should have expected global key values', () => {
win.SDG = buildFakeSDGForGlobalKeyValues({
adset: ['brsl'],
browserapp: ['chrome'],
});

const bidReq = buildBidderRequest();

const serverRequestInfo = spec.buildRequests(bidReq.bids, bidReq)[0];

assert.deepEqual(serverRequestInfo.data.kvg, {
adset: ['brsl'],
browserapp: ['chrome'],
});
});

const bidReq = buildBidderRequest();
it('should filter out invalid global key values', () => {
win.SDG = buildFakeSDGForGlobalKeyValues({
validString: ['brsl'],
validNumber: [1],
validMixed: ['brsl', 1],
invalidOne: [true],
invalidTwo: [['string']],
invalidThree: [[1]],
invalidFour: {a: 1},
invalidFive: [{a: 1}],
invalidSix: true,
invalidSeven: 'string',
invalidEight: 1,
});

const serverRequestInfo = spec.buildRequests(bidReq.bids, bidReq)[0];
const bidReq = buildBidderRequest();

const serverRequestInfo = spec.buildRequests(bidReq.bids, bidReq)[0];

assert.deepEqual(serverRequestInfo.data.kvg, {
adset: ['brsl'],
browserapp: ['chrome'],
assert.deepEqual(serverRequestInfo.data.kvg, {
validString: ['brsl'],
validNumber: [1],
validMixed: ['brsl', 1],
});
});

function buildFakeSDGForGlobalKeyValues(keyValues) {
return {
Publisher: {
getConfig: function() {
getConfig: function () {
return {
getFilteredKeyValues: function() {
getFilteredKeyValues: function () {
return keyValues;
}
}
}
}
}
}
});

it('should have expected local key values', () => {
win.SDG = buildFakeSDGForLocalKeyValues({
'div-1': {
it('should have expected local key values', () => {
win.SDG = buildFakeSDGForLocalKeyValues({
'div-1': {
as: ['banner'],
hb_unit: ['banner'],
pc: ['1'],
},
'div-2': {
as: ['bannerer'],
hb_unit: ['bannerer'],
pc: ['2'],
}
});

const bidReq = buildBidderRequest();
const serverRequestInfo = spec.buildRequests(bidReq.bids, bidReq)[0];

assert.deepEqual(serverRequestInfo.data.bids[0].kvl, {
as: ['banner'],
hb_unit: ['banner'],
pc: ['1'],
},
'div-2': {
});

assert.deepEqual(serverRequestInfo.data.bids[1].kvl, {
as: ['bannerer'],
hb_unit: ['bannerer'],
pc: ['2'],
}
});
});

const bidReq = buildBidderRequest();
const serverRequestInfo = spec.buildRequests(bidReq.bids, bidReq)[0];
it('should filter out invalid local key values', () => {
win.SDG = buildFakeSDGForLocalKeyValues({
'div-1': {
validString: ['brsl'],
validNumber: [1],
validMixed: ['brsl', 1],
invalidOne: [true],
invalidTwo: [['string']],
invalidThree: [[1]],
invalidFour: {a: 1},
invalidFive: [{a: 1}],
invalidSix: true,
invalidSeven: 'string',
invalidEight: 1,
}
});

assert.deepEqual(serverRequestInfo.data.bids[0].kvl, {
as: ['banner'],
hb_unit: ['banner'],
pc: ['1'],
});
const bidReq = buildBidderRequest();
const serverRequestInfo = spec.buildRequests(bidReq.bids, bidReq)[0];

assert.deepEqual(serverRequestInfo.data.bids[1].kvl, {
as: ['bannerer'],
hb_unit: ['bannerer'],
pc: ['2'],
assert.deepEqual(serverRequestInfo.data.bids[0].kvl, {
validString: ['brsl'],
validNumber: [1],
validMixed: ['brsl', 1],
});
});

function buildFakeSDGForLocalKeyValues(localTargeting) {
Expand All @@ -666,60 +721,60 @@ describe('stroeerCore bid adapter', function () {
}
}
}
});

it('should have expected context', () => {
win.SDG = buildFakeSDGContext({
'div-1': {
adUnits: ['adUnit-1', 'adUnit-2'],
zone: 'zone-1',
pageType: 'pageType-1'
},
'div-2': {
adUnits: ['adUnit-3', 'adUnit-4', 'adUnit-5'],
zone: 'zone-2',
pageType: 'pageType-2'
}
});
const bidReq = buildBidderRequest();
it('should have expected context', () => {
win.SDG = buildFakeSDGContext({
'div-1': {
adUnits: ['adUnit-1', 'adUnit-2'],
zone: 'zone-1',
pageType: 'pageType-1'
},
'div-2': {
adUnits: ['adUnit-3', 'adUnit-4', 'adUnit-5'],
zone: 'zone-2',
pageType: 'pageType-2'
}
});
const bidReq = buildBidderRequest();

const serverRequestInfo = spec.buildRequests(bidReq.bids, bidReq)[0];
const serverRequestInfo = spec.buildRequests(bidReq.bids, bidReq)[0];

assert.deepEqual(serverRequestInfo.data.bids[0].ctx, {
'position': 'div-1',
'adUnits': ['adUnit-1', 'adUnit-2'],
'zone': 'zone-1',
'pageType': 'pageType-1'
});
assert.deepEqual(serverRequestInfo.data.bids[0].ctx, {
'position': 'div-1',
'adUnits': ['adUnit-1', 'adUnit-2'],
'zone': 'zone-1',
'pageType': 'pageType-1'
});

assert.deepEqual(serverRequestInfo.data.bids[1].ctx, {
'position': 'div-2',
'adUnits': ['adUnit-3', 'adUnit-4', 'adUnit-5'],
'zone': 'zone-2',
'pageType': 'pageType-2'
});
assert.deepEqual(serverRequestInfo.data.bids[1].ctx, {
'position': 'div-2',
'adUnits': ['adUnit-3', 'adUnit-4', 'adUnit-5'],
'zone': 'zone-2',
'pageType': 'pageType-2'
});

function buildFakeSDGContext(config) {
return {
getCN: function () {
return {
getSlotByPosition: function (position) {
return {
getAdUnits: function () {
return config[position].adUnits;
},
getZone: function () {
return config[position].zone;
},
getPageType: function () {
return config[position].pageType;
},
};
}
};
function buildFakeSDGContext(config) {
return {
getCN: function () {
return {
getSlotByPosition: function (position) {
return {
getAdUnits: function () {
return config[position].adUnits;
},
getZone: function () {
return config[position].zone;
},
getPageType: function () {
return config[position].pageType;
},
};
}
};
}
}
}
}
});
});

it('should handle banner sizes for pre version 3', () => {
Expand Down

0 comments on commit 3349945

Please sign in to comment.