Skip to content

Commit 4d9faf3

Browse files
authored
Extend storage options (#8)
* Consistent intention on function names, update cookie name to be specific. * Add new options while preserving default experience. * Allow setting location based on options. * Improve tests. * Finalize forking of functionality based on storage location. * Add tests to cover setting client ID in local storage. * Remove update_cookie as it is no longer relevant. * Add tests for setting the opposite storage location for client and session id's. * Improve tests. * Adjust tests to add coverage. * Get rid of un-necessary if statement. * Code review feedback.
1 parent 7b5368f commit 4d9faf3

File tree

4 files changed

+223
-82
lines changed

4 files changed

+223
-82
lines changed

spec/001-initialization.js

Lines changed: 78 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ describe('ConstructorioID', function () {
99
var expectedKeys = [
1010
'set_cookie',
1111
'get_cookie',
12-
'update_cookie',
1312
'delete_cookie',
1413
'generate_client_id',
1514
'get_local_object',
1615
'set_local_object',
17-
'get_session_id'
16+
'generate_session_id'
1817
];
1918
expect(actualKeys).to.eql(expectedKeys);
2019
});
@@ -23,7 +22,12 @@ describe('ConstructorioID', function () {
2322
var session = new ConstructorioID();
2423
expect(session.user_agent).to.be.null;
2524
expect(session.persist).to.be.true;
26-
expect(session.cookie_name).to.equal('ConstructorioID_client_id');
25+
expect(session.client_id_cookie_name).to.equal('ConstructorioID_client_id');
26+
expect(session.session_id_cookie_name).to.equal('ConstructorioID_session_id');
27+
expect(session.local_name_client_id).to.equal('_constructorio_search_client');
28+
expect(session.local_name_session_id).to.equal('_constructorio_search_session');
29+
expect(session.client_id_storage_location).to.equal('cookie');
30+
expect(session.session_id_storage_location).to.equal('local');
2731
expect(session.cookie_domain).to.be.null;
2832
});
2933

@@ -34,18 +38,28 @@ describe('ConstructorioID', function () {
3438
user_agent: 'dummyagent',
3539
timeout: 1,
3640
persist: false,
37-
cookie_name: 'dummyname',
41+
client_id_cookie_name: 'dummyclientname',
42+
session_id_cookie_name: 'dummysessionname',
43+
local_name_client_id: 'dummyclientnamelocal',
44+
local_name_session_id: 'dummysessionnamelocal',
3845
cookie_prefix_for_experiment: 'dummyprefix',
39-
cookie_domain: 'dummydomain'
46+
cookie_domain: 'dummydomain',
47+
client_id_storage_location: 'foo',
48+
session_id_storage_location: 'bar'
4049
});
4150
expect(session.base_url).to.equal('dummyurl');
4251
expect(session.ip_address).to.equal('dummyip');
4352
expect(session.user_agent).to.equal('dummyagent');
4453
expect(session.timeout).to.equal(1);
4554
expect(session.persist).to.be.false;
46-
expect(session.cookie_name).to.equal('dummyname');
55+
expect(session.client_id_cookie_name).to.equal('dummyclientname');
56+
expect(session.session_id_cookie_name).to.equal('dummysessionname');
57+
expect(session.local_name_client_id).to.equal('dummyclientnamelocal');
58+
expect(session.local_name_session_id).to.equal('dummysessionnamelocal');
4759
expect(session.cookie_prefix_for_experiment).to.equal('dummyprefix');
4860
expect(session.cookie_domain).to.equal('dummydomain');
61+
expect(session.client_id_storage_location).to.equal('foo');
62+
expect(session.session_id_storage_location).to.equal('bar');
4963
});
5064

