Skip to content

Commit

Permalink
fix a bug when the iframe locator is not present on page (#4621)
Browse files Browse the repository at this point in the history
* fix a bug when the iframe locator is not present on page

* clean up
  • Loading branch information
mkendall07 authored Dec 18, 2019
1 parent df69f08 commit aff2bd4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 15 deletions.
29 changes: 14 additions & 15 deletions modules/consentManagementUsp.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,26 @@ function lookupUspConsent(uspSuccess, uspError, hookConfig) {
// - use the USPAPI locator code to see if USP's located in the current window or an ancestor window. This works in friendly or cross domain iframes
// - if USPAPI is not found, the iframe function will call the uspError exit callback to abort the rest of the USPAPI workflow
// - try to call the __uspapi() function directly, otherwise use the postMessage() api

// find the CMP frame/window
let f = window;
let uspapiFrame;
while (!uspapiFrame) {
try {
if (f.frames['__uspapiLocator']) uspapiFrame = f;
} catch (e) { }
if (f === window.top) break;
f = f.parent;
}

if (!uspapiFrame) {
return uspError('USP CMP not found.', hookConfig);
}

try {
// try to call __uspapi directly
uspapiFrame.__uspapi('getUSPData', USPAPI_VERSION, callbackHandler.consentDataCallback);
window.__uspapi('getUSPData', USPAPI_VERSION, callbackHandler.consentDataCallback);
} catch (e) {
// must not have been accessible, try using postMessage() api
let f = window;
let uspapiFrame;
while (!uspapiFrame) {
try {
if (f.frames['__uspapiLocator']) uspapiFrame = f;
} catch (e) { }
if (f === window.top) break;
f = f.parent;
}

if (!uspapiFrame) {
return uspError('USP CMP not found.', hookConfig);
}
callUspApiWhileInIframe('getUSPData', uspapiFrame, callbackHandler.consentDataCallback);
}

Expand Down
42 changes: 42 additions & 0 deletions test/spec/modules/consentManagementUsp_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,48 @@ describe('consentManagement', function () {
}
});

describe('test without iframe locater', function() {
let uspapiStub = sinon.stub();

beforeEach(function () {
didHookReturn = false;
sinon.stub(utils, 'logError');
sinon.stub(utils, 'logWarn');
window.__uspapi = function() {};
});

afterEach(function () {
config.resetConfig();
$$PREBID_GLOBAL$$.requestBids.removeAll();
uspapiStub.restore();
utils.logError.restore();
utils.logWarn.restore();
delete window.__uspapi;
resetConsentData();
});

it('Workflow for normal page withoout iframe locater', function() {
let testConsentData = {
uspString: '1NY'
};

uspapiStub = sinon.stub(window, '__uspapi').callsFake((...args) => {
args[2](testConsentData, true);
});

setConsentConfig(goodConfig);
requestBidsHook(() => { didHookReturn = true; }, {});

let consent = uspDataHandler.getConsentData();

sinon.assert.notCalled(utils.logWarn);
sinon.assert.notCalled(utils.logError);

expect(didHookReturn).to.be.true;
expect(consent).to.equal(testConsentData.uspString);
});
});

describe('USPAPI workflow for normal pages:', function () {
let uspapiStub = sinon.stub();
let ifr = null;
Expand Down

0 comments on commit aff2bd4

Please sign in to comment.