Skip to content

Commit

Permalink
ConsentManagement module - refactor test (prebid#2627)
Browse files Browse the repository at this point in the history
* ET-1691: Pulsepoint Analytics adapter for Prebid. (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 prebid#509

* ET-1765: Adding support for additional params in PulsePoint adapter (prebid#2)

* ET-1850: Fixing prebid#866

* Minor fix

* Adding mandatory parameters to Bid

* GDPR Bug Fix with String response

* minor

* Fixing iframe page test

* Fixing lint error
  • Loading branch information
anand-venkatraman authored and AdSpacesDevelopers committed Jan 30, 2019
1 parent e6f7a81 commit ce76ae2
Showing 1 changed file with 51 additions and 36 deletions.
87 changes: 51 additions & 36 deletions test/spec/modules/consentManagement_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:', () => {
Expand Down

0 comments on commit ce76ae2

Please sign in to comment.