5165
describe('when used in browser', function () {
@@ -65,27 +79,40 @@ describe('ConstructorioID', function () {
6579

6680
it('should read the client id from a named cookie', function () {
6781
document.cookie = 'dummyname=dummyid; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/';
68-
var session = new ConstructorioID({ cookie_name: 'dummyname' });
82+
var session = new ConstructorioID({ client_id_cookie_name: 'dummyname' });
6983
expect(session.client_id).to.equal('dummyid');
7084
expect(document.cookie).to.equal('dummyname=dummyid');
7185
});
7286

73-
it('should read the client id from the old cookie', function () {
74-
document.cookie = 'ConstructorioAB_client_id=tummyid; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/';
75-
var session = new ConstructorioID();
76-
expect(session.client_id).to.equal('tummyid');
77-
expect(document.cookie).to.equal('ConstructorioID_client_id=tummyid');
78-
});
79-
80-
it('should read the client id from the new cookie', function () {
87+
it('should set the client id if missing from the default storage location', function () {
8188
document.cookie = 'ConstructorioID_client_id=bummyid; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/';
8289
var session = new ConstructorioID();
8390
expect(session.client_id).to.equal('bummyid');
8491
expect(document.cookie).to.equal('ConstructorioID_client_id=bummyid');
8592
});
8693

8794
it('should set the client id if missing', function () {
88-
var session = new ConstructorioID({ cookie_name: 'missingname' });
95+
var session = new ConstructorioID({ client_id_cookie_name: 'missingname' });
96+
expect(session.client_id).to.be.a.string;
97+
expect(session.client_id).to.match(/(\w|d|-){36}/);
98+
});
99+
100+
it('should read the client id from local storage if storage location is set to local', function () {
101+
window.localStorage.setItem('dummyname', 'dummyid');
102+
var session = new ConstructorioID({ local_name_client_id: 'dummyname', client_id_storage_location: 'local' } );
103+
expect(session.client_id).to.equal('dummyid');
104+
expect(window.localStorage.getItem('dummyname')).to.equal('dummyid');
105+
});
106+
107+
it('should read the client id from the default local storage name and storage location is set to local', function () {
108+
window.localStorage.setItem('_constructorio_search_client', 'bummyid');
109+
var session = new ConstructorioID({ client_id_storage_location: 'local' });
110+
expect(session.client_id).to.equal('bummyid');
111+
expect(window.localStorage.getItem('_constructorio_search_client')).to.equal('bummyid');
112+
});
113+
114+
it('should set the client id if missing and storage location is set to local', function () {
115+
var session = new ConstructorioID({ client_id_cookie_name: 'missingname', client_id_storage_location: 'local' });
89116
expect(session.client_id).to.be.a.string;
90117
expect(session.client_id).to.match(/(\w|d|-){36}/);
91118
});
@@ -106,6 +133,19 @@ describe('ConstructorioID', function () {
106133
expect(session.session_id).to.equal(1);
107134
});
108135

136+
it('should read the session id from cookie if storage location is set to cookie', function () {
137+
document.cookie = `ConstructorioID_session_id={"sessionId":42,"lastTime":${Date.now()}}; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/`;
138+
var session = new ConstructorioID({ session_id_storage_location: 'cookie' });
139+
expect(session.session_id).to.be.a('number');
140+
expect(session.session_id).to.equal(42);
141+
});
142+
143+
it('should set the session id to 1 if there is no cookie and storage location is set to cookie', function () {
144+
var session = new ConstructorioID({ session_id_storage_location: 'cookie' });
145+
expect(session.session_id).to.be.a('number');
146+
expect(session.session_id).to.equal(1);
147+
});
148+
109149
it('should set session_is_new to false if the session is not new', function () {
110150
window.localStorage.setItem('_constructorio_search_session', JSON.stringify({
111151
sessionId: 42,
@@ -128,12 +168,34 @@ describe('ConstructorioID', function () {
128168
expect(session.session_is_new).to.equal(true);
129169
});
130170

171+
it('should set session_is_new to false if the session is not new and storage location is set to cookie', function () {
172+
document.cookie = `ConstructorioID_session_id={"sessionId":42,"lastTime":${Date.now()}}; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/`;
173+
var session = new ConstructorioID({ session_id_storage_location: 'cookie' });
174+
expect(session.session_id).to.equal(42);
175+
expect(session.session_is_new).to.be.a('boolean');
176+
expect(session.session_is_new).to.equal(false);
177+
});
178+
179+
it('should set session_is_new to true if the session is new and storage location is set to cookie', function () {
180+
document.cookie = `ConstructorioID_session_id={"sessionId":42,"lastTime":${Date.now() - 1000 * 60 * 60 * 24 * 60}}; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/`;
181+
var session = new ConstructorioID({ session_id_storage_location: 'cookie' });
182+
expect(session.session_id).to.equal(43);
183+
expect(session.session_is_new).to.be.a('boolean');
184+
expect(session.session_is_new).to.equal(true);
185+
});
186+
131187
it('should set session_is_new to true if there is no local storage data', function () {
132188
var session = new ConstructorioID();
133189
expect(session.session_is_new).to.be.a('boolean');
134190
expect(session.session_is_new).to.equal(true);
135191
});
136192

193+
it('should set session_is_new to true if there is no cookie data and storage location is set to cookie', function () {
194+
var session = new ConstructorioID({ session_id_storage_location: 'cookie' });
195+
expect(session.session_is_new).to.be.a('boolean');
196+
expect(session.session_is_new).to.equal(true);
197+
});
198+
137199
it('should set the user agent', function () {
138200
var session = new ConstructorioID();
139201
expect(session.user_agent).to.match(/jsdom/);

spec/005-storage.js

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ describe('ConstructorioID', function () {
2828
expect(adventuretime.jake).to.be.true;
2929
});
3030

31-
it('should not return a non-object', function () {
31+
it('should not return a local string', function () {
3232
window.localStorage.setItem('adventuretime', 'Come on grab your friends');
3333
var session = new ConstructorioID();
3434
var adventuretime = session.get_local_object('adventuretime');
35-
expect(adventuretime).to.be.undefined;
35+
expect(adventuretime).to.equal('Come on grab your friends');
3636
});
3737
});
3838

@@ -43,15 +43,9 @@ describe('ConstructorioID', function () {
4343
expect(window.localStorage.adventuretime).to.be.a.string;
4444
expect(JSON.parse(window.localStorage.adventuretime)).to.deep.equal({ marceline: true });
4545
});
46-
47-
it('should not set a non-object', function () {
48-
var session = new ConstructorioID();
49-
session.set_local_object('adventuretime', 'We\'re going to very distant lands.');
50-
expect(window.localStorage.adventuretime).to.be.undefined;
51-
});
5246
});
5347

54-
describe('get_session_id', function () {
48+
describe('generate_session_id', function () {
5549
it('should return a session id from local storage if recent', function () {
5650
var now = Date.now();
5751
var session = new ConstructorioID();
@@ -62,7 +56,7 @@ describe('ConstructorioID', function () {
6256
}));
6357

6458
var set_local_object = sinon.spy(ConstructorioID.prototype, 'set_local_object');
65-
var session_id = session.get_session_id();
59+
var session_id = session.generate_session_id();
6660
expect(session_id).to.be.a('number');
6761
expect(session_id).to.equal(42);
6862
expect(set_local_object.calledOnce).to.be.true;
@@ -73,7 +67,24 @@ describe('ConstructorioID', function () {
7367
set_local_object.restore();
7468
});
7569

76-
it('should increment session id from local storage if older than thirty minutes', function () {
70+
it('should return the same session id from cookie if recent and the storage location is set to cookie', function () {
71+
var now = Date.now();
72+
var session = new ConstructorioID({ session_id_storage_location: 'cookie' });
73+
document.cookie = `ConstructorioID_session_id={"sessionId":42,"lastTime":${now}}; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/`;
74+
75+
var set_cookie = sinon.spy(ConstructorioID.prototype, 'set_cookie');
76+
var session_id = session.generate_session_id();
77+
expect(session_id).to.be.a('number');
78+
expect(session_id).to.equal(42);
79+
expect(set_cookie.calledOnce).to.be.true;
80+
expect(set_cookie.calledWith('ConstructorioID_session_id')).to.be.true;
81+
expect(JSON.parse(set_cookie.getCall(0).args[1]).sessionId).to.equal(42);
82+
expect(JSON.parse(set_cookie.getCall(0).args[1]).lastTime).to.be.at.least(now);
83+
84+
set_cookie.restore();
85+
});
86+
87+
it('should increment session id in local storage if older than thirty minutes', function () {
7788
var now = Date.now();
7889
var session = new ConstructorioID();
7990
window.localStorage.clear();
@@ -83,7 +94,7 @@ describe('ConstructorioID', function () {
8394
}));
8495

8596
var set_local_object = sinon.spy(ConstructorioID.prototype, 'set_local_object');
86-
var session_id = session.get_session_id();
97+
var session_id = session.generate_session_id();
8798
expect(session_id).to.be.a('number');
8899
expect(session_id).to.equal(43);
89100
expect(set_local_object.calledOnce).to.be.true;
@@ -94,13 +105,30 @@ describe('ConstructorioID', function () {
94105
set_local_object.restore();
95106
});
96107

97-
it('should set a session id from local storage if missing', function () {
108+
it('should increment session id from cookie if older than thirty minutes and storage location is set to cookie', function () {
109+
var now = Date.now();
110+
var session = new ConstructorioID({ session_id_storage_location: 'cookie' });
111+
document.cookie = `ConstructorioID_session_id={"sessionId":42,"lastTime":${Date.now() - 1000 * 60 * 30}}; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/`;
112+
113+
var set_cookie = sinon.spy(ConstructorioID.prototype, 'set_cookie');
114+
var session_id = session.generate_session_id();
115+
expect(session_id).to.be.a('number');
116+
expect(session_id).to.equal(43);
117+
expect(set_cookie.calledOnce).to.be.true;
118+
expect(set_cookie.calledWith('ConstructorioID_session_id')).to.be.true;
119+
expect(JSON.parse(set_cookie.getCall(0).args[1]).sessionId).to.equal(43);
120+
expect(JSON.parse(set_cookie.getCall(0).args[1]).lastTime).to.be.at.least(now);
121+
122+
set_cookie.restore();
123+
});
124+
125+
it('should set a session id in local storage if missing', function () {
98126
var now = Date.now();
99127
var session = new ConstructorioID();
100128
window.localStorage.clear();
101129

102130
var set_local_object = sinon.spy(ConstructorioID.prototype, 'set_local_object');
103-
var session_id = session.get_session_id();
131+
var session_id = session.generate_session_id();
104132
expect(session_id).to.be.a('number');
105133
expect(session_id).to.equal(1);
106134
expect(set_local_object.calledOnce).to.be.true;
@@ -109,5 +137,21 @@ describe('ConstructorioID', function () {
109137
expect(set_local_object.getCall(0).args[1].lastTime).to.be.at.least(now);
110138
set_local_object.restore();
111139
});
140+
141+
it('should set a session id in cookie if missing and storage location is set to cookie', function () {
142+
var now = Date.now();
143+
var session = new ConstructorioID({ session_id_storage_location: 'cookie' });
144+
document.cookie = '';
145+
146+
var set_cookie = sinon.spy(ConstructorioID.prototype, 'set_cookie');
147+
var session_id = session.generate_session_id();
148+
expect(session_id).to.be.a('number');
149+
expect(session_id).to.equal(1);
150+
expect(set_cookie.calledOnce).to.be.true;
151+
expect(set_cookie.calledWith('ConstructorioID_session_id')).to.be.true;
152+
expect(JSON.parse(set_cookie.getCall(0).args[1]).sessionId).to.equal(1);
153+
expect(JSON.parse(set_cookie.getCall(0).args[1]).lastTime).to.be.at.least(now);
154+
set_cookie.restore();
155+
});
112156
});
113157
});

spec/006-cookies.js

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,49 +39,39 @@ describe('ConstructorioID', function () {
3939
});
4040
});
4141

42-
describe('update_cookie', function () {
43-
it('should update a matching cookie', function () {
44-
document.cookie = 'dummyname=dummyid; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/';
45-
document.cookie = 'ConstructorioAB_veggie=turnips; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/';
46-
var session = new ConstructorioID({ cookie_name: 'dummyname' });
47-
session.update_cookie('ConstructorioID_veggie');
48-
expect(document.cookie).to.equal('dummyname=dummyid; ConstructorioID_veggie=turnips');
49-
});
50-
51-
it('should skip a non matching cookie', function () {
52-
document.cookie = 'dummyname=dummyid; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/';
53-
document.cookie = 'ConstructorioBC_veggie=turnips; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/';
54-
var session = new ConstructorioID({ cookie_name: 'dummyname' });
55-
session.update_cookie('ConstructorioID_veggie');
56-
expect(document.cookie).to.equal('dummyname=dummyid; ConstructorioBC_veggie=turnips');
57-
});
58-
});
59-
6042
describe('delete_cookie', function () {
6143
it('should remove a matching cookie', function () {
6244
document.cookie = 'dummyname=dummyid; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/';
6345
document.cookie = 'deleteme=now; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/';
64-
var session = new ConstructorioID({ cookie_name: 'dummyname' });
46+
var session = new ConstructorioID({ client_id_cookie_name: 'dummyname' });
6547
session.delete_cookie('deleteme');
6648
expect(document.cookie).to.equal('dummyname=dummyid');
6749
});
6850

6951
it('should skip a non-matching cookie', function () {
7052
document.cookie = 'dummyname=dummyid; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/';
7153
document.cookie = 'skipme=now; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/';
72-
var session = new ConstructorioID({ cookie_name: 'dummyname' });
54+
var session = new ConstructorioID({ client_id_cookie_name: 'dummyname' });
7355
session.delete_cookie('deleteme');
7456
expect(document.cookie).to.equal('dummyname=dummyid; skipme=now');
7557
});
7658
});
7759

7860
describe('generate_client_id', function () {
7961
it('should return a client id and set the cookie', function () {
80-
var session = new ConstructorioID({ cookie_name: 'monster' });
62+
var session = new ConstructorioID({ client_id_cookie_name: 'monster' });
8163
var client_id = session.generate_client_id();
8264
expect(session.get_cookie('monster')).to.equal(client_id);
8365
expect(client_id).to.be.a.string;
8466
expect(client_id).to.match(/(\w|d|-){36}/);
8567
});
68+
69+
it('should return a client id and set local storage value if storage location is set to local', function () {
70+
var session = new ConstructorioID({ local_name_client_id: 'monster', client_id_storage_location: 'local' });
71+
var client_id = session.generate_client_id();
72+
expect(session.get_local_object('monster')).to.equal(client_id);
73+
expect(client_id).to.be.a.string;
74+
expect(client_id).to.match(/(\w|d|-){36}/);
75+
});
8676
});
8777
});

0 commit comments

Comments
 (0)