Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adquery ID System: add new ID module #7852

Merged
merged 2 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
96 changes: 96 additions & 0 deletions modules/adqueryIdSystem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* This module adds AdqueryID to the User ID module
* The {@link module:modules/userId} module is required
* @module modules/adqueryIdSystem
* @requires module:modules/userId
*/

import {ajax} from '../src/ajax.js';
import {getStorageManager} from '../src/storageManager.js';
import {submodule} from '../src/hook.js';
import * as utils from '../src/utils.js';

const MODULE_NAME = 'adqueryId';
const AU_GVLID = 902;

export const storage = getStorageManager(AU_GVLID, 'adquery');

/**
* Param or default.
* @param {String} param
* @param {String} defaultVal
*/
function paramOrDefault(param, defaultVal, arg) {
if (utils.isFn(param)) {
return param(arg);
} else if (utils.isStr(param)) {
return param;
}
return defaultVal;
}

/** @type {Submodule} */
export const adqueryIdSubmodule = {
/**
* used to link submodule with config
* @type {string}
*/
name: MODULE_NAME,
/**
* decode the stored id value for passing to bid requests
* @function
* @param {{value:string}} value
* @returns {{qid:Object}}
*/
decode(value) {
let qid = storage.getDataFromLocalStorage('qid');
if (utils.isStr(qid)) {
return {qid: qid};
}
return (value && typeof value['qid'] === 'string') ? { 'qid': value['qid'] } : undefined;
},
/**
* performs action to obtain id and return a value in the callback's response argument
* @function
* @param {SubmoduleConfig} [config]
* @returns {IdResponse|undefined}
*/
getId(config) {
if (!utils.isPlainObject(config.params)) {
config.params = {};
}
const url = paramOrDefault(config.params.url,
`https://bidder.adquery.io/prebid/qid`,
config.params.urlArg);

const resp = function (callback) {
let qid = storage.getDataFromLocalStorage('qid');
if (utils.isStr(qid)) {
const responseObj = {qid: qid};
callback(responseObj);
} else {
const callbacks = {
success: response => {
let responseObj;
if (response) {
try {
responseObj = JSON.parse(response);
} catch (error) {
utils.logError(error);
}
}
callback(responseObj);
},
error: error => {
utils.logError(`${MODULE_NAME}: ID fetch encountered an error`, error);
callback();
}
};
ajax(url, callbacks, undefined, {method: 'GET'});
}
};
return {callback: resp};
}
};

submodule('userId', adqueryIdSubmodule);
56 changes: 56 additions & 0 deletions test/spec/modules/adqueryIdSystem_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { adqueryIdSubmodule, storage } from 'modules/adqueryIdSystem.js';
import { server } from 'test/mocks/xhr.js';

describe('AdqueryIdSystem', function () {
describe('getId', function() {
let getDataFromLocalStorageStub;

beforeEach(function() {
getDataFromLocalStorageStub = sinon.stub(storage, 'getDataFromLocalStorage');
});

afterEach(function () {
getDataFromLocalStorageStub.restore();
});

it('gets a adqueryId', function() {
const config = {
params: {}
};
const callbackSpy = sinon.spy();
const callback = adqueryIdSubmodule.getId(config).callback;
callback(callbackSpy);
const request = server.requests[0];
expect(request.url).to.eq(`https://bidder.adquery.io/prebid/qid`);
request.respond(200, { 'Content-Type': 'application/json' }, JSON.stringify({ qid: 'qid' }));
expect(callbackSpy.lastCall.lastArg).to.deep.equal({qid: 'qid'});
});

it('gets a cached adqueryId', function() {
const config = {
params: {}
};
getDataFromLocalStorageStub.withArgs('qid').returns('qid');

const callbackSpy = sinon.spy();
const callback = adqueryIdSubmodule.getId(config).callback;
callback(callbackSpy);
expect(callbackSpy.lastCall.lastArg).to.deep.equal({qid: 'qid'});
});

it('allows configurable id url', function() {
const config = {
params: {
url: 'https://bidder.adquery.io/prebid/qid'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should probably test with a value that isn't the default to prove that you can properly override

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added missing files and fixed test

}
};
const callbackSpy = sinon.spy();
const callback = adqueryIdSubmodule.getId(config).callback;
callback(callbackSpy);
const request = server.requests[0];
expect(request.url).to.eq('https://bidder.adquery.io/prebid/qid');
request.respond(200, { 'Content-Type': 'application/json' }, JSON.stringify({ qid: 'qid' }));
expect(callbackSpy.lastCall.lastArg).to.deep.equal({qid: 'qid'});
});
});
});