From 534989453f35e114756155789de6e350791ca205 Mon Sep 17 00:00:00 2001 From: Anand Venkatraman Date: Fri, 25 May 2018 21:28:05 +0530 Subject: [PATCH] ConsentManagement module - refactor test (#2627) * ET-1691: Pulsepoint Analytics adapter for Prebid. (#1) * ET-1691: Adding pulsepoint analytics and tests for pulsepoint adapter * ET-1691: Adding pulsepoint analytics and tests for pulsepoint adapter * ET-1691: cleanup * ET-1691: minor * ET-1691: revert package.json change * Adding bidRequest to bidFactory.createBid method as per https://github.com/prebid/Prebid.js/issues/509 * ET-1765: Adding support for additional params in PulsePoint adapter (#2) * ET-1850: Fixing https://github.com/prebid/Prebid.js/issues/866 * Minor fix * Adding mandatory parameters to Bid * GDPR Bug Fix with String response * minor * Fixing iframe page test * Fixing lint error --- test/spec/modules/consentManagement_spec.js | 87 ++++++++++++--------- 1 file changed, 51 insertions(+), 36 deletions(-) diff --git a/test/spec/modules/consentManagement_spec.js b/test/spec/modules/consentManagement_spec.js index 2bb444a71aa..e825de2a184 100644 --- a/test/spec/modules/consentManagement_spec.js +++ b/test/spec/modules/consentManagement_spec.js @@ -204,61 +204,76 @@ describe('consentManagement', function () { }); describe('CMP workflow for iframed page', () => { - let eventStub = sinon.stub(); - let cmpStub = sinon.stub(); + let ifr = null; + let stringifyResponse = false; beforeEach(() => { - didHookReturn = false; - window.__cmp = function() {}; sinon.stub(utils, 'logError'); sinon.stub(utils, 'logWarn'); + ifr = createIFrameMarker(); + window.addEventListener('message', cmpMessageHandler, false); }); afterEach(() => { config.resetConfig(); $$PREBID_GLOBAL$$.requestBids.removeHook(requestBidsHook); - eventStub.restore(); - cmpStub.restore(); delete window.__cmp; utils.logError.restore(); utils.logWarn.restore(); resetConsentData(); + document.body.removeChild(ifr); + window.removeEventListener('message', cmpMessageHandler); }); - it('should return the consent string from a postmessage + addEventListener response', () => { - let testConsentData = { - data: { - __cmpReturn: { - returnValue: { - gdprApplies: true, - consentData: 'BOJy+UqOJy+UqABAB+AAAAAZ+A==' + function createIFrameMarker() { + var ifr = document.createElement('iframe'); + ifr.width = 0; + ifr.height = 0; + ifr.name = '__cmpLocator'; + document.body.appendChild(ifr); + return ifr; + } + + function cmpMessageHandler(event) { + if (event && event.data) { + var data = event.data; + if (data.__cmpCall) { + var callId = data.__cmpCall.callId; + var returnValue = null; + var response = { + __cmpReturn: { + callId, + returnValue: { + consentData: 'encoded_consent_data_via_post_message', + gdprApplies: true, + }, + success: true } - } + }; + event.source.postMessage(stringifyResponse ? JSON.stringify(response) : response, '*'); } - }; - eventStub = sinon.stub(window, 'addEventListener').callsFake((...args) => { - args[1](testConsentData); - }); - cmpStub = sinon.stub(window, '__cmp').callsFake((...args) => { - args[2]({ - gdprApplies: true, - consentData: 'BOJy+UqOJy+UqABAB+AAAAAZ+A==' + } + } + + // Run tests with JSON response and String response + // from CMP window postMessage listener. + testIFramedPage('with/JSON response', false); + testIFramedPage('with/String response', true); + + function testIFramedPage(testName, messageFormatString) { + it(`should return the consent string from a postmessage + addEventListener response - ${testName}`, (done) => { + stringifyResponse = messageFormatString; + setConfig(goodConfigWithAllowAuction); + requestBidsHook({}, () => { + let consent = gdprDataHandler.getConsentData(); + sinon.assert.notCalled(utils.logWarn); + sinon.assert.notCalled(utils.logError); + expect(consent.consentString).to.equal('encoded_consent_data_via_post_message'); + expect(consent.gdprApplies).to.be.true; + done(); }); }); - - setConfig(goodConfigWithAllowAuction); - - 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:', () => {