Skip to content

Commit

Permalink
bidResponseFilter Module : do not run if not configured (prebid#12362)
Browse files Browse the repository at this point in the history
* bidResponseFilter: do not run if not configured

* fix lint

* fix tests
  • Loading branch information
dgirardi authored Oct 24, 2024
1 parent 8b5d33e commit d4f57ee
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 11 deletions.
21 changes: 18 additions & 3 deletions modules/bidResponseFilter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {})};
Expand Down
57 changes: 49 additions & 8 deletions test/spec/modules/bidResponseFilter_spec.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -26,7 +64,8 @@ describe('bidResponseFilter', () => {
}
};

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

Expand Down Expand Up @@ -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);
});

Expand All @@ -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);
});
})

0 comments on commit d4f57ee

Please sign in to comment.