From d4f57ee85a0fb5ae2d56affdd90994ac0f4ea627 Mon Sep 17 00:00:00 2001 From: Demetrio Girardi Date: Thu, 24 Oct 2024 07:47:42 -0700 Subject: [PATCH] bidResponseFilter Module : do not run if not configured (#12362) * bidResponseFilter: do not run if not configured * fix lint * fix tests --- modules/bidResponseFilter/index.js | 21 ++++++-- test/spec/modules/bidResponseFilter_spec.js | 57 ++++++++++++++++++--- 2 files changed, 67 insertions(+), 11 deletions(-) diff --git a/modules/bidResponseFilter/index.js b/modules/bidResponseFilter/index.js index 3ace8108d2b..5b138965983 100644 --- a/modules/bidResponseFilter/index.js +++ b/modules/bidResponseFilter/index.js @@ -7,14 +7,29 @@ export const BID_CATEGORY_REJECTION_REASON = 'Category is not allowed'; export const BID_ADV_DOMAINS_REJECTION_REASON = 'Adv domain is not allowed'; export const BID_ATTR_REJECTION_REASON = 'Attr is not allowed'; +let moduleConfig; +let enabled = false; + function init() { - getHook('addBidResponse').before(addBidResponseHook); -}; + config.getConfig(MODULE_NAME, (cfg) => { + moduleConfig = cfg[MODULE_NAME]; + if (enabled && !moduleConfig) { + reset(); + } else if (!enabled && moduleConfig) { + enabled = true; + getHook('addBidResponse').before(addBidResponseHook); + } + }) +} + +export function reset() { + enabled = false; + getHook('addBidResponse').getHooks({hook: addBidResponseHook}).remove(); +} 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 || {})}; const advConfig = {enforce: true, blockUnknown: true, ...(moduleConfig?.adv || {})}; diff --git a/test/spec/modules/bidResponseFilter_spec.js b/test/spec/modules/bidResponseFilter_spec.js index 3990cd3feb3..c4b4a776243 100644 --- a/test/spec/modules/bidResponseFilter_spec.js +++ b/test/spec/modules/bidResponseFilter_spec.js @@ -1,15 +1,53 @@ -import { BID_ADV_DOMAINS_REJECTION_REASON, BID_ATTR_REJECTION_REASON, BID_CATEGORY_REJECTION_REASON, MODULE_NAME, PUBLISHER_FILTER_REJECTION_REASON, addBidResponseHook } from '../../../modules/bidResponseFilter'; -import { config } from '../../../src/config'; +import { + addBidResponseHook, + BID_ADV_DOMAINS_REJECTION_REASON, + BID_ATTR_REJECTION_REASON, + BID_CATEGORY_REJECTION_REASON, + init, + MODULE_NAME + , reset} from '../../../modules/bidResponseFilter'; +import {config} from '../../../src/config'; +import {addBidResponse} from '../../../src/auction.js'; describe('bidResponseFilter', () => { let mockAuctionIndex beforeEach(() => { - config.resetConfig(); mockAuctionIndex = { - getBidRequest: () => {}, - getAdUnit: () => {} + getBidRequest: () => { + }, + getAdUnit: () => { + } }; }); + afterEach(() => { + config.resetConfig(); + reset(); + }) + + describe('enable/disable', () => { + let reject, dispatch; + + beforeEach(() => { + reject = sinon.stub(); + dispatch = sinon.stub(); + }); + + it('should not run if not configured', () => { + reset(); + addBidResponse.call({dispatch}, 'au', {}, reject); + sinon.assert.notCalled(reject); + sinon.assert.called(dispatch); + }); + + it('should run if configured', () => { + config.setConfig({ + bidResponseFilter: {} + }); + addBidResponse.call({dispatch}, 'au', {}, reject); + sinon.assert.called(reject); + sinon.assert.notCalled(dispatch); + }) + }); it('should pass the bid after successful ortb2 rules validation', () => { const call = sinon.stub(); @@ -26,7 +64,8 @@ describe('bidResponseFilter', () => { } }; - addBidResponseHook(call, 'adcode', bid, () => {}, mockAuctionIndex); + addBidResponseHook(call, 'adcode', bid, () => { + }, mockAuctionIndex); sinon.assert.calledOnce(call); }); @@ -109,7 +148,8 @@ describe('bidResponseFilter', () => { config.setConfig({[MODULE_NAME]: {cat: {enforce: false}}}); - addBidResponseHook(call, 'adcode', bid, () => {}, mockAuctionIndex); + addBidResponseHook(call, 'adcode', bid, () => { + }, mockAuctionIndex); sinon.assert.calledOnce(call); }); @@ -129,7 +169,8 @@ describe('bidResponseFilter', () => { config.setConfig({[MODULE_NAME]: {cat: {blockUnknown: false}}}); - addBidResponseHook(call, 'adcode', bid, () => {}); + addBidResponseHook(call, 'adcode', bid, () => { + }); sinon.assert.calledOnce(call); }); })