Skip to content
This repository was archived by the owner on Feb 13, 2021. It is now read-only.

Commit fc95a52

Browse files
anand-venkatramanmkendall07
authored andcommitted
Consent Management module bug fix. (prebid#2588)
* 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 prebid#509 * ET-1765: Adding support for additional params in PulsePoint adapter (#2) * ET-1850: Fixing prebid#866 * Minor fix * Adding mandatory parameters to Bid * GDPR Bug Fix with String response * minor
1 parent 3e4d700 commit fc95a52

File tree

2 files changed

+68
-32
lines changed

2 files changed

+68
-32
lines changed

modules/consentManagement.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as utils from 'src/utils';
88
import { config } from 'src/config';
99
import { gdprDataHandler } from 'src/adaptermanager';
1010
import includes from 'core-js/library/fn/array/includes';
11+
import strIncludes from 'core-js/library/fn/string/includes';
1112

1213
const DEFAULT_CMP = 'iab';
1314
const DEFAULT_CONSENT_TIMEOUT = 10000;
@@ -132,7 +133,7 @@ function lookupIabConsent(cmpSuccess, cmpError, adUnits) {
132133

133134
function readPostMessageResponse(event) {
134135
// small customization to prevent reading strings from other sources that aren't JSON.stringified
135-
let json = (typeof event.data === 'string' && includes(event.data, 'cmpReturn')) ? JSON.parse(event.data) : event.data;
136+
let json = (typeof event.data === 'string' && strIncludes(event.data, 'cmpReturn')) ? JSON.parse(event.data) : event.data;
136137
if (json.__cmpReturn) {
137138
let i = json.__cmpReturn;
138139
cmpCallbacks[i.callId](i.returnValue, i.success);

test/spec/modules/consentManagement_spec.js

Lines changed: 66 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -201,60 +201,95 @@ describe('consentManagement', function () {
201201

202202
describe('CMP workflow for iframed page', () => {
203203
let eventStub = sinon.stub();
204-
let cmpStub = sinon.stub();
204+
let postMessageStub = sinon.stub();
205+
let ifr = null;
205206

206207
beforeEach(() => {
207208
didHookReturn = false;
208-
window.__cmp = function() {};
209209
sinon.stub(utils, 'logError');
210210
sinon.stub(utils, 'logWarn');
211+
ifr = createIFrameMarker();
211212
});
212213

213214
afterEach(() => {
214215
config.resetConfig();
215216
$$PREBID_GLOBAL$$.requestBids.removeHook(requestBidsHook);
216217
eventStub.restore();
217-
cmpStub.restore();
218+
postMessageStub.restore();
218219
delete window.__cmp;
219220
utils.logError.restore();
220221
utils.logWarn.restore();
221222
resetConsentData();
223+
document.body.removeChild(ifr);
222224
});
223225

224-
it('should return the consent string from a postmessage + addEventListener response', () => {
225-
let testConsentData = {
226-
data: {
227-
__cmpReturn: {
228-
returnValue: {
229-
gdprApplies: true,
230-
metadata: 'BOJy+UqOJy+UqABAB+AAAAAZ+A=='
231-
}
226+
function createIFrameMarker() {
227+
var ifr = document.createElement('iframe');
228+
ifr.width = 0;
229+
ifr.height = 0;
230+
ifr.name = '__cmpLocator';
231+
document.body.appendChild(ifr);
232+
return ifr;
233+
}
234+
235+
testIFramedPage('with/JSON response', {
236+
data: {
237+
__cmpReturn: {
238+
returnValue: {
239+
gdprApplies: true,
240+
metadata: 'BOJy+UqOJy+UqABAB+AAAAAZ+A=='
232241
}
233242
}
234-
};
235-
eventStub = sinon.stub(window, 'addEventListener').callsFake((...args) => {
236-
args[1](testConsentData);
237-
});
238-
cmpStub = sinon.stub(window, '__cmp').callsFake((...args) => {
239-
args[2]({
240-
gdprApplies: true,
241-
metadata: 'BOJy+UqOJy+UqABAB+AAAAAZ+A=='
243+
}
244+
}, false);
245+
246+
testIFramedPage('with/String response', {
247+
data: {
248+
__cmpReturn: {
249+
returnValue: {
250+
gdprApplies: true,
251+
metadata: 'BOJy+UqOJy+UqABAB+AAAAAZ+A=='
252+
}
253+
}
254+
}
255+
}, true);
256+
257+
function testIFramedPage(testName, testConsentData, messageFormatString) {
258+
it(`should return the consent string from a postmessage + addEventListener response - ${testName}`, () => {
259+
let messageListener;
260+
eventStub = sinon.stub(window, 'addEventListener').callsFake((...args) => {
261+
// save reference to event listener for message
262+
// so we can return the data when the message arrives via 'postMessage'
263+
messageListener = args[1];
264+
});
265+
// when the iframed window sends a message to the window
266+
// containing the CMP, intercept it and respond back with data
267+
// on the message listener.
268+
postMessageStub = sinon.stub(window, 'postMessage').callsFake((...args) => {
269+
if (messageListener && args[0] && args[0].__cmpCall) {
270+
// take the callId from request and stamp it on the response.
271+
testConsentData.data.__cmpReturn.callId = args[0].__cmpCall.callId;
272+
// serialize the data part to String if requested
273+
messageListener(messageFormatString ? {
274+
data: JSON.stringify(testConsentData.data)
275+
} : testConsentData);
276+
}
242277
});
243-
});
244278

245-
setConfig(goodConfigWithAllowAuction);
279+
setConfig(goodConfigWithAllowAuction);
246280

247-
requestBidsHook({}, () => {
248-
didHookReturn = true;
249-
});
250-
let consent = gdprDataHandler.getConsentData();
281+
requestBidsHook({}, () => {
282+
didHookReturn = true;
283+
});
284+
let consent = gdprDataHandler.getConsentData();
251285

252-
sinon.assert.notCalled(utils.logWarn);
253-
sinon.assert.notCalled(utils.logError);
254-
expect(didHookReturn).to.be.true;
255-
expect(consent.consentString).to.equal('BOJy+UqOJy+UqABAB+AAAAAZ+A==');
256-
expect(consent.gdprApplies).to.be.true;
257-
});
286+
sinon.assert.notCalled(utils.logWarn);
287+
sinon.assert.notCalled(utils.logError);
288+
expect(didHookReturn).to.be.true;
289+
expect(consent.consentString).to.equal('BOJy+UqOJy+UqABAB+AAAAAZ+A==');
290+
expect(consent.gdprApplies).to.be.true;
291+
});
292+
}
258293
});
259294

260295
describe('CMP workflow for normal pages:', () => {

0 commit comments

Comments
 (0)