Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions modules/yaleoBidAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ interface YaleoBidParams {
* Maximum CPM value. Bids with a CPM higher than the specified value will be rejected.
*/
maxCpm?: number;
/**
* Renders the ad unit in the dark theme.
*/
forceDarkTheme?: boolean;
}

interface YaleoConfig {
Expand Down Expand Up @@ -46,6 +50,7 @@ const AUDIENZZ_VENDOR_ID = 783;
const DEFAULT_ENDPOINT = 'https://bidder.yaleo.com/prebid';
const ALLOWED_ENDPOINTS = new Set([DEFAULT_ENDPOINT, 'https://dev-bidder.yaleo.com/prebid']);
const DEFAULT_TTL = 300;
const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;

const converter = ortbConverter<typeof BIDDER_CODE>({
context: {
Expand All @@ -67,12 +72,35 @@ const resolveEndpoint = (): string => {
return customEndpoint;
};

const getInvalidParamMessages = ({ placementId, memberId, maxCpm, forceDarkTheme }: YaleoBidParams): string[] => {
const messages: string[] = [];
if (!UUID_PATTERN.test(placementId)) {
messages.push(`placementId must be a UUID, got "${placementId}"`);
}
if (memberId !== undefined && !(Number.isInteger(memberId) && memberId >= 1)) {
messages.push(`memberId must be a positive integer, got "${memberId}"`);
}
if (maxCpm !== undefined && !(Number.isFinite(maxCpm) && maxCpm >= 0)) {
messages.push(`maxCpm must be a number greater than or equal to 0, got "${maxCpm}"`);
}
if (forceDarkTheme !== undefined && typeof forceDarkTheme !== 'boolean') {
messages.push(`forceDarkTheme must be a boolean, got "${forceDarkTheme}"`);
}
return messages;
};

const isBidRequestValid: BidderSpec<typeof BIDDER_CODE>['isBidRequestValid'] = (request) => {
if (!request.params || typeof request.params.placementId !== 'string') {
const { params } = request;
if (!params || typeof params.placementId !== 'string' || !params.placementId) {
return false;
}

return !!request.params.placementId;
// Warn only: rejecting previously accepted values is a breaking change.
getInvalidParamMessages(params).forEach((message) => {
logWarn(`${BIDDER_CODE}: ${message}. The bid is still sent, but a future major Prebid.js release will reject it.`);
});

return true;
};

const buildRequests: BidderSpec<typeof BIDDER_CODE>['buildRequests'] = (validBidRequests, bidderRequest) => {
Expand Down
74 changes: 67 additions & 7 deletions test/spec/modules/yaleoBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { cloneDeep } from "lodash";
import sinon from 'sinon';
import { config } from "src/config.js";
import * as utils from "src/utils.js";
import { spec } from "../../../modules/yaleoBidAdapter.ts";

const PLACEMENT_ID = '9c752583-d856-4b44-a227-c0ce6c7a8cfa';

const bannerBidRequestBase = {
adUnitCode: 'banner-ad-unit-code',
auctionId: 'banner-auction-id',
bidId: 'banner-bid-id',
bidder: 'yaleo',
bidderRequestId: 'banner-bidder-request-id',
mediaTypes: { banner: [[300, 250]] },
params: { placementId: '12345' },
params: { placementId: PLACEMENT_ID },
};

const bannerServerResponseBase = {
Expand Down Expand Up @@ -62,15 +66,16 @@ describe('Yaleo bid adapter', () => {
describe('isBidRequestValid', () => {
it('returns true when all params are specified', () => {
bannerBidRequest.params = {
placementId: '12345',
placementId: PLACEMENT_ID,
maxCpm: 5.00,
memberId: 12345,
forceDarkTheme: true,
};
expect(spec.isBidRequestValid(bannerBidRequest)).to.be.true;
});

it('returns true when required params are specified', () => {
bannerBidRequest.params = { placementId: '12345' };
bannerBidRequest.params = { placementId: PLACEMENT_ID };
expect(spec.isBidRequestValid(bannerBidRequest)).to.be.true;
});

Expand Down Expand Up @@ -118,6 +123,59 @@ describe('Yaleo bid adapter', () => {
bannerBidRequest.params = { placementId: null };
expect(spec.isBidRequestValid(bannerBidRequest)).to.be.false;
});

it('returns false when placementId is an empty string', () => {
bannerBidRequest.params = { placementId: '' };
expect(spec.isBidRequestValid(bannerBidRequest)).to.be.false;
});

describe('params outside the Prebid Server schema', () => {
const describeValue = (value) => (typeof value === 'string' ? `"${value}"` : String(value));
let logWarnStub;

beforeEach(() => {
logWarnStub = sinon.stub(utils, 'logWarn');
});

afterEach(() => {
logWarnStub.restore();
});

[
{ placementId: PLACEMENT_ID },
{ placementId: PLACEMENT_ID.toUpperCase() },
{ placementId: PLACEMENT_ID, memberId: 1, maxCpm: 0, forceDarkTheme: false },
{ placementId: PLACEMENT_ID, memberId: 3927, maxCpm: 5.5, forceDarkTheme: true },
].forEach((params) => {
it(`accepts the bid without a warning: ${JSON.stringify(params)}`, () => {
bannerBidRequest.params = params;
expect(spec.isBidRequestValid(bannerBidRequest)).to.be.true;
sinon.assert.notCalled(logWarnStub);
});
});

[
['placementId', ['12345', `${PLACEMENT_ID}0`, ` ${PLACEMENT_ID}`, PLACEMENT_ID.replace(/-/g, ''), 'zz752583-d856-4b44-a227-c0ce6c7a8cfa']],
['memberId', [0, -1, 1.5, NaN, '3927', null, true]],
['maxCpm', [-0.01, NaN, Infinity, '5', null]],
['forceDarkTheme', ['true', 1, null]],
].forEach(([param, values]) => {
values.forEach((value) => {
it(`still accepts the bid but warns when ${param} is ${describeValue(value)}`, () => {
bannerBidRequest.params = { placementId: PLACEMENT_ID, [param]: value };
expect(spec.isBidRequestValid(bannerBidRequest)).to.be.true;
sinon.assert.calledOnce(logWarnStub);
expect(logWarnStub.firstCall.args[0]).to.include(`yaleo: ${param} must be`);
});
});
});

it('warns once per invalid param', () => {
bannerBidRequest.params = { placementId: '12345', memberId: 0, maxCpm: -1, forceDarkTheme: 'true' };
expect(spec.isBidRequestValid(bannerBidRequest)).to.be.true;
sinon.assert.callCount(logWarnStub, 4);
});
});
});

describe('buildRequests', () => {
Expand Down Expand Up @@ -212,9 +270,10 @@ describe('Yaleo bid adapter', () => {

it('checks that all params are passed to the request', () => {
bannerBidRequest.params = {
placementId: '12345',
placementId: PLACEMENT_ID,
maxCpm: 5.00,
memberId: 12345,
forceDarkTheme: true,
};
const bidderRequest = {
bids: [bannerBidRequest],
Expand All @@ -225,15 +284,16 @@ describe('Yaleo bid adapter', () => {
const request = spec.buildRequests([bannerBidRequest], bidderRequest);
const yaleoParams = request.data.imp[0].ext.prebid.bidder.yaleo;

expect(yaleoParams.placementId).to.equal('12345');
expect(yaleoParams.placementId).to.equal(PLACEMENT_ID);
expect(yaleoParams.maxCpm).to.equal(5.00);
expect(yaleoParams.memberId).to.equal(12345);
expect(yaleoParams.forceDarkTheme).to.be.true;
});
});

it('checks that only specified params are passed to the request', () => {
bannerBidRequest.params = {
placementId: '12345',
placementId: PLACEMENT_ID,
};

const bidderRequest = {
Expand All @@ -245,7 +305,7 @@ describe('Yaleo bid adapter', () => {
const request = spec.buildRequests([bannerBidRequest], bidderRequest);
const yaleoParams = request.data.imp[0].ext.prebid.bidder.yaleo;

expect(yaleoParams).to.deep.equal({ placementId: '12345' });
expect(yaleoParams).to.deep.equal({ placementId: PLACEMENT_ID });
});

describe('interpretResponse', () => {
Expand Down
Loading