Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
10 changes: 1 addition & 9 deletions src/euidSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,7 @@ const productDetails: ProductDetails = {
};

export class EUID extends SdkBase {
private static cookieName = productDetails.cookieName;
// Deprecated. Integrators should never access the cookie directly!
static get COOKIE_NAME() {
console.warn(
'Detected access to EUID.COOKIE_NAME. This is deprecated and will be removed in the future. Integrators should not access the cookie directly.'
);
return EUID.cookieName;
}
static get EuidDetails(): ProductDetails {
private static get EuidDetails(): ProductDetails {
return productDetails;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this should be private to match the corresponding function in uid2Sdk.ts


Expand Down
23 changes: 11 additions & 12 deletions src/integrationTests/euidSdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import { sdkWindow, EUID, __euidInternalHandleScriptLoad, SdkOptions } from '../
import { EventType, CallbackHandler } from '../callbackManager';
import { __euidSSProviderScriptLoad } from '../secureSignalEuid';
import { UidSecureSignalProvider } from '../secureSignal_shared';
import { ProductDetails } from '../product';
import { removeConfig } from '../configManager';
import { ProductDetails } from '../product';

let callback: any;
let asyncCallback: jest.Mock<CallbackHandler>;
let euid: EUID;
let xhrMock: any;
let uid2ESP: UidSecureSignalProvider;
let secureSignalProvidersPushMock: jest.Mock<(p: any) => Promise<void>>;
Expand All @@ -26,12 +25,19 @@ jest.spyOn(document, 'URL', 'get').mockImplementation(() => mockUrl);

const makeIdentity = mocks.makeIdentityV2;

const euidProductDetails: ProductDetails = {
name: 'EUID',
defaultBaseUrl: 'https://prod.euid.eu',
localStorageKey: 'EUID-sdk-identity',
cookieName: '__euid',
};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could possibly import these product details from euidSdk.ts and same for the uid2 product details from uid2Sdk.ts instead of redefining them in multiple places - but the point is that productDetails is private and if we exported them to import in these test files, would that make them public now?

const getConfigCookie = () => {
const docCookie = document.cookie;
if (docCookie) {
const payload = docCookie
.split('; ')
.find((row) => row.startsWith(EUID.COOKIE_NAME + '_config' + '='));
.find((row) => row.startsWith(euidProductDetails.cookieName + '_config' + '='));
if (payload) {
return JSON.parse(decodeURIComponent(payload.split('=')[1]));
}
Expand Down Expand Up @@ -150,17 +156,10 @@ describe('Store config EUID', () => {
useCookie: false,
};

const productDetails: ProductDetails = {
cookieName: '__euid',
defaultBaseUrl: 'http://test-host',
localStorageKey: 'EUID-sdk-identity',
name: 'EUID',
};

beforeEach(() => {
sdkWindow.__euid = new EUID();
document.cookie =
EUID.COOKIE_NAME + '_config' + '=;expires=Tue, 1 Jan 1980 23:59:59 GMT;path=/';
euidProductDetails.cookieName + '_config' + '=;expires=Tue, 1 Jan 1980 23:59:59 GMT;path=/';
});

afterEach(() => {
Expand Down Expand Up @@ -190,7 +189,7 @@ describe('Store config EUID', () => {
});
let cookie = getConfigCookie();
expect(cookie).toBeInstanceOf(Object);
removeConfig({ ...options, useCookie: true }, productDetails);
removeConfig({ ...options, useCookie: true }, euidProductDetails);
cookie = getConfigCookie();
expect(cookie).toBeNull();
});
Expand Down
38 changes: 20 additions & 18 deletions src/integrationTests/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ mocks.setupFakeTime();
const mockDomain = 'www.uidapi.com';
const mockUrl = `http://${mockDomain}/test/index.html`;

const uid2ProductDetails: ProductDetails = {
name: 'UID2',
defaultBaseUrl: 'https://prod.uidapi.com',
localStorageKey: 'UID2-sdk-identity',
cookieName: '__uid_2',
};

beforeEach(() => {
callback = jest.fn();
uid2 = new UID2();
Expand All @@ -41,7 +48,7 @@ const getConfigCookie = () => {
if (docCookie) {
const payload = docCookie
.split('; ')
.find((row) => row.startsWith(UID2.COOKIE_NAME + '_config' + '='));
.find((row) => row.startsWith(uid2ProductDetails.cookieName + '_config' + '='));
if (payload) {
return JSON.parse(decodeURIComponent(payload.split('=')[1]));
}
Expand Down Expand Up @@ -69,7 +76,7 @@ describe('cookieDomain option', () => {
});

test('should not mention domain in the cookie string', () => {
const cookie = cookieMock.getSetCookieString(UID2.COOKIE_NAME);
const cookie = cookieMock.getSetCookieString(uid2ProductDetails.cookieName);
expect(cookie).not.toBe('');
expect(cookie).not.toContain('Domain=');
});
Expand All @@ -88,7 +95,7 @@ describe('cookieDomain option', () => {
});

test('should use domain in the cookie string', () => {
const cookie = cookieMock.getSetCookieString(UID2.COOKIE_NAME);
const cookie = cookieMock.getSetCookieString(uid2ProductDetails.cookieName);
expect(cookie).toContain(`Domain=${domain};`);
});
});
Expand All @@ -105,7 +112,7 @@ describe('cookiePath option', () => {
});

test('should use the default path in the cookie string', () => {
const cookie = cookieMock.getSetCookieString(UID2.COOKIE_NAME) as string;
const cookie = cookieMock.getSetCookieString(uid2ProductDetails.cookieName) as string;
expect(cookie + ';').toContain('Path=/;');
});
});
Expand All @@ -123,7 +130,7 @@ describe('cookiePath option', () => {
});

test('should use custom path in the cookie string', () => {
const cookie = cookieMock.getSetCookieString(UID2.COOKIE_NAME) as string;
const cookie = cookieMock.getSetCookieString(uid2ProductDetails.cookieName) as string;
expect(cookie + ';').toContain(`Path=${path};`);
});
});
Expand Down Expand Up @@ -351,7 +358,7 @@ describe('multiple init calls', () => {
});

test('should update cookie manager', () => {
const cookie = cookieMock.getSetCookieString(UID2.COOKIE_NAME);
const cookie = cookieMock.getSetCookieString(uid2ProductDetails.cookieName);
expect(cookie).toContain(`Domain=${cookieDomain};`);
expect(cookie + ';').toContain(`Path=${newCookiePath};`);
const configCookie = getConfigCookie();
Expand Down Expand Up @@ -508,18 +515,13 @@ describe('Store config UID2', () => {
refreshRetryPeriod: 1000,
useCookie: false,
};
const productDetails: ProductDetails = {
cookieName: '__uid2',
defaultBaseUrl: 'http://test-host',
localStorageKey: 'UID2-sdk-identity',
name: 'UID2',
};

const previousOptions: SdkOptions = options;

beforeEach(() => {
localStorage.removeItem('UID2-sdk-identity_config');
document.cookie =
UID2.COOKIE_NAME + '_config' + '=;expires=Tue, 1 Jan 1980 23:59:59 GMT;path=/';
uid2ProductDetails.cookieName + '_config' + '=;expires=Tue, 1 Jan 1980 23:59:59 GMT;path=/';
});

describe('when useCookie is true', () => {
Expand All @@ -528,14 +530,14 @@ describe('Store config UID2', () => {
const cookie = getConfigCookie();
expect(cookie).toBeInstanceOf(Object);
expect(cookie).toHaveProperty('cookieDomain');
const storageConfig = loadConfig(productDetails);
const storageConfig = getConfigStorage();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should have been getConfigStorage() all along. loadConfig can return the config from local storage or the cookie, but we are testing that it is not in local storage

expect(storageConfig).toBeNull();
});
});
describe('when useCookie is false', () => {
test('should store config in local storage', () => {
uid2.init({ callback: callback, identity: identity, ...options });
const storageConfig = loadConfig(productDetails);
const storageConfig = getConfigStorage();
expect(storageConfig).toBeInstanceOf(Object);
expect(storageConfig).toHaveProperty('cookieDomain');
const cookie = getConfigCookie();
Expand All @@ -545,11 +547,11 @@ describe('Store config UID2', () => {
describe('when useCookie is false', () => {
test('can successfully clear the config in storage', () => {
uid2.init({ callback: callback, identity: identity, ...options });
let storageConfig = loadConfig(productDetails);
let storageConfig = loadConfig(uid2ProductDetails);
expect(storageConfig).toBeInstanceOf(Object);
expect(storageConfig).toHaveProperty('cookieDomain');
removeConfig(previousOptions, productDetails);
storageConfig = loadConfig(productDetails);
removeConfig(previousOptions, uid2ProductDetails);
storageConfig = loadConfig(uid2ProductDetails);
expect(storageConfig).toBeNull();
});
});
Expand Down
15 changes: 12 additions & 3 deletions src/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import * as jsdom from 'jsdom';
import { Cookie } from 'tough-cookie';
import { UID2 } from './uid2Sdk';
import { Identity } from './Identity';
import { base64ToBytes, bytesToBase64 } from './encoding/base64';
import * as crypto from 'crypto';
import { ProductDetails } from './product';

const uid2LocalStorageKeyName = 'UID2-sdk-identity';
const euidLocalStorageKeyName = 'EUID-sdk-identity';

const uid2ProductDetails: ProductDetails = {
name: 'UID2',
defaultBaseUrl: 'https://prod.uidapi.com',
localStorageKey: 'UID2-sdk-identity',
cookieName: '__uid_2',
};

export class CookieMock {
jar: jsdom.CookieJar;
url: string;
Expand Down Expand Up @@ -281,7 +288,7 @@ export function setCookieMock(document: Document) {
}

export function setUid2Cookie(value: any) {
document.cookie = UID2.COOKIE_NAME + '=' + encodeURIComponent(JSON.stringify(value));
document.cookie = uid2ProductDetails.cookieName + '=' + encodeURIComponent(JSON.stringify(value));
}

export function removeUid2Cookie() {
Expand All @@ -304,7 +311,9 @@ export function setUid2(value: any, useCookie?: boolean) {
export function getUid2Cookie() {
const docCookie = document.cookie;
if (docCookie) {
const payload = docCookie.split('; ').find((row) => row.startsWith(UID2.COOKIE_NAME + '='));
const payload = docCookie
.split('; ')
.find((row) => row.startsWith(uid2ProductDetails.cookieName + '='));
if (payload) {
return JSON.parse(decodeURIComponent(payload.split('=')[1]));
}
Expand Down
8 changes: 0 additions & 8 deletions src/uid2Sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ const productDetails: ProductDetails = {
};

export class UID2 extends SdkBase {
private static cookieName = productDetails.cookieName;
// Deprecated. Integrators should never access the cookie directly!
static get COOKIE_NAME() {
console.warn(
'Detected access to UID2.COOKIE_NAME. This is deprecated and will be removed in the future. Integrators should not access the cookie directly.'
);
return UID2.cookieName;
}
private static get Uid2Details(): ProductDetails {
return productDetails;
}
Expand Down
Loading