Skip to content

Commit

Permalink
move logic to check if CMP frame is not found (prebid#2715)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnellbaker authored and AlessandroDG committed Sep 13, 2018
1 parent d3c4499 commit a149555
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
10 changes: 4 additions & 6 deletions modules/consentManagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ function lookupIabConsent(cmpSuccess, cmpError, hookConfig) {
f = f.parent;
}

if (!cmpFrame) {
return cmpError('CMP not found.', hookConfig);
}

callCmpWhileInIframe('getConsentData', cmpFrame, callbackHandler.consentDataCallback);
callCmpWhileInIframe('getVendorConsents', cmpFrame, callbackHandler.vendorConsentsCallback);
}
Expand Down Expand Up @@ -124,12 +128,6 @@ function lookupIabConsent(cmpSuccess, cmpError, hookConfig) {
/* Setup up a __cmp function to do the postMessage and stash the callback.
This function behaves (from the caller's perspective identicially to the in-frame __cmp call */
window.__cmp = function(cmd, arg, callback) {
if (!cmpFrame) {
removePostMessageListener();

let errmsg = 'CMP not found';
return cmpError(errmsg, hookConfig);
}
let callId = Math.random() + '';
let msg = {__cmpCall: {
command: cmd,
Expand Down
60 changes: 36 additions & 24 deletions test/spec/modules/consentManagement_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,35 +68,47 @@ describe('consentManagement', function () {
});

describe('error checks:', () => {
describe('unknown CMP framework ID:', () => {
beforeEach(() => {
sinon.stub(utils, 'logWarn');
});
beforeEach(() => {
didHookReturn = false;
sinon.stub(utils, 'logWarn');
sinon.stub(utils, 'logError');
});

afterEach(() => {
utils.logWarn.restore();
config.resetConfig();
$$PREBID_GLOBAL$$.requestBids.removeHook(requestBidsHook);
resetConsentData();
});
afterEach(() => {
utils.logWarn.restore();
utils.logError.restore();
config.resetConfig();
$$PREBID_GLOBAL$$.requestBids.removeHook(requestBidsHook);
resetConsentData();
});

it('should throw a warning and return to hooked function when an unknown CMP framework ID is used', () => {
let badCMPConfig = {
cmpApi: 'bad'
};
setConfig(badCMPConfig);
expect(userCMP).to.be.equal(badCMPConfig.cmpApi);

it('should return Warning message and return to hooked function', () => {
let badCMPConfig = {
cmpApi: 'bad'
};
setConfig(badCMPConfig);
expect(userCMP).to.be.equal(badCMPConfig.cmpApi);
requestBidsHook({}, () => {
didHookReturn = true;
});
let consent = gdprDataHandler.getConsentData();
sinon.assert.calledOnce(utils.logWarn);
expect(didHookReturn).to.be.true;
expect(consent).to.be.null;
});

didHookReturn = false;
it('should throw proper errors when CMP is not found', () => {
setConfig(goodConfigWithCancelAuction);

requestBidsHook({}, () => {
didHookReturn = true;
});
let consent = gdprDataHandler.getConsentData();
sinon.assert.calledOnce(utils.logWarn);
expect(didHookReturn).to.be.true;
expect(consent).to.be.null;
requestBidsHook({}, () => {
didHookReturn = true;
});
let consent = gdprDataHandler.getConsentData();
// throw 2 errors; one for no bidsBackHandler and for CMP not being found (this is an error due to gdpr config)
sinon.assert.calledTwice(utils.logError);
expect(didHookReturn).to.be.false;
expect(consent).to.be.null;
});
});

Expand Down

0 comments on commit a149555

Please sign in to comment.