Skip to content

Commit

Permalink
Kargo migration to localstorage alternatives (#3672)
Browse files Browse the repository at this point in the history
* moving kargo adaptor away from cookie usage

* kargo adapter should fall back to cookies
  • Loading branch information
samuelhorwitz authored and jsnellbaker committed Mar 26, 2019
1 parent 12882f9 commit 010f203
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 90 deletions.
50 changes: 23 additions & 27 deletions modules/kargoBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,40 +91,37 @@ export const spec = {
return null;
},

_getCrbIds() {
_getCrbFromCookie() {
try {
const crb = JSON.parse(decodeURIComponent(spec._readCookie('krg_crb')));
let syncIds = {};

if (crb && crb.v) {
let vParsed = JSON.parse(atob(crb.v));

if (vParsed && vParsed.syncIds) {
syncIds = vParsed.syncIds;
if (vParsed) {
return vParsed;
}
}

return syncIds;
return {};
} catch (e) {
return {};
}
},

_getUid() {
_getCrbFromLocalStorage() {
try {
const uid = JSON.parse(decodeURIComponent(spec._readCookie('krg_uid')));
let vData = {};

if (uid && uid.v) {
vData = uid.v;
}

return vData;
return JSON.parse(atob(spec._getLocalStorageSafely('krg_crb')));
} catch (e) {
return {};
}
},

_getCrb() {
let localStorageCrb = spec._getCrbFromLocalStorage();
if (Object.keys(localStorageCrb).length) {
return localStorageCrb;
}
return spec._getCrbFromCookie();
},

_getKruxUserId() {
return spec._getLocalStorageSafely('kxkar_user');
},
Expand Down Expand Up @@ -156,28 +153,27 @@ export const spec = {
},

_getUserIds() {
const uid = spec._getUid();
const crbIds = spec._getCrbIds();

const crb = spec._getCrb();
return {
kargoID: uid.userId,
clientID: uid.clientId,
crbIDs: crbIds,
optOut: uid.optOut
kargoID: crb.userId,
clientID: crb.clientId,
crbIDs: crb.syncIds || {},
optOut: crb.optOut
};
},

_getClientId() {
const uid = spec._getUid();
return uid.clientId;
const crb = spec._getCrb();
return crb.clientId;
},

_getAllMetadata() {
return {
userIDs: spec._getUserIds(),
krux: spec._getKrux(),
pageURL: window.location.href,
rawCRB: spec._readCookie('krg_crb')
rawCRB: spec._readCookie('krg_crb'),
rawCRBLocalStorage: spec._getLocalStorageSafely('krg_crb')
};
},

Expand Down
137 changes: 74 additions & 63 deletions test/spec/modules/kargoBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,59 +134,74 @@ describe('kargo adapter tests', function () {
setLocalStorageItem('kxkar_segs', 'qv9v984dy,rpx2gy365,qrd5u4axv,rnub9nmtd,reha00jnu');
}

function initializeKrgUid() {
setCookie('krg_uid', '%7B%22v%22%3A%7B%22userId%22%3A%225f108831-302d-11e7-bf6b-4595acd3bf6c%22%2C%22clientId%22%3A%222410d8f2-c111-4811-88a5-7b5e190e475f%22%2C%22optOut%22%3Afalse%7D%7D');
}

function getKrgCrb() {
return '%7B%22v%22%3A%22eyJzeW5jSWRzIjp7IjIiOiI4MmZhMjU1NS01OTY5LTQ2MTQtYjRjZS00ZGNmMTA4MGU5ZjkiLCIxNiI6IlZveElrOEFvSnowQUFFZENleUFBQUFDMiY1MDIiLCIyMyI6ImQyYTg1NWE1LTFiMWMtNDMwMC05NDBlLWE3MDhmYTFmMWJkZSIsIjI0IjoiVm94SWs4QW9KejBBQUVkQ2V5QUFBQUMyJjUwMiIsIjI1IjoiNWVlMjQxMzgtNWUwMy00YjlkLWE5NTMtMzhlODMzZjI4NDlmIiwiMl84MCI6ImQyYTg1NWE1LTFiMWMtNDMwMC05NDBlLWE3MDhmYTFmMWJkZSIsIjJfOTMiOiI1ZWUyNDEzOC01ZTAzLTRiOWQtYTk1My0zOGU4MzNmMjg0OWYifSwiZXhwaXJlVGltZSI6MTQ5NzQ0OTM4MjY2OCwibGFzdFN5bmNlZEF0IjoxNDk3MzYyOTc5MDEyfQ%3D%3D%22%7D';
return 'eyJzeW5jSWRzIjp7IjIiOiI4MmZhMjU1NS01OTY5LTQ2MTQtYjRjZS00ZGNmMTA4MGU5ZjkiLCIxNiI6IlZveElrOEFvSnowQUFFZENleUFBQUFDMiY1MDIiLCIyMyI6ImQyYT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W50SWQiOiIyNDEwZDhmMi1jMTExLTQ4MTEtODhhNS03YjVlMTkwZTQ3NWYiLCJvcHRPdXQiOmZhbHNlLCJleHBpcmVUaW1lIjoxNDk3NDQ5MzgyNjY4LCJsYXN0U3luY2VkQXQiOjE0OTczNjI5NzkwMTJ9%22%7D';
}

function initializeInvalidKrgUid() {
setCookie('krg_uid', 'invalid-krg-uid');
function initializeKrgCrb(cookieOnly) {
if (!cookieOnly) {
setLocalStorageItem('krg_crb', getKrgCrb());
}
setCookie('krg_crb', getKrgCrbOldStyle());
}

function getInvalidKrgCrbType1() {
return 'invalid-krg-crb';
}

function initializeInvalidKrgCrbType1() {
setLocalStorageItem('krg_crb', getInvalidKrgCrbType1());
}

function initializeInvalidKrgCrbType1Cookie() {
setCookie('krg_crb', getInvalidKrgCrbType1());
}

function getInvalidKrgCrbType2() {
return 'Ly8v';
}

function getInvalidKrgCrbType2OldStyle() {
return '%7B%22v%22%3A%22%26%26%26%26%26%26%22%7D';
}

function initializeInvalidKrgCrbType2() {
setCookie('krg_crb', getInvalidKrgCrbType2());
setLocalStorageItem('krg_crb', getInvalidKrgCrbType2());
}

function getInvalidKrgCrbType3() {
return '%7B%22v%22%3A%22Ly8v%22%7D';
function initializeInvalidKrgCrbType2Cookie() {
setCookie('krg_crb', getInvalidKrgCrbType2OldStyle());
}

function initializeInvalidKrgCrbType3() {
setCookie('krg_crb', getInvalidKrgCrbType3());
function getInvalidKrgCrbType3OldStyle() {
return '%7B%22v%22%3A%22Ly8v%22%7D';
}

function initializeEmptyKrgUid() {
setCookie('krg_uid', '%7B%7D');
function initializeInvalidKrgCrbType3Cookie() {
setCookie('krg_crb', getInvalidKrgCrbType3OldStyle());
}

function getEmptyKrgCrb() {
return 'eyJleHBpcmVUaW1lIjoxNDk3NDQ5MzgyNjY4LCJsYXN0U3luY2VkQXQiOjE0OTczNjI5NzkwMTJ9';
}

function getEmptyKrgCrbOldStyle() {
return '%7B%22v%22%3A%22eyJleHBpcmVUaW1lIjoxNDk3NDQ5MzgyNjY4LCJsYXN0U3luY2VkQXQiOjE0OTczNjI5NzkwMTJ9%22%7D';
}

function initializeEmptyKrgCrb() {
setCookie('krg_crb', getEmptyKrgCrb());
setLocalStorageItem('krg_crb', getEmptyKrgCrb());
}

function getExpectedKrakenParams(excludeUserIds, excludeKrux, expectedRawCRB) {
function initializeEmptyKrgCrbCookie() {
setCookie('krg_crb', getEmptyKrgCrbOldStyle());
}

function getExpectedKrakenParams(excludeUserIds, excludeKrux, expectedRawCRB, expectedRawCRBCookie) {
var base = {
timeout: 200,
currency: 'USD',
Expand Down Expand Up @@ -226,23 +241,14 @@ describe('kargo adapter tests', function () {
]
},
pageURL: window.location.href,
rawCRB: expectedRawCRB
rawCRB: expectedRawCRBCookie,
rawCRBLocalStorage: expectedRawCRB
};

if (excludeUserIds === true) {
base.userIDs = {
crbIDs: {}
};
} else if (excludeUserIds) {
if (excludeUserIds.uid) {
delete base.userIDs.kargoID;
delete base.userIDs.clientID;
delete base.userIDs.optOut;
}

if (excludeUserIds.crb) {
base.userIDs.crbIDs = {};
}
}

if (excludeKrux) {
Expand All @@ -267,81 +273,86 @@ describe('kargo adapter tests', function () {
expect(krakenParams).to.deep.equal(expected);
}

it('works when all params and cookies are correctly set', function() {
it('works when all params and localstorage and cookies are correctly set', function() {
initializeKruxUser();
initializeKruxSegments();
initializeKrgUid();
initializeKrgCrb();
testBuildRequests(getExpectedKrakenParams(undefined, undefined, getKrgCrb()));
testBuildRequests(getExpectedKrakenParams(undefined, undefined, getKrgCrb(), getKrgCrbOldStyle()));
});

it('works when all params and cookies are correctly set but no localstorage', function() {
initializeKruxUser();
initializeKruxSegments();
initializeKrgCrb(true);
testBuildRequests(getExpectedKrakenParams(undefined, undefined, null, getKrgCrbOldStyle()));
});

it('gracefully handles nothing being set', function() {
testBuildRequests(getExpectedKrakenParams(true, true, null));
testBuildRequests(getExpectedKrakenParams(true, true, null, null));
});

it('gracefully handles browsers without localStorage', function() {
simulateNoLocalStorage();
initializeKrgUid();
initializeKrgCrb();
testBuildRequests(getExpectedKrakenParams(false, true, getKrgCrb()));
testBuildRequests(getExpectedKrakenParams(true, true, null, null));
});

it('handles empty yet valid Kargo CRBs and UIDs', function() {
it('handles empty yet valid Kargo CRB', function() {
initializeKruxUser();
initializeKruxSegments();
initializeEmptyKrgUid();
initializeEmptyKrgCrb();
testBuildRequests(getExpectedKrakenParams(true, undefined, getEmptyKrgCrb()));
initializeEmptyKrgCrbCookie();
testBuildRequests(getExpectedKrakenParams(true, undefined, getEmptyKrgCrb(), getEmptyKrgCrbOldStyle()));
});

it('handles broken Kargo UIDs', function() {
it('handles broken Kargo CRBs where base64 encoding is invalid', function() {
initializeKruxUser();
initializeKruxSegments();
initializeInvalidKrgUid();
initializeKrgCrb();
testBuildRequests(getExpectedKrakenParams({uid: true}, undefined, getKrgCrb()));
initializeInvalidKrgCrbType1();
testBuildRequests(getExpectedKrakenParams(true, undefined, getInvalidKrgCrbType1(), null));
});

it('handles broken Kargo CRBs where top level JSON is invalid', function() {
it('handles broken Kargo CRBs where top level JSON is invalid on cookie', function() {
initializeKruxUser();
initializeKruxSegments();
initializeKrgUid();
initializeInvalidKrgCrbType1();
testBuildRequests(getExpectedKrakenParams({crb: true}, undefined, getInvalidKrgCrbType1()));
initializeInvalidKrgCrbType1Cookie();
testBuildRequests(getExpectedKrakenParams(true, undefined, null, getInvalidKrgCrbType1()));
});

it('handles broken Kargo CRBs where inner base 64 is invalid', function() {
it('handles broken Kargo CRBs where decoded JSON is invalid', function() {
initializeKruxUser();
initializeKruxSegments();
initializeKrgUid();
initializeInvalidKrgCrbType2();
testBuildRequests(getExpectedKrakenParams({crb: true}, undefined, getInvalidKrgCrbType2()));
testBuildRequests(getExpectedKrakenParams(true, undefined, getInvalidKrgCrbType2(), null));
});

it('handles broken Kargo CRBs where inner base 64 is invalid on cookie', function() {
initializeKruxUser();
initializeKruxSegments();
initializeInvalidKrgCrbType2Cookie();
testBuildRequests(getExpectedKrakenParams(true, undefined, null, getInvalidKrgCrbType2OldStyle()));
});

it('handles broken Kargo CRBs where inner JSON is invalid', function() {
it('handles broken Kargo CRBs where inner JSON is invalid on cookie', function() {
initializeKruxUser();
initializeKruxSegments();
initializeKrgUid();
initializeInvalidKrgCrbType3();
testBuildRequests(getExpectedKrakenParams({crb: true}, undefined, getInvalidKrgCrbType3()));
initializeInvalidKrgCrbType3Cookie();
testBuildRequests(getExpectedKrakenParams(true, undefined, null, getInvalidKrgCrbType3OldStyle()));
});

it('handles a non-existant currency object on the config', function() {
simulateNoCurrencyObject();
initializeKruxUser();
initializeKruxSegments();
initializeKrgUid();
initializeKrgCrb();
testBuildRequests(getExpectedKrakenParams(undefined, undefined, getKrgCrb()));
testBuildRequests(getExpectedKrakenParams(undefined, undefined, getKrgCrb(), getKrgCrbOldStyle()));
});

it('handles no ad server currency being set on the currency object in the config', function() {
simulateNoAdServerCurrency();
initializeKruxUser();
initializeKruxSegments();
initializeKrgUid();
initializeKrgCrb();
testBuildRequests(getExpectedKrakenParams(undefined, undefined, getKrgCrb()));
testBuildRequests(getExpectedKrakenParams(undefined, undefined, getKrgCrb(), getKrgCrbOldStyle()));
});
});

Expand Down Expand Up @@ -429,10 +440,10 @@ describe('kargo adapter tests', function () {

describe('user sync handler', function() {
const clientId = '74c81cbb-7d07-46d9-be9b-68ccb291c949';
var shouldSimulateOutdatedBrowser, uid, isActuallyOutdatedBrowser;
var shouldSimulateOutdatedBrowser, crb, isActuallyOutdatedBrowser;

beforeEach(() => {
uid = {};
crb = {};
shouldSimulateOutdatedBrowser = false;
isActuallyOutdatedBrowser = false;

Expand All @@ -455,8 +466,8 @@ describe('kargo adapter tests', function () {
});
}

sandbox.stub(spec, '_getUid').callsFake(function() {
return uid;
sandbox.stub(spec, '_getCrb').callsFake(function() {
return crb;
});
});

Expand All @@ -469,7 +480,7 @@ describe('kargo adapter tests', function () {
}

function turnOnClientId() {
uid.clientId = clientId;
crb.clientId = clientId;
}

function simulateOutdatedBrowser() {
Expand Down

0 comments on commit 010f203

Please sign in to comment.