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

Gdpr fix unit test #2604

Merged
merged 2 commits into from
May 23, 2018
Merged
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
97 changes: 31 additions & 66 deletions test/spec/modules/consentManagement_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:', () => {
Expand Down