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

move logic to check if CMP is not found #2715

Merged
merged 1 commit into from
Jun 21, 2018
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
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