-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathuser.scope.spec.ts
More file actions
334 lines (263 loc) · 8.6 KB
/
user.scope.spec.ts
File metadata and controls
334 lines (263 loc) · 8.6 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import { UserDiscoverableQuery } from '../../domain';
import { UserScope } from './user.scope';
describe('UserScope', () => {
let scope1: UserScope;
beforeEach(() => {
scope1 = new UserScope();
scope1.allowEmptyQuery(true);
});
describe('isOutdated is called', () => {
it('should return scope with added query where outdatedSince exists is true', () => {
scope1.isOutdated(true);
expect(scope1.query).toEqual({
outdatedSince: {
$exists: true,
},
});
});
it('should return scope with added query where outdatedSince exists is false', () => {
scope1.isOutdated(false);
expect(scope1.query).toEqual({
outdatedSince: {
$exists: false,
},
});
});
it('should return scope without added outdatedSince to query', () => {
scope1.isOutdated(undefined);
expect(scope1.query).toEqual({});
});
});
describe('bySchool is called', () => {
describe('when school parameter is undefined', () => {
it('should return scope without added schoolId to query', () => {
scope1.bySchoolId(undefined);
expect(scope1.query).toEqual({});
});
});
describe('when school parameter is defined', () => {
it('should return scope with added schoolId to query', () => {
const schoolId = 'schoolId';
scope1.bySchoolId(schoolId);
expect(scope1.query).toEqual({ school: schoolId });
});
});
});
describe('byRole is called', () => {
describe('when role parameter is undefined', () => {
it('should return scope without added role to query', () => {
scope1.byRoleId(undefined);
expect(scope1.query).toEqual({});
});
});
describe('when role parameter is defined', () => {
it('should return scope with added role to query', () => {
const roleId = 'roleId';
scope1.byRoleId(roleId);
expect(scope1.query).toEqual({ roles: roleId });
});
});
});
describe('whereLastLoginSystemChangeSmallerThan is called', () => {
it('should return scope with added query where loginSystemChangeSmallerThan is given', () => {
const date: Date = new Date();
scope1.whereLastLoginSystemChangeSmallerThan(date);
expect(scope1.query).toEqual({
$or: [
{
lastLoginSystemChange: {
$lt: date,
},
},
{
lastLoginSystemChange: {
$exists: false,
},
},
],
});
});
it('should return scope without added loginSystemChangeSmallerThan to query', () => {
scope1.whereLastLoginSystemChangeSmallerThan(undefined);
expect(scope1.query).toEqual({});
});
});
describe('whereOutdatedSinceEquals is called', () => {
it('should return scope with added query where outdatedSinceEquals is given', () => {
const date: Date = new Date();
scope1.withOutdatedSince(date);
expect(scope1.query).toEqual({
outdatedSince: {
$eq: date,
},
});
});
it('should return scope without added whereOutdatedSinceEquals to query', () => {
scope1.whereLastLoginSystemChangeSmallerThan(undefined);
expect(scope1.query).toEqual({});
});
});
describe('whereLastLoginSystemChangeIsBetween', () => {
const setup = () => {
const startDate: Date = new Date();
const endDate: Date = new Date();
return {
startDate,
endDate,
};
};
it('should return scope with added query where lastLoginSystemChange gte and lt is given', () => {
const { startDate, endDate } = setup();
scope1.whereLastLoginSystemChangeIsBetween(startDate, endDate);
expect(scope1.query).toEqual({
lastLoginSystemChange: {
$gte: startDate,
$lt: endDate,
},
});
});
it('should return scope without added whereLastLoginSystemChangeIsBetween to query', () => {
const { startDate } = setup();
scope1.whereLastLoginSystemChangeIsBetween(startDate);
expect(scope1.query).toEqual({});
});
});
describe('byName', () => {
describe('when name is undefined', () => {
const setup = () => {
const scope = new UserScope();
const name = undefined;
return { scope, name };
};
it('should use the default query', () => {
const { scope, name } = setup();
const result = scope.byName(name);
expect(result.query).toEqual({
$and: [{ id: false }],
});
});
});
describe('when name is valid', () => {
const setup = () => {
const scope = new UserScope();
const name = 'John Doe';
return { scope, name };
};
it('should add a query for firstName and lastName using regex', () => {
const { scope, name } = setup();
const result = scope.byName(name);
const expectedRegex = new RegExp('John Doe', 'i');
expect(result.query).toEqual({
$or: [{ firstName: expectedRegex }, { lastName: expectedRegex }],
});
});
});
describe('when name contains invalid characters', () => {
const setup = () => {
const scope = new UserScope();
const name = 'John$Doe';
return { scope, name };
};
it('should remove this character', () => {
const { scope, name } = setup();
const result = scope.byName(name);
const expectedRegex = new RegExp('JohnDoe', 'i');
expect(result.query).toEqual({
$or: [{ firstName: expectedRegex }, { lastName: expectedRegex }],
});
});
});
describe('when name exceeds maximum length', () => {
const setup = () => {
const scope = new UserScope();
const name = 'A'.repeat(101); // Name with 101 characters
return { scope, name };
};
it('should throw an error for exceeding maximum length', () => {
const { scope, name } = setup();
expect(() => scope.byName(name)).toThrowError('Seached value is too long');
});
});
describe('when name contains leading or trailing spaces', () => {
const setup = () => {
const scope = new UserScope();
const name = ' John Doe ';
return { scope, name };
};
it('should trim the name and add a query for firstName and lastName using regex', () => {
const { scope, name } = setup();
const result = scope.byName(name);
const expectedRegex = new RegExp('John Doe', 'i');
expect(result.query).toEqual({
$or: [{ firstName: expectedRegex }, { lastName: expectedRegex }],
});
});
});
describe('when name contains a potential NoSQL injection', () => {
const setup = () => {
const scope = new UserScope();
const name = '{"$ne": ""}'; //simulate NoSQL-Injection
return { scope, name };
};
it('should throw an error for invalid search format', () => {
const { scope, name } = setup();
const result = scope.byName(name);
const expectedRegex = new RegExp('ne', 'i');
expect(result.query).toEqual({
$or: [{ firstName: expectedRegex }, { lastName: expectedRegex }],
});
});
});
describe('when a user name contains one of the special characters "áàâäãåçéèêëíìîïñóòôöõúùûüýÿæœÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜÝŸÆŒß"', () => {
const setup = () => {
const scope = new UserScope();
const name = 'A0_áàâäãåçéèêëíìîïñóòôöõúùûüýÿæœÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜÝŸÆŒß-B9';
return { scope, name };
};
it('should not escape them', () => {
const { scope, name } = setup();
const result = scope.byName(name);
const expectedRegex = new RegExp('A0_áàâäãåçéèêëíìîïñóòôöõúùûüýÿæœÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜÝŸÆŒß-B9', 'i');
expect(result.query).toEqual({
$or: [{ firstName: expectedRegex }, { lastName: expectedRegex }],
});
});
});
});
describe('withDiscoverabilityTrue', () => {
describe('when undefined', () => {
it('should not add a query', () => {
scope1.withDiscoverableTrue();
expect(scope1.query).toEqual({});
});
});
describe('when not false', () => {
it('should add query to find true and undefined', () => {
scope1.withDiscoverableTrue(UserDiscoverableQuery.NOT_FALSE);
expect(scope1.query).toEqual({ discoverable: { $ne: false } });
});
});
describe('when tue', () => {
it('should add a query to find true', () => {
scope1.withDiscoverableTrue(UserDiscoverableQuery.TRUE);
expect(scope1.query).toEqual({ discoverable: true });
});
});
});
describe('withDeleted', () => {
describe('when deleted users are included', () => {
it('should not add a query', () => {
scope1.withDeleted(true);
expect(scope1.query).toEqual({});
});
});
describe('when deleted users are excluded', () => {
it('should add a query that removes deleted users', () => {
scope1.withDeleted(false);
expect(scope1.query).toEqual({
$or: [{ deletedAt: { $exists: false } }, { deletedAt: null }, { deletedAt: { $gte: expect.any(Date) } }],
});
});
});
});
});