Repository navigation
Expand file tree
/
Copy pathadqueryIdSystem_spec.js
More file actions
182 lines (142 loc) · 6.41 KB
/
Copy pathadqueryIdSystem_spec.js
File metadata and controls
182 lines (142 loc) · 6.41 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
import { adqueryIdSubmodule, storage } from 'modules/adqueryIdSystem.js';
import sinon from 'sinon';
import { attachIdSystem } from '../../../modules/userId/index.js';
import { createEidsArray } from '../../../modules/userId/eids.js';
import { expect } from 'chai/index.mjs';
describe('AdqueryIdSystem', function () {
describe('qid submodule', () => {
it('should expose a "name" property containing qid', () => {
expect(adqueryIdSubmodule.name).to.equal('qid');
});
it('should expose a "gvlid" property containing the GVL ID 902', () => {
expect(adqueryIdSubmodule.gvlid).to.equal(902);
});
});
describe('getId', function () {
let getDataFromLocalStorageStub;
let setDataInLocalStorageStub;
let removeDataFromLocalStorageStub;
beforeEach(function () {
getDataFromLocalStorageStub = sinon.stub(storage, 'getDataFromLocalStorage');
setDataInLocalStorageStub = sinon.stub(storage, 'setDataInLocalStorage');
removeDataFromLocalStorageStub = sinon.stub(storage, 'removeDataFromLocalStorage');
});
afterEach(function () {
getDataFromLocalStorageStub.restore();
setDataInLocalStorageStub.restore();
removeDataFromLocalStorageStub.restore();
});
it('returns the persisted qid synchronously when one already exists', function () {
getDataFromLocalStorageStub.withArgs('qid').returns('existing-qid');
const result = adqueryIdSubmodule.getId();
expect(result).to.deep.equal({ id: 'existing-qid' });
expect(setDataInLocalStorageStub.called).to.be.false;
});
it('generates and persists a new qid when none is stored yet', function () {
getDataFromLocalStorageStub.withArgs('qid').returns(null);
const result = adqueryIdSubmodule.getId();
expect(result.id).to.be.a('string').that.is.not.empty;
expect(setDataInLocalStorageStub.calledWith('qid', result.id)).to.be.true;
});
it('reuses the same qid across multiple getId calls once persisted', function () {
getDataFromLocalStorageStub.withArgs('qid').returns(null);
const first = adqueryIdSubmodule.getId();
getDataFromLocalStorageStub.withArgs('qid').returns(first.id);
const second = adqueryIdSubmodule.getId();
expect(second.id).to.equal(first.id);
});
it('falls back to Math.random when window.crypto.getRandomValues is unavailable', function () {
getDataFromLocalStorageStub.withArgs('qid').returns(null);
const cryptoTmp = window.crypto;
delete window.crypto;
let result;
try {
result = adqueryIdSubmodule.getId();
} finally {
window.crypto = cryptoTmp;
}
expect(result.id).to.be.a('string').that.is.not.empty;
expect(setDataInLocalStorageStub.calledWith('qid', result.id)).to.be.true;
});
it('discards a stored qid longer than 40 characters and generates a fresh one', function () {
const oversizedQid = 'a'.repeat(41);
getDataFromLocalStorageStub.withArgs('qid').returns(oversizedQid);
const result = adqueryIdSubmodule.getId();
expect(removeDataFromLocalStorageStub.calledWith('qid')).to.be.true;
expect(result.id).to.not.equal(oversizedQid);
expect(result.id.length).to.be.at.most(40);
expect(setDataInLocalStorageStub.calledWith('qid', result.id)).to.be.true;
});
});
describe('extendId', function () {
let getDataFromLocalStorageStub;
let setDataInLocalStorageStub;
let removeDataFromLocalStorageStub;
beforeEach(function () {
getDataFromLocalStorageStub = sinon.stub(storage, 'getDataFromLocalStorage');
setDataInLocalStorageStub = sinon.stub(storage, 'setDataInLocalStorage');
removeDataFromLocalStorageStub = sinon.stub(storage, 'removeDataFromLocalStorage');
});
afterEach(function () {
getDataFromLocalStorageStub.restore();
setDataInLocalStorageStub.restore();
removeDataFromLocalStorageStub.restore();
});
it('keeps a valid stored qid untouched', function () {
const result = adqueryIdSubmodule.extendId({}, {}, 'existing-qid');
expect(result).to.be.undefined;
expect(setDataInLocalStorageStub.called).to.be.false;
});
it('keeps a stored qid of exactly 40 characters', function () {
const result = adqueryIdSubmodule.extendId({}, {}, 'a'.repeat(40));
expect(result).to.be.undefined;
});
it('replaces a stored qid longer than 40 characters with a fresh one', function () {
const oversizedQid = 'a'.repeat(41);
const result = adqueryIdSubmodule.extendId({}, {}, oversizedQid);
expect(result.id).to.be.a('string').that.is.not.empty;
expect(result.id).to.not.equal(oversizedQid);
expect(result.id.length).to.be.at.most(40);
expect(setDataInLocalStorageStub.calledWith('qid', result.id)).to.be.true;
});
it('replaces a stored qid that is not a string', function () {
const result = adqueryIdSubmodule.extendId({}, {}, { qid: 'a'.repeat(41) });
expect(result.id).to.be.a('string').that.is.not.empty;
expect(result.id.length).to.be.at.most(40);
expect(setDataInLocalStorageStub.calledWith('qid', result.id)).to.be.true;
});
it('reuses a valid qid from localStorage instead of generating a new one', function () {
getDataFromLocalStorageStub.withArgs('qid').returns('existing-qid');
const result = adqueryIdSubmodule.extendId({}, {}, 'a'.repeat(41));
expect(result).to.deep.equal({ id: 'existing-qid' });
expect(setDataInLocalStorageStub.called).to.be.false;
});
it('removes an oversized qid from localStorage and generates a fresh one', function () {
const oversizedQid = 'a'.repeat(41);
getDataFromLocalStorageStub.withArgs('qid').returns(oversizedQid);
const result = adqueryIdSubmodule.extendId({}, {}, oversizedQid);
expect(removeDataFromLocalStorageStub.calledWith('qid')).to.be.true;
expect(result.id).to.not.equal(oversizedQid);
expect(setDataInLocalStorageStub.calledWith('qid', result.id)).to.be.true;
});
});
describe('eid', () => {
before(() => {
attachIdSystem(adqueryIdSubmodule);
});
it('qid', function() {
const userId = {
qid: 'some-random-id-value'
};
const newEids = createEidsArray(userId);
expect(newEids.length).to.equal(1);
expect(newEids[0]).to.deep.equal({
source: 'adquery.io',
uids: [{
id: 'some-random-id-value',
atype: 1
}]
});
});
});
});