@@ -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