Skip to content

Commit

Permalink
Merge pull request #95 from IABTechLab/sas-UID2-3837-allow-include-js…
Browse files Browse the repository at this point in the history
…-sdk-multiple-times

fix config removal
  • Loading branch information
ssundahlTTD authored Aug 13, 2024
2 parents ece5ba3 + b89b6cf commit 41dc686
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 40 deletions.
12 changes: 10 additions & 2 deletions src/configManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ export const loadConfig = (
}
};

export const removeConfig = (options: SdkOptions, productDetails: ProductDetails) => {
if (options.useCookie) {
removeConfigCookie(options, productDetails);
} else {
removeConfigFromLocalStorage(productDetails);
}
};

const setConfigCookie = (options: SdkOptions, productDetails: ProductDetails) => {
const cookieDomain = options.cookieDomain;
const path = options.cookiePath ?? '/';
Expand All @@ -48,9 +56,9 @@ const setConfigCookie = (options: SdkOptions, productDetails: ProductDetails) =>
document.cookie = cookie;
};

const removeConfigCookie = (productDetails: ProductDetails) => {
const removeConfigCookie = (options: SdkOptions, productDetails: ProductDetails) => {
document.cookie =
productDetails.cookieName + '_config' + '=;expires=Tue, 1 Jan 1980 23:59:59 GMT';
productDetails.cookieName + '_config' + '=;expires=Tue, 1 Jan 1980 23:59:59 GMT;path=/';
};

const getConfigCookie = (productDetails: ProductDetails) => {
Expand Down
64 changes: 38 additions & 26 deletions src/integrationTests/euidSdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ 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';

let callback: any;
let asyncCallback: jest.Mock<CallbackHandler>;
Expand All @@ -19,6 +21,8 @@ const debugOutput = false;

mocks.setupFakeTime();
const mockDomain = 'www.uidapi.com';
const mockUrl = `http://${mockDomain}/test/index.html`;
jest.spyOn(document, 'URL', 'get').mockImplementation(() => mockUrl);

const makeIdentity = mocks.makeIdentityV2;

Expand All @@ -35,15 +39,6 @@ const getConfigCookie = () => {
return null;
};

const getConfigStorage = () => {
const data = localStorage.getItem('UID2-sdk-identity_config');
if (data) {
const result = JSON.parse(data);
return result;
}
return null;
};

describe('when a callback is provided', () => {
beforeEach(() => {
jest.clearAllMocks();
Expand Down Expand Up @@ -130,21 +125,18 @@ describe('when a callback is provided', () => {
});

describe('When SDK initialized after both SDK and SS script loaded - EUID', () => {
test.only('should send identity to Google ESP', async () => {
test('should send identity to Google ESP', async () => {
__euidInternalHandleScriptLoad();
__euidSSProviderScriptLoad();
(sdkWindow.__euid as EUID).init({ identity });

expect(secureSignalProvidersPushMock).toHaveBeenCalledTimes(1);
await expect(secureSignalProvidersPushMock).toHaveBeenCalledWith(
expect.objectContaining({
id: 'euid.eu',
})
);
await mocks.flushPromises();
expect(await secureSignalProvidersPushMock.mock.results[0].value).toBe(
identity.advertising_token
);
expect(await secureSignalProvidersPushMock.mock.results[0].value).toBe('testToken');
});
});
});
Expand All @@ -158,28 +150,48 @@ describe('Store config EUID', () => {
useCookie: false,
};

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

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

afterEach(() => {
sdkWindow.__euid = undefined;
});

describe('when useCookie is true', () => {
test('should store config in cookie', () => {
euid.init({ callback: callback, identity: identity, ...options, useCookie: true });
(sdkWindow.__euid as EUID).init({
callback: callback,
identity: identity,
...options,
useCookie: true,
});
const cookie = getConfigCookie();
expect(cookie).toBeInstanceOf(Object);
expect(cookie).toHaveProperty('cookieDomain');
const storageConfig = getConfigStorage();
expect(storageConfig).toBeNull();
});
});
describe('when useCookie is false', () => {
test('should store config in local storage', () => {
euid.init({ callback: callback, identity: identity, ...options });
const storageConfig = getConfigStorage();
expect(storageConfig).toBeInstanceOf(Object);
expect(storageConfig).toHaveProperty('cookieDomain');
const cookie = getConfigCookie();
describe('when useCookie is true', () => {
test('can successfully clear the config cookie', () => {
(sdkWindow.__euid as EUID).init({
callback: callback,
identity: identity,
...options,
useCookie: true,
});
let cookie = getConfigCookie();
expect(cookie).toBeInstanceOf(Object);
removeConfig({ ...options, useCookie: true }, productDetails);
cookie = getConfigCookie();
expect(cookie).toBeNull();
});
});
Expand Down
35 changes: 23 additions & 12 deletions src/integrationTests/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { afterEach, beforeEach, describe, expect, jest, test } from '@jest/globa

import * as mocks from '../mocks';
import { SdkOptions, sdkWindow, UID2 } from '../uid2Sdk';
import { loadConfig, removeConfig } from '../configManager';
import { ProductDetails } from '../product';

let callback: any;
let uid2: UID2;
Expand Down Expand Up @@ -46,15 +48,6 @@ const getConfigCookie = () => {
return null;
};

const getConfigStorage = () => {
const data = localStorage.getItem('UID2-sdk-identity_config');
if (data) {
const result = JSON.parse(data);
return result;
}
return null;
};

describe('cookieDomain option', () => {
describe('when using default value', () => {
beforeEach(() => {
Expand Down Expand Up @@ -224,10 +217,17 @@ describe('Store config UID2', () => {
refreshRetryPeriod: 1000,
useCookie: false,
};
const productDetails: ProductDetails = {
cookieName: '__uid2',
defaultBaseUrl: 'http://test-host',
localStorageKey: 'UID2-sdk-identity',
name: 'UID2',
};

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

describe('when useCookie is true', () => {
Expand All @@ -236,18 +236,29 @@ describe('Store config UID2', () => {
const cookie = getConfigCookie();
expect(cookie).toBeInstanceOf(Object);
expect(cookie).toHaveProperty('cookieDomain');
const storageConfig = getConfigStorage();
const storageConfig = loadConfig(options, productDetails);
expect(storageConfig).toBeNull();
});
});
describe('when useCookie is false', () => {
test('should store config in local storage', () => {
uid2.init({ callback: callback, identity: identity, ...options });
const storageConfig = getConfigStorage();
const storageConfig = loadConfig(options, productDetails);
expect(storageConfig).toBeInstanceOf(Object);
expect(storageConfig).toHaveProperty('cookieDomain');
const cookie = getConfigCookie();
expect(cookie).toBeNull();
});
});
describe('when useCookie is false', () => {
test('can successfully clear the config in storage', () => {
uid2.init({ callback: callback, identity: identity, ...options });
let storageConfig = loadConfig(options, productDetails);
expect(storageConfig).toBeInstanceOf(Object);
expect(storageConfig).toHaveProperty('cookieDomain');
removeConfig(options, productDetails);
storageConfig = loadConfig(options, productDetails);
expect(storageConfig).toBeNull();
});
});
});

0 comments on commit 41dc686

Please sign in to comment.