diff --git a/modules/consentManagementUsp.js b/modules/consentManagementUsp.js index af9f4f05dbf..a210132947c 100644 --- a/modules/consentManagementUsp.js +++ b/modules/consentManagementUsp.js @@ -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); } diff --git a/test/spec/modules/consentManagementUsp_spec.js b/test/spec/modules/consentManagementUsp_spec.js index d6e0ef22f83..d757ea0b86f 100644 --- a/test/spec/modules/consentManagementUsp_spec.js +++ b/test/spec/modules/consentManagementUsp_spec.js @@ -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;