Skip to content

Commit

Permalink
Lotame panorama id system updates (prebid#6187)
Browse files Browse the repository at this point in the history
* Add the root domain check method as a global method added for use modules

* Add a date suffix to keep the test cookie random

* Per review, moved the findRootDomain method to utils out of the userId modules

* More the domain method back the user module but attach it to the submodule so that userId modules can have access to it

* Remove the storageManager as it is no longer used in utils

* lotamePanoramaIdSubmodule: Add root cookie domain to all cookie writes

* lotamePanoramaIdSubmodule: Don't update profile id storage for a particular error

* lotamePanoramaIdSystem: Switch to indexOf instead of includes to support IE 11
  • Loading branch information
markaconrad authored and stsepelin committed May 28, 2021
1 parent 7547336 commit 74c885d
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 7 deletions.
40 changes: 33 additions & 7 deletions modules/lotamePanoramaIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ const MODULE_NAME = 'lotamePanoramaId';
const NINE_MONTHS_MS = 23328000 * 1000;
const DAYS_TO_CACHE = 7;
const DAY_MS = 60 * 60 * 24 * 1000;
const MISSING_CORE_CONSENT = 111;
const GVLID = 95;

export const storage = getStorageManager(GVLID, MODULE_NAME);
let cookieDomain;

/**
* Set the Lotame First Party Profile ID in the first party namespace
Expand All @@ -27,7 +29,14 @@ export const storage = getStorageManager(GVLID, MODULE_NAME);
function setProfileId(profileId) {
if (storage.cookiesAreEnabled()) {
let expirationDate = new Date(utils.timestamp() + NINE_MONTHS_MS).toUTCString();
storage.setCookie(KEY_PROFILE, profileId, expirationDate, 'Lax', undefined, undefined);
storage.setCookie(
KEY_PROFILE,
profileId,
expirationDate,
'Lax',
cookieDomain,
undefined
);
}
if (storage.hasLocalStorage()) {
storage.setDataInLocalStorage(KEY_PROFILE, profileId, undefined);
Expand Down Expand Up @@ -89,7 +98,7 @@ function saveLotameCache(
value,
expirationDate,
'Lax',
undefined,
cookieDomain,
undefined
);
}
Expand All @@ -116,7 +125,7 @@ function getLotameLocalCache() {
try {
const rawExpiry = getFromStorage(KEY_EXPIRY);
if (utils.isStr(rawExpiry)) {
cache.expiryTimestampMs = parseInt(rawExpiry, 0);
cache.expiryTimestampMs = parseInt(rawExpiry, 10);
}
} catch (error) {
utils.logError(error);
Expand All @@ -133,7 +142,14 @@ function clearLotameCache(key) {
if (key) {
if (storage.cookiesAreEnabled()) {
let expirationDate = new Date(0).toUTCString();
storage.setCookie(key, '', expirationDate, 'Lax', undefined, undefined);
storage.setCookie(
key,
'',
expirationDate,
'Lax',
cookieDomain,
undefined
);
}
if (storage.hasLocalStorage()) {
storage.removeDataFromLocalStorage(key, undefined);
Expand Down Expand Up @@ -174,6 +190,7 @@ export const lotamePanoramaIdSubmodule = {
* @returns {IdResponse|undefined}
*/
getId(config, consentData, cacheIdObj) {
cookieDomain = lotamePanoramaIdSubmodule.findRootDomain();
let localCache = getLotameLocalCache();

let refreshNeeded = Date.now() > localCache.expiryTimestampMs;
Expand Down Expand Up @@ -222,10 +239,17 @@ export const lotamePanoramaIdSubmodule = {
if (response) {
try {
let responseObj = JSON.parse(response);
saveLotameCache(KEY_EXPIRY, responseObj.expiry_ts);
const shouldUpdateProfileId = !(
utils.isArray(responseObj.errors) &&
responseObj.errors.indexOf(MISSING_CORE_CONSENT) !== -1
);

saveLotameCache(KEY_EXPIRY, responseObj.expiry_ts, responseObj.expiry_ts);

if (utils.isStr(responseObj.profile_id)) {
setProfileId(responseObj.profile_id);
if (shouldUpdateProfileId) {
setProfileId(responseObj.profile_id);
}

if (utils.isStr(responseObj.core_id)) {
saveLotameCache(
Expand All @@ -238,7 +262,9 @@ export const lotamePanoramaIdSubmodule = {
clearLotameCache(KEY_ID);
}
} else {
clearLotameCache(KEY_PROFILE);
if (shouldUpdateProfileId) {
clearLotameCache(KEY_PROFILE);
}
clearLotameCache(KEY_ID);
}
} catch (error) {
Expand Down
113 changes: 113 additions & 0 deletions test/spec/modules/lotamePanoramaIdSystem_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,4 +612,117 @@ describe('LotameId', function() {
expect(request.url).to.be.eq('https://id.crwdcntrl.net/id');
});
});

describe('with an empty cache, ignore profile id for error 111', function () {
let request;
let callBackSpy = sinon.spy();

beforeEach(function () {
let submoduleCallback = lotamePanoramaIdSubmodule.getId({}).callback;
submoduleCallback(callBackSpy);

request = server.requests[0];

request.respond(
200,
responseHeader,
JSON.stringify({
profile_id: '4ec137245858469eb94a4e248f238694',
expiry_ts: 10,
errors: [111],
core_id:
'ca22992567e3cd4d116a5899b88a55d0d857a23610db939ae6ac13ba2335d87a',
})
);
});

it('should not save the first party id', function () {
sinon.assert.neverCalledWith(
setLocalStorageStub,
'_cc_id',
'4ec137245858469eb94a4e248f238694'
);
sinon.assert.neverCalledWith(
setCookieStub,
'_cc_id',
'4ec137245858469eb94a4e248f238694'
);
});

it('should save the expiry', function () {
sinon.assert.calledWith(setLocalStorageStub, 'panoramaId_expiry', 10);

sinon.assert.calledWith(setCookieStub, 'panoramaId_expiry', 10);
});

it('should save the id', function () {
sinon.assert.calledWith(
setLocalStorageStub,
'panoramaId',
'ca22992567e3cd4d116a5899b88a55d0d857a23610db939ae6ac13ba2335d87a'
);

sinon.assert.calledWith(
setCookieStub,
'panoramaId',
'ca22992567e3cd4d116a5899b88a55d0d857a23610db939ae6ac13ba2335d87a'
);
});
});

describe('receives an optout request with an error 111', function () {
let request;
let callBackSpy = sinon.spy();

beforeEach(function () {
getCookieStub.withArgs('panoramaId_expiry').returns('1000');
getCookieStub
.withArgs('panoramaId')
.returns(
'ca22992567e3cd4d116a5899b88a55d0d857a23610db939ae6ac13ba2335d87d'
);

let submoduleCallback = lotamePanoramaIdSubmodule.getId({}).callback;
submoduleCallback(callBackSpy);

request = server.requests[0];

request.respond(
200,
responseHeader,
JSON.stringify({
errors: [111],
expiry_ts: Date.now() + 30 * 24 * 60 * 60 * 1000,
})
);
});

it('should call the remote server when getId is called', function () {
expect(callBackSpy.calledOnce).to.be.true;
});

it('should clear the panorama id', function () {
sinon.assert.calledWith(removeFromLocalStorageStub, 'panoramaId');

sinon.assert.calledWith(
setCookieStub,
'panoramaId',
'',
'Thu, 01 Jan 1970 00:00:00 GMT',
'Lax'
);
});

it('should not clear the profile id', function () {
sinon.assert.neverCalledWith(removeFromLocalStorageStub, '_cc_id');

sinon.assert.neverCalledWith(
setCookieStub,
'_cc_id',
'',
'Thu, 01 Jan 1970 00:00:00 GMT',
'Lax'
);
});
});
});

0 comments on commit 74c885d

Please sign in to comment.