@adaptivestone/framework 5.0.0-alpha.2 → 5.0.0-alpha.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +96 -0
- package/commands/CreateUser.js +3 -1
- package/commands/GenerateRandomBytes.js +15 -0
- package/commands/migration/Migrate.js +1 -1
- package/config/auth.js +5 -1
- package/config/ipDetector.js +14 -0
- package/controllers/Auth.js +2 -2
- package/controllers/Home.js +1 -1
- package/folderConfig.js +0 -1
- package/helpers/files.js +8 -8
- package/jsconfig.json +9 -0
- package/models/User.js +7 -1
- package/modules/AbstractCommand.js +2 -1
- package/modules/AbstractController.js +22 -18
- package/modules/AbstractModel.d.ts +48 -0
- package/modules/AbstractModel.js +20 -2
- package/modules/Base.d.ts +5 -4
- package/modules/Base.js +11 -1
- package/package.json +13 -16
- package/server.d.ts +7 -5
- package/server.js +11 -7
- package/services/cache/Cache.d.ts +2 -2
- package/services/cache/Cache.js +10 -6
- package/services/http/HttpServer.js +12 -20
- package/services/http/middleware/GetUserByToken.js +3 -2
- package/services/http/middleware/I18n.js +20 -21
- package/services/http/middleware/IpDetector.js +59 -0
- package/services/http/middleware/Pagination.js +3 -2
- package/services/http/middleware/RateLimiter.js +8 -2
- package/services/http/middleware/RequestLogger.js +3 -3
- package/services/http/middleware/RequestParser.js +5 -2
- package/services/messaging/email/index.js +7 -15
- package/services/validate/ValidateService.js +1 -1
- package/tests/setup.js +3 -1
- package/tests/setupVitest.js +1 -1
- package/types/TFoldersConfig.d.ts +0 -2
- package/.eslintrc.cjs +0 -41
- package/controllers/Auth.test.js +0 -451
- package/controllers/Home.test.js +0 -12
- package/models/Migration.test.js +0 -20
- package/models/Sequence.test.js +0 -43
- package/models/User.test.js +0 -143
- package/modules/Modules.test.js +0 -18
- package/services/cache/Cache.test.js +0 -81
- package/services/http/middleware/Auth.test.js +0 -57
- package/services/http/middleware/Cors.test.js +0 -147
- package/services/http/middleware/GetUserByToken.test.js +0 -108
- package/services/http/middleware/I18n.test.js +0 -96
- package/services/http/middleware/PrepareAppInfo.test.js +0 -26
- package/services/http/middleware/RateLimiter.test.js +0 -233
- package/services/http/middleware/RequestParser.test.js +0 -112
- package/services/http/middleware/Role.test.js +0 -93
- package/services/http/middleware/StaticFiles.js +0 -59
- package/services/validate/ValidateService.test.js +0 -107
package/controllers/Auth.test.js
DELETED
|
@@ -1,451 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
|
|
3
|
-
const userEmail = 'testing@test.com';
|
|
4
|
-
const userPassword = 'SuperNiceSecret123$';
|
|
5
|
-
|
|
6
|
-
const userEmail2 = 'testing2@test.com';
|
|
7
|
-
|
|
8
|
-
describe('auth', () => {
|
|
9
|
-
describe('registration', () => {
|
|
10
|
-
it('code NOT able to create user with wrong email', async () => {
|
|
11
|
-
expect.assertions(1);
|
|
12
|
-
const { status } = await fetch(
|
|
13
|
-
global.server.testingGetUrl('/auth/register'),
|
|
14
|
-
{
|
|
15
|
-
method: 'POST',
|
|
16
|
-
headers: {
|
|
17
|
-
'Content-type': 'application/json',
|
|
18
|
-
},
|
|
19
|
-
body: JSON.stringify({
|
|
20
|
-
email: 'bad email',
|
|
21
|
-
password: userPassword,
|
|
22
|
-
nickName: 'test',
|
|
23
|
-
}),
|
|
24
|
-
},
|
|
25
|
-
).catch(() => {});
|
|
26
|
-
|
|
27
|
-
expect(status).toBe(400);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('can create user', async () => {
|
|
31
|
-
expect.assertions(1);
|
|
32
|
-
const { status } = await fetch(
|
|
33
|
-
global.server.testingGetUrl('/auth/register'),
|
|
34
|
-
{
|
|
35
|
-
method: 'POST',
|
|
36
|
-
headers: {
|
|
37
|
-
'Content-type': 'application/json',
|
|
38
|
-
},
|
|
39
|
-
body: JSON.stringify({
|
|
40
|
-
email: userEmail,
|
|
41
|
-
password: userPassword,
|
|
42
|
-
nickName: 'test',
|
|
43
|
-
}),
|
|
44
|
-
},
|
|
45
|
-
);
|
|
46
|
-
expect(status).toBe(201);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it('can not create user with the same nickname', async () => {
|
|
50
|
-
expect.assertions(1);
|
|
51
|
-
await fetch(global.server.testingGetUrl('/auth/register'), {
|
|
52
|
-
method: 'POST',
|
|
53
|
-
headers: {
|
|
54
|
-
'Content-type': 'application/json',
|
|
55
|
-
},
|
|
56
|
-
body: JSON.stringify({
|
|
57
|
-
email: userEmail,
|
|
58
|
-
password: userPassword,
|
|
59
|
-
nickName: 'test',
|
|
60
|
-
}),
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
const { status } = await fetch(
|
|
64
|
-
global.server.testingGetUrl('/auth/register'),
|
|
65
|
-
{
|
|
66
|
-
method: 'POST',
|
|
67
|
-
headers: {
|
|
68
|
-
'Content-type': 'application/json',
|
|
69
|
-
},
|
|
70
|
-
body: JSON.stringify({
|
|
71
|
-
email: userEmail2,
|
|
72
|
-
password: '123',
|
|
73
|
-
nickName: 'test',
|
|
74
|
-
}),
|
|
75
|
-
},
|
|
76
|
-
).catch(() => {});
|
|
77
|
-
|
|
78
|
-
expect(status).toBe(400);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it('can NOT create SAME user', async () => {
|
|
82
|
-
expect.assertions(1);
|
|
83
|
-
|
|
84
|
-
const { status } = await fetch(
|
|
85
|
-
global.server.testingGetUrl('/auth/register'),
|
|
86
|
-
{
|
|
87
|
-
method: 'POST',
|
|
88
|
-
headers: {
|
|
89
|
-
'Content-type': 'application/json',
|
|
90
|
-
},
|
|
91
|
-
body: JSON.stringify({
|
|
92
|
-
email: userEmail,
|
|
93
|
-
password: userPassword,
|
|
94
|
-
nickName: 'test',
|
|
95
|
-
}),
|
|
96
|
-
},
|
|
97
|
-
);
|
|
98
|
-
|
|
99
|
-
expect(status).toBe(400);
|
|
100
|
-
});
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
describe('login', () => {
|
|
104
|
-
it('can NOT login with normal creds and not verified email', async () => {
|
|
105
|
-
expect.assertions(1);
|
|
106
|
-
const { status } = await fetch(
|
|
107
|
-
global.server.testingGetUrl('/auth/login'),
|
|
108
|
-
{
|
|
109
|
-
method: 'POST',
|
|
110
|
-
headers: {
|
|
111
|
-
'Content-type': 'application/json',
|
|
112
|
-
},
|
|
113
|
-
body: JSON.stringify({
|
|
114
|
-
email: userEmail,
|
|
115
|
-
password: userPassword,
|
|
116
|
-
}),
|
|
117
|
-
},
|
|
118
|
-
).catch(() => {});
|
|
119
|
-
|
|
120
|
-
expect(status).toBe(400);
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it('can NOT login with WRONG creds', async () => {
|
|
124
|
-
expect.assertions(1);
|
|
125
|
-
const { status } = await fetch(
|
|
126
|
-
global.server.testingGetUrl('/auth/login'),
|
|
127
|
-
{
|
|
128
|
-
method: 'POST',
|
|
129
|
-
headers: {
|
|
130
|
-
'Content-type': 'application/json',
|
|
131
|
-
},
|
|
132
|
-
body: JSON.stringify({
|
|
133
|
-
email: 'test@test.by',
|
|
134
|
-
password: 'noPassword$',
|
|
135
|
-
}),
|
|
136
|
-
},
|
|
137
|
-
).catch(() => {});
|
|
138
|
-
expect(status).toBe(400);
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
it('can login with normal creds and verified email', async () => {
|
|
142
|
-
expect.assertions(2);
|
|
143
|
-
|
|
144
|
-
const user = await global.server.app
|
|
145
|
-
.getModel('User')
|
|
146
|
-
.findOne({ email: userEmail });
|
|
147
|
-
user.isVerified = true;
|
|
148
|
-
await user.save();
|
|
149
|
-
|
|
150
|
-
const response = await fetch(global.server.testingGetUrl('/auth/login'), {
|
|
151
|
-
method: 'POST',
|
|
152
|
-
headers: {
|
|
153
|
-
'Content-type': 'application/json',
|
|
154
|
-
},
|
|
155
|
-
body: JSON.stringify({
|
|
156
|
-
email: userEmail,
|
|
157
|
-
password: userPassword,
|
|
158
|
-
}),
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
const responseBody = await response.json();
|
|
162
|
-
|
|
163
|
-
expect(response.status).toBe(200);
|
|
164
|
-
expect(responseBody.token).toBeDefined();
|
|
165
|
-
});
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
describe('isAuthWithVefificationFlow auth option', () => {
|
|
169
|
-
it('can verify user', async () => {
|
|
170
|
-
expect.assertions(2);
|
|
171
|
-
const user = await global.server.app.getModel('User').create({
|
|
172
|
-
email: 'Test@gmail.com',
|
|
173
|
-
password: 'userPassword',
|
|
174
|
-
name: {
|
|
175
|
-
nick: 'nickname',
|
|
176
|
-
},
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
user.verificationTokens.push({
|
|
180
|
-
token: 'testToken',
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
await user.save();
|
|
184
|
-
|
|
185
|
-
const { status } = await fetch(
|
|
186
|
-
`${global.server.testingGetUrl(
|
|
187
|
-
'/auth/verify',
|
|
188
|
-
)}?verification_token=testToken`,
|
|
189
|
-
{
|
|
190
|
-
method: 'POST',
|
|
191
|
-
},
|
|
192
|
-
);
|
|
193
|
-
|
|
194
|
-
const { isVerified } = await global.server.app.getModel('User').findOne({
|
|
195
|
-
email: 'Test@gmail.com',
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
expect(status).toBe(200);
|
|
199
|
-
expect(isVerified).toBeTruthy();
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
it('can not verify user with wrong token', async () => {
|
|
203
|
-
expect.assertions(2);
|
|
204
|
-
const user = await global.server.app.getModel('User').create({
|
|
205
|
-
email: 'Test423@gmail.com',
|
|
206
|
-
password: 'userPassword',
|
|
207
|
-
name: {
|
|
208
|
-
nick: 'nicknameee',
|
|
209
|
-
},
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
user.verificationTokens.push({
|
|
213
|
-
token: 'testToken',
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
await user.save();
|
|
217
|
-
|
|
218
|
-
const { status } = await fetch(
|
|
219
|
-
`${global.server.testingGetUrl(
|
|
220
|
-
'/auth/verify',
|
|
221
|
-
)}?verification_token=testToken123wrong`,
|
|
222
|
-
{
|
|
223
|
-
method: 'POST',
|
|
224
|
-
},
|
|
225
|
-
);
|
|
226
|
-
|
|
227
|
-
const { isVerified } = await global.server.app.getModel('User').findOne({
|
|
228
|
-
email: 'Test423@gmail.com',
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
expect(status).toBe(400);
|
|
232
|
-
expect(isVerified).toBeFalsy();
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
it('can NOT send recovery to not exist email', async () => {
|
|
236
|
-
expect.assertions(1);
|
|
237
|
-
const { status } = await fetch(
|
|
238
|
-
global.server.testingGetUrl('/auth/send-recovery-email'),
|
|
239
|
-
{
|
|
240
|
-
method: 'POST',
|
|
241
|
-
headers: {
|
|
242
|
-
'Content-type': 'application/json',
|
|
243
|
-
},
|
|
244
|
-
body: JSON.stringify({
|
|
245
|
-
email: 'notExists@gmail.com',
|
|
246
|
-
}),
|
|
247
|
-
},
|
|
248
|
-
);
|
|
249
|
-
|
|
250
|
-
expect(status).toBe(400);
|
|
251
|
-
});
|
|
252
|
-
|
|
253
|
-
it('can send recovery to exist email', async () => {
|
|
254
|
-
expect.assertions(1);
|
|
255
|
-
const { status } = await fetch(
|
|
256
|
-
global.server.testingGetUrl('/auth/send-recovery-email'),
|
|
257
|
-
{
|
|
258
|
-
method: 'POST',
|
|
259
|
-
headers: {
|
|
260
|
-
'Content-type': 'application/json',
|
|
261
|
-
},
|
|
262
|
-
body: JSON.stringify({
|
|
263
|
-
email: userEmail,
|
|
264
|
-
}),
|
|
265
|
-
},
|
|
266
|
-
);
|
|
267
|
-
|
|
268
|
-
expect(status).toBe(200);
|
|
269
|
-
});
|
|
270
|
-
|
|
271
|
-
it('can recover password', async () => {
|
|
272
|
-
expect.assertions(1);
|
|
273
|
-
|
|
274
|
-
const user = await global.server.app.getModel('User').create({
|
|
275
|
-
email: 'Test1@gmail.com',
|
|
276
|
-
password: 'userPassword',
|
|
277
|
-
name: {
|
|
278
|
-
nick: 'nickname1',
|
|
279
|
-
},
|
|
280
|
-
});
|
|
281
|
-
|
|
282
|
-
user.passwordRecoveryTokens.push({
|
|
283
|
-
token: 'superPassword',
|
|
284
|
-
});
|
|
285
|
-
|
|
286
|
-
await user.save();
|
|
287
|
-
|
|
288
|
-
const { status } = await fetch(
|
|
289
|
-
global.server.testingGetUrl('/auth/recover-password'),
|
|
290
|
-
{
|
|
291
|
-
method: 'POST',
|
|
292
|
-
headers: {
|
|
293
|
-
'Content-type': 'application/json',
|
|
294
|
-
},
|
|
295
|
-
body: JSON.stringify({
|
|
296
|
-
password: 'newPass',
|
|
297
|
-
passwordRecoveryToken: 'superPassword',
|
|
298
|
-
}),
|
|
299
|
-
},
|
|
300
|
-
);
|
|
301
|
-
|
|
302
|
-
expect(status).toBe(200);
|
|
303
|
-
});
|
|
304
|
-
|
|
305
|
-
it('can not recover password with wrong token', async () => {
|
|
306
|
-
expect.assertions(1);
|
|
307
|
-
|
|
308
|
-
const user = await global.server.app.getModel('User').create({
|
|
309
|
-
email: 'Test2@gmail.com',
|
|
310
|
-
password: 'userPassword',
|
|
311
|
-
name: {
|
|
312
|
-
nick: 'nickname2',
|
|
313
|
-
},
|
|
314
|
-
});
|
|
315
|
-
|
|
316
|
-
user.passwordRecoveryTokens.push({
|
|
317
|
-
token: 'superPassword',
|
|
318
|
-
});
|
|
319
|
-
|
|
320
|
-
await user.save();
|
|
321
|
-
|
|
322
|
-
const { status } = await fetch(
|
|
323
|
-
global.server.testingGetUrl('/auth/recover-password'),
|
|
324
|
-
{
|
|
325
|
-
method: 'POST',
|
|
326
|
-
headers: {
|
|
327
|
-
'Content-type': 'application/json',
|
|
328
|
-
},
|
|
329
|
-
body: JSON.stringify({
|
|
330
|
-
password: 'newPass',
|
|
331
|
-
passwordRecoveryToken: '13123',
|
|
332
|
-
}),
|
|
333
|
-
},
|
|
334
|
-
);
|
|
335
|
-
|
|
336
|
-
expect(status).toBe(400);
|
|
337
|
-
});
|
|
338
|
-
|
|
339
|
-
it('can login with normal creds and NOT verifyed email if option isAuthWithVefificationFlow is set', async () => {
|
|
340
|
-
expect.assertions(4);
|
|
341
|
-
|
|
342
|
-
const { status } = await fetch(
|
|
343
|
-
global.server.testingGetUrl('/auth/register'),
|
|
344
|
-
{
|
|
345
|
-
method: 'POST',
|
|
346
|
-
headers: {
|
|
347
|
-
'Content-type': 'application/json',
|
|
348
|
-
},
|
|
349
|
-
body: JSON.stringify({
|
|
350
|
-
email: userEmail2,
|
|
351
|
-
password: userPassword,
|
|
352
|
-
}),
|
|
353
|
-
},
|
|
354
|
-
);
|
|
355
|
-
|
|
356
|
-
const { status: status2 } = await fetch(
|
|
357
|
-
global.server.testingGetUrl('/auth/login'),
|
|
358
|
-
{
|
|
359
|
-
method: 'POST',
|
|
360
|
-
headers: {
|
|
361
|
-
'Content-type': 'application/json',
|
|
362
|
-
},
|
|
363
|
-
body: JSON.stringify({
|
|
364
|
-
email: userEmail2,
|
|
365
|
-
password: userPassword,
|
|
366
|
-
}),
|
|
367
|
-
},
|
|
368
|
-
);
|
|
369
|
-
|
|
370
|
-
global.server.app.updateConfig('auth', {
|
|
371
|
-
isAuthWithVefificationFlow: false,
|
|
372
|
-
});
|
|
373
|
-
|
|
374
|
-
const response3 = await fetch(
|
|
375
|
-
global.server.testingGetUrl('/auth/login'),
|
|
376
|
-
{
|
|
377
|
-
method: 'POST',
|
|
378
|
-
headers: {
|
|
379
|
-
'Content-type': 'application/json',
|
|
380
|
-
},
|
|
381
|
-
body: JSON.stringify({
|
|
382
|
-
email: userEmail2,
|
|
383
|
-
password: userPassword,
|
|
384
|
-
}),
|
|
385
|
-
},
|
|
386
|
-
);
|
|
387
|
-
|
|
388
|
-
const responseBody3 = await response3.json();
|
|
389
|
-
|
|
390
|
-
expect(status).toBe(201);
|
|
391
|
-
expect(status2).toBe(400);
|
|
392
|
-
expect(response3.status).toBe(200);
|
|
393
|
-
expect(responseBody3.token).toBeDefined();
|
|
394
|
-
});
|
|
395
|
-
});
|
|
396
|
-
|
|
397
|
-
it('can user send verification', async () => {
|
|
398
|
-
expect.assertions(1);
|
|
399
|
-
|
|
400
|
-
const { status } = await fetch(
|
|
401
|
-
global.server.testingGetUrl('/auth/send-verification'),
|
|
402
|
-
{
|
|
403
|
-
method: 'POST',
|
|
404
|
-
headers: {
|
|
405
|
-
'Content-type': 'application/json',
|
|
406
|
-
},
|
|
407
|
-
body: JSON.stringify({
|
|
408
|
-
email: userEmail2,
|
|
409
|
-
}),
|
|
410
|
-
},
|
|
411
|
-
);
|
|
412
|
-
|
|
413
|
-
expect(status).toBe(200);
|
|
414
|
-
});
|
|
415
|
-
|
|
416
|
-
it('can not user send verification to wrong email', async () => {
|
|
417
|
-
expect.assertions(1);
|
|
418
|
-
|
|
419
|
-
const { status } = await fetch(
|
|
420
|
-
global.server.testingGetUrl('/auth/send-verification'),
|
|
421
|
-
{
|
|
422
|
-
method: 'POST',
|
|
423
|
-
headers: {
|
|
424
|
-
'Content-type': 'application/json',
|
|
425
|
-
},
|
|
426
|
-
body: JSON.stringify({
|
|
427
|
-
email: 'wrong@gmail.com',
|
|
428
|
-
}),
|
|
429
|
-
},
|
|
430
|
-
);
|
|
431
|
-
|
|
432
|
-
expect(status).toBe(400);
|
|
433
|
-
});
|
|
434
|
-
|
|
435
|
-
describe('rate limiter', () => {
|
|
436
|
-
it('should receive 429 on rate limit exceeded', async () => {
|
|
437
|
-
expect.assertions(1);
|
|
438
|
-
|
|
439
|
-
const requests = Array.from({ length: 11 }, () =>
|
|
440
|
-
fetch(global.server.testingGetUrl('/auth/logout'), {
|
|
441
|
-
method: 'POST',
|
|
442
|
-
}),
|
|
443
|
-
);
|
|
444
|
-
|
|
445
|
-
const responses = await Promise.all(requests);
|
|
446
|
-
const statusCodes = responses.map((response) => response.status);
|
|
447
|
-
|
|
448
|
-
expect(statusCodes).toContain(429);
|
|
449
|
-
});
|
|
450
|
-
});
|
|
451
|
-
});
|
package/controllers/Home.test.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
|
|
3
|
-
describe('home', () => {
|
|
4
|
-
it('can open home have', async () => {
|
|
5
|
-
expect.assertions(1);
|
|
6
|
-
const { status } = await fetch(global.server.testingGetUrl('/')).catch(
|
|
7
|
-
() => {},
|
|
8
|
-
);
|
|
9
|
-
|
|
10
|
-
expect(status).toBe(200);
|
|
11
|
-
});
|
|
12
|
-
});
|
package/models/Migration.test.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
|
|
3
|
-
describe('migration model', () => {
|
|
4
|
-
it('migrationFile should be uniqe', async () => {
|
|
5
|
-
expect.assertions(1);
|
|
6
|
-
let errorCode;
|
|
7
|
-
const MigrationModel = global.server.app.getModel('Migration');
|
|
8
|
-
await MigrationModel.syncIndexes();
|
|
9
|
-
await MigrationModel.create({
|
|
10
|
-
migrationFile: 'a',
|
|
11
|
-
});
|
|
12
|
-
await MigrationModel.create({
|
|
13
|
-
migrationFile: 'a',
|
|
14
|
-
}).catch((e) => {
|
|
15
|
-
errorCode = e.code;
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
expect(errorCode).toBe(11000);
|
|
19
|
-
});
|
|
20
|
-
});
|
package/models/Sequence.test.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
|
|
3
|
-
describe('sequence model', () => {
|
|
4
|
-
it('should produce sequence', async () => {
|
|
5
|
-
expect.assertions(1);
|
|
6
|
-
const SequenceModel = global.server.app.getModel('Sequence');
|
|
7
|
-
|
|
8
|
-
const number1 = await SequenceModel.getSequence('typeOne');
|
|
9
|
-
|
|
10
|
-
expect(number1).toBe(1);
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
it('should produce sequence different for different types', async () => {
|
|
14
|
-
expect.assertions(3);
|
|
15
|
-
const SequenceModel = global.server.app.getModel('Sequence');
|
|
16
|
-
|
|
17
|
-
const number1 = await SequenceModel.getSequence('typeOneAgain');
|
|
18
|
-
const number2 = await SequenceModel.getSequence('typeTwo');
|
|
19
|
-
const number3 = await SequenceModel.getSequence('typeThree');
|
|
20
|
-
|
|
21
|
-
expect(number1).toBe(1);
|
|
22
|
-
expect(number2).toBe(1);
|
|
23
|
-
expect(number3).toBe(1);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it('should works on async env', async () => {
|
|
27
|
-
expect.assertions(1);
|
|
28
|
-
const SequenceModel = global.server.app.getModel('Sequence');
|
|
29
|
-
|
|
30
|
-
const promises = [];
|
|
31
|
-
const upTo = 100;
|
|
32
|
-
for (let i = 0; i < upTo; i += 1) {
|
|
33
|
-
promises.push(SequenceModel.getSequence('asyncTypeOne'));
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const data = await Promise.all(promises);
|
|
37
|
-
const summ = ((1 + upTo) / 2) * upTo; // Arithmetic progression
|
|
38
|
-
|
|
39
|
-
const summ2 = data.reduce((a, b) => a + b, 0);
|
|
40
|
-
|
|
41
|
-
expect(summ2).toBe(summ);
|
|
42
|
-
});
|
|
43
|
-
});
|
package/models/User.test.js
DELETED
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
|
|
3
|
-
const userEmail = 'testing@test.com';
|
|
4
|
-
const userPassword = 'SuperNiceSecret123$';
|
|
5
|
-
|
|
6
|
-
let globalUser;
|
|
7
|
-
|
|
8
|
-
describe('user model', () => {
|
|
9
|
-
it('can create user', async () => {
|
|
10
|
-
expect.assertions(1);
|
|
11
|
-
globalUser = await global.server.app.getModel('User').create({
|
|
12
|
-
email: userEmail,
|
|
13
|
-
password: userPassword,
|
|
14
|
-
name: {
|
|
15
|
-
nick: 'nickname',
|
|
16
|
-
},
|
|
17
|
-
});
|
|
18
|
-
expect(globalUser.id).toBeDefined();
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
it('passwords should be hashed', async () => {
|
|
22
|
-
expect.assertions(1);
|
|
23
|
-
|
|
24
|
-
const user = await global.server.app.getModel('User').findOne({
|
|
25
|
-
email: userEmail,
|
|
26
|
-
});
|
|
27
|
-
expect(user.password).not.toBe(userPassword);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('passwords should not be changed on other fields save', async () => {
|
|
31
|
-
expect.assertions(1);
|
|
32
|
-
const user = await global.server.app.getModel('User').findOne({
|
|
33
|
-
email: userEmail,
|
|
34
|
-
});
|
|
35
|
-
const psw = user.password;
|
|
36
|
-
user.email = 'rrrr';
|
|
37
|
-
await user.save();
|
|
38
|
-
user.email = userEmail;
|
|
39
|
-
await user.save();
|
|
40
|
-
|
|
41
|
-
expect(user.password).toBe(psw);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
describe('getUserByEmailAndPassword', () => {
|
|
45
|
-
it('should WORK with valid creds', async () => {
|
|
46
|
-
expect.assertions(1);
|
|
47
|
-
const userModel = await global.server.app.getModel('User');
|
|
48
|
-
const user = await userModel.getUserByEmailAndPassword(
|
|
49
|
-
userEmail,
|
|
50
|
-
userPassword,
|
|
51
|
-
);
|
|
52
|
-
expect(user.id).toBe(globalUser.id);
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it('should NOT with INvalid creds', async () => {
|
|
56
|
-
expect.assertions(1);
|
|
57
|
-
const userModel = await global.server.app.getModel('User');
|
|
58
|
-
const user = await userModel.getUserByEmailAndPassword(
|
|
59
|
-
userEmail,
|
|
60
|
-
'wrongPassword',
|
|
61
|
-
);
|
|
62
|
-
expect(user).toBeFalsy();
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it('should NOT with wrong email', async () => {
|
|
66
|
-
expect.assertions(1);
|
|
67
|
-
const userModel = await global.server.app.getModel('User');
|
|
68
|
-
const user = await userModel.getUserByEmailAndPassword(
|
|
69
|
-
'not@exists.com',
|
|
70
|
-
userPassword,
|
|
71
|
-
);
|
|
72
|
-
expect(user).toBeFalsy();
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
describe('getUserByToken', () => {
|
|
77
|
-
it('should NOT work for non valid token', async () => {
|
|
78
|
-
expect.assertions(1);
|
|
79
|
-
|
|
80
|
-
const user = await global.server.app
|
|
81
|
-
.getModel('User')
|
|
82
|
-
.getUserByToken('fake one');
|
|
83
|
-
expect(user).toBeFalsy();
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it('should work for VALID token', async () => {
|
|
87
|
-
expect.assertions(1);
|
|
88
|
-
const token = await globalUser.generateToken();
|
|
89
|
-
const user = await global.server.app
|
|
90
|
-
.getModel('User')
|
|
91
|
-
.getUserByToken(token.token);
|
|
92
|
-
expect(user.id).toBe(globalUser.id);
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
describe('getUserByVerificationToken', () => {
|
|
97
|
-
it('should NOT work for non valid token', async () => {
|
|
98
|
-
expect.assertions(1);
|
|
99
|
-
|
|
100
|
-
await expect(
|
|
101
|
-
global.server.app
|
|
102
|
-
.getModel('User')
|
|
103
|
-
.getUserByVerificationToken('fake one'),
|
|
104
|
-
).rejects.toStrictEqual(new Error('User not exists'));
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it('should work for VALID token', async () => {
|
|
108
|
-
expect.assertions(1);
|
|
109
|
-
const token = await global.server.app
|
|
110
|
-
.getModel('User')
|
|
111
|
-
.generateUserVerificationToken(globalUser);
|
|
112
|
-
|
|
113
|
-
const user = await global.server.app
|
|
114
|
-
.getModel('User')
|
|
115
|
-
.getUserByVerificationToken(token.token);
|
|
116
|
-
expect(user.id).toBe(globalUser.id);
|
|
117
|
-
});
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
describe('getUserByPasswordRecoveryToken', () => {
|
|
121
|
-
it('should NOT work for non valid token', async () => {
|
|
122
|
-
expect.assertions(1);
|
|
123
|
-
|
|
124
|
-
await expect(
|
|
125
|
-
global.server.app
|
|
126
|
-
.getModel('User')
|
|
127
|
-
.getUserByPasswordRecoveryToken('fake one'),
|
|
128
|
-
).rejects.toStrictEqual(new Error('User not exists'));
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
it('should work for VALID token', async () => {
|
|
132
|
-
expect.assertions(1);
|
|
133
|
-
const token = await global.server.app
|
|
134
|
-
.getModel('User')
|
|
135
|
-
.generateUserPasswordRecoveryToken(globalUser);
|
|
136
|
-
|
|
137
|
-
const user = await global.server.app
|
|
138
|
-
.getModel('User')
|
|
139
|
-
.getUserByPasswordRecoveryToken(token.token);
|
|
140
|
-
expect(user.id).toBe(globalUser.id);
|
|
141
|
-
});
|
|
142
|
-
});
|
|
143
|
-
});
|
package/modules/Modules.test.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import SomeController from '../controllers/test/SomeController.js';
|
|
3
|
-
import AbstractController from './AbstractController.js';
|
|
4
|
-
|
|
5
|
-
describe('abstract controller methods', () => {
|
|
6
|
-
it('can get routes', async () => {
|
|
7
|
-
expect.assertions(2);
|
|
8
|
-
|
|
9
|
-
const controller = new AbstractController(global.server.app);
|
|
10
|
-
const childController = new SomeController(global.server.app);
|
|
11
|
-
|
|
12
|
-
const { routes } = controller;
|
|
13
|
-
const { routes: childRoutes } = childController;
|
|
14
|
-
|
|
15
|
-
expect(routes).toStrictEqual({});
|
|
16
|
-
expect(childRoutes).toBeDefined();
|
|
17
|
-
});
|
|
18
|
-
});
|