prebid-universal-creative 1.12.0 → 1.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,7 @@ import { newNativeAssetManager } from 'src/nativeAssetManager';
4
4
  import { mocks } from 'test/helpers/mocks';
5
5
  import * as utils from 'src/utils';
6
6
 
7
+ const ORIGIN = 'https://origin.com'
7
8
  const AD_ID = 'abc123';
8
9
  const AD_ID2 = 'def456';
9
10
  const NATIVE_KEYS = {
@@ -43,17 +44,18 @@ function createResponder(assets,url,template) {
43
44
  if (type !== 'message') { return; }
44
45
 
45
46
  const data = { message: 'assetResponse', adId: AD_ID, assets, adTemplate:template, rendererUrl:url };
46
- listener({ data: JSON.stringify(data) });
47
+ listener({ data: JSON.stringify(data), origin: ORIGIN});
47
48
  };
48
49
  }
49
50
 
50
51
  // creates mock postmessage response from prebid's native.js:getAssetMessage
51
- function createAllResponder(assets,url,template) {
52
+ function createAllResponder(nativePayload, url, template, key = 'assets') {
52
53
  return function(type, listener) {
53
54
  if (type !== 'message') { return; }
54
55
 
55
- const data = { message: 'assetResponse', adId: AD_ID, assets, adTemplate:template, rendererUrl:url };
56
- listener({ data: JSON.stringify(data) });
56
+ const data = { message: 'assetResponse', adId: AD_ID, adTemplate:template, rendererUrl:url };
57
+ data[key] = nativePayload;
58
+ listener({ data: JSON.stringify(data), origin: ORIGIN });
57
59
  };
58
60
  }
59
61
 
@@ -63,14 +65,14 @@ function createAltAllResponder(assets,url,template) {
63
65
  if (type !== 'message') { return; }
64
66
 
65
67
  const data = { message: 'assetResponse', adId: AD_ID2, assets, adTemplate:template, rendererUrl:url };
66
- listener({ data: JSON.stringify(data) });
68
+ listener({ data: JSON.stringify(data), origin: ORIGIN});
67
69
  };
68
70
  }
69
71
 
70
72
  // creates mock html markup responsse from renderUrl
