Skip to content

Commit

Permalink
Read from external publink cookies if available
Browse files Browse the repository at this point in the history
  • Loading branch information
johnwier committed Aug 31, 2021
1 parent e750d03 commit bdc0043
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 49 deletions.
47 changes: 33 additions & 14 deletions modules/publinkIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {uspDataHandler} from '../src/adapterManager.js';

const MODULE_NAME = 'publinkId';
const GVLID = 24;
const PUBLINK_COOKIE = '_publink';
const PUBLINK_S2S_COOKIE = '_publink_srv';

export const storage = getStorageManager(GVLID);

Expand Down Expand Up @@ -47,24 +49,37 @@ function makeCallback(config = {}, consentData) {
}
}
};
if (window.coreid && typeof window.coreid.getPublinkId === 'function') {
window.coreid.getPublinkId((newId) => {
if (newId) {
prebidCallback(newId);
} else {
if (config.params && config.params.e) {
ajax(publinkIdUrl(config.params, consentData), handleResponse, undefined, options);
}
}
});
} else {
if (config.params && config.params.e) {
ajax(publinkIdUrl(config.params, consentData), handleResponse, undefined, options);
}
if (config.params && config.params.e) {
ajax(publinkIdUrl(config.params, consentData), handleResponse, undefined, options);
}
};
}

function getlocalValue() {
let result;
function getData(key) {
let value;
if (storage.hasLocalStorage()) {
value = storage.getDataFromLocalStorage(key);
}
if (!value) {
value = storage.getCookie(key);
}

if (typeof value === 'string') {
const obj = JSON.parse(value);
if (obj && obj.exp > Date.now()) {
return obj.publink;
}
}
}
result = getData(PUBLINK_S2S_COOKIE);
if (!result) {
result = getData(PUBLINK_COOKIE);
}
return result;
}

/** @type {Submodule} */
export const publinkIdSubmodule = {
/**
Expand Down Expand Up @@ -92,6 +107,10 @@ export const publinkIdSubmodule = {
*/
getId: function(config, consentData, storedId) {
let result = {};
const storedValue = getlocalValue();
if (storedValue) {
result.id = storedValue;
}
if (storedId) {
result.id = storedId;
}
Expand Down
83 changes: 48 additions & 35 deletions test/spec/modules/publinkIdSystem_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,54 @@ describe('PublinkIdSystem', () => {
});
});

