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

fix a bug when the iframe locator is not present on page #4621

Merged
merged 2 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
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
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