From 90afc1a18bcd32f7f91669cc504a04f120da668e Mon Sep 17 00:00:00 2001 From: JonGoSonobi Date: Tue, 5 Feb 2019 07:02:53 -0500 Subject: [PATCH] Fix issue where the ${AUCTION_PRICE} macro was not being replaced in secure creatives (#3493) * fixed issue where the creative AUCTION_PRICE macro was not being replaced in secure creatives. * fixed issue where the creative AUCTION_PRICE macro was not being replaced in secure creatives. * removed cpm from postMessage payload] * removed unneeded cpm deconstruct * pulling cpm from the adObject * also macro repalce adUrl * added unit test for secureCreative sendAdToCreative * added more realistic mock values --- src/secureCreatives.js | 12 +++---- test/spec/unit/secureCreatives_spec.js | 45 ++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 test/spec/unit/secureCreatives_spec.js diff --git a/src/secureCreatives.js b/src/secureCreatives.js index 67579bae76c..32ad27a0496 100644 --- a/src/secureCreatives.js +++ b/src/secureCreatives.js @@ -6,7 +6,7 @@ import events from './events'; import { fireNativeTrackers } from './native'; import { EVENTS } from './constants'; -import { isSlotMatchingAdUnitCode, logWarn } from './utils'; +import { isSlotMatchingAdUnitCode, logWarn, replaceAuctionPrice } from './utils'; import { auctionManager } from './auctionManager'; import find from 'core-js/library/fn/array/find'; import { isRendererRequired, executeRenderer } from './Renderer'; @@ -32,7 +32,7 @@ function receiveMessage(ev) { }); if (data.message === 'Prebid Request') { - sendAdToCreative(adObject, data.adServerDomain, ev.source); + _sendAdToCreative(adObject, data.adServerDomain, ev.source); // save winning bids auctionManager.addWinningBid(adObject); @@ -53,8 +53,8 @@ function receiveMessage(ev) { } } -function sendAdToCreative(adObject, remoteDomain, source) { - const { adId, ad, adUrl, width, height, renderer } = adObject; +export function _sendAdToCreative(adObject, remoteDomain, source) { + const { adId, ad, adUrl, width, height, renderer, cpm } = adObject; // rendering for outstream safeframe if (isRendererRequired(renderer)) { executeRenderer(renderer, adObject); @@ -62,8 +62,8 @@ function sendAdToCreative(adObject, remoteDomain, source) { resizeRemoteCreative(adObject); source.postMessage(JSON.stringify({ message: 'Prebid Response', - ad, - adUrl, + ad: replaceAuctionPrice(ad, cpm), + adUrl: replaceAuctionPrice(adUrl, cpm), adId, width, height diff --git a/test/spec/unit/secureCreatives_spec.js b/test/spec/unit/secureCreatives_spec.js new file mode 100644 index 00000000000..f0f26bf5653 --- /dev/null +++ b/test/spec/unit/secureCreatives_spec.js @@ -0,0 +1,45 @@ +import { + _sendAdToCreative +} from '../../../src/secureCreatives'; +import { expect } from 'chai'; +import * as utils from 'src/utils'; + +describe('secureCreatives', () => { + describe('_sendAdToCreative', () => { + beforeEach(function () { + sinon.stub(utils, 'logError'); + sinon.stub(utils, 'logWarn'); + }); + + afterEach(function () { + utils.logError.restore(); + utils.logWarn.restore(); + }); + it('should macro replace ${AUCTION_PRICE} with the winning bid for ad and adUrl', () => { + const oldVal = window.googletag; + const oldapntag = window.apntag; + window.apntag = null + window.googletag = null; + const mockAdObject = { + adId: 'someAdId', + ad: '', + adUrl: 'http://creative.prebid.org/${AUCTION_PRICE}', + width: 300, + height: 250, + renderer: null, + cpm: '1.00', + adUnitCode: 'some_dom_id' + }; + const remoteDomain = '*'; + const source = { + postMessage: sinon.stub() + }; + + _sendAdToCreative(mockAdObject, remoteDomain, source); + expect(JSON.parse(source.postMessage.args[0][0]).ad).to.equal(''); + expect(JSON.parse(source.postMessage.args[0][0]).adUrl).to.equal('http://creative.prebid.org/1.00'); + window.googletag = oldVal; + window.apntag = oldapntag; + }); + }); +});