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

Merkle Id System: make endpoint optionally configurable #7404

Merged
merged 4 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions integrationExamples/gpt/userId_example.html
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
{
"name": "merkleId",
"params": {
"endpoint": "https://test_endpoint/",
"vendor": "sdfg",
"sv_cid": "dfg",
"sv_pubid": "xcv",
Expand Down
22 changes: 14 additions & 8 deletions modules/merkleIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

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

Expand Down Expand Up @@ -42,7 +42,7 @@ function setSession(storage, response) {

function constructUrl(configParams) {
const session = getSession(configParams);
let url = ID_URL + `?vendor=${configParams.vendor}&sv_cid=${configParams.sv_cid}&sv_domain=${configParams.sv_domain}&sv_pubid=${configParams.sv_pubid}`;
let url = configParams.endpoint + `?vendor=${configParams.vendor}&sv_cid=${configParams.sv_cid}&sv_domain=${configParams.sv_domain}&sv_pubid=${configParams.sv_pubid}`;
if (session) {
url = `${url}&sv_session=${session}`;
}
Expand All @@ -54,8 +54,9 @@ function generateId(configParams, configStorage) {
const url = constructUrl(configParams);

const resp = function (callback) {
const callbacks = {
success: response => {
ajaxLib.ajaxBuilder()(
url,
response => {
let responseObj;
if (response) {
try {
Expand All @@ -72,12 +73,12 @@ function generateId(configParams, configStorage) {
utils.logInfo('Merkle responseObj with date ' + JSON.stringify(responseObj));
callback(responseObj);
},
error: error => {
error => {
utils.logError(`${MODULE_NAME}: merkleId fetch encountered an error`, error);
callback();
}
};
ajax(url, callbacks, undefined, {method: 'GET', withCredentials: true});
},
{method: 'GET', withCredentials: true}
);
};
return resp;
}
Expand Down Expand Up @@ -126,10 +127,15 @@ export const merkleIdSubmodule = {
utils.logError('User ID - merkleId submodule requires a valid sv_pubid string to be defined');
return;
}

if (consentData && typeof consentData.gdprApplies === 'boolean' && consentData.gdprApplies) {
utils.logError('User ID - merkleId submodule does not currently handle consent strings');
return;
}
if (typeof configParams.endpoint !== 'string') {
utils.logWarn('User ID - merkleId submodule endpoint string is not defined');
configParams.endpoint = ID_URL
}

if (typeof configParams.sv_domain !== 'string') {
configParams.sv_domain = merkleIdSubmodule.findRootDomain();
Expand Down
212 changes: 212 additions & 0 deletions test/spec/modules/merkleIdSystem_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
import * as ajaxLib from 'src/ajax.js';
import * as utils from 'src/utils.js';
import {merkleIdSubmodule} from 'modules/merkleIdSystem.js';

import sinon from 'sinon';

let expect = require('chai').expect;

const CONFIG_PARAMS = {
endpoint: 'https://test/id',
vendor: 'idsv2',
sv_cid: '5344_04531',
sv_pubid: '11314',
sv_domain: 'www.testDomain.com',
sv_session: 'testsession'
};

const STORAGE_PARAMS = {
type: 'cookie',
name: 'merkle',
expires: 10,
refreshInSeconds: 10
};

const MOCK_RESPONSE = {
c: {
name: '_svsid',
value: '123876327647627364236478'
}
};

function mockResponse(
responseText,
response = (url, successCallback) => successCallback(responseText)) {
return function() {
return response;
}
}

describe('Merkle System', function () {
describe('Merkle System getId()', function () {
const callbackSpy = sinon.spy();
let sandbox;
let ajaxStub;

beforeEach(function () {
sandbox = sinon.sandbox.create();
sinon.stub(utils, 'logInfo');
sinon.stub(utils, 'logWarn');
sinon.stub(utils, 'logError');
callbackSpy.resetHistory();
ajaxStub = sinon.stub(ajaxLib, 'ajaxBuilder').callsFake(mockResponse(JSON.stringify(MOCK_RESPONSE)));
});

afterEach(function () {
utils.logInfo.restore();
utils.logWarn.restore();
utils.logError.restore();
ajaxStub.restore();
});

it('getId() should fail on missing vendor', function () {
let config = {
params: {
...CONFIG_PARAMS,
vendor: undefined
},
storage: STORAGE_PARAMS
};

let submoduleCallback = merkleIdSubmodule.getId(config, undefined);
expect(submoduleCallback).to.be.undefined;
expect(utils.logError.args[0][0]).to.exist.and.to.equal('User ID - merkleId submodule requires a valid vendor to be defined');
});

it('getId() should fail on missing vendor', function () {
let config = {
params: {
...CONFIG_PARAMS,
vendor: undefined
},
storage: STORAGE_PARAMS
};

let submoduleCallback = merkleIdSubmodule.getId(config, undefined);
expect(submoduleCallback).to.be.undefined;
expect(utils.logError.args[0][0]).to.exist.and.to.equal('User ID - merkleId submodule requires a valid vendor to be defined');
});

it('getId() should fail on missing sv_cid', function () {
let config = {
params: {
...CONFIG_PARAMS,
sv_cid: undefined
},
storage: STORAGE_PARAMS
};

let submoduleCallback = merkleIdSubmodule.getId(config, undefined);
expect(submoduleCallback).to.be.undefined;
expect(utils.logError.args[0][0]).to.exist.and.to.equal('User ID - merkleId submodule requires a valid sv_cid string to be defined');
});

it('getId() should fail on missing sv_pubid', function () {
let config = {
params: {
...CONFIG_PARAMS,
sv_pubid: undefined
},
storage: STORAGE_PARAMS
};

let submoduleCallback = merkleIdSubmodule.getId(config, undefined);
expect(submoduleCallback).to.be.undefined;
expect(utils.logError.args[0][0]).to.exist.and.to.equal('User ID - merkleId submodule requires a valid sv_pubid string to be defined');
});

it('getId() should warn on missing endpoint', function () {
let config = {
params: {
...CONFIG_PARAMS,
endpoint: undefined
},
storage: STORAGE_PARAMS
};

let submoduleCallback = merkleIdSubmodule.getId(config, undefined).callback;
submoduleCallback(callbackSpy);
expect(callbackSpy.calledOnce).to.be.true;
expect(utils.logWarn.args[0][0]).to.exist.and.to.equal('User ID - merkleId submodule endpoint string is not defined');
});

it('getId() should handle callback with valid configuration', function () {
let config = {
params: CONFIG_PARAMS,
storage: STORAGE_PARAMS
};

let submoduleCallback = merkleIdSubmodule.getId(config, undefined).callback;
submoduleCallback(callbackSpy);
expect(callbackSpy.calledOnce).to.be.true;
});
});

describe('Merkle System extendId()', function () {
const callbackSpy = sinon.spy();
let sandbox;
let ajaxStub;

beforeEach(function () {
sandbox = sinon.sandbox.create();
sinon.stub(utils, 'logInfo');
sinon.stub(utils, 'logWarn');
sinon.stub(utils, 'logError');
callbackSpy.resetHistory();
ajaxStub = sinon.stub(ajaxLib, 'ajaxBuilder').callsFake(mockResponse(JSON.stringify(MOCK_RESPONSE)));
});

afterEach(function () {
utils.logInfo.restore();
utils.logWarn.restore();
utils.logError.restore();
ajaxStub.restore();
});

it('extendId() get storedid', function () {
let config = {
params: {
...CONFIG_PARAMS,
},
storage: STORAGE_PARAMS
};

let id = merkleIdSubmodule.extendId(config, undefined, 'Merkle_Stored_ID');
expect(id.id).to.exist.and.to.equal('Merkle_Stored_ID');
});

it('extendId() get storedId on configured storageParam.refreshInSeconds', function () {
let config = {
params: {
...CONFIG_PARAMS,
refreshInSeconds: 1000
},
storage: STORAGE_PARAMS
};

let yesterday = new Date(Date.now() - 86400000).toUTCString();
let storedId = {value: 'Merkle_Stored_ID', date: yesterday};

let id = merkleIdSubmodule.extendId(config, undefined,
storedId);

expect(id.id).to.exist.and.to.equal(storedId);
});

it('extendId() callback on configured storageParam.refreshInSeconds', function () {
let config = {
params: {
...CONFIG_PARAMS,
refreshInSeconds: 1
}
};

let yesterday = new Date(Date.now() - 86400000).toUTCString();
let storedId = {value: 'Merkle_Stored_ID', date: yesterday};

let submoduleCallback = merkleIdSubmodule.extendId(config, undefined, storedId).callback;
submoduleCallback(callbackSpy);
expect(callbackSpy.calledOnce).to.be.true;
});
});
});