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

bidResponseFilter Module : do not run if not configured #12362

Merged
merged 3 commits into from
Oct 24, 2024
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
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);
});
})