describe('Fetch Local Cookies', () => {
const PUBLINK_COOKIE = '_publink';
const PUBLINK_SRV_COOKIE = '_publink_srv';
const EXP = Date.now() + 60 * 60 * 24 * 7 * 1000;
const COOKIE_VALUE = {publink: 'publinkCookieValue', exp: EXP};
const LOCAL_VALUE = {publink: 'publinkLocalStorageValue', exp: EXP};
const COOKIE_EXPIRATION = (new Date(Date.now() + 60 * 60 * 24 * 1000)).toUTCString();
const DELETE_COOKIE = 'Thu, 01 Jan 1970 00:00:01 GMT';
it('publink srv cookie', () => {
storage.setCookie(PUBLINK_SRV_COOKIE, JSON.stringify(COOKIE_VALUE), COOKIE_EXPIRATION);
const result = publinkIdSubmodule.getId();
expect(result.id).to.equal(COOKIE_VALUE.publink);
storage.setCookie(PUBLINK_SRV_COOKIE, '', DELETE_COOKIE);
});
it('publink srv local storage', () => {
storage.setDataInLocalStorage(PUBLINK_SRV_COOKIE, JSON.stringify(LOCAL_VALUE));
const result = publinkIdSubmodule.getId();
expect(result.id).to.equal(LOCAL_VALUE.publink);
storage.removeDataFromLocalStorage(PUBLINK_SRV_COOKIE);
});
it('publink cookie', () => {
storage.setCookie(PUBLINK_COOKIE, JSON.stringify(COOKIE_VALUE), COOKIE_EXPIRATION);
const result = publinkIdSubmodule.getId();
expect(result.id).to.equal(COOKIE_VALUE.publink);
storage.setCookie(PUBLINK_COOKIE, '', DELETE_COOKIE);
});
it('publink local storage', () => {
storage.setDataInLocalStorage(PUBLINK_COOKIE, JSON.stringify(LOCAL_VALUE));
const result = publinkIdSubmodule.getId();
expect(result.id).to.equal(LOCAL_VALUE.publink);
storage.removeDataFromLocalStorage(PUBLINK_COOKIE);
});
it('ignore expired cookie', () => {
storage.setDataInLocalStorage(PUBLINK_COOKIE, JSON.stringify({publink: 'value', exp: Date.now() - 60 * 60 * 24 * 1000}));
const result = publinkIdSubmodule.getId();
expect(result.id).to.be.undefined;
storage.removeDataFromLocalStorage(PUBLINK_COOKIE);
});
it('priority goes to publink_srv cookie', () => {
storage.setCookie(PUBLINK_SRV_COOKIE, JSON.stringify(COOKIE_VALUE), COOKIE_EXPIRATION);
storage.setDataInLocalStorage(PUBLINK_COOKIE, JSON.stringify(LOCAL_VALUE));
const result = publinkIdSubmodule.getId();
expect(result.id).to.equal(COOKIE_VALUE.publink);
storage.setCookie(PUBLINK_SRV_COOKIE, '', DELETE_COOKIE);
storage.removeDataFromLocalStorage(PUBLINK_COOKIE);
});
});

describe('getId', () => {
const serverResponse = {publink: 'ec0xHT3yfAOnykP64Qf0ORSi7LjNT1wju04ZSCsoPBekOJdBwK-0Zl_lXKDNnzhauC4iszBc-PvA1Be6IMlh1QocA'};
it('no config', () => {
Expand Down Expand Up @@ -86,40 +134,5 @@ describe('PublinkIdSystem', () => {
expect(callbackSpy.lastCall.lastArg).to.equal(serverResponse.publink);
});
});
describe('fetch from coreid', () => {
let callbackSpy = sinon.spy();
afterEach(() => {
callbackSpy.resetHistory();
delete window.coreid;
});
it('get id', () => {
window.coreid = {};
window.coreid.getPublinkId = (cb) => { cb(TEST_COOKIE_VALUE) };
const submoduleCallback = publinkIdSubmodule.getId({}).callback;
submoduleCallback(callbackSpy);
expect(callbackSpy.calledOnce).to.be.true;
expect(callbackSpy.lastCall.lastArg).to.equal(TEST_COOKIE_VALUE);
});
it('coreid fails', () => {
window.coreid = {};
window.coreid.getPublinkId = (cb) => { cb() };
const submoduleCallback = publinkIdSubmodule.getId({params: {e: 'hashedemailvalue'}}).callback;
submoduleCallback(callbackSpy);
let request = server.requests[0];
expect(request.url).to.equal('https://proc.ad.cpe.dotomi.com/cvx/client/sync/publink?deh=hashedemailvalue&mpn=Prebid.js&mpv=$prebid.version$');
request.respond(200, {}, JSON.stringify(serverResponse));
expect(callbackSpy.calledOnce).to.be.true;
expect(callbackSpy.lastCall.lastArg).to.equal(serverResponse.publink);
});
it('no hashed email param', () => {
window.coreid = {};
window.coreid.getPublinkId = (cb) => { cb() };
const submoduleCallback = publinkIdSubmodule.getId({params: {}}).callback;
submoduleCallback(callbackSpy);
let request = server.requests[0];
expect(request).to.be.undefined;
expect(callbackSpy.calledOnce).to.be.false;
});
});
});
});

0 comments on commit bdc0043

Please sign in to comment.