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

identityLinkSubmodule: add additional check on retrieving the envelope #10355

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
16 changes: 15 additions & 1 deletion modules/identityLinkIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const MODULE_NAME = 'identityLink';

export const storage = getStorageManager({moduleType: MODULE_TYPE_UID, moduleName: MODULE_NAME});

const liverampEnvelopeName = '_lr_env';

/** @type {Submodule} */
export const identityLinkSubmodule = {
/**
Expand Down Expand Up @@ -74,7 +76,14 @@ export const identityLinkSubmodule = {
}
});
} else {
getEnvelope(url, callback, configParams);
// try to get envelope directly from storage if ats lib is not present on a page
let envelope = getEnvelopeFromStorage();
if (envelope) {
utils.logInfo('identityLink: LiveRamp envelope successfully retrieved from storage!');
callback(JSON.parse(envelope).envelope);
} else {
getEnvelope(url, callback, configParams);
}
}
};

Expand Down Expand Up @@ -127,4 +136,9 @@ function setEnvelopeSource(src) {
storage.setCookie('_lr_env_src_ats', src, now.toUTCString());
}

export function getEnvelopeFromStorage() {
let rawEnvelope = storage.getCookie(liverampEnvelopeName) || storage.getDataFromLocalStorage(liverampEnvelopeName);
return rawEnvelope ? window.atob(rawEnvelope) : undefined;
}

submodule('userId', identityLinkSubmodule);
53 changes: 51 additions & 2 deletions test/spec/modules/identityLinkIdSystem_spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import {identityLinkSubmodule} from 'modules/identityLinkIdSystem.js';
import {getEnvelopeFromStorage, identityLinkSubmodule} from 'modules/identityLinkIdSystem.js';
import * as utils from 'src/utils.js';
import {server} from 'test/mocks/xhr.js';
import {getCoreStorageManager} from '../../../src/storageManager.js';
import {stub} from 'sinon';

const storage = getCoreStorageManager();

const pid = '14';
let defaultConfigParams;
const responseHeader = {'Content-Type': 'application/json'}
const responseHeader = {'Content-Type': 'application/json'};
const testEnvelope = 'eyJ0aW1lc3RhbXAiOjE2OTEwNjU5MzQwMTcsInZlcnNpb24iOiIxLjIuMSIsImVudmVsb3BlIjoiQWhIenUyMFN3WHZ6T0hPd3c2bkxaODAtd2hoN2Nnd0FqWllNdkQ0UjBXT25xRVc1N21zR2Vral9QejU2b1FwcGdPOVB2aFJFa3VHc2lMdG56c3A2aG13eDRtTTRNLTctRy12NiJ9';
const testEnvelopeValue = '{"timestamp":1691065934017,"version":"1.2.1","envelope":"AhHzu20SwXvzOHOww6nLZ80-whh7cgwAjZYMvD4R0WOnqEW57msGekj_Pz56oQppgO9PvhREkuGsiLtnzsp6hmwx4mM4M-7-G-v6"}';

function setTestEnvelopeCookie () {
let now = new Date();
now.setTime(now.getTime() + 3000);
storage.setCookie('_lr_env', testEnvelope, now.toUTCString());
}

describe('IdentityLinkId tests', function () {
let logErrorStub;
Expand All @@ -17,6 +26,7 @@ describe('IdentityLinkId tests', function () {
logErrorStub = sinon.stub(utils, 'logError');
// remove _lr_retry_request cookie before test
storage.setCookie('_lr_retry_request', 'true', 'Thu, 01 Jan 1970 00:00:01 GMT');
storage.setCookie('_lr_env', testEnvelope, 'Thu, 01 Jan 1970 00:00:01 GMT');
});

afterEach(function () {
Expand Down Expand Up @@ -163,4 +173,43 @@ describe('IdentityLinkId tests', function () {
let request = server.requests[0];
expect(request).to.be.eq(undefined);
});

it('should get envelope from storage if ats is not present on a page and pass it to callback', function () {
setTestEnvelopeCookie();
let envelopeValueFromStorage = getEnvelopeFromStorage();
let callBackSpy = sinon.spy();
let submoduleCallback = identityLinkSubmodule.getId(defaultConfigParams).callback;
submoduleCallback(callBackSpy);
expect(envelopeValueFromStorage).to.be.a('string');
expect(callBackSpy.calledOnce).to.be.true;
})

it('if there is no envelope in storage and ats is not present on a page try to call 3p url', function () {
let envelopeValueFromStorage = getEnvelopeFromStorage();
let callBackSpy = sinon.spy();
let submoduleCallback = identityLinkSubmodule.getId(defaultConfigParams).callback;
submoduleCallback(callBackSpy);
let request = server.requests[0];
expect(request.url).to.be.eq('https://api.rlcdn.com/api/identity/envelope?pid=14');
request.respond(
204,
responseHeader,
);
expect(envelopeValueFromStorage).to.be.a('undefined');
expect(callBackSpy.calledOnce).to.be.true;
})

it('if ats is present on a page, and envelope is generated and stored in storage, call a callback', function () {
setTestEnvelopeCookie();
let envelopeValueFromStorage = getEnvelopeFromStorage();
window.ats = {retrieveEnvelope: function() {
}}
// mock ats.retrieveEnvelope to return envelope
stub(window.ats, 'retrieveEnvelope').callsFake(function() { return envelopeValueFromStorage })
let callBackSpy = sinon.spy();
let submoduleCallback = identityLinkSubmodule.getId(defaultConfigParams).callback;
submoduleCallback(callBackSpy);
expect(envelopeValueFromStorage).to.be.a('string');
expect(envelopeValueFromStorage).to.be.eq(testEnvelopeValue);
})
});