Skip to content

Commit

Permalink
attempt to fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smenzer committed Nov 9, 2020
1 parent 61c6fe7 commit a9a49d9
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions modules/pubCommonIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ const SHAREDID_DEFAULT_STATE = false;

const storage = getStorageManager(null, 'pubCommonId');

let COOKIE_DOMAIN;

/**
* Store sharedid in either cookie or local storage
* @param {Object} config Need config.storage object to derive key, expiry time, and storage type.
Expand All @@ -39,7 +37,7 @@ function storeData(config, value) {

if (config.storage.type === COOKIE) {
if (storage.cookiesAreEnabled()) {
storage.setCookie(key, value, expiresStr, 'LAX', pubCommonIdSubmodule.domainOverride());
storage.setCookie(key, value, expiresStr, 'LAX', pubCommonIdSubmodule.COOKIE_DOMAIN);
}
} else if (config.storage.type === LOCAL_STORAGE) {
if (storage.hasLocalStorage()) {
Expand Down Expand Up @@ -162,6 +160,8 @@ export const pubCommonIdSubmodule = {
*/
name: MODULE_NAME,

COOKIE_DOMAIN: undefined,

/**
* Return a callback function that calls the pixelUrl with id as a query parameter
* @param pixelUrl
Expand Down Expand Up @@ -282,33 +282,41 @@ export const pubCommonIdSubmodule = {
* @return {(string|undefined)}
*/
domainOverride: function () {
if (COOKIE_DOMAIN) {
return COOKIE_DOMAIN;
}
return getDomainOverride();
}
};

const domainElements = document.domain.split('.');
const cookieName = `_gd${Date.now()}`;
for (let i = 0, testCookie; i < domainElements.length; i++) {
const nextDomain = domainElements.slice(i).join('.');
/**
* test for the domain to set cookies in
* @return {(string|undefined)}
*/
function getDomainOverride() {
if (pubCommonIdSubmodule.COOKIE_DOMAIN) {
return pubCommonIdSubmodule.COOKIE_DOMAIN;
}

// write test cookie
storage.setCookie(cookieName, '1', undefined, undefined, nextDomain);
const domainElements = document.domain.split('.');
const cookieName = `_gd${Date.now()}`;
for (let i = 0, testCookie; i < domainElements.length; i++) {
const nextDomain = domainElements.slice(i).join('.');

// read test cookie to verify domain was valid
testCookie = storage.getCookie(cookieName);
// write test cookie
storage.setCookie(cookieName, '1', undefined, undefined, nextDomain);

// delete test cookie
storage.setCookie(cookieName, '', 'Thu, 01 Jan 1970 00:00:01 GMT', undefined, nextDomain);
// read test cookie to verify domain was valid
testCookie = storage.getCookie(cookieName);

if (testCookie === '1') {
// cookie was written successfully using test domain so the COOKIE_DOMAIN is updated
COOKIE_DOMAIN = nextDomain;
} else {
// cookie failed to write using test domain so exit by returning the COOKIE_DOMAIN
return COOKIE_DOMAIN;
}
// delete test cookie
storage.setCookie(cookieName, '', 'Thu, 01 Jan 1970 00:00:01 GMT', undefined, nextDomain);

if (testCookie === '1') {
// cookie was written successfully using test domain so the topDomain is updated
pubCommonIdSubmodule.COOKIE_DOMAIN = nextDomain;
} else {
// cookie failed to write using test domain so exit by returning the topDomain
return pubCommonIdSubmodule.COOKIE_DOMAIN;
}
}
};
}

submodule('userId', pubCommonIdSubmodule);

0 comments on commit a9a49d9

Please sign in to comment.