Skip to content

Commit

Permalink
+ auction index
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Komorski authored and Marcin Komorski committed Aug 28, 2024
1 parent ed8dda8 commit 5697f4e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 27 deletions.
6 changes: 4 additions & 2 deletions modules/bidResponseFilter/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { auctionManager } from '../../src/auctionManager.js';
import { config } from '../../src/config.js';
import { getHook } from '../../src/hook.js';

Expand All @@ -10,8 +11,9 @@ function init() {
getHook('addBidResponse').before(addBidResponseHook);
};

export function addBidResponseHook(next, adUnitCode, bid, reject) {
const { bcat = [], badv = [], battr = [] } = config.getAnyConfig('ortb2') || {};
export function addBidResponseHook(next, adUnitCode, bid, reject, index = auctionManager.index) {
const {bcat = [], badv = []} = index.getOrtb2(bid) || {};
const battr = index.getBidRequest(bid)?.ortb2Imp[bid.mediaType]?.battr || index.getAdUnit(bid)?.ortb2Imp[bid.mediaType]?.battr || [];
const moduleConfig = config.getConfig(MODULE_NAME);

const catConfig = {enforce: true, blockUnknown: true, ...(moduleConfig?.cat || {})};
Expand Down
13 changes: 13 additions & 0 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ function validateSizes(sizes, targLength) {
return cleanSizes;
}

function setBattrForAdUnit(adUnit, mediaType) {
const battr = adUnit.ortb2Imp?.[mediaType]?.battr || adUnit.mediaType?.[mediaType]?.battr;
if (adUnit.ortb2Imp?.[mediaType] && battr) {
adUnit.ortb2Imp[mediaType].battr = battr;
}
if (adUnit.mediaTypes?.[mediaType] && battr) {
adUnit.mediaTypes[mediaType].battr = battr;
}
}

function validateBannerMediaType(adUnit) {
const validatedAdUnit = deepClone(adUnit);
const banner = validatedAdUnit.mediaTypes.banner;
Expand All @@ -112,6 +122,7 @@ function validateBannerMediaType(adUnit) {
logError('Detected a mediaTypes.banner object without a proper sizes field. Please ensure the sizes are listed like: [[300, 250], ...]. Removing invalid mediaTypes.banner object from request.');
delete validatedAdUnit.mediaTypes.banner
}
setBattrForAdUnit(validatedAdUnit, 'banner');
return validatedAdUnit;
}

Expand All @@ -135,6 +146,7 @@ function validateVideoMediaType(adUnit) {
}
}
validateOrtbVideoFields(validatedAdUnit);
setBattrForAdUnit(validatedAdUnit, 'video');
return validatedAdUnit;
}

Expand Down Expand Up @@ -184,6 +196,7 @@ function validateNativeMediaType(adUnit) {
logError('Please use an array of sizes for native.icon.sizes field. Removing invalid mediaTypes.native.icon.sizes property from request.');
delete validatedAdUnit.mediaTypes.native.icon.sizes;
}
setBattrForAdUnit(validatedAdUnit, 'native');
return validatedAdUnit;
}

