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

Lotame panorama id system updates #6187

Merged
merged 11 commits into from
Jan 21, 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
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 @@ -211,10 +228,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);
Copy link

Choose a reason for hiding this comment

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

should this key-value ever get cleared from the cache?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is used to tell us when we should check the server again so removing is the same as not updating it and there is no practical benefit to explicitly clear this value from the cache


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 @@ -227,7 +251,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 @@ -446,4 +446,117 @@ describe('LotameId', function() {
'lotamePanoramaId': '1234'
});
});

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'
);
});
});
});