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

FreepassIdSystem: get userId from cookie #10298

Merged
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
28 changes: 16 additions & 12 deletions modules/freepassIdSystem.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { submodule } from '../src/hook.js';
import {generateUUID, logMessage} from '../src/utils.js';
import { logMessage } from '../src/utils.js';
import { getCoreStorageManager } from '../src/storageManager.js';

const MODULE_NAME = 'freepassId';

export const FREEPASS_COOKIE_KEY = '_f_UF8cCRlr';
export const storage = getCoreStorageManager(MODULE_NAME);

export const freepassIdSubmodule = {
name: MODULE_NAME,
decode: function (value, config) {
Expand All @@ -15,7 +19,12 @@ export const freepassIdSubmodule = {
logMessage('Getting FreePass ID using config: ' + JSON.stringify(config));

const freepassData = config.params !== undefined ? (config.params.freepassData || {}) : {}
let idObject = {userId: generateUUID()};
const idObject = {};

const userId = storage.getCookie(FREEPASS_COOKIE_KEY);
if (userId !== null) {
idObject.userId = userId;
}

if (freepassData.commonId !== undefined) {
idObject.commonId = config.params.freepassData.commonId;
Expand All @@ -29,21 +38,16 @@ export const freepassIdSubmodule = {
},

extendId: function (config, consent, cachedIdObject) {
let freepassData = config.params.freepassData;
let hasFreepassData = freepassData !== undefined;
const freepassData = config.params.freepassData;
const hasFreepassData = freepassData !== undefined;
if (!hasFreepassData) {
logMessage('No Freepass Data. CachedIdObject will not be extended: ' + JSON.stringify(cachedIdObject));
return {
id: cachedIdObject
};
}

if (freepassData.commonId === cachedIdObject.commonId && freepassData.userIp === cachedIdObject.userIp) {
logMessage('FreePass ID is already up-to-date: ' + JSON.stringify(cachedIdObject));
return {
id: cachedIdObject
};
}
const currentCookieId = storage.getCookie(FREEPASS_COOKIE_KEY);

logMessage('Extending FreePass ID object: ' + JSON.stringify(cachedIdObject));
logMessage('Extending FreePass ID using config: ' + JSON.stringify(config));
Expand All @@ -52,8 +56,8 @@ export const freepassIdSubmodule = {
id: {
commonId: freepassData.commonId,
userIp: freepassData.userIp,
userId: cachedIdObject.userId,
},
userId: currentCookieId
}
};
}
};
Expand Down
87 changes: 27 additions & 60 deletions test/spec/modules/freepassIdSystem_spec.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import {freepassIdSubmodule} from 'modules/freepassIdSystem';
import { freepassIdSubmodule, storage, FREEPASS_COOKIE_KEY } from 'modules/freepassIdSystem';
import sinon from 'sinon';
import * as utils from '../../../src/utils';

let expect = require('chai').expect;

describe('FreePass ID System', function () {
const UUID = '15fde1dc-1861-4894-afdf-b757272f3568';
let getCookieStub;

before(function () {
sinon.stub(utils, 'generateUUID').returns(UUID);
sinon.stub(utils, 'logMessage');
getCookieStub = sinon.stub(storage, 'getCookie');
});

after(function () {
utils.generateUUID.restore();
utils.logMessage.restore();
getCookieStub.restore();
});

describe('freepassIdSubmodule', function () {
Expand All @@ -39,71 +40,19 @@ describe('FreePass ID System', function () {
};

it('should return an IdObject with a UUID', function () {
getCookieStub.withArgs(FREEPASS_COOKIE_KEY).returns(UUID);
const objectId = freepassIdSubmodule.getId(config, undefined);
expect(objectId).to.be.an('object');
expect(objectId.id).to.be.an('object');
expect(objectId.id.userId).to.equal(UUID);
});

it('should include userIp in IdObject', function () {
it('should return an IdObject without UUID when absent in cookie', function () {
getCookieStub.withArgs(FREEPASS_COOKIE_KEY).returns(null);
const objectId = freepassIdSubmodule.getId(config, undefined);
expect(objectId).to.be.an('object');
expect(objectId.id).to.be.an('object');
expect(objectId.id.userIp).to.equal('127.0.0.1');
});
it('should skip userIp in IdObject if not available', function () {
const localConfig = Object.assign({}, config);
delete localConfig.params.freepassData.userIp;
const objectId = freepassIdSubmodule.getId(localConfig, undefined);
expect(objectId).to.be.an('object');
expect(objectId.id).to.be.an('object');
expect(objectId.id.userIp).to.be.undefined;
});
it('should skip userIp in IdObject if freepassData is not available', function () {
const localConfig = JSON.parse(JSON.stringify(config));
delete localConfig.params.freepassData;
const objectId = freepassIdSubmodule.getId(localConfig, undefined);
expect(objectId).to.be.an('object');
expect(objectId.id).to.be.an('object');
expect(objectId.id.userIp).to.be.undefined;
});
it('should skip userIp in IdObject if params is not available', function () {
const localConfig = JSON.parse(JSON.stringify(config));
delete localConfig.params;
const objectId = freepassIdSubmodule.getId(localConfig, undefined);
expect(objectId).to.be.an('object');
expect(objectId.id).to.be.an('object');
expect(objectId.id.userIp).to.be.undefined;
});
it('should include commonId in IdObject', function () {
const objectId = freepassIdSubmodule.getId(config, undefined);
expect(objectId).to.be.an('object');
expect(objectId.id).to.be.an('object');
expect(objectId.id.commonId).to.equal('commonId');
});
it('should skip commonId in IdObject if not available', function () {
const localConfig = Object.assign({}, config);
delete localConfig.params.freepassData.commonId;
const objectId = freepassIdSubmodule.getId(localConfig, undefined);
expect(objectId).to.be.an('object');
expect(objectId.id).to.be.an('object');
expect(objectId.id.commonId).to.be.undefined;
});
it('should skip commonId in IdObject if freepassData is not available', function () {
const localConfig = JSON.parse(JSON.stringify(config));
delete localConfig.params.freepassData;
const objectId = freepassIdSubmodule.getId(localConfig, undefined);
expect(objectId).to.be.an('object');
expect(objectId.id).to.be.an('object');
expect(objectId.id.commonId).to.be.undefined;
});
it('should skip commonId in IdObject if params is not available', function () {
const localConfig = JSON.parse(JSON.stringify(config));
delete localConfig.params;
const objectId = freepassIdSubmodule.getId(localConfig, undefined);
expect(objectId).to.be.an('object');
expect(objectId.id).to.be.an('object');
expect(objectId.id.commonId).to.be.undefined;
expect(objectId.id.userId).to.be.undefined;
});
});

Expand Down Expand Up @@ -142,12 +91,15 @@ describe('FreePass ID System', function () {
};

it('should return cachedIdObject if there are no changes', function () {
getCookieStub.withArgs(FREEPASS_COOKIE_KEY).returns(UUID);
const idObject = freepassIdSubmodule.getId(config, undefined);
const cachedIdObject = Object.assign({}, idObject.id);
const extendedIdObject = freepassIdSubmodule.extendId(config, undefined, cachedIdObject);
expect(extendedIdObject).to.be.an('object');
expect(extendedIdObject.id).to.be.an('object');
expect(extendedIdObject.id).to.equal(cachedIdObject);
expect(extendedIdObject.id.userId).to.equal(UUID);
expect(extendedIdObject.id.userIp).to.equal(config.params.freepassData.userIp);
expect(extendedIdObject.id.commonId).to.equal(config.params.freepassData.commonId);
});

it('should return cachedIdObject if there are no new data', function () {
Expand Down Expand Up @@ -182,5 +134,20 @@ describe('FreePass ID System', function () {
expect(extendedIdObject.id).to.be.an('object');
expect(extendedIdObject.id.userIp).to.equal('192.168.1.1');
});

it('should return new userId when changed from cache', function () {
getCookieStub.withArgs(FREEPASS_COOKIE_KEY).returns(UUID);
const idObject = freepassIdSubmodule.getId(config, undefined);
const cachedIdObject = Object.assign({}, idObject.id);
const localConfig = JSON.parse(JSON.stringify(config));
localConfig.params.freepassData.userIp = '192.168.1.1';

getCookieStub.withArgs(FREEPASS_COOKIE_KEY).returns('NEW_UUID');
const extendedIdObject = freepassIdSubmodule.extendId(localConfig, undefined, cachedIdObject);
expect(extendedIdObject).to.be.an('object');
expect(extendedIdObject.id).to.be.an('object');
expect(extendedIdObject.id.userIp).to.equal('192.168.1.1');
expect(extendedIdObject.id.userId).to.equal('NEW_UUID');
});
});
});