diff --git a/test/spec/modules/consentManagement_spec.js b/test/spec/modules/consentManagement_spec.js index 18df72f044d..eb1614e4965 100644 --- a/test/spec/modules/consentManagement_spec.js +++ b/test/spec/modules/consentManagement_spec.js @@ -201,95 +201,60 @@ describe('consentManagement', function () { describe('CMP workflow for iframed page', () => { let eventStub = sinon.stub(); - let postMessageStub = sinon.stub(); - let ifr = null; + let cmpStub = sinon.stub(); beforeEach(() => { didHookReturn = false; + window.__cmp = function() {}; sinon.stub(utils, 'logError'); sinon.stub(utils, 'logWarn'); - ifr = createIFrameMarker(); }); afterEach(() => { config.resetConfig(); $$PREBID_GLOBAL$$.requestBids.removeHook(requestBidsHook); eventStub.restore(); - postMessageStub.restore(); + cmpStub.restore(); delete window.__cmp; utils.logError.restore(); utils.logWarn.restore(); resetConsentData(); - document.body.removeChild(ifr); }); - function createIFrameMarker() { - var ifr = document.createElement('iframe'); - ifr.width = 0; - ifr.height = 0; - ifr.name = '__cmpLocator'; - document.body.appendChild(ifr); - return ifr; - } - - testIFramedPage('with/JSON response', { - data: { - __cmpReturn: { - returnValue: { - gdprApplies: true, - metadata: 'BOJy+UqOJy+UqABAB+AAAAAZ+A==' - } - } - } - }, false); - - testIFramedPage('with/String response', { - data: { - __cmpReturn: { - returnValue: { - gdprApplies: true, - metadata: 'BOJy+UqOJy+UqABAB+AAAAAZ+A==' + it('should return the consent string from a postmessage + addEventListener response', () => { + let testConsentData = { + data: { + __cmpReturn: { + returnValue: { + gdprApplies: true, + metadata: 'BOJy+UqOJy+UqABAB+AAAAAZ+A==' + } } } - } - }, true); - - function testIFramedPage(testName, testConsentData, messageFormatString) { - it(`should return the consent string from a postmessage + addEventListener response - ${testName}`, () => { - let messageListener; - eventStub = sinon.stub(window, 'addEventListener').callsFake((...args) => { - // save reference to event listener for message - // so we can return the data when the message arrives via 'postMessage' - messageListener = args[1]; - }); - // when the iframed window sends a message to the window - // containing the CMP, intercept it and respond back with data - // on the message listener. - postMessageStub = sinon.stub(window, 'postMessage').callsFake((...args) => { - if (messageListener && args[0] && args[0].__cmpCall) { - // take the callId from request and stamp it on the response. - testConsentData.data.__cmpReturn.callId = args[0].__cmpCall.callId; - // serialize the data part to String if requested - messageListener(messageFormatString ? { - data: JSON.stringify(testConsentData.data) - } : testConsentData); - } + }; + eventStub = sinon.stub(window, 'addEventListener').callsFake((...args) => { + args[1](testConsentData); + }); + cmpStub = sinon.stub(window, '__cmp').callsFake((...args) => { + args[2]({ + gdprApplies: true, + metadata: 'BOJy+UqOJy+UqABAB+AAAAAZ+A==' }); + }); - setConfig(goodConfigWithAllowAuction); - - requestBidsHook({}, () => { - didHookReturn = true; - }); - let consent = gdprDataHandler.getConsentData(); + setConfig(goodConfigWithAllowAuction); - sinon.assert.notCalled(utils.logWarn); - sinon.assert.notCalled(utils.logError); - expect(didHookReturn).to.be.true; - expect(consent.consentString).to.equal('BOJy+UqOJy+UqABAB+AAAAAZ+A=='); - expect(consent.gdprApplies).to.be.true; + requestBidsHook({}, () => { + didHookReturn = true; }); - } + let consent = gdprDataHandler.getConsentData(); + + sinon.assert.notCalled(utils.logWarn); + sinon.assert.notCalled(utils.logError); + expect(didHookReturn).to.be.true; + expect(consent.consentString).to.equal('BOJy+UqOJy+UqABAB+AAAAAZ+A=='); + expect(consent.gdprApplies).to.be.true; + }); }); describe('CMP workflow for normal pages:', () => {