-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathjixieIdSystem_spec.js
More file actions
301 lines (286 loc) · 13 KB
/
jixieIdSystem_spec.js
File metadata and controls
301 lines (286 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import { expect } from 'chai';
import { jixieIdSubmodule, storage } from 'modules/jixieIdSystem.js';
import { server } from '../../mocks/xhr.js';
import { parseUrl } from '../../../src/utils.js';
const COOKIE_EXPIRATION_FUTURE = (new Date(Date.now() + 60 * 60 * 24 * 1000)).toUTCString();
const COOKIE_EXPIRATION_PAST = (new Date(Date.now() - 60 * 60 * 24 * 1000)).toUTCString();
describe('JixieId Submodule', () => {
const SERVER_HOST = 'traid.jixie.io';
const SERVER_PATH = '/api/usersyncpbjs';
const CLIENTID1 = '822bc904-249b-11f0-9cd2-0242ac120002';
const CLIENTID2 = '822bc904-249b-11f0-9cd2-0242ac120003';
const IDLOG1 = '1745845981000_abc';
const IDLOG_VALID = `${Date.now() + 60 * 60 * 24 * 1000}_abc`;
const IDLOG_EXPIRED = `${Date.now() - 1000}_abc`;
const ACCOUNTID = 'abcdefg';
const STD_JXID_KEY = '_jxx';
const PBJS_JXID_KEY = 'pbjx_jxx';
const PBJS_IDLOGSTR_KEY = 'pbjx_idlog';
const MOCK_CONSENT_STRING = 'myconsentstring';
const EID_TYPE1_PARAMNAME = 'somesha1';
const EID_TYPE2_PARAMNAME = 'somesha2';
const EID_TYPE1_COOKIENAME = 'somesha1cookie';
const EID_TYPE2_LSNAME = 'somesha2ls';
const EID_TYPE1_SAMPLEVALUE = 'pppppppppp';
const EID_TYPE2_SAMPLEVALUE = 'eeeeeeeeee';
it('should have the correct module name declared', () => {
expect(jixieIdSubmodule.name).to.equal('jixieId');
});
describe('decode', () => {
it('should respond with an object with clientid key containing the value', () => {
expect(jixieIdSubmodule.decode(CLIENTID1)).to.deep.equal({
jixieId: CLIENTID1
});
});
it('should respond with undefined if the value is not a string', () => {
[1, null, undefined, NaN, [], {}].forEach((value) => {
expect(jixieIdSubmodule.decode(value)).to.equal(undefined);
});
});
});
describe('getId()', () => {
describe('getId', () => {
context('when there is jixie_o in the window object (jx script on site)', () => {
context('when there is _jxx in the cookie', () => {
it('should return callback with the clientid in that cookie', () => {
window.jixie_o = {};
storage.setCookie(STD_JXID_KEY, CLIENTID1, COOKIE_EXPIRATION_FUTURE);
const completeCallback = sinon.spy();
const { callback } = jixieIdSubmodule.getId({
params: {
stdjxidckname: STD_JXID_KEY,
pubExtIds: [
{ pname: EID_TYPE1_PARAMNAME, ckname: EID_TYPE1_COOKIENAME },
{ pname: EID_TYPE2_PARAMNAME, lsname: EID_TYPE2_LSNAME }
]
}
});
callback(completeCallback);
const [request] = server.requests;
expect(request).to.be.undefined;
expect(completeCallback.calledOnceWithExactly(CLIENTID1)).to.be.true;
storage.setCookie(STD_JXID_KEY, '', COOKIE_EXPIRATION_PAST);
window.jixie_o = undefined;
})
})
context('when there is no _jxx in the cookie', () => {
it('should return callback with null', () => {
window.jixie_o = {};
const completeCallback = sinon.spy();
const { callback } = jixieIdSubmodule.getId({
params: {
stdjxidckname: STD_JXID_KEY,
pubExtIds: [
{ pname: EID_TYPE1_PARAMNAME, ckname: EID_TYPE1_COOKIENAME },
{ pname: EID_TYPE2_PARAMNAME, lsname: EID_TYPE2_LSNAME }
]
}
});
callback(completeCallback);
const [request] = server.requests;
expect(request).to.be.undefined;
expect(completeCallback.calledOnceWithExactly(null)).to.be.true;
window.jixie_o = undefined;
})
})
})
context('when there is no jixie_o in the window object', () => {
context('when there is no pbjs jixie cookie', () => {
it('should call the server and set the id', () => {
const completeCallback = sinon.spy();
const { callback } = jixieIdSubmodule.getId({
params: {
stdjxidckname: STD_JXID_KEY,
pubExtIds: [
{ pname: EID_TYPE1_PARAMNAME, ckname: EID_TYPE1_COOKIENAME },
{ pname: EID_TYPE2_PARAMNAME, lsname: EID_TYPE2_LSNAME }
]
}
});
callback(completeCallback);
const [request] = server.requests;
request.respond(200, {
'Content-Type': 'application/json'
}, JSON.stringify({
data: {
success: true,
client_id: CLIENTID1,
idlog: IDLOG1
}
}));
expect(completeCallback.calledOnceWithExactly(CLIENTID1)).to.be.true;
});
it('should call the server and set the id. HERE we check all params to server in detail as more parameters since more was found in cookie', () => {
storage.setCookie(EID_TYPE1_COOKIENAME, EID_TYPE1_SAMPLEVALUE, COOKIE_EXPIRATION_FUTURE)
storage.setDataInLocalStorage(EID_TYPE2_LSNAME, EID_TYPE2_SAMPLEVALUE);
const completeCallback = sinon.spy();
const { callback } = jixieIdSubmodule.getId({
params: {
accountid: ACCOUNTID,
pubExtIds: [
{ pname: EID_TYPE1_PARAMNAME, ckname: EID_TYPE1_COOKIENAME },
{ pname: EID_TYPE2_PARAMNAME, lsname: EID_TYPE2_LSNAME }
]
}
});
callback(completeCallback);
const [request] = server.requests;
request.respond(200, {
'Content-Type': 'application/json'
}, JSON.stringify({
data: {
success: true,
client_id: CLIENTID1,
idlog: IDLOG1
}
}));
const parsed = parseUrl(request.url);
expect(parsed.hostname).to.equal(SERVER_HOST);
expect(parsed.pathname).to.equal(SERVER_PATH);
expect(parsed.search[EID_TYPE1_PARAMNAME]).to.equal(EID_TYPE1_SAMPLEVALUE);
expect(parsed.search[EID_TYPE2_PARAMNAME]).to.equal(EID_TYPE2_SAMPLEVALUE);
expect(request.method).to.equal('GET');
expect(request.withCredentials).to.be.true;
expect(completeCallback.calledOnceWithExactly(CLIENTID1)).to.be.true;
storage.setCookie(EID_TYPE1_COOKIENAME, EID_TYPE1_SAMPLEVALUE, COOKIE_EXPIRATION_PAST)
storage.setDataInLocalStorage(EID_TYPE2_LSNAME, '');
});
it('should call the server and set the id and when telcocp (fire-n-forget) is given then that should be called too', () => {
const completeCallback = sinon.spy();
const { callback } = jixieIdSubmodule.getId({
params: {
stdjxidckname: STD_JXID_KEY,
pubExtIds: [
{ pname: EID_TYPE1_PARAMNAME, ckname: EID_TYPE1_COOKIENAME },
{ pname: EID_TYPE2_PARAMNAME, lsname: EID_TYPE2_LSNAME }
]
}
});
callback(completeCallback);
const [request] = server.requests;
request.respond(200, {
'Content-Type': 'application/json'
}, JSON.stringify({
data: {
success: true,
client_id: CLIENTID1,
idlog: IDLOG1,
telcoep: 'https://www.telcoep.com/xxx'
}
}));
expect(server.requests.length).to.equal(2);
expect(server.requests[1].url).to.equal('https://www.telcoep.com/xxx');
server.requests[1].respond(200, {
'Content-Type': 'application/json'
}, JSON.stringify({
data: {
success: true
}
}));
});
});
context('when has rather fresh pbjs jixie cookie', () => {
it('should not call the server ; just return the id', () => {
storage.setCookie(PBJS_JXID_KEY, CLIENTID1, COOKIE_EXPIRATION_FUTURE)
storage.setCookie(PBJS_IDLOGSTR_KEY, IDLOG_VALID, COOKIE_EXPIRATION_FUTURE)
const setCookieStub = sinon.stub(storage, 'setCookie');
const completeCallback = sinon.spy();
const { callback } = jixieIdSubmodule.getId({
params: {
stdjxidckname: STD_JXID_KEY,
pubExtIds: [
{ pname: EID_TYPE1_PARAMNAME, ckname: EID_TYPE1_COOKIENAME },
{ pname: EID_TYPE2_PARAMNAME, lsname: EID_TYPE2_LSNAME }
]
}
});
callback(completeCallback);
const [request] = server.requests;
expect(setCookieStub.neverCalledWith(PBJS_JXID_KEY)).to.be.true;
expect(completeCallback.calledOnceWithExactly(CLIENTID1)).to.be.true;
expect(request).to.be.undefined;
setCookieStub.restore();
storage.setCookie(PBJS_JXID_KEY, CLIENTID1, COOKIE_EXPIRATION_PAST)
storage.setCookie(PBJS_IDLOGSTR_KEY, IDLOG_VALID, COOKIE_EXPIRATION_PAST)
})
});
context('when has rather stale pbjs jixie cookie', () => {
it('should call the server and set the id; send available extra info (e.g. esha,psha, consent if available)', () => {
const consentData = { gdpr: { gdprApplies: 1, consentString: MOCK_CONSENT_STRING } };
storage.setCookie(PBJS_JXID_KEY, CLIENTID1, COOKIE_EXPIRATION_FUTURE)
storage.setCookie(PBJS_IDLOGSTR_KEY, IDLOG_EXPIRED, COOKIE_EXPIRATION_FUTURE)
storage.setCookie(EID_TYPE1_COOKIENAME, EID_TYPE1_SAMPLEVALUE, COOKIE_EXPIRATION_FUTURE)
storage.setDataInLocalStorage(EID_TYPE2_LSNAME, EID_TYPE2_SAMPLEVALUE);
const setCookieStub = sinon.stub(storage, 'setCookie');
const completeCallback = sinon.spy();
const { callback } = jixieIdSubmodule.getId({
params: {
stdjxidckname: STD_JXID_KEY,
pubExtIds: [
{ pname: EID_TYPE1_PARAMNAME, ckname: EID_TYPE1_COOKIENAME },
{ pname: EID_TYPE2_PARAMNAME, lsname: EID_TYPE2_LSNAME }
]
}
}, consentData);
callback(completeCallback);
const [request] = server.requests;
request.respond(200, {
'Content-Type': 'application/json'
}, JSON.stringify({
data: {
success: true,
client_id: CLIENTID2,
idlog: IDLOG1
},
expires: Date.now()
}));
const parsed = parseUrl(request.url);
expect(parsed.hostname).to.equal(SERVER_HOST);
expect(parsed.pathname).to.equal(SERVER_PATH);
expect(parsed.search.client_id).to.equal(CLIENTID1);
expect(parsed.search.idlog).to.equal(IDLOG_EXPIRED);
expect(parsed.search[EID_TYPE1_PARAMNAME]).to.equal(EID_TYPE1_SAMPLEVALUE);
expect(parsed.search[EID_TYPE2_PARAMNAME]).to.equal(EID_TYPE2_SAMPLEVALUE);
expect(parsed.search.gdpr_consent).to.equal(MOCK_CONSENT_STRING);
expect(request.method).to.equal('GET');
expect(request.withCredentials).to.be.true;
expect(setCookieStub.calledWith(PBJS_JXID_KEY, CLIENTID2, sinon.match.string)).to.be.true;
expect(setCookieStub.calledWith(PBJS_IDLOGSTR_KEY, IDLOG1, sinon.match.string)).to.be.true;
expect(completeCallback.calledOnceWithExactly(CLIENTID2)).to.be.true;
setCookieStub.restore();
storage.setCookie(PBJS_JXID_KEY, CLIENTID1, COOKIE_EXPIRATION_PAST);
storage.setCookie(PBJS_IDLOGSTR_KEY, IDLOG_EXPIRED, COOKIE_EXPIRATION_PAST);
storage.setCookie(EID_TYPE1_COOKIENAME, EID_TYPE1_SAMPLEVALUE, COOKIE_EXPIRATION_PAST)
storage.setDataInLocalStorage(EID_TYPE2_LSNAME, '');
});
});
context('when has corrupted idlog cookie', () => {
it('should still call the server even though thre is a pbs jixie id', () => {
storage.setCookie(PBJS_JXID_KEY, CLIENTID1, COOKIE_EXPIRATION_FUTURE)
storage.setCookie(PBJS_IDLOGSTR_KEY, 'junk', COOKIE_EXPIRATION_FUTURE)
const completeCallback = sinon.spy();
const { callback } = jixieIdSubmodule.getId({
params: {
accountid: ACCOUNTID
}
});
callback(completeCallback);
const [request] = server.requests;
request.respond(200, {
'Content-Type': 'application/json'
}, JSON.stringify({
data: {
success: true,
client_id: CLIENTID1,
idlog: IDLOG1
},
expires: Date.now()
}));
const parsed = parseUrl(request.url);
expect(parsed.hostname).to.equal(SERVER_HOST);
});
});
});
});
});
});