From d04a3443a1af47237a4aa0d8e9c4844d052e191b Mon Sep 17 00:00:00 2001 From: ehb-mtk Date: Wed, 14 Aug 2024 00:47:17 +0000 Subject: [PATCH 1/2] update API endpoint for mobian RTD provider --- modules/mobianRtdProvider.js | 24 ++---- test/spec/modules/mobianRtdProvider_spec.js | 87 +++++++++++++++------ 2 files changed, 70 insertions(+), 41 deletions(-) diff --git a/modules/mobianRtdProvider.js b/modules/mobianRtdProvider.js index d71948046b9..6499c3519f5 100644 --- a/modules/mobianRtdProvider.js +++ b/modules/mobianRtdProvider.js @@ -10,7 +10,7 @@ import { deepSetValue, safeJSONParse } from '../src/utils.js'; * @typedef {import('../modules/rtdModule/index.js').RtdSubmodule} RtdSubmodule */ -export const MOBIAN_URL = 'https://impact-api-prod.themobian.com/brand_safety'; +export const MOBIAN_URL = 'https://prebid.outcomes.net/api/prebid/v1/assessment/async'; /** @type {RtdSubmodule} */ export const mobianBrandSafetySubmodule = { @@ -26,7 +26,7 @@ function init() { function getBidRequestData(bidReqConfig, callback, config) { const { site: ortb2Site } = bidReqConfig.ortb2Fragments.global; const pageUrl = encodeURIComponent(getPageUrl()); - const requestUrl = `${MOBIAN_URL}/by_url?url=${pageUrl}`; + const requestUrl = `${MOBIAN_URL}?url=${pageUrl}`; const ajax = ajaxBuilder(); @@ -34,25 +34,17 @@ function getBidRequestData(bidReqConfig, callback, config) { ajax(requestUrl, { success: function(responseData) { let response = safeJSONParse(responseData); - if (!response) { + if (!response || !response.meta.has_results) { resolve({}); callback(); return; } - let mobianRisk = response.garm_risk || 'unknown'; - - const contentCategories = Object.keys(response) - .filter(key => key.startsWith('garm_content_category_') && response[key]) - .map(key => key.replace('garm_content_category_', '')); - - const sentiment = Object.keys(response) - .find(key => key.startsWith('sentiment_') && response[key]) - ?.replace('sentiment_', '') || 'unknown'; - - const emotions = Object.keys(response) - .filter(key => key.startsWith('emotion_') && response[key]) - .map(key => key.replace('emotion_', '')); + const results = response.results; + const mobianRisk = results.mobianRisk || 'unknown'; + const contentCategories = results.mobianContentCategories || []; + const sentiment = results.mobianSentiment || 'unknown'; + const emotions = results.mobianEmotions || []; const risk = { risk: mobianRisk, diff --git a/test/spec/modules/mobianRtdProvider_spec.js b/test/spec/modules/mobianRtdProvider_spec.js index 717745c02f2..3fff103cc46 100644 --- a/test/spec/modules/mobianRtdProvider_spec.js +++ b/test/spec/modules/mobianRtdProvider_spec.js @@ -25,12 +25,19 @@ describe('Mobian RTD Submodule', function () { ajaxStub.restore(); }); - it('should set key-value pairs when server responds with garm_risk', function () { + it('should set key-value pairs when server responds with valid data', function () { ajaxStub = sinon.stub(ajax, 'ajaxBuilder').returns(function(url, callbacks) { callbacks.success(JSON.stringify({ - garm_risk: 'low', - sentiment_positive: true, - emotion_joy: true + meta: { + url: "https://example.com", + has_results: true + }, + results: { + mobianRisk: "low", + mobianSentiment: "positive", + mobianContentCategories: [], + mobianEmotions: ["joy"] + } })); }); @@ -50,15 +57,19 @@ describe('Mobian RTD Submodule', function () { }); }); - it('should handle response with GARM content categories, sentiment, and emotions', function () { + it('should handle response with content categories and multiple emotions', function () { ajaxStub = sinon.stub(ajax, 'ajaxBuilder').returns(function(url, callbacks) { callbacks.success(JSON.stringify({ - garm_risk: 'medium', - garm_content_category_arms: true, - garm_content_category_crime: true, - sentiment_negative: true, - emotion_anger: true, - emotion_fear: true + meta: { + url: "https://example.com", + has_results: true + }, + results: { + mobianRisk: "medium", + mobianSentiment: "negative", + mobianContentCategories: ["arms", "crime"], + mobianEmotions: ["anger", "fear"] + } })); }); @@ -78,26 +89,22 @@ describe('Mobian RTD Submodule', function () { }); }); - it('should return unknown risk when garm_risk is not present', function () { + it('should return empty object when server responds with has_results: false', function () { ajaxStub = sinon.stub(ajax, 'ajaxBuilder').returns(function(url, callbacks) { callbacks.success(JSON.stringify({ - sentiment_neutral: true + meta: { + url: "https://example.com", + has_results: false + }, + results: {} })); }); return mobianBrandSafetySubmodule.getBidRequestData(bidReqConfig, {}, {}).then((result) => { - expect(result).to.deep.equal({ - risk: 'unknown', - contentCategories: [], - sentiment: 'neutral', - emotions: [] - }); - expect(bidReqConfig.ortb2Fragments.global.site.ext.data).to.deep.include({ - mobianRisk: 'unknown', - mobianContentCategories: [], - mobianSentiment: 'neutral', - mobianEmotions: [] - }); + expect(result).to.deep.equal({}); + expect(bidReqConfig.ortb2Fragments.global.site.ext.data).to.not.have.any.keys( + 'mobianRisk', 'mobianContentCategories', 'mobianSentiment', 'mobianEmotions' + ); }); }); @@ -122,4 +129,34 @@ describe('Mobian RTD Submodule', function () { expect(result).to.deep.equal({}); }); }); + + it('should use default values when fields are missing in the response', function () { + ajaxStub = sinon.stub(ajax, 'ajaxBuilder').returns(function(url, callbacks) { + callbacks.success(JSON.stringify({ + meta: { + url: "https://example.com", + has_results: true + }, + results: { + mobianRisk: "high" + // Missing other fields + } + })); + }); + + return mobianBrandSafetySubmodule.getBidRequestData(bidReqConfig, {}, {}).then((result) => { + expect(result).to.deep.equal({ + risk: 'high', + contentCategories: [], + sentiment: 'unknown', + emotions: [] + }); + expect(bidReqConfig.ortb2Fragments.global.site.ext.data).to.deep.include({ + mobianRisk: 'high', + mobianContentCategories: [], + mobianSentiment: 'unknown', + mobianEmotions: [] + }); + }); + }); }); From a494eeafe04416dbe8211f8e8623dff7ad5d7853 Mon Sep 17 00:00:00 2001 From: ehb-mtk Date: Wed, 14 Aug 2024 21:36:16 +0000 Subject: [PATCH 2/2] lint mobian RTD provider --- test/spec/modules/mobianRtdProvider_spec.js | 24 ++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/test/spec/modules/mobianRtdProvider_spec.js b/test/spec/modules/mobianRtdProvider_spec.js index 3fff103cc46..278d8fdef92 100644 --- a/test/spec/modules/mobianRtdProvider_spec.js +++ b/test/spec/modules/mobianRtdProvider_spec.js @@ -29,14 +29,14 @@ describe('Mobian RTD Submodule', function () { ajaxStub = sinon.stub(ajax, 'ajaxBuilder').returns(function(url, callbacks) { callbacks.success(JSON.stringify({ meta: { - url: "https://example.com", + url: 'https://example.com', has_results: true }, results: { - mobianRisk: "low", - mobianSentiment: "positive", + mobianRisk: 'low', + mobianSentiment: 'positive', mobianContentCategories: [], - mobianEmotions: ["joy"] + mobianEmotions: ['joy'] } })); }); @@ -61,14 +61,14 @@ describe('Mobian RTD Submodule', function () { ajaxStub = sinon.stub(ajax, 'ajaxBuilder').returns(function(url, callbacks) { callbacks.success(JSON.stringify({ meta: { - url: "https://example.com", + url: 'https://example.com', has_results: true }, results: { - mobianRisk: "medium", - mobianSentiment: "negative", - mobianContentCategories: ["arms", "crime"], - mobianEmotions: ["anger", "fear"] + mobianRisk: 'medium', + mobianSentiment: 'negative', + mobianContentCategories: ['arms', 'crime'], + mobianEmotions: ['anger', 'fear'] } })); }); @@ -93,7 +93,7 @@ describe('Mobian RTD Submodule', function () { ajaxStub = sinon.stub(ajax, 'ajaxBuilder').returns(function(url, callbacks) { callbacks.success(JSON.stringify({ meta: { - url: "https://example.com", + url: 'https://example.com', has_results: false }, results: {} @@ -134,11 +134,11 @@ describe('Mobian RTD Submodule', function () { ajaxStub = sinon.stub(ajax, 'ajaxBuilder').returns(function(url, callbacks) { callbacks.success(JSON.stringify({ meta: { - url: "https://example.com", + url: 'https://example.com', has_results: true }, results: { - mobianRisk: "high" + mobianRisk: 'high' // Missing other fields } }));