Skip to content

Commit

Permalink
Intentiq id add validation (#5797)
Browse files Browse the repository at this point in the history
* Add validity check to ignore not-available response

* Added tests

* Added error log
  • Loading branch information
yuvalgg committed Oct 1, 2020
1 parent 804ff2f commit 907a9c7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
36 changes: 33 additions & 3 deletions modules/intentIqIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,32 @@ import {submodule} from '../src/hook.js'

const MODULE_NAME = 'intentIqId';

const NOT_AVAILABLE = 'NA';

/**
* Verify the id is valid - Id value or Not Found (ignore not available response)
* @param id
* @returns {boolean|*|boolean}
*/
function isValidId(id) {
return id && id != '' && id != NOT_AVAILABLE && isValidResponse(id);
}

/**
* Ignore not available response JSON
* @param obj
* @returns {boolean}
*/
function isValidResponse(obj) {
try {
obj = JSON.parse(obj);
return obj && obj['RESULT'] != NOT_AVAILABLE;
} catch (error) {
utils.logError(error);
return true;
}
}

/** @type {Submodule} */
export const intentIqIdSubmodule = {
/**
Expand All @@ -22,10 +48,10 @@ export const intentIqIdSubmodule = {
* decode the stored id value for passing to bid requests
* @function
* @param {{string}} value
* @returns {{intentIqId:string}}
* @returns {{intentIqId: {string}}|undefined}
*/
decode(value) {
return (value && value != '') ? { 'intentIqId': value } : undefined;
return isValidId(value) ? { 'intentIqId': value } : undefined;
},
/**
* performs action to obtain id and return a value in the callback's response argument
Expand All @@ -47,7 +73,11 @@ export const intentIqIdSubmodule = {
const resp = function (callback) {
const callbacks = {
success: response => {
callback(response);
if (isValidId(response)) {
callback(response);
} else {
callback();
}
},
error: error => {
utils.logError(`${MODULE_NAME}: ID fetch encountered an error`, error);
Expand Down
8 changes: 8 additions & 0 deletions test/spec/modules/intentIqIdSystem_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ describe('IntentIQ tests', function () {
expect(callBackSpy.calledOnce).to.be.true;
});

it('should ignore NA and invalid responses', function () {
let resp = JSON.stringify({'RESULT': 'NA'});
expect(intentIqIdSubmodule.decode(resp)).to.equal(undefined);
expect(intentIqIdSubmodule.decode('NA')).to.equal(undefined);
expect(intentIqIdSubmodule.decode('')).to.equal(undefined);
expect(intentIqIdSubmodule.decode(undefined)).to.equal(undefined);
});

it('should call the IntentIQ endpoint with only partner, pai', function () {
let callBackSpy = sinon.spy();
let submoduleCallback = intentIqIdSubmodule.getId(paiConfigParams).callback;
Expand Down

0 comments on commit 907a9c7

Please sign in to comment.