Expand Down
68 changes: 43 additions & 25 deletions test/spec/modules/bidResponseFilter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,31 @@ import { BID_ADV_DOMAINS_REJECTION_REASON, BID_ATTR_REJECTION_REASON, BID_CATEGO
import { config } from '../../../src/config';

describe('bidResponseFilter', () => {
afterEach(() => {
let mockAuctionIndex
beforeEach(() => {
config.resetConfig();
mockAuctionIndex = {
getBidRequest: () => {},
getAdUnit: () => {}
};
});

it('should pass the bid after successful ortb2 rules validation', () => {
const call = sinon.stub();

mockAuctionIndex.getOrtb2 = () => ({
badv: [], bcat: ['BANNED_CAT1', 'BANNED_CAT2']
});

const bid = {
meta: {
advertiserDomains: ['domain1.com', 'domain2.com'],
primaryCatId: 'EXAMPLE-CAT-ID',
attr: 'attr'
}
};
config.setConfig({ortb2: {
badv: [], bcat: ['BANNED_CAT1', 'BANNED_CAT2'], battr: 'BANNED_ATTR'
}});

addBidResponseHook(call, 'adcode', bid, () => {});
addBidResponseHook(call, 'adcode', bid, () => {}, mockAuctionIndex);
sinon.assert.calledOnce(call);
});

Expand All @@ -33,11 +40,11 @@ describe('bidResponseFilter', () => {
attr: 'attr'
}
};
config.setConfig({ortb2: {
badv: [], bcat: ['BANNED_CAT1', 'BANNED_CAT2'], battr: 'BANNED_ATTR'
}});
mockAuctionIndex.getOrtb2 = () => ({
badv: [], bcat: ['BANNED_CAT1', 'BANNED_CAT2']
});

addBidResponseHook(call, 'adcode', bid, reject);
addBidResponseHook(call, 'adcode', bid, reject, mockAuctionIndex);
sinon.assert.calledWith(reject, BID_CATEGORY_REJECTION_REASON);
});

Expand All @@ -51,11 +58,11 @@ describe('bidResponseFilter', () => {
attr: 'attr'
}
};
config.setConfig({ortb2: {
badv: ['domain2.com'], bcat: ['BANNED_CAT1', 'BANNED_CAT2'], battr: 'BANNED_ATTR'
}});
mockAuctionIndex.getOrtb2 = () => ({
badv: ['domain2.com'], bcat: ['BANNED_CAT1', 'BANNED_CAT2']
});

addBidResponseHook(call, 'adcode', bid, rejection);
addBidResponseHook(call, 'adcode', bid, rejection, mockAuctionIndex);
sinon.assert.calledWith(rejection, BID_ADV_DOMAINS_REJECTION_REASON);
});

Expand All @@ -67,13 +74,22 @@ describe('bidResponseFilter', () => {
advertiserDomains: ['validdomain1.com', 'validdomain2.com'],
primaryCatId: 'VALID_CAT',
attr: 'BANNED_ATTR'
}
},
mediaType: 'video'
};
config.setConfig({ortb2: {
badv: ['domain2.com'], bcat: ['BANNED_CAT1', 'BANNED_CAT2'], battr: 'BANNED_ATTR'
}});
mockAuctionIndex.getOrtb2 = () => ({
badv: ['domain2.com'], bcat: ['BANNED_CAT1', 'BANNED_CAT2']
});

addBidResponseHook(call, 'adcode', bid, reject);
mockAuctionIndex.getBidRequest = () => ({
ortb2Imp: {
video: {
battr: 'BANNED_ATTR'
}
}
})

addBidResponseHook(call, 'adcode', bid, reject, mockAuctionIndex);
sinon.assert.calledWith(reject, BID_ATTR_REJECTION_REASON);
});

Expand All @@ -86,13 +102,14 @@ describe('bidResponseFilter', () => {
attr: 'valid_attr'
}
};
config.setConfig({ortb2: {
badv: ['domain2.com'], bcat: ['BANNED_CAT1', 'BANNED_CAT2'], battr: 'BANNED_ATTR'
}});

mockAuctionIndex.getOrtb2 = () => ({
badv: ['domain2.com'], bcat: ['BANNED_CAT1', 'BANNED_CAT2']
});

config.setConfig({[MODULE_NAME]: {cat: {enforce: false}}});

addBidResponseHook(call, 'adcode', bid, () => {});
addBidResponseHook(call, 'adcode', bid, () => {}, mockAuctionIndex);
sinon.assert.calledOnce(call);
});

Expand All @@ -105,9 +122,10 @@ describe('bidResponseFilter', () => {
attr: 'valid_attr'
}
};
config.setConfig({ortb2: {
badv: ['domain2.com'], bcat: ['BANNED_CAT1', 'BANNED_CAT2'], battr: 'BANNED_ATTR'
}});

mockAuctionIndex.getOrtb2 = () => ({
badv: ['domain2.com'], bcat: ['BANNED_CAT1', 'BANNED_CAT2']
});

config.setConfig({[MODULE_NAME]: {cat: {blockUnknown: false}}});

Expand Down

0 comments on commit 5697f4e

Please sign in to comment.