Skip to content

Commit

Permalink
Mgid Adapter: make placementId optional (prebid#3992)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaudeamus authored and leonardlabat committed Jul 30, 2019
1 parent 9a68159 commit e407775
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 16 deletions.
16 changes: 9 additions & 7 deletions modules/mgidBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ utils._each(NATIVE_ASSETS, anAsset => { _NATIVE_ASSET_ID_TO_KEY_MAP[anAsset.ID]
utils._each(NATIVE_ASSETS, anAsset => { _NATIVE_ASSET_KEY_TO_ASSET_MAP[anAsset.KEY] = anAsset });

export const spec = {
VERSION: '1.2',
VERSION: '1.3',
code: BIDDER_CODE,
supportedMediaTypes: [BANNER, NATIVE],
reId: /^[0-9]+$/,
reId: /^[1-9][0-9]*$/,
NATIVE_ASSET_ID_TO_KEY_MAP: _NATIVE_ASSET_ID_TO_KEY_MAP,
NATIVE_ASSET_KEY_TO_ASSET_MAP: _NATIVE_ASSET_KEY_TO_ASSET_MAP,
/**
Expand Down Expand Up @@ -102,10 +102,10 @@ export const spec = {
bannerOk = sizes[f].length === 2;
}
}
return utils.isPlainObject(bid.params) && !!bid.params.accountId && !!bid.params.placementId &&
utils.isStr(bid.params.accountId) && utils.isStr(bid.params.placementId) &&
bid.params.accountId.toString().match(spec.reId) > 0 && bid.params.placementId.toString().match(spec.reId) &&
(bannerOk || nativeOk);
let acc = Number(bid.params.accountId);
let plcmt = Number(bid.params.placementId);
return (bannerOk || nativeOk) && utils.isPlainObject(bid.params) && !!bid.adUnitCode && utils.isStr(bid.adUnitCode) && (plcmt > 0 ? bid.params.placementId.toString().search(spec.reId) === 0 : true) &&
!!acc && acc > 0 && bid.params.accountId.toString().search(spec.reId) === 0;
},
/**
* Make a server request from the list of BidRequests.
Expand All @@ -132,9 +132,11 @@ export const spec = {
const secure = window.location.protocol === 'https:' ? 1 : 0;
let imp = [];
validBidRequests.forEach(bid => {
let tagid = utils.deepAccess(bid, 'params.placementId') || 0;
tagid = !tagid ? bid.adUnitCode : tagid + '/' + bid.adUnitCode;
let impObj = {
id: bid.bidId,
tagid: utils.deepAccess(bid, 'params.placementId'),
tagid,
secure,
};
const bidFloor = utils.deepAccess(bid, 'params.bidFloor') || utils.deepAccess(bid, 'params.bidfloor') || 0;
Expand Down
86 changes: 77 additions & 9 deletions test/spec/modules/mgidBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('Mgid bid adapter', function () {

describe('isBidRequestValid', function () {
let bid = {
'adUnitCode': 'div',
'bidder': 'mgid',
'params': {
'property': '10433394',
Expand Down Expand Up @@ -62,6 +63,7 @@ describe('Mgid bid adapter', function () {
it('should return false when valid params are not passed', function () {
let bid = Object.assign({}, bid);
delete bid.params;
bid.adUnitCode = '';
bid.mediaTypes = {
banner: {
sizes: [[300, 250]]
Expand All @@ -71,6 +73,32 @@ describe('Mgid bid adapter', function () {
expect(spec.isBidRequestValid(bid)).to.equal(false);
});

it('should return false when adUnitCode not passed', function () {
let bid = Object.assign({}, bid);
delete bid.params;
bid.adUnitCode = '';
bid.mediaTypes = {
banner: {
sizes: [[300, 250]]
}
};
bid.params = {accountId: 2, placementId: 1};
expect(spec.isBidRequestValid(bid)).to.equal(false);
});

it('should return true when valid params are passed as nums', function () {
let bid = Object.assign({}, bid);
delete bid.params;
bid.adUnitCode = 'div';
bid.mediaTypes = {
banner: {
sizes: [[300, 250]]
}
};
bid.params = {accountId: 2, placementId: 1};
expect(spec.isBidRequestValid(bid)).to.equal(true);
});

it('should return false when valid params are not passed', function () {
let bid = Object.assign({}, bid);
delete bid.params;
Expand Down Expand Up @@ -119,9 +147,10 @@ describe('Mgid bid adapter', function () {
expect(spec.isBidRequestValid(bid)).to.equal(false);
});

it('should return true when valid params are passed', function () {
it('should return true when valid params are passed as strings', function () {
let bid = Object.assign({}, bid);
delete bid.params;
bid.adUnitCode = 'div';
bid.params = {accountId: '1', placementId: '1'};
bid.mediaTypes = {
banner: {
Expand Down Expand Up @@ -185,6 +214,7 @@ describe('Mgid bid adapter', function () {

it('should return true when mediaTypes.native all assets needed', function () {
let bid = Object.assign({}, bid);
bid.adUnitCode = 'div';
bid.params = {accountId: '2', placementId: '1'};
bid.mediaTypes = {
native: {
Expand Down Expand Up @@ -281,13 +311,46 @@ describe('Mgid bid adapter', function () {
});

describe('buildRequests', function () {
it('should return undefined if no validBidRequests passed', function () {
expect(spec.buildRequests([])).to.be.undefined;
});

let abid = {
adUnitCode: 'div',
bidder: 'mgid',
params: {
accountId: '1',
placementId: '2',
},
};
it('should return proper request url', function () {
localStorage.setItem('mgMuidn', 'xxx');
let bid = Object.assign({}, abid);
bid.mediaTypes = {
banner: {
sizes: [[300, 250]]
}
};
let bidRequests = [bid];
const request = spec.buildRequests(bidRequests);
expect(request.url).deep.equal('https://prebid.mgid.com/prebid/1?muid=xxx');
localStorage.removeItem('mgMuidn')
});
it('should proper handle gdpr', function () {
let bid = Object.assign({}, abid);
bid.mediaTypes = {
banner: {
sizes: [[300, 250]]
}
};
let bidRequests = [bid];
const request = spec.buildRequests(bidRequests, {gdprConsent: {consentString: 'gdpr', gdprApplies: true}});
expect(request.url).deep.equal('https://prebid.mgid.com/prebid/1');
expect(request.method).deep.equal('POST');
const data = JSON.parse(request.data);
expect(data.user).deep.equal({ext: {consent: 'gdpr'}});
expect(data.regs).deep.equal({ext: {gdpr: 1}});
});
it('should return proper banner imp', function () {
let bid = Object.assign({}, abid);
bid.mediaTypes = {
Expand All @@ -309,13 +372,13 @@ describe('Mgid bid adapter', function () {
expect(data.device.h).equal(screenHeight);
expect(data.device.w).equal(screenWidth);
expect(data.device.language).to.deep.equal(lang);
expect(data.imp[0].tagid).to.deep.equal('2');
expect(data.imp[0].tagid).to.deep.equal('2/div');
expect(data.imp[0].banner).to.deep.equal({w: 300, h: 250, format: []});
expect(data.imp[0].secure).to.deep.equal(secure);
expect(request).to.deep.equal({
'method': 'POST',
'url': 'https://prebid.mgid.com/prebid/1',
'data': '{\"site\":{\"domain\":\"' + domain + '\"},\"cur\":[\"USD\"],\"device\":{\"ua\":\"' + ua + '\",\"js\":1,\"dnt\":' + dnt + ',\"h\":' + screenHeight + ',\"w\":' + screenWidth + ',\"language\":\"' + lang + '\"},\"user\":{},\"regs\":{},\"ext\":{\"mgid_ver\":\"' + mgid_ver + '\",\"prebid_ver\":\"' + prebid_ver + '\"},\"imp\":[{\"tagid\":\"2\",\"secure\":' + secure + ',\"banner\":{\"w\":300,\"h\":250,\"format\":[]}}]}',
'data': '{\"site\":{\"domain\":\"' + domain + '\"},\"cur\":[\"USD\"],\"device\":{\"ua\":\"' + ua + '\",\"js\":1,\"dnt\":' + dnt + ',\"h\":' + screenHeight + ',\"w\":' + screenWidth + ',\"language\":\"' + lang + '\"},\"user\":{},\"regs\":{},\"ext\":{\"mgid_ver\":\"' + mgid_ver + '\",\"prebid_ver\":\"' + prebid_ver + '\"},\"imp\":[{\"tagid\":\"2/div\",\"secure\":' + secure + ',\"banner\":{\"w\":300,\"h\":250,\"format\":[]}}]}',
});
});
it('should not return native imp if minimum asset list not requested', function () {
Expand Down Expand Up @@ -357,13 +420,13 @@ describe('Mgid bid adapter', function () {
expect(data.device.h).equal(screenHeight);
expect(data.device.w).equal(screenWidth);
expect(data.device.language).to.deep.equal(lang);
expect(data.imp[0].tagid).to.deep.equal('2');
expect(data.imp[0].tagid).to.deep.equal('2/div');
expect(data.imp[0].native).is.a('object').and.to.deep.equal({'request': {'assets': [{'id': 1, 'required': 1, 'title': {'len': 80}}, {'id': 2, 'img': {'h': 80, 'type': 3, 'w': 80}, 'required': 0}, {'data': {'type': 1}, 'id': 11, 'required': 0}], 'plcmtcnt': 1}});
expect(data.imp[0].secure).to.deep.equal(secure);
expect(request).to.deep.equal({
'method': 'POST',
'url': 'https://prebid.mgid.com/prebid/1',
'data': '{\"site\":{\"domain\":\"' + domain + '\"},\"cur\":[\"USD\"],\"device\":{\"ua\":\"' + ua + '\",\"js\":1,\"dnt\":' + dnt + ',\"h\":' + screenHeight + ',\"w\":' + screenWidth + ',\"language\":\"' + lang + '\"},\"user\":{},\"regs\":{},\"ext\":{\"mgid_ver\":\"' + mgid_ver + '\",\"prebid_ver\":\"' + prebid_ver + '\"},\"imp\":[{\"tagid\":\"2\",\"secure\":' + secure + ',\"native\":{\"request\":{\"plcmtcnt\":1,\"assets\":[{\"id\":1,\"required\":1,\"title\":{\"len\":80}},{\"id\":2,\"required\":0,\"img\":{\"type\":3,\"w\":80,\"h\":80}},{\"id\":11,\"required\":0,\"data\":{\"type\":1}}]}}}]}',
'data': '{\"site\":{\"domain\":\"' + domain + '\"},\"cur\":[\"USD\"],\"device\":{\"ua\":\"' + ua + '\",\"js\":1,\"dnt\":' + dnt + ',\"h\":' + screenHeight + ',\"w\":' + screenWidth + ',\"language\":\"' + lang + '\"},\"user\":{},\"regs\":{},\"ext\":{\"mgid_ver\":\"' + mgid_ver + '\",\"prebid_ver\":\"' + prebid_ver + '\"},\"imp\":[{\"tagid\":\"2/div\",\"secure\":' + secure + ',\"native\":{\"request\":{\"plcmtcnt\":1,\"assets\":[{\"id\":1,\"required\":1,\"title\":{\"len\":80}},{\"id\":2,\"required\":0,\"img\":{\"type\":3,\"w\":80,\"h\":80}},{\"id\":11,\"required\":0,\"data\":{\"type\":1}}]}}}]}',
});
});
it('should return proper native imp with sponsoredBy', function () {
Expand Down Expand Up @@ -392,13 +455,13 @@ describe('Mgid bid adapter', function () {
expect(data.device.h).equal(screenHeight);
expect(data.device.w).equal(screenWidth);
expect(data.device.language).to.deep.equal(lang);
expect(data.imp[0].tagid).to.deep.equal('2');
expect(data.imp[0].tagid).to.deep.equal('2/div');
expect(data.imp[0].native).is.a('object').and.to.deep.equal({'request': {'assets': [{'id': 1, 'required': 1, 'title': {'len': 80}}, {'id': 2, 'img': {'h': 80, 'type': 3, 'w': 80}, 'required': 0}, {'data': {'type': 1}, 'id': 4, 'required': 0}], 'plcmtcnt': 1}});
expect(data.imp[0].secure).to.deep.equal(secure);
expect(request).to.deep.equal({
'method': 'POST',
'url': 'https://prebid.mgid.com/prebid/1',
'data': '{\"site\":{\"domain\":\"' + domain + '\"},\"cur\":[\"USD\"],\"device\":{\"ua\":\"' + ua + '\",\"js\":1,\"dnt\":' + dnt + ',\"h\":' + screenHeight + ',\"w\":' + screenWidth + ',\"language\":\"' + lang + '\"},\"user\":{},\"regs\":{},\"ext\":{\"mgid_ver\":\"' + mgid_ver + '\",\"prebid_ver\":\"' + prebid_ver + '\"},\"imp\":[{\"tagid\":\"2\",\"secure\":' + secure + ',\"native\":{\"request\":{\"plcmtcnt\":1,\"assets\":[{\"id\":1,\"required\":1,\"title\":{\"len\":80}},{\"id\":2,\"required\":0,\"img\":{\"type\":3,\"w\":80,\"h\":80}},{\"id\":4,\"required\":0,\"data\":{\"type\":1}}]}}}]}',
'data': '{\"site\":{\"domain\":\"' + domain + '\"},\"cur\":[\"USD\"],\"device\":{\"ua\":\"' + ua + '\",\"js\":1,\"dnt\":' + dnt + ',\"h\":' + screenHeight + ',\"w\":' + screenWidth + ',\"language\":\"' + lang + '\"},\"user\":{},\"regs\":{},\"ext\":{\"mgid_ver\":\"' + mgid_ver + '\",\"prebid_ver\":\"' + prebid_ver + '\"},\"imp\":[{\"tagid\":\"2/div\",\"secure\":' + secure + ',\"native\":{\"request\":{\"plcmtcnt\":1,\"assets\":[{\"id\":1,\"required\":1,\"title\":{\"len\":80}},{\"id\":2,\"required\":0,\"img\":{\"type\":3,\"w\":80,\"h\":80}},{\"id\":4,\"required\":0,\"data\":{\"type\":1}}]}}}]}',
});
});
it('should return proper banner request', function () {
Expand All @@ -423,14 +486,14 @@ describe('Mgid bid adapter', function () {
expect(data.device.h).equal(screenHeight);
expect(data.device.w).equal(screenWidth);
expect(data.device.language).to.deep.equal(lang);
expect(data.imp[0].tagid).to.deep.equal('2');
expect(data.imp[0].tagid).to.deep.equal('2/div');
expect(data.imp[0].banner).to.deep.equal({w: 300, h: 600, format: [{w: 300, h: 600}, {w: 300, h: 250}]});
expect(data.imp[0].secure).to.deep.equal(secure);

expect(request).to.deep.equal({
'method': 'POST',
'url': 'https://prebid.mgid.com/prebid/1',
'data': '{\"site\":{\"domain\":\"' + domain + '\"},\"cur\":[\"USD\"],\"device\":{\"ua\":\"' + ua + '\",\"js\":1,\"dnt\":' + dnt + ',\"h\":' + screenHeight + ',\"w\":' + screenWidth + ',\"language\":\"' + lang + '\"},\"user\":{},\"regs\":{},\"ext\":{\"mgid_ver\":\"' + mgid_ver + '\",\"prebid_ver\":\"' + prebid_ver + '\"},\"imp\":[{\"tagid\":\"2\",\"secure\":' + secure + ',\"banner\":{\"w\":300,\"h\":600,\"format\":[{\"w\":300,\"h\":600},{\"w\":300,\"h\":250}]}}]}',
'data': '{\"site\":{\"domain\":\"' + domain + '\"},\"cur\":[\"USD\"],\"device\":{\"ua\":\"' + ua + '\",\"js\":1,\"dnt\":' + dnt + ',\"h\":' + screenHeight + ',\"w\":' + screenWidth + ',\"language\":\"' + lang + '\"},\"user\":{},\"regs\":{},\"ext\":{\"mgid_ver\":\"' + mgid_ver + '\",\"prebid_ver\":\"' + prebid_ver + '\"},\"imp\":[{\"tagid\":\"2/div\",\"secure\":' + secure + ',\"banner\":{\"w\":300,\"h\":600,\"format\":[{\"w\":300,\"h\":600},{\"w\":300,\"h\":250}]}}]}',
});
});
});
Expand Down Expand Up @@ -565,6 +628,11 @@ describe('Mgid bid adapter', function () {
});
});

describe('getUserSyncs', function () {
it('should do nothing on getUserSyncs', function () {
spec.getUserSyncs()
});
});
describe('on bidWon', function () {
it('should replace nurl and burl for native', function () {
const burl = 'burl&s=${' + 'AUCTION_PRICE}';
Expand Down

0 comments on commit e407775

Please sign in to comment.