Skip to content

Commit

Permalink
fix(cookies): reduce cookie lifetime (#306)
Browse files Browse the repository at this point in the history
* fix(cookies): reduce cookie lifetime

* fix broken test

* fix broken tests and light linting
  • Loading branch information
kelvin-lu authored Nov 5, 2020
1 parent 75066bf commit 84e1a57
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ if (BUILD_COMPAT_REACT_NATIVE) {
export default {
apiEndpoint: 'api.amplitude.com',
batchEvents: false,
cookieExpiration: 365 * 10,
cookieExpiration: 365, // 12 months is for GDPR compliance
cookieName: 'amplitude_id', // this is a deprecated option
sameSiteCookie: 'Lax', // cookie privacy policy
cookieForceUpgrade: false,
Expand Down Expand Up @@ -52,7 +52,7 @@ export default {
os_version: true,
platform: true,
region: true,
version_name: true
version_name: true,
},
unsetParamsReferrerOnNewSession: false,
unsentKey: 'amplitude_unsent',
Expand Down
6 changes: 3 additions & 3 deletions test/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ describe('AmplitudeClient', function() {
amplitude.init(apiKey, userId, config);
assert.equal(amplitude.options.apiEndpoint, 'api.amplitude.com');
assert.equal(amplitude.options.batchEvents, false);
assert.equal(amplitude.options.cookieExpiration, 3650);
assert.equal(amplitude.options.cookieExpiration, 365);
assert.equal(amplitude.options.cookieName, 'amplitude_id');
assert.equal(amplitude.options.eventUploadPeriodMillis, 30000);
assert.equal(amplitude.options.eventUploadThreshold, 30);
Expand Down Expand Up @@ -3311,7 +3311,7 @@ describe('setVersionName', function() {
describe('setDomain', function() {
beforeEach(() => {
reset();
amplitude.init(apiKey, null, { cookieExpiration: 365, secureCookie: true });
amplitude.init(apiKey, null, { cookieExpiration: 1, secureCookie: true });
});

it('should set the cookie domain to null for an invalid domain', () => {
Expand All @@ -3323,7 +3323,7 @@ describe('setVersionName', function() {
it('should not change the expirationDays options', () => {
amplitude.setDomain('.foobar.com');
const options = cookie.options();
assert.equal(options.expirationDays, 365);
assert.equal(options.expirationDays, 1);
});

it('should not change the secureCookie options', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/amplitude.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ describe('Amplitude', function() {
amplitude.init(apiKey, userId, config);
assert.equal(amplitude.options.apiEndpoint, 'api.amplitude.com');
assert.equal(amplitude.options.batchEvents, false);
assert.equal(amplitude.options.cookieExpiration, 3650);
assert.equal(amplitude.options.cookieExpiration, 365);
assert.equal(amplitude.options.cookieName, 'amplitude_id');
assert.equal(amplitude.options.eventUploadPeriodMillis, 30000);
assert.equal(amplitude.options.eventUploadThreshold, 30);
Expand Down
41 changes: 19 additions & 22 deletions test/cookie.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,47 @@
import cookie from '../src/cookie.js';
import baseCookie from '../src/base-cookie';
import getLocation from '../src/get-location';

describe('Cookie', function() {

before(function() {
describe('Cookie', () => {
before(() => {
cookie.reset();
});

afterEach(function() {
afterEach(() => {
cookie.remove('x');
cookie.reset();
});

describe('get', function() {
it('should get an existing cookie', function() {
cookie.set('x', { a : 'b' });
assert.deepEqual(cookie.get('x'), { a : 'b' });
describe('get', () => {
it('should get an existing cookie', () => {
cookie.set('x', { a: 'b' });
assert.deepEqual(cookie.get('x'), { a: 'b' });
});

it('should not throw an error on a malformed cookie', function () {
document.cookie="x=y; path=/";
it('should not throw an error on a malformed cookie', () => {
document.cookie = 'x=y; path=/';
assert.isNull(cookie.get('x'));
});
});

describe('remove', function () {
it('should remove a cookie', function() {
cookie.set('x', { a : 'b' });
assert.deepEqual(cookie.get('x'), { a : 'b' });
describe('remove', () => {
it('should remove a cookie', () => {
cookie.set('x', { a: 'b' });
assert.deepEqual(cookie.get('x'), { a: 'b' });
cookie.remove('x');
assert.isNull(cookie.get('x'));
});
});

describe('options', function() {
it('should set default options', function() {
describe('options', () => {
it('should set default options', () => {
assert.deepEqual(cookie.options(), {
expirationDays: undefined,
domain: undefined
domain: undefined,
});
});

it('should save options', function() {
cookie.options({ expirationDays: 365 });
assert.equal(cookie.options().expirationDays, 365);
it('should save options', () => {
cookie.options({ expirationDays: 1 });
assert.equal(cookie.options().expirationDays, 1);
});
});
});

0 comments on commit 84e1a57

Please sign in to comment.