From de0f0af10f65d7c616a59ac97703505807d2d80e Mon Sep 17 00:00:00 2001 From: gaudeamus Date: Fri, 13 Aug 2021 14:58:06 +0300 Subject: [PATCH 1/4] restore mgidBidAdapter.js with 5.x support --- modules/mgidBidAdapter.js | 613 +++++++++++++++++ test/spec/modules/mgidBidAdapter_spec.js | 801 +++++++++++++++++++++++ 2 files changed, 1414 insertions(+) create mode 100644 modules/mgidBidAdapter.js create mode 100644 test/spec/modules/mgidBidAdapter_spec.js diff --git a/modules/mgidBidAdapter.js b/modules/mgidBidAdapter.js new file mode 100644 index 00000000000..ff5212a6738 --- /dev/null +++ b/modules/mgidBidAdapter.js @@ -0,0 +1,613 @@ +import {registerBidder} from '../src/adapters/bidderFactory.js'; +import * as utils from '../src/utils.js'; +import {BANNER, NATIVE} from '../src/mediaTypes.js'; +import {config} from '../src/config.js'; +import { getStorageManager } from '../src/storageManager.js'; + +export const storage = getStorageManager(); +const DEFAULT_CUR = 'USD'; +const BIDDER_CODE = 'mgid'; +const ENDPOINT_URL = 'https://prebid.mgid.com/prebid/'; +const LOG_WARN_PREFIX = '[MGID warn]: '; +const LOG_INFO_PREFIX = '[MGID info]: '; +const NATIVE_ASSETS = { + 'TITLE': { ID: 1, KEY: 'title', TYPE: 0 }, + 'IMAGE': { ID: 2, KEY: 'image', TYPE: 0 }, + 'ICON': { ID: 3, KEY: 'icon', TYPE: 0 }, + 'SPONSOREDBY': { ID: 4, KEY: 'sponsoredBy', TYPE: 1 }, // please note that type of SPONSORED is also 1 + 'DESC': { ID: 5, KEY: 'data', TYPE: 2 }, // please note that type of BODY is also set to 2 + 'PRICE': { ID: 6, KEY: 'price', TYPE: 6 }, + 'SALEPRICE': { ID: 7, KEY: 'saleprice', TYPE: 7 }, + 'DISPLAYURL': { ID: 8, KEY: 'displayurl', TYPE: 11 }, + 'CTA': { ID: 9, KEY: 'cta', TYPE: 12 }, + 'BODY': { ID: 10, KEY: 'body', TYPE: 2 }, // please note that type of DESC is also set to 2 + 'SPONSORED': { ID: 11, KEY: 'sponsored', TYPE: 1 }, // please note that type of SPONSOREDBY is also set to 1 +}; +const NATIVE_ASSET_IMAGE_TYPE = { + 'ICON': 1, + 'IMAGE': 3 +}; +const DEFAULT_IMAGE_WIDTH = 492; +const DEFAULT_IMAGE_HEIGHT = 328; +const DEFAULT_ICON_WIDTH = 50; +const DEFAULT_ICON_HEIGHT = 50; +const DEFAULT_TITLE_LENGTH = 80; + +let isInvalidNativeRequest = false; + +// check if title, image can be added with mandatory field default values +const NATIVE_MINIMUM_REQUIRED_IMAGE_ASSETS = [ + { + id: NATIVE_ASSETS.SPONSOREDBY.ID, + required: true, + data: { + type: 1 + } + }, + { + id: NATIVE_ASSETS.TITLE.ID, + required: true, + }, + { + id: NATIVE_ASSETS.IMAGE.ID, + required: true, + } +]; +let _NATIVE_ASSET_ID_TO_KEY_MAP = {}; +let _NATIVE_ASSET_KEY_TO_ASSET_MAP = {}; + +// loading _NATIVE_ASSET_ID_TO_KEY_MAP +utils._each(NATIVE_ASSETS, anAsset => { _NATIVE_ASSET_ID_TO_KEY_MAP[anAsset.ID] = anAsset.KEY }); +// loading _NATIVE_ASSET_KEY_TO_ASSET_MAP +utils._each(NATIVE_ASSETS, anAsset => { _NATIVE_ASSET_KEY_TO_ASSET_MAP[anAsset.KEY] = anAsset }); + +export const spec = { + VERSION: '1.5', + code: BIDDER_CODE, + supportedMediaTypes: [BANNER, NATIVE], + 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, + /** + * Determines whether or not the given bid request is valid. + * + * @param {BidRequest} bid The bid params to validate. + * @return boolean True if this is a valid bid, and false otherwise. + */ + isBidRequestValid: (bid) => { + const banner = utils.deepAccess(bid, 'mediaTypes.banner'); + const native = utils.deepAccess(bid, 'mediaTypes.native'); + let nativeOk = utils.isPlainObject(native); + if (nativeOk) { + const nativeParams = utils.deepAccess(bid, 'nativeParams'); + let assetsCount = 0; + if (utils.isPlainObject(nativeParams)) { + for (let k in nativeParams) { + let v = nativeParams[k]; + const supportProp = spec.NATIVE_ASSET_KEY_TO_ASSET_MAP.hasOwnProperty(k); + if (supportProp) { + assetsCount++ + } + if (!utils.isPlainObject(v) || (!supportProp && utils.deepAccess(v, 'required'))) { + nativeOk = false; + break; + } + } + } + nativeOk = nativeOk && (assetsCount > 0); + } + let bannerOk = utils.isPlainObject(banner); + if (bannerOk) { + const sizes = utils.deepAccess(banner, 'sizes'); + bannerOk = utils.isArray(sizes) && sizes.length > 0; + for (let f = 0; bannerOk && f < sizes.length; f++) { + bannerOk = sizes[f].length === 2; + } + } + 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. + * + * @param {validBidRequests[]} - an array of bids + * @return ServerRequest Info describing the request to the server. + */ + buildRequests: (validBidRequests, bidderRequest) => { + utils.logInfo(LOG_INFO_PREFIX + `buildRequests`); + if (validBidRequests.length === 0) { + return; + } + const info = pageInfo(); + const page = info.location || utils.deepAccess(bidderRequest, 'refererInfo.referer') || utils.deepAccess(bidderRequest, 'refererInfo.canonicalUrl'); + const hostname = utils.parseUrl(page).hostname; + let domain = extractDomainFromHost(hostname) || hostname; + const accountId = setOnAny(validBidRequests, 'params.accountId'); + const muid = getLocalStorageSafely('mgMuidn'); + let url = (setOnAny(validBidRequests, 'params.bidUrl') || ENDPOINT_URL) + accountId; + if (utils.isStr(muid) && muid.length > 0) { + url += '?muid=' + muid; + } + const cur = setOnAny(validBidRequests, 'params.currency') || setOnAny(validBidRequests, 'params.cur') || config.getConfig('currency.adServerCurrency') || DEFAULT_CUR; + 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, + secure, + }; + const floorData = getBidFloor(bid, cur); + if (floorData.floor) { + impObj.bidfloor = floorData.floor; + } + if (floorData.cur) { + impObj.bidfloorcur = floorData.cur + } + for (let mediaTypes in bid.mediaTypes) { + switch (mediaTypes) { + case BANNER: + impObj.banner = createBannerRequest(bid); + imp.push(impObj); + break; + case NATIVE: + const native = createNativeRequest(bid.nativeParams); + if (!isInvalidNativeRequest) { + impObj.native = { + 'request': native + }; + imp.push(impObj); + } + break; + } + } + }); + + if (imp.length === 0) { + return; + } + + let request = { + id: utils.deepAccess(bidderRequest, 'bidderRequestId'), + site: {domain, page}, + cur: [cur], + geo: {utcoffset: info.timeOffset}, + device: { + ua: navigator.userAgent, + js: 1, + dnt: (navigator.doNotTrack === 'yes' || navigator.doNotTrack === '1' || navigator.msDoNotTrack === '1') ? 1 : 0, + h: screen.height, + w: screen.width, + language: getLanguage() + }, + ext: {mgid_ver: spec.VERSION, prebid_ver: '$prebid.version$'}, + imp + }; + if (bidderRequest && bidderRequest.gdprConsent) { + request.user = {ext: {consent: bidderRequest.gdprConsent.consentString}}; + request.regs = {ext: {gdpr: (bidderRequest.gdprConsent.gdprApplies ? 1 : 0)}} + } + if (info.referrer) { + request.site.ref = info.referrer + } + utils.logInfo(LOG_INFO_PREFIX + `buildRequest:`, request); + return { + method: 'POST', + url: url, + data: JSON.stringify(request), + }; + }, + /** + * Unpack the response from the server into a list of bids. + * + * @param {ServerResponse} serverResponse A successful response from the server. + * @return {Bid[]} An array of bids which were nested inside the server. + */ + interpretResponse: (serverResponse, bidRequests) => { + utils.logInfo(LOG_INFO_PREFIX + `interpretResponse`, serverResponse); + if (serverResponse == null || serverResponse.body == null || serverResponse.body === '' || !utils.isArray(serverResponse.body.seatbid) || !serverResponse.body.seatbid.length) { + return; + } + const returnedBids = []; + const muidn = utils.deepAccess(serverResponse.body, 'ext.muidn') + if (utils.isStr(muidn) && muidn.length > 0) { + setLocalStorageSafely('mgMuidn', muidn) + } + serverResponse.body.seatbid.forEach((bids) => { + bids.bid.forEach((bid) => { + const pbid = prebidBid(bid, serverResponse.body.cur); + if (pbid.mediaType === NATIVE && utils.isEmpty(pbid.native)) { + return; + } + returnedBids.push(pbid); + }) + }); + + utils.logInfo(LOG_INFO_PREFIX + `interpretedResponse`, returnedBids); + return returnedBids; + }, + onBidWon: (bid) => { + const cpm = utils.deepAccess(bid, 'adserverTargeting.hb_pb') || ''; + if (utils.isStr(bid.nurl) && bid.nurl !== '') { + bid.nurl = bid.nurl.replace( + /\${AUCTION_PRICE}/, + cpm + ); + utils.triggerPixel(bid.nurl); + } + if (bid.isBurl) { + if (bid.mediaType === BANNER) { + bid.ad = bid.ad.replace( + /\${AUCTION_PRICE}/, + cpm + ) + } else { + bid.burl = bid.burl.replace( + /\${AUCTION_PRICE}/, + cpm + ); + utils.triggerPixel(bid.burl); + } + } + utils.logInfo(LOG_INFO_PREFIX + `onBidWon`); + }, + getUserSyncs: (syncOptions, serverResponses) => { + utils.logInfo(LOG_INFO_PREFIX + `getUserSyncs`); + } +}; + +registerBidder(spec); + +function setOnAny(collection, key) { + for (let i = 0, result; i < collection.length; i++) { + result = utils.deepAccess(collection[i], key); + if (result) { + return result; + } + } +} + +/** + * Unpack the Server's Bid into a Prebid-compatible one. + * @param serverBid + * @return Bid + */ +function prebidBid(serverBid, cur) { + if (!utils.isStr(cur) || cur === '') { + cur = DEFAULT_CUR; + } + const bid = { + requestId: serverBid.impid, + ad: serverBid.adm, + cpm: serverBid.price, + creativeId: serverBid.adid, + currency: cur, + dealId: serverBid.dealid || '', + width: serverBid.w, + height: serverBid.h, + mediaType: 'banner', + netRevenue: true, + ttl: serverBid.ttl || 300, + nurl: serverBid.nurl || '', + burl: serverBid.burl || '', + isBurl: utils.isStr(serverBid.burl) && serverBid.burl.length > 0, + meta: { advertiserDomains: (utils.isArray(serverBid.adomain) && serverBid.adomain.length > 0 ? serverBid.adomain : []) }, + }; + setMediaType(serverBid, bid); + switch (bid.mediaType) { + case BANNER: + break; + case NATIVE: + parseNativeResponse(serverBid, bid); + break; + } + return bid; +} + +function setMediaType(bid, newBid) { + if (utils.deepAccess(bid, 'ext.crtype') === 'native') { + newBid.mediaType = NATIVE; + } else { + newBid.mediaType = BANNER; + } +} + +function extractDomainFromHost(pageHost) { + if (pageHost == 'localhost') { + return 'localhost' + } + let domain = null; + try { + let domains = /[-\w]+\.([-\w]+|[-\w]{3,}|[-\w]{1,3}\.[-\w]{2})$/i.exec(pageHost); + if (domains != null && domains.length > 0) { + domain = domains[0]; + for (let i = 1; i < domains.length; i++) { + if (domains[i].length > domain.length) { + domain = domains[i]; + } + } + } + } catch (e) { + domain = null; + } + return domain; +} + +function getLanguage() { + const language = navigator.language ? 'language' : 'userLanguage'; + const lang2 = navigator[language].split('-')[0]; + if (lang2.length === 2 || lang2.length === 3) { + return lang2; + } + return ''; +} + +function getLocalStorageSafely(key) { + try { + return storage.getDataFromLocalStorage(key); + } catch (e) { + return null; + } +} + +function setLocalStorageSafely(key, val) { + try { + return storage.setDataInLocalStorage(key, val); + } catch (e) { + return null; + } +} + +function createBannerRequest(bid) { + const sizes = utils.deepAccess(bid, 'mediaTypes.banner.sizes'); + let format = []; + if (sizes.length > 1) { + for (let f = 0; f < sizes.length; f++) { + if (sizes[f].length === 2) { + format.push({w: sizes[f][0], h: sizes[f][1]}); + } + } + } + let r = { + w: sizes && sizes[0][0], + h: sizes && sizes[0][1], + }; + if (format.length) { + r.format = format + } + const pos = utils.getBidIdParameter('position', bid.params) || 0 + if (pos) { + r.pos = pos + } + return r +} + +function createNativeRequest(params) { + let nativeRequestObject = { + plcmtcnt: 1, + assets: [] + }; + for (let key in params) { + let assetObj = {}; + if (params.hasOwnProperty(key)) { + if (!(nativeRequestObject.assets && nativeRequestObject.assets.length > 0 && nativeRequestObject.assets.hasOwnProperty(key))) { + switch (key) { + case NATIVE_ASSETS.TITLE.KEY: + assetObj = { + id: NATIVE_ASSETS.TITLE.ID, + required: params[key].required ? 1 : 0, + title: { + len: params[key].len || params[key].length || DEFAULT_TITLE_LENGTH + } + }; + break; + case NATIVE_ASSETS.IMAGE.KEY: + const wmin = params[key].wmin || params[key].minimumWidth || (utils.isArray(params[key].minsizes) && params[key].minsizes.length > 0 ? params[key].minsizes[0] : 0); + const hmin = params[key].hmin || params[key].minimumHeight || (utils.isArray(params[key].minsizes) && params[key].minsizes.length > 1 ? params[key].minsizes[1] : 0); + assetObj = { + id: NATIVE_ASSETS.IMAGE.ID, + required: params[key].required ? 1 : 0, + img: { + type: NATIVE_ASSET_IMAGE_TYPE.IMAGE, + w: params[key].w || params[key].width || (utils.isArray(params[key].sizes) && params[key].sizes.length > 0 ? params[key].sizes[0] : 0), + h: params[key].h || params[key].height || (utils.isArray(params[key].sizes) && params[key].sizes.length > 1 ? params[key].sizes[1] : 0), + mimes: params[key].mimes, + ext: params[key].ext, + } + }; + if (wmin > 0) { + assetObj.img.wmin = wmin; + } + if (hmin > 0) { + assetObj.img.hmin = hmin; + } + if (!assetObj.img.w) { + assetObj.img.w = DEFAULT_IMAGE_WIDTH; + } + if (!assetObj.img.h) { + assetObj.img.h = DEFAULT_IMAGE_HEIGHT; + } + break; + case NATIVE_ASSETS.ICON.KEY: + assetObj = { + id: NATIVE_ASSETS.ICON.ID, + required: params[key].required ? 1 : 0, + img: { + type: NATIVE_ASSET_IMAGE_TYPE.ICON, + w: params[key].w || params[key].width || (utils.isArray(params[key].sizes) && params[key].sizes.length > 0 ? params[key].sizes[0] : 0), + h: params[key].h || params[key].height || (utils.isArray(params[key].sizes) && params[key].sizes.length > 0 ? params[key].sizes[1] : 0), + } + }; + if (!assetObj.img.w) { + assetObj.img.w = DEFAULT_ICON_WIDTH; + } + if (!assetObj.img.h) { + assetObj.img.h = DEFAULT_ICON_HEIGHT; + } + break; + case NATIVE_ASSETS.SPONSORED.KEY: + case NATIVE_ASSETS.SPONSOREDBY.KEY: + case NATIVE_ASSETS.PRICE.KEY: + case NATIVE_ASSETS.SALEPRICE.KEY: + case NATIVE_ASSETS.DESC.KEY: + case NATIVE_ASSETS.BODY.KEY: + case NATIVE_ASSETS.DISPLAYURL.KEY: + case NATIVE_ASSETS.CTA.KEY: + assetObj = commonNativeRequestObject(spec.NATIVE_ASSET_KEY_TO_ASSET_MAP[key], params); + break; + default: + if (params[key].required) { + isInvalidNativeRequest = true; + return; + } + } + } + } + if (assetObj.id) { + nativeRequestObject.assets[nativeRequestObject.assets.length] = assetObj; + } + } + + // for native image adtype prebid has to have few required assests i.e. title,sponsoredBy, image + // if any of these are missing from the request then request will not be sent + let requiredAssetCount = NATIVE_MINIMUM_REQUIRED_IMAGE_ASSETS.length; + let presentrequiredAssetCount = 0; + NATIVE_MINIMUM_REQUIRED_IMAGE_ASSETS.forEach(ele => { + let lengthOfExistingAssets = nativeRequestObject.assets.length; + for (let i = 0; i < lengthOfExistingAssets; i++) { + if (ele.id === nativeRequestObject.assets[i].id) { + presentrequiredAssetCount++; + break; + } else { + if (ele.id === 4 && nativeRequestObject.assets[i].id === 11) { + if (utils.deepAccess(nativeRequestObject.assets[i], 'data.type') === ele.data.type) { + presentrequiredAssetCount++; + break; + } + } + } + } + }); + isInvalidNativeRequest = requiredAssetCount !== presentrequiredAssetCount; + return nativeRequestObject; +} + +function commonNativeRequestObject(nativeAsset, params) { + const key = nativeAsset.KEY; + return { + id: nativeAsset.ID, + required: params[key].required ? 1 : 0, + data: { + type: nativeAsset.TYPE, + len: params[key].len, + ext: params[key].ext + } + }; +} + +function parseNativeResponse(bid, newBid) { + newBid.native = {}; + if (bid.hasOwnProperty('adm')) { + let adm = ''; + try { + adm = JSON.parse(bid.adm); + } catch (ex) { + utils.logWarn(LOG_WARN_PREFIX + 'Error: Cannot parse native response for ad response: ' + newBid.adm); + return; + } + if (adm && adm.native && adm.native.assets && adm.native.assets.length > 0) { + newBid.mediaType = NATIVE; + for (let i = 0, len = adm.native.assets.length; i < len; i++) { + switch (adm.native.assets[i].id) { + case NATIVE_ASSETS.TITLE.ID: + newBid.native.title = adm.native.assets[i].title && adm.native.assets[i].title.text; + break; + case NATIVE_ASSETS.IMAGE.ID: + newBid.native.image = { + url: adm.native.assets[i].img && adm.native.assets[i].img.url, + height: adm.native.assets[i].img && adm.native.assets[i].img.h, + width: adm.native.assets[i].img && adm.native.assets[i].img.w, + }; + break; + case NATIVE_ASSETS.ICON.ID: + newBid.native.icon = { + url: adm.native.assets[i].img && adm.native.assets[i].img.url, + height: adm.native.assets[i].img && adm.native.assets[i].img.h, + width: adm.native.assets[i].img && adm.native.assets[i].img.w, + }; + break; + case NATIVE_ASSETS.SPONSOREDBY.ID: + case NATIVE_ASSETS.SPONSORED.ID: + case NATIVE_ASSETS.PRICE: + case NATIVE_ASSETS.SALEPRICE.ID: + case NATIVE_ASSETS.DESC.ID: + case NATIVE_ASSETS.BODY.ID: + case NATIVE_ASSETS.DISPLAYURL.ID: + case NATIVE_ASSETS.CTA.ID: + newBid.native[spec.NATIVE_ASSET_ID_TO_KEY_MAP[adm.native.assets[i].id]] = adm.native.assets[i].data && adm.native.assets[i].data.value; + break; + } + } + newBid.native.clickUrl = adm.native.link && adm.native.link.url; + newBid.native.clickTrackers = (adm.native.link && adm.native.link.clicktrackers) || []; + newBid.native.impressionTrackers = adm.native.imptrackers || []; + newBid.native.jstracker = adm.native.jstracker || []; + newBid.width = 0; + newBid.height = 0; + } + } +} + +function pageInfo() { + var w, d, l, r, m, p, t; + for (w = window, d = w.document, l = d.location.href, r = d.referrer, m = 0, t = new Date(); w !== w.parent;) { + try { + p = w.parent; l = p.location.href; r = p.document.referrer; w = p; + } catch (e) { + m = top !== w.parent ? 2 : 1; + break + } + } + return { + location: l, + referrer: r || '', + masked: m, + wWidth: w.innerWidth, + wHeight: w.innerHeight, + date: t.toUTCString(), + timeOffset: t.getTimezoneOffset() + }; +} + +/** + * Get the floor price from bid.params for backward compatibility. + * If not found, then check floor module. + * @param bid A valid bid object + * @returns {*|number} floor price + */ +function getBidFloor(bid, cur) { + let bidFloor = utils.getBidIdParameter('bidfloor', bid.params) || utils.getBidIdParameter('bidFloor', bid.params) || 0; + const reqCur = cur + + if (!bidFloor && utils.isFn(bid.getFloor)) { + const floorObj = bid.getFloor({ + currency: '*', + mediaType: '*', + size: '*' + }); + if (utils.isPlainObject(floorObj) && utils.isNumber(floorObj.floor)) { + if (cur !== floorObj.currency) { + cur = floorObj.currency + } + bidFloor = floorObj.floor; + } + } + if (reqCur === cur) { + cur = '' + } + return {floor: bidFloor, cur: cur} +} diff --git a/test/spec/modules/mgidBidAdapter_spec.js b/test/spec/modules/mgidBidAdapter_spec.js new file mode 100644 index 00000000000..019b0c521ac --- /dev/null +++ b/test/spec/modules/mgidBidAdapter_spec.js @@ -0,0 +1,801 @@ +import {assert, expect} from 'chai'; +import { spec, storage } from 'modules/mgidBidAdapter.js'; +import { version } from 'package.json'; +import * as utils from '../../../src/utils.js'; + +describe('Mgid bid adapter', function () { + let sandbox; + let logErrorSpy; + let logWarnSpy; + beforeEach(function () { + sandbox = sinon.sandbox.create(); + logErrorSpy = sinon.spy(utils, 'logError'); + logWarnSpy = sinon.spy(utils, 'logWarn'); + }); + + afterEach(function () { + sandbox.restore(); + utils.logError.restore(); + utils.logWarn.restore(); + }); + const ua = navigator.userAgent; + const screenHeight = screen.height; + const screenWidth = screen.width; + const dnt = (navigator.doNotTrack == 'yes' || navigator.doNotTrack == '1' || navigator.msDoNotTrack == '1') ? 1 : 0; + const language = navigator.language ? 'language' : 'userLanguage'; + let lang = navigator[language].split('-')[0]; + if (lang.length != 2 && lang.length != 3) { + lang = ''; + } + const secure = window.location.protocol === 'https:' ? 1 : 0; + const mgid_ver = spec.VERSION; + const utcOffset = (new Date()).getTimezoneOffset().toString(); + + describe('isBidRequestValid', function () { + let bid = { + 'adUnitCode': 'div', + 'bidder': 'mgid', + 'params': { + 'property': '10433394', + 'zone': 'zone' + }, + }; + + it('should not accept bid without required params', function () { + let isValid = spec.isBidRequestValid(bid); + expect(isValid).to.equal(false); + }); + + it('should return false when params are not passed', function () { + let bid = Object.assign({}, bid); + delete bid.params; + bid.params = {}; + expect(spec.isBidRequestValid(bid)).to.equal(false); + }); + + it('should return false when valid params are not passed', function () { + let bid = Object.assign({}, bid); + delete bid.params; + bid.params = {accountId: '', placementId: ''}; + expect(spec.isBidRequestValid(bid)).to.equal(false); + }); + + 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]] + } + }; + bid.params = {accountId: 2, placementId: 1}; + 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; + bid.mediaTypes = { + native: { + sizes: [[300, 250]] + } + }; + bid.params = {accountId: '0', placementId: '00'}; + expect(spec.isBidRequestValid(bid)).to.equal(false); + }); + + it('should return false when valid mediaTypes are not passed', function () { + let bid = Object.assign({}, bid); + delete bid.params; + bid.params = {accountId: '1', placementId: '1'}; + expect(spec.isBidRequestValid(bid)).to.equal(false); + }); + + it('should return false when valid mediaTypes.banner are not passed', function () { + let bid = Object.assign({}, bid); + delete bid.params; + bid.params = {accountId: '1', placementId: '1'}; + bid.mediaTypes = { + }; + expect(spec.isBidRequestValid(bid)).to.equal(false); + }); + + it('should return false when valid mediaTypes.banner.sizes are not passed', function () { + let bid = Object.assign({}, bid); + delete bid.params; + bid.params = {accountId: '1', placementId: '1'}; + bid.mediaTypes = { + sizes: [] + }; + expect(spec.isBidRequestValid(bid)).to.equal(false); + }); + + it('should return false when valid mediaTypes.banner.sizes are not valid', function () { + let bid = Object.assign({}, bid); + delete bid.params; + bid.params = {accountId: '1', placementId: '1'}; + bid.mediaTypes = { + sizes: [300, 250] + }; + expect(spec.isBidRequestValid(bid)).to.equal(false); + }); + + 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: { + sizes: [[300, 250]] + } + }; + expect(spec.isBidRequestValid(bid)).to.equal(true); + }); + + it('should return false when valid mediaTypes.native is not object', function () { + let bid = Object.assign({}, bid); + bid.params = {accountId: '1', placementId: '1'}; + bid.mediaTypes = { + native: [] + }; + expect(spec.isBidRequestValid(bid)).to.equal(false); + }); + + it('should return false when mediaTypes.native is empty object', function () { + let bid = Object.assign({}, bid); + delete bid.params; + bid.params = {accountId: '1', placementId: '1'}; + bid.mediaTypes = { + native: {} + }; + expect(spec.isBidRequestValid(bid)).to.equal(false); + }); + + it('should return false when mediaTypes.native is invalid object', function () { + let bid = Object.assign({}, bid); + delete bid.params; + bid.params = {accountId: '1', placementId: '1'}; + bid.mediaTypes = { + native: { + image: { + sizes: [80, 80] + }, + } + }; + expect(spec.isBidRequestValid(bid)).to.equal(false); + }); + + it('should return false when mediaTypes.native has unsupported required asset', function () { + let bid = Object.assign({}, bid); + bid.params = {accountId: '2', placementId: '1'}; + bid.mediaTypes = { + native: { + title: {required: true}, + image: {required: false, sizes: [80, 80]}, + sponsored: {required: false}, + }, + }; + bid.nativeParams = { + title: {required: true}, + image: {required: false, sizes: [80, 80]}, + sponsored: {required: false}, + unsupported: {required: true}, + }; + expect(spec.isBidRequestValid(bid)).to.equal(false); + }); + + 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: { + title: {required: true}, + image: {required: false, sizes: [80, 80]}, + sponsored: {required: false}, + }, + }; + bid.nativeParams = { + title: {required: true}, + image: {required: false, sizes: [80, 80]}, + sponsored: {required: false}, + }; + expect(spec.isBidRequestValid(bid)).to.equal(true); + }); + }); + + describe('override defaults', function () { + let bid = { + bidder: 'mgid', + params: { + accountId: '1', + placementId: '2', + }, + }; + it('should return object', function () { + let bid = Object.assign({}, bid); + bid.mediaTypes = { + banner: { + sizes: [[300, 250]] + } + }; + let bidRequests = [bid]; + const request = spec.buildRequests(bidRequests); + expect(request).to.exist.and.to.be.a('object'); + }); + + it('should return overwrite default bidurl', function () { + let bid = Object.assign({}, bid); + bid.params = { + bidUrl: 'https://newbidurl.com/', + accountId: '1', + placementId: '2', + }; + bid.mediaTypes = { + banner: { + sizes: [[300, 250]] + } + }; + let bidRequests = [bid]; + const request = spec.buildRequests(bidRequests); + expect(request.url).to.include('https://newbidurl.com/1'); + }); + it('should return overwrite default bidFloor', function () { + let bid = Object.assign({}, bid); + bid.params = { + bidFloor: 1.1, + accountId: '1', + placementId: '2', + }; + bid.mediaTypes = { + banner: { + sizes: [[300, 250]] + } + }; + let bidRequests = [bid]; + const request = spec.buildRequests(bidRequests); + expect(request.data).to.be.a('string'); + const data = JSON.parse(request.data); + expect(data).to.be.a('object'); + expect(data.imp).to.be.a('array'); + expect(data.imp).to.have.lengthOf(1); + expect(data.imp[0].bidfloor).to.deep.equal(1.1); + }); + it('should return overwrite default currency', function () { + let bid = Object.assign({}, bid); + bid.params = { + cur: 'GBP', + accountId: '1', + placementId: '2', + }; + bid.mediaTypes = { + banner: { + sizes: [[300, 250]] + } + }; + let bidRequests = [bid]; + const request = spec.buildRequests(bidRequests); + expect(request.data).to.be.a('string'); + const data = JSON.parse(request.data); + expect(data).to.be.a('object'); + expect(data.cur).to.deep.equal(['GBP']); + }); + }); + + describe('buildRequests', function () { + let abid = { + adUnitCode: 'div', + bidder: 'mgid', + params: { + accountId: '1', + placementId: '2', + }, + }; + + it('should return undefined if no validBidRequests passed', function () { + expect(spec.buildRequests([])).to.be.undefined; + }); + it('should return request url with muid', function () { + let getDataFromLocalStorageStub = sinon.stub(storage, 'getDataFromLocalStorage'); + getDataFromLocalStorageStub.withArgs('mgMuidn').returns('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'); + + getDataFromLocalStorageStub.restore(); + }); + 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 = { + banner: { + sizes: [[300, 250]] + } + }; + let bidRequests = [bid]; + const page = top.location.href; + const domain = utils.parseUrl(page).hostname; + const request = spec.buildRequests(bidRequests); + 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.site.domain).to.deep.equal(domain); + expect(data.site.page).to.deep.equal(page); + expect(data.cur).to.deep.equal(['USD']); + expect(data.device.ua).to.deep.equal(ua); + expect(data.device.dnt).equal(dnt); + 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/div'); + expect(data.imp[0].banner).to.deep.equal({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 + '","page":"' + page + '"},"cur":["USD"],"geo":{"utcoffset":' + utcOffset + '},"device":{"ua":"' + ua + '","js":1,"dnt":' + dnt + ',"h":' + screenHeight + ',"w":' + screenWidth + ',"language":"' + lang + '"},"ext":{"mgid_ver":"' + mgid_ver + '","prebid_ver":"' + version + '"},"imp":[{"tagid":"2/div","secure":' + secure + ',"banner":{"w":300,"h":250}}]}', + }); + }); + it('should not return native imp if minimum asset list not requested', function () { + let bid = Object.assign({}, abid); + bid.mediaTypes = { + native: '', + }; + bid.nativeParams = { + title: {required: true}, + image: {sizes: [80, 80]}, + }; + let bidRequests = [bid]; + const request = spec.buildRequests(bidRequests); + expect(request).to.be.undefined; + }); + it('should return proper native imp', function () { + let bid = Object.assign({}, abid); + bid.mediaTypes = { + native: '', + }; + bid.nativeParams = { + title: {required: true}, + image: {sizes: [80, 80]}, + sponsored: { }, + }; + + let bidRequests = [bid]; + const page = top.location.href; + const domain = utils.parseUrl(page).hostname; + const request = spec.buildRequests(bidRequests); + expect(request).to.be.a('object'); + 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.site.domain).to.deep.equal(domain); + expect(data.site.page).to.deep.equal(page); + expect(data.cur).to.deep.equal(['USD']); + expect(data.device.ua).to.deep.equal(ua); + expect(data.device.dnt).equal(dnt); + 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/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 + '","page":"' + page + '"},"cur":["USD"],"geo":{"utcoffset":' + utcOffset + '},"device":{"ua":"' + ua + '","js":1,"dnt":' + dnt + ',"h":' + screenHeight + ',"w":' + screenWidth + ',"language":"' + lang + '"},"ext":{"mgid_ver":"' + mgid_ver + '","prebid_ver":"' + version + '"},"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 image altered', function () { + let bid = Object.assign({}, abid); + bid.mediaTypes = { + native: '', + }; + bid.nativeParams = { + title: {required: true}, + image: {wmin: 50, hmin: 50, required: true}, + icon: {}, + sponsored: { }, + }; + + let bidRequests = [bid]; + const page = top.location.href; + const domain = utils.parseUrl(page).hostname; + const request = spec.buildRequests(bidRequests); + expect(request).to.be.a('object'); + 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.site.domain).to.deep.equal(domain); + expect(data.site.page).to.deep.equal(page); + expect(data.cur).to.deep.equal(['USD']); + expect(data.device.ua).to.deep.equal(ua); + expect(data.device.dnt).equal(dnt); + 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/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': 328, hmin: 50, 'type': 3, 'w': 492, wmin: 50}, 'required': 1}, {'id': 3, 'img': {'h': 50, 'type': 1, 'w': 50}, '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 + '","page":"' + page + '"},"cur":["USD"],"geo":{"utcoffset":' + utcOffset + '},"device":{"ua":"' + ua + '","js":1,"dnt":' + dnt + ',"h":' + screenHeight + ',"w":' + screenWidth + ',"language":"' + lang + '"},"ext":{"mgid_ver":"' + mgid_ver + '","prebid_ver":"' + version + '"},"imp":[{"tagid":"2/div","secure":' + secure + ',"native":{"request":{"plcmtcnt":1,"assets":[{"id":1,"required":1,"title":{"len":80}},{"id":2,"required":1,"img":{"type":3,"w":492,"h":328,"wmin":50,"hmin":50}},{"id":3,"required":0,"img":{"type":1,"w":50,"h":50}},{"id":11,"required":0,"data":{"type":1}}]}}}]}', + }); + }); + it('should return proper native imp with sponsoredBy', function () { + let bid = Object.assign({}, abid); + bid.mediaTypes = { + native: '', + }; + bid.nativeParams = { + title: {required: true}, + image: {sizes: [80, 80]}, + sponsoredBy: { }, + }; + + let bidRequests = [bid]; + const page = top.location.href; + const domain = utils.parseUrl(page).hostname; + const request = spec.buildRequests(bidRequests); + expect(request).to.be.a('object'); + 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.site.domain).to.deep.equal(domain); + expect(data.site.page).to.deep.equal(page); + expect(data.cur).to.deep.equal(['USD']); + expect(data.device.ua).to.deep.equal(ua); + expect(data.device.dnt).equal(dnt); + 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/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 + '","page":"' + page + '"},"cur":["USD"],"geo":{"utcoffset":' + utcOffset + '},"device":{"ua":"' + ua + '","js":1,"dnt":' + dnt + ',"h":' + screenHeight + ',"w":' + screenWidth + ',"language":"' + lang + '"},"ext":{"mgid_ver":"' + mgid_ver + '","prebid_ver":"' + version + '"},"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 () { + let bid = Object.assign({}, abid); + bid.mediaTypes = { + banner: { + sizes: [[300, 600], [300, 250]], + }, + }; + bid.params.position = 1; + let bidRequests = [bid]; + const request = spec.buildRequests(bidRequests); + + const page = top.location.href; + const domain = utils.parseUrl(page).hostname; + 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.site.domain).to.deep.equal(domain); + expect(data.site.page).to.deep.equal(page); + expect(data.cur).to.deep.equal(['USD']); + expect(data.device.ua).to.deep.equal(ua); + expect(data.device.dnt).equal(dnt); + 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/div'); + expect(data.imp[0].banner).to.deep.equal({w: 300, h: 600, format: [{w: 300, h: 600}, {w: 300, h: 250}], pos: 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 + '","page":"' + page + '"},"cur":["USD"],"geo":{"utcoffset":' + utcOffset + '},"device":{"ua":"' + ua + '","js":1,"dnt":' + dnt + ',"h":' + screenHeight + ',"w":' + screenWidth + ',"language":"' + lang + '"},"ext":{"mgid_ver":"' + mgid_ver + '","prebid_ver":"' + version + '"},"imp":[{"tagid":"2/div","secure":' + secure + ',"banner":{"w":300,"h":600,"format":[{"w":300,"h":600},{"w":300,"h":250}],"pos":1}}]}', + }); + }); + }); + + describe('interpretResponse', function () { + it('should not push proper native bid response if adm is missing', function () { + let resp = { + body: {'id': '57c0c2b1b732ca', 'bidid': '57c0c2b1b732ca', 'cur': 'GBP', 'seatbid': [{'bid': [{'price': 1.5, 'h': 600, 'w': 300, 'id': '1', 'impid': '61e40632c53fc2', 'adid': '2898532/2419121/2592854/2499195', 'nurl': 'https nurl', 'burl': 'https burl', 'cid': '44082', 'crid': '2898532/2419121/2592854/2499195', 'cat': ['IAB7', 'IAB14', 'IAB18-3', 'IAB1-2'], 'ext': {'place': 0, 'crtype': 'native'}, 'adomain': ['test.com']}], 'seat': '44082'}]} + }; + let bids = spec.interpretResponse(resp); + expect(bids).to.deep.equal([]) + }); + it('should not push proper native bid response if assets is empty', function () { + let resp = { + body: {'id': '57c0c2b1b732ca', 'bidid': '57c0c2b1b732ca', 'cur': 'GBP', 'seatbid': [{'bid': [{'price': 1.5, 'h': 600, 'w': 300, 'id': '1', 'impid': '61e40632c53fc2', 'adid': '2898532/2419121/2592854/2499195', 'nurl': 'https nurl', 'burl': 'https burl', 'adm': '{"native":{"ver":"1.1","link":{"url":"link_url"},"assets":[],"imptrackers":["imptrackers1"]}}', 'cid': '44082', 'crid': '2898532/2419121/2592854/2499195', 'cat': ['IAB7', 'IAB14', 'IAB18-3', 'IAB1-2'], 'ext': {'place': 0, 'crtype': 'native'}, 'adomain': ['test.com']}], 'seat': '44082'}]} + }; + let bids = spec.interpretResponse(resp); + expect(bids).to.deep.equal([]) + }); + it('should push proper native bid response, assets1', function () { + let resp = { + body: {'id': '57c0c2b1b732ca', 'bidid': '57c0c2b1b732ca', 'cur': 'GBP', 'seatbid': [{'bid': [{'price': 1.5, 'h': 600, 'w': 300, 'id': '1', 'impid': '61e40632c53fc2', 'adid': '2898532/2419121/2592854/2499195', 'nurl': 'https nurl', 'burl': 'https burl', 'adm': '{"native":{"ver":"1.1","link":{"url":"link_url"},"assets":[{"id":1,"required":0,"title":{"text":"title1"}},{"id":2,"required":0,"img":{"w":80,"h":80,"type":3,"url":"image_src"}},{"id":3,"required":0,"img":{"w":50,"h":50,"type":1,"url":"icon_src"}},{"id":4,"required":0,"data":{"type":4,"value":"sponsored"}},{"id":5,"required":0,"data":{"type":6,"value":"price1"}},{"id":6,"required":0,"data":{"type":7,"value":"price2"}}],"imptrackers":["imptrackers1"]}}', 'cid': '44082', 'crid': '2898532/2419121/2592854/2499195', 'cat': ['IAB7', 'IAB14', 'IAB18-3', 'IAB1-2'], 'ext': {'place': 0, 'crtype': 'native'}, 'adomain': ['test.com']}], 'seat': '44082'}], ext: {'muidn': 'userid'}} + }; + let bids = spec.interpretResponse(resp); + expect(bids).to.deep.equal([{ + 'ad': '{"native":{"ver":"1.1","link":{"url":"link_url"},"assets":[{"id":1,"required":0,"title":{"text":"title1"}},{"id":2,"required":0,"img":{"w":80,"h":80,"type":3,"url":"image_src"}},{"id":3,"required":0,"img":{"w":50,"h":50,"type":1,"url":"icon_src"}},{"id":4,"required":0,"data":{"type":4,"value":"sponsored"}},{"id":5,"required":0,"data":{"type":6,"value":"price1"}},{"id":6,"required":0,"data":{"type":7,"value":"price2"}}],"imptrackers":["imptrackers1"]}}', + 'burl': 'https burl', + 'cpm': 1.5, + 'creativeId': '2898532/2419121/2592854/2499195', + 'currency': 'GBP', + 'dealId': '', + 'height': 0, + 'isBurl': true, + 'mediaType': 'native', + 'meta': {'advertiserDomains': ['test.com']}, + 'native': { + 'clickTrackers': [], + 'clickUrl': 'link_url', + 'data': 'price1', + 'icon': { + 'height': 50, + 'url': 'icon_src', + 'width': 50 + }, + 'image': { + 'height': 80, + 'url': 'image_src', + 'width': 80 + }, + 'impressionTrackers': [ + 'imptrackers1' + ], + 'jstracker': [], + 'sponsoredBy': 'sponsored', + 'title': 'title1' + }, + 'netRevenue': true, + 'nurl': 'https nurl', + 'requestId': '61e40632c53fc2', + 'ttl': 300, + 'width': 0 + }]) + }); + it('should push proper native bid response, assets2', function () { + let resp = { + body: {'id': '57c0c2b1b732ca', 'bidid': '57c0c2b1b732ca', 'cur': 'GBP', 'seatbid': [{'bid': [{'price': 1.5, 'h': 600, 'w': 300, 'id': '1', 'impid': '61e40632c53fc2', 'adid': '2898532/2419121/2592854/2499195', 'nurl': 'https nurl', 'burl': 'https burl', 'adm': '{"native":{"ver":"1.1","link":{"url":"link_url"},"assets":[{"id":1,"required":0,"title":{"text":"title1"}},{"id":2,"required":0,"img":{"w":80,"h":80,"type":3,"url":"image_src"}},{"id":3,"required":0,"img":{"w":50,"h":50,"type":1,"url":"icon_src"}}],"imptrackers":["imptrackers1"]}}', 'cid': '44082', 'crid': '2898532/2419121/2592854/2499195', 'cat': ['IAB7', 'IAB14', 'IAB18-3', 'IAB1-2'], 'ext': {'place': 0, 'crtype': 'native'}, 'adomain': ['test.com']}], 'seat': '44082'}]} + }; + let bids = spec.interpretResponse(resp); + expect(bids).to.deep.equal([ + { + 'ad': '{"native":{"ver":"1.1","link":{"url":"link_url"},"assets":[{"id":1,"required":0,"title":{"text":"title1"}},{"id":2,"required":0,"img":{"w":80,"h":80,"type":3,"url":"image_src"}},{"id":3,"required":0,"img":{"w":50,"h":50,"type":1,"url":"icon_src"}}],"imptrackers":["imptrackers1"]}}', + 'cpm': 1.5, + 'creativeId': '2898532/2419121/2592854/2499195', + 'currency': 'GBP', + 'dealId': '', + 'height': 0, + 'isBurl': true, + 'mediaType': 'native', + 'meta': {'advertiserDomains': ['test.com']}, + 'netRevenue': true, + 'nurl': 'https nurl', + 'burl': 'https burl', + 'requestId': '61e40632c53fc2', + 'ttl': 300, + 'width': 0, + 'native': { + clickTrackers: [], + title: 'title1', + image: { + url: 'image_src', + width: 80, + height: 80, + }, + icon: { + url: 'icon_src', + width: 50, + height: 50, + }, + impressionTrackers: ['imptrackers1'], + jstracker: [], + clickUrl: 'link_url', + } + } + ]); + }); + + it('should not push bid response', function () { + let bids = spec.interpretResponse(); + expect(bids).to.be.undefined; + }); + it('should push proper banner bid response', function () { + let resp = { + body: {'id': '57c0c2b1b732ca', 'bidid': '57c0c2b1b732ca', 'cur': '', 'seatbid': [{'bid': [{'price': 1.5, 'h': 600, 'w': 300, 'id': '1', 'impid': '61e40632c53fc2', 'adid': '2898532/2419121/2592854/2499195', 'nurl': 'https nurl', 'burl': 'https burl', 'adm': 'html: adm', 'cid': '44082', 'crid': '2898532/2419121/2592854/2499195', 'cat': ['IAB7', 'IAB14', 'IAB18-3', 'IAB1-2'], 'adomain': ['test.com']}], 'seat': '44082'}]} + }; + let bids = spec.interpretResponse(resp); + expect(bids).to.deep.equal([ + { + 'ad': 'html: adm', + 'cpm': 1.5, + 'creativeId': '2898532/2419121/2592854/2499195', + 'currency': 'USD', + 'dealId': '', + 'height': 600, + 'isBurl': true, + 'mediaType': 'banner', + 'meta': {'advertiserDomains': ['test.com']}, + 'netRevenue': true, + 'nurl': 'https nurl', + 'burl': 'https burl', + 'requestId': '61e40632c53fc2', + 'ttl': 300, + 'width': 300, + } + ]); + }); + }); + + describe('getUserSyncs', function () { + it('should do nothing on getUserSyncs', function () { + spec.getUserSyncs() + }); + }); + + describe('on bidWon', function () { + beforeEach(function() { + sinon.stub(utils, 'triggerPixel'); + }); + afterEach(function() { + utils.triggerPixel.restore(); + }); + it('should replace nurl and burl for native', function () { + const burl = 'burl&s=${' + 'AUCTION_PRICE}'; + const nurl = 'nurl&s=${' + 'AUCTION_PRICE}'; + const bid = {'bidderCode': 'mgid', 'width': 0, 'height': 0, 'statusMessage': 'Bid available', 'adId': '3d0b6ff1dda89', 'requestId': '2a423489e058a1', 'mediaType': 'native', 'source': 'client', 'ad': '{"native":{"ver":"1.1","link":{"url":"LinkURL"},"assets":[{"id":1,"required":0,"title":{"text":"TITLE"}},{"id":2,"required":0,"img":{"w":80,"h":80,"type":3,"url":"ImageURL"}},{"id":3,"required":0,"img":{"w":50,"h":50,"type":1,"url":"IconURL"}},{"id":11,"required":0,"data":{"type":1,"value":"sponsored"}}],"imptrackers":["ImpTrackerURL"]}}', 'cpm': 0.66, 'creativeId': '353538_591471', 'currency': 'USD', 'dealId': '', 'netRevenue': true, 'ttl': 300, 'nurl': nurl, 'burl': burl, 'isBurl': true, 'native': {'title': 'TITLE', 'image': {'url': 'ImageURL', 'height': 80, 'width': 80}, 'icon': {'url': 'IconURL', 'height': 50, 'width': 50}, 'sponsored': 'sponsored', 'clickUrl': 'LinkURL', 'clickTrackers': [], 'impressionTrackers': ['ImpTrackerURL'], 'jstracker': []}, 'auctionId': 'a92bffce-14d2-4f8f-a78a-7b9b5e4d28fa', 'responseTimestamp': 1556867386065, 'requestTimestamp': 1556867385916, 'bidder': 'mgid', 'adUnitCode': 'div-gpt-ad-1555415275793-0', 'timeToRespond': 149, 'pbLg': '0.50', 'pbMg': '0.60', 'pbHg': '0.66', 'pbAg': '0.65', 'pbDg': '0.66', 'pbCg': '', 'size': '0x0', 'adserverTargeting': {'hb_bidder': 'mgid', 'hb_adid': '3d0b6ff1dda89', 'hb_pb': '0.66', 'hb_size': '0x0', 'hb_source': 'client', 'hb_format': 'native', 'hb_native_title': 'TITLE', 'hb_native_image': 'hb_native_image:3d0b6ff1dda89', 'hb_native_icon': 'IconURL', 'hb_native_linkurl': 'hb_native_linkurl:3d0b6ff1dda89'}, 'status': 'targetingSet', 'params': [{'accountId': '184', 'placementId': '353538'}]}; + spec.onBidWon(bid); + expect(bid.nurl).to.deep.equal('nurl&s=0.66'); + expect(bid.burl).to.deep.equal('burl&s=0.66'); + }); + it('should replace nurl and burl for banner', function () { + const burl = 'burl&s=${' + 'AUCTION_PRICE}'; + const nurl = 'nurl&s=${' + 'AUCTION_PRICE}'; + const bid = {'bidderCode': 'mgid', 'width': 0, 'height': 0, 'statusMessage': 'Bid available', 'adId': '3d0b6ff1dda89', 'requestId': '2a423489e058a1', 'mediaType': 'banner', 'source': 'client', 'ad': burl, 'cpm': 0.66, 'creativeId': '353538_591471', 'currency': 'USD', 'dealId': '', 'netRevenue': true, 'ttl': 300, 'nurl': nurl, 'burl': burl, 'isBurl': true, 'auctionId': 'a92bffce-14d2-4f8f-a78a-7b9b5e4d28fa', 'responseTimestamp': 1556867386065, 'requestTimestamp': 1556867385916, 'bidder': 'mgid', 'adUnitCode': 'div-gpt-ad-1555415275793-0', 'timeToRespond': 149, 'pbLg': '0.50', 'pbMg': '0.60', 'pbHg': '0.66', 'pbAg': '0.65', 'pbDg': '0.66', 'pbCg': '', 'size': '0x0', 'adserverTargeting': {'hb_bidder': 'mgid', 'hb_adid': '3d0b6ff1dda89', 'hb_pb': '0.66', 'hb_size': '0x0', 'hb_source': 'client', 'hb_format': 'banner', 'hb_banner_title': 'TITLE', 'hb_banner_image': 'hb_banner_image:3d0b6ff1dda89', 'hb_banner_icon': 'IconURL', 'hb_banner_linkurl': 'hb_banner_linkurl:3d0b6ff1dda89'}, 'status': 'targetingSet', 'params': [{'accountId': '184', 'placementId': '353538'}]}; + spec.onBidWon(bid); + expect(bid.nurl).to.deep.equal('nurl&s=0.66'); + expect(bid.burl).to.deep.equal(burl); + expect(bid.ad).to.deep.equal('burl&s=0.66'); + }); + }); + + describe('price floor module', function() { + let bidRequest; + let bidRequests0 = { + adUnitCode: 'div', + bidder: 'mgid', + params: { + accountId: '1', + placementId: '2', + }, + mediaTypes: { + banner: { + sizes: [[300, 250]] + } + }, + sizes: [[300, 250]], + } + beforeEach(function() { + bidRequest = [utils.deepClone(bidRequests0)]; + }); + + it('obtain floor from getFloor', function() { + bidRequest[0].getFloor = () => { + return { + currency: 'USD', + floor: 1.23 + }; + }; + + const payload = JSON.parse(spec.buildRequests(bidRequest).data); + expect(payload.imp[0]).to.have.property('bidfloor', 1.23); + expect(payload.imp[0]).to.not.have.property('bidfloorcur'); + }); + it('obtain floor from params', function() { + bidRequest[0].getFloor = () => { + return { + currency: 'USD', + floor: 1.23 + }; + }; + bidRequest[0].params.bidfloor = 0.1; + + const payload = JSON.parse(spec.buildRequests(bidRequest).data); + expect(payload.imp[0]).to.have.property('bidfloor', 0.1); + expect(payload.imp[0]).to.not.have.property('bidfloorcur'); + }); + + it('unsupported currency', function() { + bidRequest[0].getFloor = () => { + return { + currency: 'EUR', + floor: 1.23 + }; + }; + + const payload = JSON.parse(spec.buildRequests(bidRequest).data); + expect(payload.imp[0]).to.have.property('bidfloor', 1.23); + expect(payload.imp[0]).to.have.property('bidfloorcur', 'EUR'); + }); + + it('bad floor value', function() { + bidRequest[0].getFloor = () => { + return { + currency: 'USD', + floor: 'test' + }; + }; + + const payload = JSON.parse(spec.buildRequests(bidRequest).data); + expect(payload.imp[0]).to.not.have.property('bidfloor'); + expect(payload.imp[0]).to.not.have.property('bidfloorcur'); + }); + + it('empty floor object', function() { + bidRequest[0].getFloor = () => { + return {}; + }; + + const payload = JSON.parse(spec.buildRequests(bidRequest).data); + expect(payload.imp[0]).to.not.have.property('bidfloor'); + expect(payload.imp[0]).to.not.have.property('bidfloorcur'); + }); + + it('undefined floor result', function() { + bidRequest[0].getFloor = () => {}; + + const payload = JSON.parse(spec.buildRequests(bidRequest).data); + expect(payload.imp[0]).to.not.have.property('bidfloor'); + expect(payload.imp[0]).to.not.have.property('bidfloorcur'); + }); + }); +}); From e9497d630c8b096c767c1de7b488fc67e79362e7 Mon Sep 17 00:00:00 2001 From: gaudeamus Date: Fri, 13 Aug 2021 15:13:36 +0300 Subject: [PATCH 2/4] use gvlid --- modules/mgidBidAdapter.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/mgidBidAdapter.js b/modules/mgidBidAdapter.js index ff5212a6738..d4855a95ef7 100644 --- a/modules/mgidBidAdapter.js +++ b/modules/mgidBidAdapter.js @@ -4,9 +4,10 @@ import {BANNER, NATIVE} from '../src/mediaTypes.js'; import {config} from '../src/config.js'; import { getStorageManager } from '../src/storageManager.js'; -export const storage = getStorageManager(); +const GVLID = 358; const DEFAULT_CUR = 'USD'; const BIDDER_CODE = 'mgid'; +export const storage = getStorageManager(GVLID, BIDDER_CODE); const ENDPOINT_URL = 'https://prebid.mgid.com/prebid/'; const LOG_WARN_PREFIX = '[MGID warn]: '; const LOG_INFO_PREFIX = '[MGID info]: '; From fc5f8f83c1b3d7ff9870ebc351b8c1d06958c341 Mon Sep 17 00:00:00 2001 From: gaudeamus Date: Tue, 17 Aug 2021 12:00:23 +0300 Subject: [PATCH 3/4] restore mgidBidAdapter.js with 5.x support --- modules/mgidBidAdapter.js | 5 ++++- test/spec/modules/mgidBidAdapter_spec.js | 27 +++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/modules/mgidBidAdapter.js b/modules/mgidBidAdapter.js index d4855a95ef7..fce02b78b5c 100644 --- a/modules/mgidBidAdapter.js +++ b/modules/mgidBidAdapter.js @@ -601,7 +601,10 @@ function getBidFloor(bid, cur) { size: '*' }); if (utils.isPlainObject(floorObj) && utils.isNumber(floorObj.floor)) { - if (cur !== floorObj.currency) { + if (!floorObj.currency && reqCur !== DEFAULT_CUR) { + floorObj.currency = DEFAULT_CUR + } + if (floorObj.currency && reqCur !== floorObj.currency) { cur = floorObj.currency } bidFloor = floorObj.floor; diff --git a/test/spec/modules/mgidBidAdapter_spec.js b/test/spec/modules/mgidBidAdapter_spec.js index 019b0c521ac..8492fc61d70 100644 --- a/test/spec/modules/mgidBidAdapter_spec.js +++ b/test/spec/modules/mgidBidAdapter_spec.js @@ -754,7 +754,19 @@ describe('Mgid bid adapter', function () { expect(payload.imp[0]).to.not.have.property('bidfloorcur'); }); - it('unsupported currency', function() { + it('undefined currency -> USD', function() { + bidRequest[0].params.currency = 'EUR' + bidRequest[0].getFloor = () => { + return { + floor: 1.23 + }; + }; + + const payload = JSON.parse(spec.buildRequests(bidRequest).data); + expect(payload.imp[0]).to.have.property('bidfloor', 1.23); + expect(payload.imp[0]).to.have.property('bidfloorcur', 'USD'); + }); + it('altered currency', function() { bidRequest[0].getFloor = () => { return { currency: 'EUR', @@ -766,6 +778,19 @@ describe('Mgid bid adapter', function () { expect(payload.imp[0]).to.have.property('bidfloor', 1.23); expect(payload.imp[0]).to.have.property('bidfloorcur', 'EUR'); }); + it('altered currency, same as in request', function() { + bidRequest[0].params.cur = 'EUR' + bidRequest[0].getFloor = () => { + return { + currency: 'EUR', + floor: 1.23 + }; + }; + + const payload = JSON.parse(spec.buildRequests(bidRequest).data); + expect(payload.imp[0]).to.have.property('bidfloor', 1.23); + expect(payload.imp[0]).to.not.have.property('bidfloorcur'); + }); it('bad floor value', function() { bidRequest[0].getFloor = () => { From ad95f9b67e36cddf4d0bc8281460deef8dd2ba9b Mon Sep 17 00:00:00 2001 From: gaudeamus Date: Wed, 25 Aug 2021 11:37:37 +0300 Subject: [PATCH 4/4] read pos in standard way --- modules/mgidBidAdapter.js | 2 +- test/spec/modules/mgidBidAdapter_spec.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/mgidBidAdapter.js b/modules/mgidBidAdapter.js index fce02b78b5c..3b45c1e69a5 100644 --- a/modules/mgidBidAdapter.js +++ b/modules/mgidBidAdapter.js @@ -380,7 +380,7 @@ function createBannerRequest(bid) { if (format.length) { r.format = format } - const pos = utils.getBidIdParameter('position', bid.params) || 0 + const pos = utils.deepAccess(bid, 'mediaTypes.banner.pos') || 0 if (pos) { r.pos = pos } diff --git a/test/spec/modules/mgidBidAdapter_spec.js b/test/spec/modules/mgidBidAdapter_spec.js index 8492fc61d70..34ad29b3e92 100644 --- a/test/spec/modules/mgidBidAdapter_spec.js +++ b/test/spec/modules/mgidBidAdapter_spec.js @@ -512,9 +512,9 @@ describe('Mgid bid adapter', function () { bid.mediaTypes = { banner: { sizes: [[300, 600], [300, 250]], + pos: 1, }, }; - bid.params.position = 1; let bidRequests = [bid]; const request = spec.buildRequests(bidRequests);