Skip to content

Commit

Permalink
add a test for addLegacyFieldsIfNeeded
Browse files Browse the repository at this point in the history
  • Loading branch information
JulieLorin committed Nov 29, 2022
1 parent 9a9c234 commit 0d1b4f0
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/auction.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ function tryAddVideoBid(auctionInstance, bidResponse, afterBidAdded, {index = au
}

// Native bid response might be in ortb2 format - adds legacy field for backward compatibility
function addLegacyFieldsIfNeeded(bidResponse) {
const addLegacyFieldsIfNeeded = (bidResponse) => {
const nativeOrtbRequest = auctionManager.index.getAdUnit(bidResponse)?.nativeOrtbRequest;
const nativeOrtbResponse = bidResponse.native?.ortb

Expand Down
2 changes: 1 addition & 1 deletion src/native.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const NATIVE_TARGETING_KEYS = Object.keys(CONSTANTS.NATIVE_KEYS).map(
key => CONSTANTS.NATIVE_KEYS[key]
);

const IMAGE = {
export const IMAGE = {
ortb: {
ver: '1.2',
assets: [
Expand Down
74 changes: 74 additions & 0 deletions test/spec/auctionmanager_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import 'modules/debugging/index.js' // some tests look for debugging side effect
import {AuctionIndex} from '../../src/auctionIndex.js';
import {expect} from 'chai';
import {deepClone} from '../../src/utils.js';
import { IMAGE as ortbNativeRequest } from 'src/native.js';

var assert = require('assert');

Expand Down Expand Up @@ -1276,6 +1277,79 @@ describe('auctionmanager.js', function () {
});
});

describe('addBidResponse native', function () {
let makeRequestsStub;
let ajaxStub;
let spec;
let auction;

beforeEach(function () {
makeRequestsStub = sinon.stub(adapterManager, 'makeBidRequests');
ajaxStub = sinon.stub(ajaxLib, 'ajaxBuilder').callsFake(mockAjaxBuilder);

const adUnits = [{
code: ADUNIT_CODE,
transactionId: ADUNIT_CODE,
bids: [
{bidder: BIDDER_CODE, params: {placementId: 'id'}},
],
nativeOrtbRequest: ortbNativeRequest.ortb,
mediaTypes: { native: { type: 'image' } }
}];
auction = auctionModule.newAuction({adUnits, adUnitCodes: [ADUNIT_CODE], callback: function() {}, cbTimeout: 3000});
indexAuctions = [auction];
const createAuctionStub = sinon.stub(auctionModule, 'newAuction');
createAuctionStub.returns(auction);

spec = mockBidder(BIDDER_CODE);
registerBidder(spec);
});

afterEach(function () {
ajaxStub.restore();
auctionModule.newAuction.restore();
adapterManager.makeBidRequests.restore();
});

it('should add legacy fields to native response', function () {
let nativeBid = mockBid();
nativeBid.mediaType = 'native';
nativeBid.native = {
ortb: {
ver: '1.2',
assets: [
{ id: 2, title: { text: 'Sample title' } },
{ id: 4, data: { value: 'Sample body' } },
{ id: 3, data: { value: 'Sample sponsoredBy' } },
{ id: 1, img: { url: 'https://www.example.com/image.png', w: 200, h: 200 } },
{ id: 5, img: { url: 'https://www.example.com/icon.png', w: 32, h: 32 } }
],
link: { url: 'http://www.click.com' },
eventtrackers: [
{ event: 1, method: 1, url: 'http://www.imptracker.com' },
{ event: 1, method: 2, url: 'http://www.jstracker.com/file.js' }
]
}
}

let bidRequest = mockBidRequest(nativeBid, { mediaType: { native: ortbNativeRequest } });
makeRequestsStub.returns([bidRequest]);

spec.interpretResponse.returns(nativeBid);
auction.callBids();

const addedBid = auction.getBidsReceived().pop();
assert.equal(addedBid.native.body, 'Sample body')
assert.equal(addedBid.native.title, 'Sample title')
assert.equal(addedBid.native.sponsoredBy, 'Sample sponsoredBy')
assert.equal(addedBid.native.clickUrl, 'http://www.click.com')
assert.equal(addedBid.native.image, 'https://www.example.com/image.png')
assert.equal(addedBid.native.icon, 'https://www.example.com/icon.png')
assert.equal(addedBid.native.impressionTrackers[0], 'http://www.imptracker.com')
assert.equal(addedBid.native.javascriptTrackers, '<script async src="http://www.jstracker.com/file.js"></script>')
});
});

describe('getMediaTypeGranularity', function () {
it('video', function () {
let mediaTypes = { video: {id: '1'} };
Expand Down

0 comments on commit 0d1b4f0

Please sign in to comment.