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

Britepool user id module update #5750

Merged
merged 8 commits into from
Sep 23, 2020
11 changes: 9 additions & 2 deletions modules/britepoolIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import * as utils from '../src/utils.js'
import {ajax} from '../src/ajax.js';
import {submodule} from '../src/hook.js';
const PIXEL = 'https://px.britepool.com/new?partner_id=t';

/** @type {Submodule} */
export const britepoolIdSubmodule = {
Expand All @@ -29,6 +30,7 @@ export const britepoolIdSubmodule = {
* Performs action to obtain id and return a value in the callback's response argument
* @function
* @param {SubmoduleParams} [configParams]
* @param {ConsentData|undefined} consentData
* @returns {function(callback:function)}
*/
getId(submoduleConfigParams, consentData) {
Expand Down Expand Up @@ -65,6 +67,7 @@ export const britepoolIdSubmodule = {
} else {
ajax(url, {
success: response => {
utils.triggerPixel(PIXEL);
bansawbanchee marked this conversation as resolved.
Show resolved Hide resolved
const responseObj = britepoolIdSubmodule.normalizeValue(response);
callback(responseObj ? { primaryBPID: responseObj.primaryBPID } : null);
},
Expand All @@ -80,12 +83,16 @@ export const britepoolIdSubmodule = {
/**
* Helper method to create params for our API call
* @param {SubmoduleParams} [configParams]
* @param {ConsentData|undefined} consentData
* @returns {object} Object with parsed out params
*/
createParams(submoduleConfigParams, consentData) {
const hasGdprData = consentData && typeof consentData.gdprApplies === 'boolean' && consentData.gdprApplies;
const gdprConsentString = hasGdprData ? consentData.consentString : undefined;
let errors = [];
const headers = {};
let params = Object.assign({}, submoduleConfigParams);
const dynamicVars = typeof britepool_pubparams !== 'undefined' ? britepool_pubparams : {}; // eslint-disable-line camelcase, no-undef
let params = Object.assign({}, submoduleConfigParams, dynamicVars);
if (params.getter) {
// Custom getter will not require other params
if (typeof params.getter !== 'function') {
Expand All @@ -98,7 +105,7 @@ export const britepoolIdSubmodule = {
headers['x-api-key'] = params.api_key;
}
}
const url = params.url || 'https://api.britepool.com/v1/britepool/id';
const url = params.url || `https://api.britepool.com/v1/britepool/id${gdprConsentString ? '?gdprString=' + encodeURIComponent(gdprConsentString) : ''}`;
const getter = params.getter;
delete params.api_key;
delete params.url;
Expand Down
36 changes: 36 additions & 0 deletions test/spec/modules/britepoolIdSystem_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,42 @@ describe('BritePool Submodule', () => {
expect(params.url).to.be.undefined;
});

it('test gdpr consent string in url', () => {
const { params, headers, url, errors } = britepoolIdSubmodule.createParams({ api_key, aaid }, { gdprApplies: true, consentString: 'expectedConsentString' });
expect(url).to.equal('https://api.britepool.com/v1/britepool/id?gdprString=expectedConsentString');
});

it('test gdpr consent string not in url if gdprApplies false', () => {
const { params, headers, url, errors } = britepoolIdSubmodule.createParams({ api_key, aaid }, { gdprApplies: false, consentString: 'expectedConsentString' });
expect(url).to.equal('https://api.britepool.com/v1/britepool/id');
});

it('test gdpr consent string not in url if consent string undefined', () => {
const { params, headers, url, errors } = britepoolIdSubmodule.createParams({ api_key, aaid }, { gdprApplies: true, consentString: undefined });
expect(url).to.equal('https://api.britepool.com/v1/britepool/id');
});

it('dynamic pub params should be added to params', () => {
window.britepool_pubparams = { ppid: '12345' };
const { params, headers, url, errors } = britepoolIdSubmodule.createParams({ api_key, aaid });
expect(params).to.eql({ aaid, ppid: '12345' });
window.britepool_pubparams = undefined;
});

it('dynamic pub params should override submodule params', () => {
window.britepool_pubparams = { ppid: '67890' };
const { params, headers, url, errors } = britepoolIdSubmodule.createParams({ api_key, ppid: '12345' });
expect(params).to.eql({ ppid: '67890' });
window.britepool_pubparams = undefined;
});

it('if dynamic pub params undefined do nothing', () => {
window.britepool_pubparams = undefined;
const { params, headers, url, errors } = britepoolIdSubmodule.createParams({ api_key, aaid });
expect(params).to.eql({ aaid });
window.britepool_pubparams = undefined;
});

it('test getter override with value', () => {
const { params, headers, url, getter, errors } = britepoolIdSubmodule.createParams({ api_key, aaid, url: url_override, getter: getter_override });
expect(getter).to.equal(getter_override);
Expand Down