71
73
  function generateRenderer(assets) {
72
74
  let newhtml = '<div class=\"sponsored-post\">\r\n <div class=\"thumbnail\"><\/div>\r\n <div class=\"content\">\r\n <h1>\r\n <a href=\"##hb_native_linkurl##\" target=\"_blank\" class=\"pb-click\">##hb_native_title##<\/a>\r\n <\/h1>\r\n <p>##hb_native_body##<\/p>\r\n \t<div class=\"attribution\">\r\n \t<img class=\"pb-icon\" src=\"##hb_native_image##\" alt=\"icon\" height=\"150\" width=\"50\">\r\n \t\r\n \t<\/div>\r\n\t<\/div>\r\n<\/div>';
73
-
75
+
74
76
  (assets || []).forEach(asset => {
75
77
  const searchString = '##'+`${NATIVE_KEYS[asset.key]}`+'##';
76
78
  const searchStringRegex = new RegExp(searchString, 'g');
@@ -80,355 +82,499 @@ function generateRenderer(assets) {
80
82
  return newhtml;
81
83
  }
82
84
 
83
- describe('nativeTrackerManager', () => {
85
+ describe('nativeAssetManager', () => {
84
86
  let win;
85
87
 
88
+ function makeManager() {
89
+ return newNativeAssetManager(win, ORIGIN);
90
+ }
91
+
86
92
  beforeEach(() => {
87
93
  win = merge(mocks.createFakeWindow(), mockDocument.getWindowObject());
88
94
  });
89
95
 
90
- it('replaces native placeholders with their asset values', () => {
91
- win.document.body.innerHTML = `
92
- <h1>hb_native_title</h1>
93
- <p>hb_native_body:${AD_ID}</p>
94
- <a href="hb_native_linkurl:${AD_ID}">Click Here</a>
95
- `;
96
- win.addEventListener = createResponder([
97
- { key: 'body', value: 'new value' },
98
- { key: 'clickUrl', value: 'http://www.example.com' },
99
- ]);
100
-
101
- const nativeAssetManager = newNativeAssetManager(win);
102
- nativeAssetManager.loadAssets(AD_ID);
103
-
104
- expect(win.document.body.innerHTML).to.include('<p>new value</p>');
105
- expect(win.document.body.innerHTML).to.include(`
106
- <a href="http://www.example.com">Click Here</a>
107
- `);
108
- // title was not a requested asset so this should stay as is
109
- expect(win.document.body.innerHTML).to.include('<h1>hb_native_title</h1>');
110
- });
111
-
112
- it('replaces all occurrences of the placeholder if it appears more than once', () => {
113
- win.document.body.innerHTML = `
114
- <a href="hb_native_linkurl:${AD_ID}">Click Here</a>
115
- <a href="hb_native_linkurl:${AD_ID}">Or Here</a>
116
- `;
117
- win.addEventListener = createResponder([{ key: 'clickUrl', value: 'http://www.example.com' }]);
118
-
119
- const nativeAssetManager = newNativeAssetManager(win);
120
- nativeAssetManager.loadAssets(AD_ID);
121
-
122
- expect(win.document.body.innerHTML).to.include(`
123
- <a href="http://www.example.com">Click Here</a>
124
- `);
125
- expect(win.document.body.innerHTML).to.include(`
126
- <a href="http://www.example.com">Or Here</a>
127
- `);
128
- });
129
-
130
- it('attaches and removes message listeners', () => {
131
- win.document.body.innerHTML = `<h1>hb_native_title:${AD_ID}</h1>`;
132
- win.addEventListener = createResponder();
133
-
134
- const nativeAssetManager = newNativeAssetManager(win);
135
- nativeAssetManager.loadAssets(AD_ID);
136
-
137
- expect(win.parent.postMessage.callCount).to.equal(1);
138
- expect(win.removeEventListener.callCount).to.equal(1);
139
- });
140
-
141
- it('replaces native placeholders with their asset values from adTemplate', () => {
142
- const html = `<script>
143
- let nativeTag = {};
144
- nativeTag.adTemplate = "<div class=\"sponsored-post\">\r\n <div class=\"thumbnail\"><\/div>\r\n <div class=\"content\">\r\n <h1>\r\n <a href=\"##hb_native_linkurl##\" target=\"_blank\" class=\"pb-click\">##hb_native_title##<\/a>\r\n <\/h1>\r\n <p>##hb_native_body##<\/p>\r\n \t<div class=\"attribution\">\r\n \t<img class=\"pb-icon\" src=\"##hb_native_image##\" alt=\"icon\" height=\"150\" width=\"50\">\r\n \t\r\n \t<\/div>\r\n\t<\/div>\r\n<\/div>";
145
- nativeTag.pubUrl = "https://www.url.com";
146
- nativeTag.adId = "`+AD_ID+`";
147
- nativeTag.requestAllAssets = true;
148
- window.pbNativeTag.renderNativeAd(nativeTag);
149
- </script>`;
150
- win.pbNativeData = {
151
- pubUrl : 'https://www.url.com',
152
- adId : AD_ID,
153
- adTemplate : '<div class=\"sponsored-post\">\r\n <div class=\"thumbnail\"><\/div>\r\n <div class=\"content\">\r\n <h1>\r\n <a href=\"##hb_native_linkurl##\" target=\"_blank\" class=\"pb-click\">##hb_native_title##<\/a>\r\n <\/h1>\r\n <p>##hb_native_body##<\/p>\r\n \t<div class=\"attribution\">\r\n \t<img class=\"pb-icon\" src=\"##hb_native_image##\" alt=\"icon\" height=\"150\" width=\"50\">\r\n \t\r\n \t<\/div>\r\n\t<\/div>\r\n<\/div>'
154
- };
155
-
156
- win.document.body.innerHTML = html;
157
- win.addEventListener = createResponder([
158
- { key: 'body', value: 'Body content' },
159
- { key: 'title', value: 'new value' },
160
- { key: 'clickUrl', value: 'http://www.example.com' },
161
- { key: 'image', value: 'http://www.image.com/picture.jpg' },
162
- ]);
163
-
164
- const nativeAssetManager = newNativeAssetManager(win);
165
- nativeAssetManager.loadAssets(AD_ID);
166
-
167
- expect(win.document.body.innerHTML).to.include(`<a href="http://www.example.com" target="_blank" class="pb-click">new value</a>`);
168
- expect(win.document.body.innerHTML).to.include(`<img class="pb-icon" src="http://www.image.com/picture.jpg" alt="icon" height="150" width="50">`);
169
- expect(win.document.body.innerHTML).to.include(`<p>Body content</p>`);
170
- });
171
-
172
- it('loads rendererUrl and passes assets to renderAd - writes response to innerHtml', () => {
173
- const html = `<script>
174
- let nativeTag = {};
175
- nativeTag.pubUrl = "https://www.url.com";
176
- nativeTag.adId = "`+AD_ID+`";
177
- nativeTag.requestAllAssets = true;
178
- window.pbNativeTag.renderNativeAd(nativeTag);
179
- </script>`;
180
- win.pbNativeData = {
181
- pubUrl : 'https://www.url.com',
182
- adId : AD_ID,
183
- rendererUrl : 'https://www.renderer.com/render.js',
184
- requestAllAssets : true
185
- };
186
-
187
- win.document.body.innerHTML = html;
188
- win.renderAd = generateRenderer;
96
+ describe('safe frames enabled', () => {
189
97
 
190
- win.addEventListener = createAllResponder([
191
- { key: 'body', value: 'Body content' },
192
- { key: 'title', value: 'new value' },
193
- { key: 'clickUrl', value: 'http://www.example.com' },
194
- { key: 'image', value: 'http://www.image.com/picture.jpg' },
195
- ],null,null);
196
-
197
- const nativeAssetManager = newNativeAssetManager(win);
198
- nativeAssetManager.loadAssets(AD_ID);
199
-
200
- expect(win.document.body.innerHTML).to.include(`<a href="http://www.example.com" target="_blank" class="pb-click">new value</a>`);
201
- expect(win.document.body.innerHTML).to.include(`<img class="pb-icon" src="http://www.image.com/picture.jpg" alt="icon" height="150" width="50">`);
202
- expect(win.document.body.innerHTML).to.include(`<p>Body content</p>`);
203
- });
204
-
205
- it('adId does not match, so assets are not replaced', () => {
206
- const html = `<script>
207
- let nativeTag = {};
208
- nativeTag.pubUrl = "https://www.url.com";
209
- nativeTag.adId = "OTHERID123";
210
- nativeTag.requestAllAssets = true;
211
- window.pbNativeTag.renderNativeAd(nativeTag);
212
- </script>`;
213
- win.pbNativeData = {
214
- pubUrl : 'https://www.url.com',
215
- adId : 'OTHERID123',
216
- rendererUrl : 'https://www.renderer.com/render.js',
217
- requestAllAssets : true
218
- };
219
-
220
- win.document.body.innerHTML = html;
221
- win.renderAd = generateRenderer;
222
-
223
- win.addEventListener = createAllResponder([
224
- { key: 'body', value: 'Body content' },
225
- { key: 'title', value: 'new value' },
226
- { key: 'clickUrl', value: 'http://www.example.com' },
227
- { key: 'image', value: 'http://www.image.com/picture.jpg' },
228
- ],null,null);
229
-
230
- const nativeAssetManager = newNativeAssetManager(win);
231
- nativeAssetManager.loadAssets(AD_ID);
232
-
233
- expect(win.document.body.innerHTML).to.equal(`<script>
234
- let nativeTag = {};
235
- nativeTag.pubUrl = "https://www.url.com";
236
- nativeTag.adId = "OTHERID123";
237
- nativeTag.requestAllAssets = true;
238
- window.pbNativeTag.renderNativeAd(nativeTag);
239
- </script>`);
240
- });
241
-
242
- it('adId does not match on first response, so assets are not replaced until match on second response', () => {
243
- const html = `<script>
244
- let nativeTag = {};
245
- nativeTag.pubUrl = "https://www.url.com";
246
- nativeTag.adId = "def456";
247
- nativeTag.requestAllAssets = true;
248
- window.pbNativeTag.renderNativeAd(nativeTag);
249
- </script>`;
250
- win.pbNativeData = {
251
- pubUrl : 'https://www.url.com',
252
- adId : 'def456',
253
- rendererUrl : 'https://www.renderer.com/render.js',
254
- requestAllAssets : true
255
- };
256
-
257
- win.document.body.innerHTML = html;
258
- win.renderAd = generateRenderer;
259
-
260
- win.addEventListener = createAllResponder([
261
- { key: 'body', value: 'Body No Replace' },
262
- { key: 'title', value: 'new value no replace' },
263
- { key: 'clickUrl', value: 'http://www.example.com/noreplace' },
264
- { key: 'image', value: 'http://www.image.com/picture.jpg?noreplace=true' },
265
- ],null,null);
266
-
267
- const nativeAssetManager = newNativeAssetManager(win);
268
- nativeAssetManager.loadAssets(AD_ID2);
269
-
270
- expect(win.document.body.innerHTML).to.equal(`<script>
271
- let nativeTag = {};
272
- nativeTag.pubUrl = "https://www.url.com";
273
- nativeTag.adId = "def456";
274
- nativeTag.requestAllAssets = true;
275
- window.pbNativeTag.renderNativeAd(nativeTag);
276
- </script>`);
277
-
278
- win.addEventListener = createAltAllResponder([
279
- { key: 'body', value: 'Body content' },
280
- { key: 'title', value: 'new value' },
281
- { key: 'clickUrl', value: 'http://www.example.com' },
282
- { key: 'image', value: 'http://www.image.com/picture.jpg' },
283
- ],null,null);
284
-
285
- nativeAssetManager.loadAssets(AD_ID2);
286
-
287
- expect(win.document.body.innerHTML).to.include(`<a href="http://www.example.com" target="_blank" class="pb-click">new value</a>`);
288
- expect(win.document.body.innerHTML).to.include(`<img class="pb-icon" src="http://www.image.com/picture.jpg" alt="icon" height="150" width="50">`);
289
- expect(win.document.body.innerHTML).to.include(`<p>Body content</p>`);
290
- });
291
-
292
- it('no placeholders found but requests all assets flag set - rendererUrl', () => {
293
- const html = `<script>
294
- let nativeTag = {};
295
- nativeTag.pubUrl = "https://www.url.com";
296
- nativeTag.adId = "`+AD_ID+`";
297
- nativeTag.requestAllAssets = true;
298
- window.pbNativeTag.renderNativeAd(nativeTag);
299
- </script>`,
300
- url = 'https://www.renderer.com/render.js';
301
- win.pbNativeData = {
302
- pubUrl : 'https://www.url.com',
303
- adId : AD_ID,
304
- rendererUrl : 'https://www.renderer.com/render.js',
305
- requestAllAssets : true
306
- };
307
-
308
- win.document.body.innerHTML = html;
309
- win.renderAd = generateRenderer;
310
-
311
- win.addEventListener = createAllResponder([
312
- { key: 'body', value: 'Body content' },
313
- { key: 'title', value: 'new value' },
314
- { key: 'clickUrl', value: 'http://www.example.com' },
315
- { key: 'image', value: 'http://www.image.com/picture.jpg' },
316
- ],url,null);
317
-
318
- const nativeAssetManager = newNativeAssetManager(win);
319
- nativeAssetManager.loadAssets(AD_ID);
320
-
321
- expect(win.document.body.innerHTML).to.include(`<a href="http://www.example.com" target="_blank" class="pb-click">new value</a>`);
322
- expect(win.document.body.innerHTML).to.include(`<img class="pb-icon" src="http://www.image.com/picture.jpg" alt="icon" height="150" width="50">`);
323
- expect(win.document.body.innerHTML).to.include(`<p>Body content</p>`);
324
- });
325
-
326
- it('no placeholders found but requests all assets flag set - adTemplate', () => {
327
- const html = `<script>
328
- let nativeTag = {};
329
- nativeTag.pubUrl = "https://www.url.com";
330
- nativeTag.adId = "`+AD_ID+`";
331
- nativeTag.requestAllAssets = true;
332
- window.pbNativeTag.renderNativeAd(nativeTag);
333
- </script>`,
334
- template = '<div class=\"sponsored-post\">\r\n <div class=\"thumbnail\"><\/div>\r\n <div class=\"content\">\r\n <h1>\r\n <a href=\"##hb_native_linkurl##\" target=\"_blank\" class=\"pb-click\">##hb_native_title##<\/a>\r\n <\/h1>\r\n <p>##hb_native_body##<\/p>\r\n \t<div class=\"attribution\">\r\n \t<img class=\"pb-icon\" src=\"##hb_native_image##\" alt=\"icon\" height=\"150\" width=\"50\">\r\n \t\r\n \t<\/div>\r\n\t<\/div>\r\n<\/div>';
335
- win.pbNativeData = {
336
- pubUrl : 'https://www.url.com',
337
- adId : AD_ID,
338
- requestAllAssets : true
339
- };
340
-
341
- win.document.body.innerHTML = html;
342
-
343
- win.addEventListener = createAllResponder([
344
- { key: 'body', value: 'Body content' },
345
- { key: 'title', value: 'new value' },
346
- { key: 'clickUrl', value: 'http://www.example.com' },
347
- { key: 'image', value: 'http://www.image.com/picture.jpg' },
348
- ],null,template);
349
-
350
- const nativeAssetManager = newNativeAssetManager(win);
351
- nativeAssetManager.loadAssets(AD_ID);
352
-
353
- expect(win.document.body.innerHTML).to.include(`<a href="http://www.example.com" target="_blank" class="pb-click">new value</a>`);
354
- expect(win.document.body.innerHTML).to.include(`<img class="pb-icon" src="http://www.image.com/picture.jpg" alt="icon" height="150" width="50">`);
355
- expect(win.document.body.innerHTML).to.include(`<p>Body content</p>`);
356
- });
357
-
358
- it('no placeholders found but assets defined in nativeTag - adTemplate', () => {
359
- const html = `<script>
360
- let nativeTag = {};
361
- nativeTag.pubUrl = "https://www.url.com";
362
- nativeTag.adId = "`+AD_ID+`";
363
- nativeTag.requestAllAssets = true;
364
- window.pbNativeTag.renderNativeAd(nativeTag);
365
- </script>`,
366
- template = '<div class=\"sponsored-post\">\r\n <div class=\"thumbnail\"><\/div>\r\n <div class=\"content\">\r\n <h1>\r\n <a href=\"##hb_native_linkurl##\" target=\"_blank\" class=\"pb-click\">##hb_native_title##<\/a>\r\n <\/h1>\r\n <p>##hb_native_body##<\/p>\r\n \t<div class=\"attribution\">\r\n \t<img class=\"pb-icon\" src=\"##hb_native_image##\" alt=\"icon\" height=\"150\" width=\"50\">\r\n \t\r\n \t<\/div>\r\n\t<\/div>\r\n<\/div>';
367
- win.pbNativeData = {
368
- pubUrl : 'https://www.url.com',
369
- adId : AD_ID,
370
- assetsToReplace: ['image','hb_native_body','clickUrl','hb_native_title']
371
- };
372
-
373
- win.document.body.innerHTML = html;
374
-
375
- win.addEventListener = createAllResponder([
376
- { key: 'body', value: 'Body content' },
377
- { key: 'title', value: 'new value' },
378
- { key: 'clickUrl', value: 'http://www.example.com' },
379
- { key: 'image', value: 'http://www.image.com/picture.jpg' },
380
- ],null,template);
381
-
382
- const nativeAssetManager = newNativeAssetManager(win);
383
- nativeAssetManager.loadAssets(AD_ID);
384
-
385
- expect(win.document.body.innerHTML).to.include(`<a href="http://www.example.com" target="_blank" class="pb-click">new value</a>`);
386
- expect(win.document.body.innerHTML).to.include(`<img class="pb-icon" src="http://www.image.com/picture.jpg" alt="icon" height="150" width="50">`);
387
- expect(win.document.body.innerHTML).to.include(`<p>Body content</p>`);
388
- });
389
-
390
- it('does not replace anything if no placeholders found', () => {
391
- const html = `
392
- <h1>Native Ad</h1>
393
- <p>Cool Description</p>
394
- <a href="http://www.example.com">Click</a>
395
- `;
396
-
397
- win.document.body.innerHTML = html;
398
- win.addEventListener = createResponder();
399
-
400
- const nativeAssetManager = newNativeAssetManager(win);
401
- nativeAssetManager.loadAssets(AD_ID);
402
-
403
- expect(win.document.body.innerHTML).to.equal(html);
404
- });
405
-
406
- it('replace mobile native placeholder with their values', function() {
407
- win.document.body.innerHTML = `
408
- <h1>hb_native_cta</h1>
409
- <p>hb_native_body</p>
410
- <a href="hb_native_linkurl">Click Here</a>
411
- `;
412
-
413
- let cb = sinon.spy();
414
- let targetingData = {
415
- uuid: '123'
416
- }
417
-
418
- sinon.stub(utils, 'sendRequest').callsFake(function(arg1, cb) {
419
- let response = JSON.stringify({"id":"6572251357847878203","impid":"some-imp-id","price":10,"adm":"{\"assets\":[{\"id\":1,\"img\":{\"type\":3,\"url\":\"http://vcdn.adnxs.com/p/creative-image/f8/7f/0f/13/f87f0f13-230c-4f05-8087-db9216e393de.jpg\",\"w\":989,\"h\":742,\"ext\":{\"appnexus\":{\"prevent_crop\":0}}}},{\"title\":{\"text\":\"This is a Prebid Native Creative\"}},{\"id\":2,\"data\":{\"type\":1,\"value\":\"Prebid.org\"}},{\"id\":3,\"data\":{\"type\":2,\"value\":\"new value\"}}],\"link\":{\"url\":\"http://example.com\"},\"imptrackers\":[\"http://some-tracker.com\"],\"jstracker\":\"\\u003cscript type=\\\"text/javascript\\\" async=\\\"true\\\" src=\\\"http://cdn.adnxs.com/v/app/179/trk.js#app;vk=appnexus.com-omid;tv=app-native-23h;dom_id=%native_dom_id%;st=2;d=1x1;vc=iab;vid_ccr=1;tag_id=13232354;cb=http%3A%2F%2Fnym1-ib.adnxs.com%2Fvevent%3Fan_audit%3D0%26test%3D1%26e%3DwqT_3QLXB2zXAwAAAwDWAAUBCJqev-0FEN2mtMzg8dSSPxj_EQEQASo2CQAFAQgkQBEFCAwAJEAZEQkAIREJACkRCQAxEQmoMOLRpwY47UhA7UhIAlC8yb4uWJzxW2AAaM26dXjjrAWAAQGKAQNVU0SSAQEG8FKYAQGgAQGoAQGwAQC4AQLAAQPIAQLQAQDYAQDgAQHwAQCKAjt1ZignYScsIDI1Mjk4ODUsIDE1NzE4MDI5MDYpO3VmKCdyJywgOTc0OTQyMDQsIC4eAPQOAZICuQIhTEVEX1hnajgtTHdLRUx6SnZpNFlBQ0NjOFZzd0FEZ0FRQVJJN1VoUTR0R25CbGdBWVBfX19fOFBhQUJ3QVhnQmdBRUJpQUVCa0FFQm1BRUJvQUVCcUFFRHNBRUF1UUh6cldxa0FBQWtRTUVCODYxcXBBQUFKRURKQWJSM21tYW5MdWNfMlFFQUFBQUFBQUR3UC1BQkFQVUJBQUFBQUpnQ0FLQUNBTFVDQUFBQUFMMENBQUFBQU1BQ0FjZ0NBZEFDQWRnQ0FlQUNBT2dDQVBnQ0FJQURBWmdEQWFnRF9QaThDcm9EQ1U1WlRUSTZORFl6Tk9BRHBSU0lCQUNRQkFDWUJBSEJCQUFBQUEJgwh5UVEJCQEBGE5nRUFQRUUBCwkBUEQ0QkFDSUJab2uaAokBIVp3LWhMQTY9ASRuUEZiSUFRb0FEFThUa1FEb0pUbGxOTWpvME5qTTBRS1VVUxFoDFBBX1URDAxBQUFXHQwAWR0MAGEdDABjHQzweWVBQS7YAgDgAq2YSIADAYgDAJADAJgDFKADAaoDAMAD4KgByAMA2AMA4AMA6AMC-AMAgAQAkgQJL29wZW5ydGIymAQAqAQAsgQMCAAQABgAIAAwADgAuAQAwAQAyAQA0gQOOTMyNSNOWU0yOjQ2MzTaBAIIAeAEAPAEQcCQggUab3JnLnByZWJpZC5tb2JpbGUuYXBpMWRlbW-IBQGYBQCgBXE4QP8BqgUHc29tZS1pZMAFAMkFaRgU8D_SBQkJCQw8AADYBQHgBQHwBZn0IfoFBAGUKJAGAZgGALgGAMEGCSU48D_IBgDQBvUv2gYWChAAOgEAUBAAGADgBgzyBgIIAIAHAYgHAKAHQQ..%26s%3Da7b19f6eede870d487a7bec88354794855bf8161;ts=1571802906;cet=0;cecb=\\\"\\u003e\\u003c/script\\u003e\"}","adid":"97494204","adomain":["http://prebid.org"],"iurl":"http://nym1-ib.adnxs.com/cr?id=97494204","cid":"9325","crid":"97494204","cat":["IAB3-1"],"ext":{"appnexus":{"brand_id":555545,"auction_id":4550134868038456157,"bidder_id":2,"bid_ad_type":3}}});
420
- cb(response);
98
+ beforeEach(() => {
99
+ win.$sf = {
100
+ ext: {}
101
+ }
421
102
  });
422
103
 
423
- const nativeAssetManager = newNativeAssetManager(win);
424
- nativeAssetManager.loadMobileAssets(targetingData, cb);
425
-
426
- expect(win.document.body.innerHTML).to.include('<p>new value</p>');
427
- expect(win.document.body.innerHTML).to.include(`
428
- <a href="http://example.com">Click Here</a>
429
- `);
430
- // cta was not a requested asset so this should stay as is
431
- expect(win.document.body.innerHTML).to.include('<h1>hb_native_cta</h1>');
432
- utils.sendRequest.restore();
104
+ it('replaces native placeholders with their asset values', () => {
105
+ win.document.body.innerHTML = `
106
+ <h1>hb_native_title</h1>
107
+ <p>hb_native_body:${AD_ID}</p>
108
+ <a href="hb_native_linkurl:${AD_ID}">Click Here</a>
109
+ `;
110
+ win.addEventListener = createResponder([
111
+ { key: 'body', value: 'new value' },
112
+ { key: 'clickUrl', value: 'http://www.example.com' },
113
+ ]);
114
+
115
+ const nativeAssetManager = makeManager();
116
+ nativeAssetManager.loadAssets(AD_ID);
117
+
118
+ expect(win.document.body.innerHTML).to.include('<p>new value</p>');
119
+ expect(win.document.body.innerHTML).to.include(`
120
+ <a href="http://www.example.com">Click Here</a>
121
+ `);
122
+ // title was not a requested asset so this should stay as is
123
+ expect(win.document.body.innerHTML).to.include('<h1>hb_native_title</h1>');
124
+ });
125
+
126
+ it('replaces all occurrences of the placeholder if it appears more than once', () => {
127
+ win.document.body.innerHTML = `
128
+ <a href="hb_native_linkurl:${AD_ID}">Click Here</a>
129
+ <a href="hb_native_linkurl:${AD_ID}">Or Here</a>
130
+ `;
131
+ win.addEventListener = createResponder([{ key: 'clickUrl', value: 'http://www.example.com' }]);
132
+
133
+ const nativeAssetManager = makeManager();
134
+ nativeAssetManager.loadAssets(AD_ID);
135
+
136
+ expect(win.document.body.innerHTML).to.include(`
137
+ <a href="http://www.example.com">Click Here</a>
138
+ `);
139
+ expect(win.document.body.innerHTML).to.include(`
140
+ <a href="http://www.example.com">Or Here</a>
141
+ `);
142
+ });
143
+
144
+ it('attaches and removes message listeners', (done) => {
145
+ win.document.body.innerHTML = `<h1>hb_native_title:${AD_ID}</h1>`;
146
+ const responder = createResponder();
147
+ win.addEventListener = function (evType, listener) {
148
+ setTimeout(() => responder(evType, listener), 0);
149
+ }
150
+
151
+ const nativeAssetManager = makeManager();
152
+ nativeAssetManager.loadAssets(AD_ID);
153
+ setTimeout(() => {
154
+ expect(win.parent.postMessage.callCount).to.equal(1);
155
+ expect(win.removeEventListener.callCount).to.equal(1);
156
+ done();
157
+ }, 0);
158
+ });
159
+
160
+ it('replaces native placeholders with their asset values from adTemplate', () => {
161
+ const html = `<script>
162
+ let nativeTag = {};
163
+ nativeTag.adTemplate = "<div class=\"sponsored-post\">\r\n <div class=\"thumbnail\"><\/div>\r\n <div class=\"content\">\r\n <h1>\r\n <a href=\"##hb_native_linkurl##\" target=\"_blank\" class=\"pb-click\">##hb_native_title##<\/a>\r\n <\/h1>\r\n <p>##hb_native_body##<\/p>\r\n \t<div class=\"attribution\">\r\n \t<img class=\"pb-icon\" src=\"##hb_native_image##\" alt=\"icon\" height=\"150\" width=\"50\">\r\n \t\r\n \t<\/div>\r\n\t<\/div>\r\n<\/div>";
164
+ nativeTag.pubUrl = "https://www.url.com";
165
+ nativeTag.adId = "`+AD_ID+`";
166
+ nativeTag.requestAllAssets = true;
167
+ window.pbNativeTag.renderNativeAd(nativeTag);
168
+ </script>`;
169
+ win.pbNativeData = {
170
+ pubUrl : 'https://www.url.com',
171
+ adId : AD_ID,
172
+ adTemplate : '<div class=\"sponsored-post\">\r\n <div class=\"thumbnail\"><\/div>\r\n <div class=\"content\">\r\n <h1>\r\n <a href=\"##hb_native_linkurl##\" target=\"_blank\" class=\"pb-click\">##hb_native_title##<\/a>\r\n <\/h1>\r\n <p>##hb_native_body##<\/p>\r\n \t<div class=\"attribution\">\r\n \t<img class=\"pb-icon\" src=\"##hb_native_image##\" alt=\"icon\" height=\"150\" width=\"50\">\r\n \t\r\n \t<\/div>\r\n\t<\/div>\r\n<\/div>'
173
+ };
174
+
175
+ win.document.body.innerHTML = html;
176
+ win.addEventListener = createResponder([
177
+ { key: 'body', value: 'Body content' },
178
+ { key: 'title', value: 'new value' },
179
+ { key: 'clickUrl', value: 'http://www.example.com' },
180
+ { key: 'image', value: 'http://www.image.com/picture.jpg' },
181
+ ]);
182
+
183
+ const nativeAssetManager = makeManager();
184
+ nativeAssetManager.loadAssets(AD_ID);
185
+
186
+ expect(win.document.body.innerHTML).to.include(`<a href="http://www.example.com" target="_blank" class="pb-click">new value</a>`);
187
+ expect(win.document.body.innerHTML).to.include(`<img class="pb-icon" src="http://www.image.com/picture.jpg" alt="icon" height="150" width="50">`);
188
+ expect(win.document.body.innerHTML).to.include(`<p>Body content</p>`);
189
+ });
190
+
191
+ it('loads rendererUrl and passes assets to renderAd - writes response to innerHtml', () => {
192
+ const html = `<script>
193
+ let nativeTag = {};
194
+ nativeTag.pubUrl = "https://www.url.com";
195
+ nativeTag.adId = "`+AD_ID+`";
196
+ nativeTag.requestAllAssets = true;
197
+ window.pbNativeTag.renderNativeAd(nativeTag);
198
+ </script>`;
199
+ win.pbNativeData = {
200
+ pubUrl : 'https://www.url.com',
201
+ adId : AD_ID,
202
+ rendererUrl : 'https://www.renderer.com/render.js',
203
+ requestAllAssets : true
204
+ };
205
+
206
+ win.document.body.innerHTML = html;
207
+ win.renderAd = generateRenderer;
208
+
209
+ win.addEventListener = createAllResponder([
210
+ { key: 'body', value: 'Body content' },
211
+ { key: 'title', value: 'new value' },
212
+ { key: 'clickUrl', value: 'http://www.example.com' },
213
+ { key: 'image', value: 'http://www.image.com/picture.jpg' },
214
+ ],null,null);
215
+
216
+ const nativeAssetManager = makeManager();
217
+ nativeAssetManager.loadAssets(AD_ID);
218
+
219
+ expect(win.document.body.innerHTML).to.include(`<a href="http://www.example.com" target="_blank" class="pb-click">new value</a>`);
220
+ expect(win.document.body.innerHTML).to.include(`<img class="pb-icon" src="http://www.image.com/picture.jpg" alt="icon" height="150" width="50">`);
221
+ expect(win.document.body.innerHTML).to.include(`<p>Body content</p>`);
222
+ });
223
+
224
+ it('adId does not match, so assets are not replaced', () => {
225
+ const html = `<script>
226
+ let nativeTag = {};
227
+ nativeTag.pubUrl = "https://www.url.com";
228
+ nativeTag.adId = "OTHERID123";
229
+ nativeTag.requestAllAssets = true;
230
+ window.pbNativeTag.renderNativeAd(nativeTag);
231
+ </script>`;
232
+ win.pbNativeData = {
233
+ pubUrl : 'https://www.url.com',
234
+ adId : 'OTHERID123',
235
+ rendererUrl : 'https://www.renderer.com/render.js',
236
+ requestAllAssets : true
237
+ };
238
+
239
+ win.document.body.innerHTML = html;
240
+ win.renderAd = generateRenderer;
241
+
242
+ win.addEventListener = createAllResponder([
243
+ { key: 'body', value: 'Body content' },
244
+ { key: 'title', value: 'new value' },
245
+ { key: 'clickUrl', value: 'http://www.example.com' },
246
+ { key: 'image', value: 'http://www.image.com/picture.jpg' },
247
+ ],null,null);
248
+
249
+ const nativeAssetManager = makeManager();
250
+ nativeAssetManager.loadAssets(AD_ID);
251
+
252
+ expect(win.document.body.innerHTML).to.equal(`<script>
253
+ let nativeTag = {};
254
+ nativeTag.pubUrl = "https://www.url.com";
255
+ nativeTag.adId = "OTHERID123";
256
+ nativeTag.requestAllAssets = true;
257
+ window.pbNativeTag.renderNativeAd(nativeTag);
258
+ </script>`);
259
+ });
260
+
261
+ it('adId does not match on first response, so assets are not replaced until match on second response', () => {
262
+ const html = `<script>
263
+ let nativeTag = {};
264
+ nativeTag.pubUrl = "https://www.url.com";
265
+ nativeTag.adId = "def456";
266
+ nativeTag.requestAllAssets = true;
267
+ window.pbNativeTag.renderNativeAd(nativeTag);
268
+ </script>`;
269
+ win.pbNativeData = {
270
+ pubUrl : 'https://www.url.com',
271
+ adId : 'def456',
272
+ rendererUrl : 'https://www.renderer.com/render.js',
273
+ requestAllAssets : true
274
+ };
275
+
276
+ win.document.body.innerHTML = html;
277
+ win.renderAd = generateRenderer;
278
+
279
+ win.addEventListener = createAllResponder([
280
+ { key: 'body', value: 'Body No Replace' },
281
+ { key: 'title', value: 'new value no replace' },
282
+ { key: 'clickUrl', value: 'http://www.example.com/noreplace' },
283
+ { key: 'image', value: 'http://www.image.com/picture.jpg?noreplace=true' },
284
+ ],null,null);
285
+
286
+ const nativeAssetManager = makeManager();
287
+ nativeAssetManager.loadAssets(AD_ID2);
288
+
289
+ expect(win.document.body.innerHTML).to.equal(`<script>
290
+ let nativeTag = {};
291
+ nativeTag.pubUrl = "https://www.url.com";
292
+ nativeTag.adId = "def456";
293
+ nativeTag.requestAllAssets = true;
294
+ window.pbNativeTag.renderNativeAd(nativeTag);
295
+ </script>`);
296
+
297
+ win.addEventListener = createAltAllResponder([
298
+ { key: 'body', value: 'Body content' },
299
+ { key: 'title', value: 'new value' },
300
+ { key: 'clickUrl', value: 'http://www.example.com' },
301
+ { key: 'image', value: 'http://www.image.com/picture.jpg' },
302
+ ],null,null);
303
+
304
+ nativeAssetManager.loadAssets(AD_ID2);
305
+
306
+ expect(win.document.body.innerHTML).to.include(`<a href="http://www.example.com" target="_blank" class="pb-click">new value</a>`);
307
+ expect(win.document.body.innerHTML).to.include(`<img class="pb-icon" src="http://www.image.com/picture.jpg" alt="icon" height="150" width="50">`);
308
+ expect(win.document.body.innerHTML).to.include(`<p>Body content</p>`);
309
+ });
310
+
311
+ it('no placeholders found but requests all assets flag set - rendererUrl', () => {
312
+ const url = 'https://www.renderer.com/render.js';
313
+ win.pbNativeData = {
314
+ pubUrl : 'https://www.url.com',
315
+ adId : AD_ID,
316
+ rendererUrl : 'https://www.renderer.com/render.js',
317
+ requestAllAssets : true
318
+ };
319
+
320
+ win.document.body.innerHTML = '';
321
+ win.renderAd = generateRenderer;
322
+
323
+ win.addEventListener = createAllResponder([
324
+ { key: 'body', value: 'Body content' },
325
+ { key: 'title', value: 'new value' },
326
+ { key: 'clickUrl', value: 'http://www.example.com' },
327
+ { key: 'image', value: 'http://www.image.com/picture.jpg' },
328
+ ],url,null);
329
+
330
+ const nativeAssetManager = makeManager();
331
+ nativeAssetManager.loadAssets(AD_ID);
332
+
333
+ expect(win.document.body.innerHTML).to.include(`<a href="http://www.example.com" target="_blank" class="pb-click">new value</a>`);
334
+ expect(win.document.body.innerHTML).to.include(`<img class="pb-icon" src="http://www.image.com/picture.jpg" alt="icon" height="150" width="50">`);
335
+ expect(win.document.body.innerHTML).to.include(`<p>Body content</p>`);
336
+ });
337
+
338
+ it("no placeholders found but requests all assets flag set - adTemplate - openRTB", () => {
339
+ const template = `
340
+ <div class="sponsored-post">
341
+ <div class="thumbnail">
342
+ </div>
343
+ <div class="content">
344
+ <h1>
345
+ <a href="##hb_native_linkurl##" target="_blank" class="pb-click">##hb_native_asset_id_1##</a>
346
+ </h1>
347
+ <p>##hb_native_asset_id_2##</p>
348
+ <div class="attribution">
349
+ <img class="pb-icon" src="##hb_native_asset_id_3##" alt="icon" height="150" width="50">
350
+ </div>
351
+ <p>##hb_native_asset_id_4##</p>
352
+ </div>
353
+ </div>
354
+ `;
355
+ win.pbNativeData = {
356
+ pubUrl: "https://www.url.com",
357
+ adId: AD_ID,
358
+ requestAllAssets: true,
359
+ };
360
+
361
+ win.document.body.innerHTML = "";
362
+
363
+ win.addEventListener = createAllResponder(
364
+ {
365
+ assets: [
366
+ {
367
+ id: 1,
368
+ title: {
369
+ text: "new value",
370
+ },
371
+ },
372
+ {
373
+ id: 2,
374
+ data: {
375
+ value: "Body content",
376
+ },
377
+ },
378
+ {
379
+ id: 3,
380
+ img: {
381
+ url: "http://www.image.com/picture.jpg",
382
+ },
383
+ },
384
+ ],
385
+ link: {
386
+ url: "http://www.example.com",
387
+ },
388
+
389
+ },
390
+ null,
391
+ template,
392
+ "ortb"
393
+ );
394
+
395
+ const nativeAssetManager = makeManager();
396
+ nativeAssetManager.loadAssets(AD_ID);
397
+
398
+ expect(win.document.body.innerHTML).to.include(
399
+ `<a href="http://www.example.com" target="_blank" class="pb-click">new value</a>`
400
+ );
401
+ expect(win.document.body.innerHTML).to.include(
402
+ `<img class="pb-icon" src="http://www.image.com/picture.jpg" alt="icon" height="150" width="50">`
403
+ );
404
+ expect(win.document.body.innerHTML).to.include(`<p>Body content</p>`);
405
+
406
+ // ##hb_native_asset_id_4## was not returned in the response, it should
407
+ // be transformed into an empty string
408
+ expect(win.document.body.innerHTML).to.not.include(`##hb_native_asset_id_4##`);
409
+ });
410
+
411
+ it('no placeholders found but requests all assets flag set - adTemplate', () => {
412
+ const template = '<div class=\"sponsored-post\">\r\n <div class=\"thumbnail\"><\/div>\r\n <div class=\"content\">\r\n <h1>\r\n <a href=\"##hb_native_linkurl##\" target=\"_blank\" class=\"pb-click\">##hb_native_title##<\/a>\r\n <\/h1>\r\n <p>##hb_native_body##<\/p>\r\n \t<div class=\"attribution\">\r\n \t<img class=\"pb-icon\" src=\"##hb_native_image##\" alt=\"icon\" height=\"150\" width=\"50\">\r\n \t\r\n \t<\/div>\r\n\t<\/div>\r\n<\/div>';
413
+ win.pbNativeData = {
414
+ pubUrl : 'https://www.url.com',
415
+ adId : AD_ID,
416
+ requestAllAssets : true
417
+ };
418
+
419
+ win.document.body.innerHTML = '';
420
+
421
+ win.addEventListener = createAllResponder([
422
+ { key: 'body', value: 'Body content' },
423
+ { key: 'title', value: 'new value' },
424
+ { key: 'clickUrl', value: 'http://www.example.com' },
425
+ { key: 'image', value: 'http://www.image.com/picture.jpg' },
426
+ ],null,template);
427
+
428
+ const nativeAssetManager = makeManager();
429
+ nativeAssetManager.loadAssets(AD_ID);
430
+
431
+ expect(win.document.body.innerHTML).to.include(`<a href="http://www.example.com" target="_blank" class="pb-click">new value</a>`);
432
+ expect(win.document.body.innerHTML).to.include(`<img class="pb-icon" src="http://www.image.com/picture.jpg" alt="icon" height="150" width="50">`);
433
+ expect(win.document.body.innerHTML).to.include(`<p>Body content</p>`);
434
+ });
435
+
436
+ it('no placeholders found but assets defined in nativeTag - adTemplate', () => {
437
+ const template = '<div class=\"sponsored-post\">\r\n <div class=\"thumbnail\"><\/div>\r\n <div class=\"content\">\r\n <h1>\r\n <a href=\"##hb_native_linkurl##\" target=\"_blank\" class=\"pb-click\">##hb_native_title##<\/a>\r\n <\/h1>\r\n <p>##hb_native_body##<\/p>\r\n \t<div class=\"attribution\">\r\n \t<img class=\"pb-icon\" src=\"##hb_native_image##\" alt=\"icon\" height=\"150\" width=\"50\">\r\n \t\r\n \t<\/div>\r\n\t<\/div>\r\n<\/div>';
438
+ win.pbNativeData = {
439
+ pubUrl : 'https://www.url.com',
440
+ adId : AD_ID,
441
+ assetsToReplace: ['image','hb_native_body','clickUrl','hb_native_title']
442
+ };
443
+
444
+ win.document.body.innerHTML = '';
445
+
446
+ win.addEventListener = createAllResponder([
447
+ { key: 'body', value: 'Body content' },
448
+ { key: 'title', value: 'new value' },
449
+ { key: 'clickUrl', value: 'http://www.example.com' },
450
+ { key: 'image', value: 'http://www.image.com/picture.jpg' },
451
+ ],null,template);
452
+
453
+ const nativeAssetManager = makeManager();
454
+ nativeAssetManager.loadAssets(AD_ID);
455
+
456
+ expect(win.document.body.innerHTML).to.include(`<a href="http://www.example.com" target="_blank" class="pb-click">new value</a>`);
457
+ expect(win.document.body.innerHTML).to.include(`<img class="pb-icon" src="http://www.image.com/picture.jpg" alt="icon" height="150" width="50">`);
458
+ expect(win.document.body.innerHTML).to.include(`<p>Body content</p>`);
459
+ });
460
+
461
+ it('does not replace anything if no placeholders found', () => {
462
+ const html = `
463
+ <h1>Native Ad</h1>
464
+ <p>Cool Description</p>
465
+ <a href="http://www.example.com">Click</a>
466
+ `;
467
+
468
+ win.document.body.innerHTML = html;
469
+ win.addEventListener = createResponder();
470
+
471
+ const nativeAssetManager = makeManager();
472
+ nativeAssetManager.loadAssets(AD_ID);
473
+
474
+ expect(win.document.body.innerHTML).to.equal(html);
475
+ });
476
+
477
+ it('replace mobile native placeholder with their values', function() {
478
+ win.document.body.innerHTML = `
479
+ <h1>hb_native_cta</h1>
480
+ <p>hb_native_body</p>
481
+ <a href="hb_native_linkurl">Click Here</a>
482
+ `;
483
+
484
+ let cb = sinon.spy();
485
+ let targetingData = {
486
+ uuid: '123'
487
+ }
488
+
489
+ sinon.stub(utils, 'sendRequest').callsFake(function(arg1, cb) {
490
+ let response = JSON.stringify({
491
+ id: '6572251357847878203',
492
+ impid: 'some-imp-id',
493
+ price: 10,
494
+ adm: '{"assets":[{"id":1,"img":{"type":3,"url":"http://vcdn.adnxs.com/p/creative-image/f8/7f/0f/13/f87f0f13-230c-4f05-8087-db9216e393de.jpg","w":989,"h":742,"ext":{"appnexus":{"prevent_crop":0}}}},{"title":{"text":"This is a Prebid Native Creative"}},{"id":2,"data":{"type":1,"value":"Prebid.org"}},{"id":3,"data":{"type":2,"value":"new value"}}],"link":{"url":"http://example.com"},"imptrackers":["http://some-tracker.com"],"jstracker":"\\u003cscript type=\\"text/javascript\\" async=\\"true\\" src=\\"http://cdn.adnxs.com/v/app/179/trk.js#app;vk=appnexus.com-omid;tv=app-native-23h;dom_id=%native_dom_id%;st=2;d=1x1;vc=iab;vid_ccr=1;tag_id=13232354;cb=http%3A%2F%2Fnym1-ib.adnxs.com%2Fvevent%3Fan_audit%3D0%26test%3D1%26e%3DwqT_3QLXB2zXAwAAAwDWAAUBCJqev-0FEN2mtMzg8dSSPxj_EQEQASo2CQAFAQgkQBEFCAwAJEAZEQkAIREJACkRCQAxEQmoMOLRpwY47UhA7UhIAlC8yb4uWJzxW2AAaM26dXjjrAWAAQGKAQNVU0SSAQEG8FKYAQGgAQGoAQGwAQC4AQLAAQPIAQLQAQDYAQDgAQHwAQCKAjt1ZignYScsIDI1Mjk4ODUsIDE1NzE4MDI5MDYpO3VmKCdyJywgOTc0OTQyMDQsIC4eAPQOAZICuQIhTEVEX1hnajgtTHdLRUx6SnZpNFlBQ0NjOFZzd0FEZ0FRQVJJN1VoUTR0R25CbGdBWVBfX19fOFBhQUJ3QVhnQmdBRUJpQUVCa0FFQm1BRUJvQUVCcUFFRHNBRUF1UUh6cldxa0FBQWtRTUVCODYxcXBBQUFKRURKQWJSM21tYW5MdWNfMlFFQUFBQUFBQUR3UC1BQkFQVUJBQUFBQUpnQ0FLQUNBTFVDQUFBQUFMMENBQUFBQU1BQ0FjZ0NBZEFDQWRnQ0FlQUNBT2dDQVBnQ0FJQURBWmdEQWFnRF9QaThDcm9EQ1U1WlRUSTZORFl6Tk9BRHBSU0lCQUNRQkFDWUJBSEJCQUFBQUEJgwh5UVEJCQEBGE5nRUFQRUUBCwkBUEQ0QkFDSUJab2uaAokBIVp3LWhMQTY9ASRuUEZiSUFRb0FEFThUa1FEb0pUbGxOTWpvME5qTTBRS1VVUxFoDFBBX1URDAxBQUFXHQwAWR0MAGEdDABjHQzweWVBQS7YAgDgAq2YSIADAYgDAJADAJgDFKADAaoDAMAD4KgByAMA2AMA4AMA6AMC-AMAgAQAkgQJL29wZW5ydGIymAQAqAQAsgQMCAAQABgAIAAwADgAuAQAwAQAyAQA0gQOOTMyNSNOWU0yOjQ2MzTaBAIIAeAEAPAEQcCQggUab3JnLnByZWJpZC5tb2JpbGUuYXBpMWRlbW-IBQGYBQCgBXE4QP8BqgUHc29tZS1pZMAFAMkFaRgU8D_SBQkJCQw8AADYBQHgBQHwBZn0IfoFBAGUKJAGAZgGALgGAMEGCSU48D_IBgDQBvUv2gYWChAAOgEAUBAAGADgBgzyBgIIAIAHAYgHAKAHQQ..%26s%3Da7b19f6eede870d487a7bec88354794855bf8161;ts=1571802906;cet=0;cecb=\\"\\u003e\\u003c/script\\u003e\", \"eventtrackers\":[{\"type\": 1, \"method\": 1, \"url\": \"eventtracker-image\"},{\"type\": 1, \"method\": 2, \"url\": \"eventtracker-js\"}]}',
495
+ adid: '97494204',
496
+ adomain: [ 'http://prebid.org' ],
497
+ iurl: 'http://nym1-ib.adnxs.com/cr?id=97494204',
498
+ cid: '9325',
499
+ crid: '97494204',
500
+ cat: [ 'IAB3-1' ],
501
+ ext: {
502
+ appnexus: {
503
+ brand_id: 555545,
504
+ auction_id: 4550134868038456300,
505
+ bidder_id: 2,
506
+ bid_ad_type: 3
507
+ }
508
+ }
509
+ });
510
+ cb(response);
511
+ });
512
+
513
+ const nativeAssetManager = makeManager();
514
+ nativeAssetManager.loadMobileAssets(targetingData, cb);
515
+
516
+ utils.sendRequest.restore();
517
+
518
+ expect(win.document.body.innerHTML).to.include('<p>new value</p>');
519
+ expect(win.document.body.innerHTML).to.include(`
520
+ <a href="http://example.com">Click Here</a>
521
+ `);
522
+ // cta was not in the response so it should default to an empty string
523
+ expect(win.document.body.innerHTML).to.include('<h1></h1>');
524
+ expect(cb.getCall(0).args[0]).to.haveOwnProperty('eventtrackers');
525
+ })
433
526
  })
527
+
528
+ describe('safe frame disabled', () => {
529
+
530
+ beforeEach(() => {
531
+ win.parent.frames = [win];
532
+ win.parent.document = {
533
+ getElementsByTagName: sinon.stub().returns([{
534
+ contentWindow: win,
535
+ parentElement: {
536
+ getBoundingClientRect: () => ({
537
+ width: 600
538
+ }),
539
+ children: [{
540
+ width: '1',
541
+ height: '1'
542
+ }]
543
+ }
544
+ }])
545
+ }
546
+ })
547
+
548
+ it('should set the iframe to the width of the container', () => {
549
+ const html = `<script>
550
+ let nativeTag = {};
551
+ nativeTag.adTemplate = "<div class=\"sponsored-post\">\r\n <div class=\"thumbnail\"><\/div>\r\n <div class=\"content\">\r\n <h1>\r\n <a href=\"##hb_native_linkurl##\" target=\"_blank\" class=\"pb-click\">##hb_native_title##<\/a>\r\n <\/h1>\r\n <p>##hb_native_body##<\/p>\r\n \t<div class=\"attribution\">\r\n \t<img class=\"pb-icon\" src=\"##hb_native_image##\" alt=\"icon\" height=\"150\" width=\"50\">\r\n \t\r\n \t<\/div>\r\n\t<\/div>\r\n<\/div>";
552
+ nativeTag.pubUrl = "https://www.url.com";
553
+ nativeTag.adId = "`+AD_ID+`";
554
+ nativeTag.requestAllAssets = true;
555
+ window.pbNativeTag.renderNativeAd(nativeTag);
556
+ </script>`;
557
+ win.pbNativeData = {
558
+ pubUrl : 'https://www.url.com',
559
+ adId : AD_ID,
560
+ adTemplate : '<div class=\"sponsored-post\">\r\n <div class=\"thumbnail\"><\/div>\r\n <div class=\"content\">\r\n <h1>\r\n <a href=\"##hb_native_linkurl##\" target=\"_blank\" class=\"pb-click\">##hb_native_title##<\/a>\r\n <\/h1>\r\n <p>##hb_native_body##<\/p>\r\n \t<div class=\"attribution\">\r\n \t<img class=\"pb-icon\" src=\"##hb_native_image##\" alt=\"icon\" height=\"150\" width=\"50\">\r\n \t\r\n \t<\/div>\r\n\t<\/div>\r\n<\/div>'
561
+ };
562
+
563
+ win.document.body.innerHTML = html;
564
+ win.addEventListener = createResponder([
565
+ { key: 'body', value: 'Body content' },
566
+ { key: 'title', value: 'new value' },
567
+ { key: 'clickUrl', value: 'http://www.example.com' },
568
+ { key: 'image', value: 'http://www.image.com/picture.jpg' },
569
+ ]);
570
+
571
+ const nativeAssetManager = makeManager();
572
+ nativeAssetManager.loadAssets(AD_ID);
573
+
574
+ expect(win.document.body.innerHTML).to.include(`<a href="http://www.example.com" target="_blank" class="pb-click">new value</a>`);
575
+ expect(win.document.body.innerHTML).to.include(`<img class="pb-icon" src="http://www.image.com/picture.jpg" alt="icon" height="150" width="50">`);
576
+ expect(win.document.body.innerHTML).to.include(`<p>Body content</p>`);
577
+ expect(win.document.body.style.width).to.equal('600px');
578
+ });
579
+ });
434
580
  });