Skip to content

Commit

Permalink
New function markWinningBidAsUsed for marking video bids (prebid#2777)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Caspervw authored and florevallatmrf committed Sep 6, 2018
1 parent 1dbe240 commit fc75fb4
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 69 additions & 1 deletion test/spec/unit/pbjs_api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit fc75fb4

Please sign in to comment.