From fc75fb40ecc7a28b326b2156882541b8a6b444c5 Mon Sep 17 00:00:00 2001 From: Casper Date: Fri, 20 Jul 2018 15:45:47 +0200 Subject: [PATCH] New function markWinningBidAsUsed for marking video bids (#2777) * Quick function to mark a video-bid as used * Added tests for the mark function * comments * Changed the function to accept a markBidRequest object to improve security * Changed the markWinningBidAsUsed to take and/or adUnitCode/adId --- src/prebid.js | 27 +++++++++++++ test/spec/unit/pbjs_api_spec.js | 70 ++++++++++++++++++++++++++++++++- 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/src/prebid.js b/src/prebid.js index 767180f32867..75e1d117c2a3 100644 --- a/src/prebid.js +++ b/src/prebid.js @@ -603,6 +603,33 @@ $$PREBID_GLOBAL$$.getHighestCpmBids = function (adUnitCode) { .map(removeRequestId); }; +/** + * Mark the winning bid as used, should only be used in conjunction with video + * @typedef {Object} MarkBidRequest + * @property {string} adUnitCode The ad unit code + * @property {string} adId The id representing the ad we want to mark + * + * @alias module:pbjs.markWinningBidAsUsed +*/ +$$PREBID_GLOBAL$$.markWinningBidAsUsed = function (markBidRequest) { + let bids = []; + + if (markBidRequest.adUnitCode && markBidRequest.adId) { + bids = auctionManager.getBidsReceived() + .filter(bid => bid.adId === markBidRequest.adId && bid.adUnitCode === markBidRequest.adUnitCode); + } else if (markBidRequest.adUnitCode) { + bids = targeting.getWinningBids(markBidRequest.adUnitCode); + } else if (markBidRequest.adId) { + bids = auctionManager.getBidsReceived().filter(bid => bid.adId === markBidRequest.adId); + } else { + utils.logWarn('Inproper usage of markWinningBidAsUsed. It\'ll need an adUnitCode and/or adId to function.'); + } + + if (bids.length > 0) { + bids[0].status = RENDERED; + } +}; + /** * Get Prebid config options * @param {Object} options diff --git a/test/spec/unit/pbjs_api_spec.js b/test/spec/unit/pbjs_api_spec.js index 3d403a34ff2c..d46a8d740a56 100644 --- a/test/spec/unit/pbjs_api_spec.js +++ b/test/spec/unit/pbjs_api_spec.js @@ -9,7 +9,7 @@ import { createBidReceived } from 'test/fixtures/fixtures'; import { auctionManager, newAuctionManager } from 'src/auctionManager'; -import { targeting, newTargeting } from 'src/targeting'; +import { targeting, newTargeting, RENDERED } from 'src/targeting'; import { config as configObj } from 'src/config'; import * as ajaxLib from 'src/ajax'; import * as auctionModule from 'src/auction'; @@ -1838,6 +1838,74 @@ describe('Unit: Prebid Module', function () { }); }); + describe('markWinningBidAsUsed', () => { + it('marks the bid object as used for the given adUnitCode/adId combination', () => { + // make sure the auction has "state" and does not reload the fixtures + const adUnitCode = '/19968336/header-bid-tag-0'; + const bidsReceived = $$PREBID_GLOBAL$$.getBidResponsesForAdUnitCode(adUnitCode); + auction.getBidsReceived = function() { return bidsReceived.bids }; + + // mark the bid and verify the state has changed to RENDERED + const winningBid = targeting.getWinningBids(adUnitCode)[0]; + $$PREBID_GLOBAL$$.markWinningBidAsUsed({ adUnitCode, adId: winningBid.adId }); + const markedBid = $$PREBID_GLOBAL$$.getBidResponsesForAdUnitCode(adUnitCode) + .bids + .find(bid => bid.adId === winningBid.adId); + + expect(markedBid.status).to.equal(RENDERED); + resetAuction(); + }); + + it('try and mark the bid object, but fail because we supplied the wrong adId', () => { + const adUnitCode = '/19968336/header-bid-tag-0'; + const bidsReceived = $$PREBID_GLOBAL$$.getBidResponsesForAdUnitCode(adUnitCode); + auction.getBidsReceived = function() { return bidsReceived.bids }; + + const winningBid = targeting.getWinningBids(adUnitCode)[0]; + $$PREBID_GLOBAL$$.markWinningBidAsUsed({ adUnitCode, adId: 'miss' }); + const markedBid = $$PREBID_GLOBAL$$.getBidResponsesForAdUnitCode(adUnitCode) + .bids + .find(bid => bid.adId === winningBid.adId); + + expect(markedBid.status).to.not.equal(RENDERED); + resetAuction(); + }); + + it('marks the winning bid object as used for the given adUnitCode', () => { + // make sure the auction has "state" and does not reload the fixtures + const adUnitCode = '/19968336/header-bid-tag-0'; + const bidsReceived = $$PREBID_GLOBAL$$.getBidResponsesForAdUnitCode(adUnitCode); + auction.getBidsReceived = function() { return bidsReceived.bids }; + + // mark the bid and verify the state has changed to RENDERED + const winningBid = targeting.getWinningBids(adUnitCode)[0]; + $$PREBID_GLOBAL$$.markWinningBidAsUsed({ adUnitCode }); + const markedBid = $$PREBID_GLOBAL$$.getBidResponsesForAdUnitCode(adUnitCode) + .bids + .find(bid => bid.adId === winningBid.adId); + + expect(markedBid.status).to.equal(RENDERED); + resetAuction(); + }); + + it('marks a bid object as used for the given adId', () => { + // make sure the auction has "state" and does not reload the fixtures + const adUnitCode = '/19968336/header-bid-tag-0'; + const bidsReceived = $$PREBID_GLOBAL$$.getBidResponsesForAdUnitCode(adUnitCode); + auction.getBidsReceived = function() { return bidsReceived.bids }; + + // mark the bid and verify the state has changed to RENDERED + const winningBid = targeting.getWinningBids(adUnitCode)[0]; + $$PREBID_GLOBAL$$.markWinningBidAsUsed({ adId: winningBid.adId }); + const markedBid = $$PREBID_GLOBAL$$.getBidResponsesForAdUnitCode(adUnitCode) + .bids + .find(bid => bid.adId === winningBid.adId); + + expect(markedBid.status).to.equal(RENDERED); + resetAuction(); + }); + }); + describe('setTargetingForAst', () => { let targeting; let auctionManagerInstance;