@adaptivestone/framework 4.3.1 → 4.4.0
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 +4 -0
- package/controllers/Auth.test.js +258 -132
- package/controllers/Home.test.js +3 -4
- package/package.json +4 -4
- package/tests/setup.js +2 -0
package/CHANGELOG.md
CHANGED
package/controllers/Auth.test.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
const request = require('supertest');
|
|
2
|
-
|
|
3
1
|
const userEmail = 'testing@test.com';
|
|
4
2
|
const userPassword = 'SuperNiceSecret123$';
|
|
5
3
|
|
|
@@ -9,85 +7,136 @@ describe('auth', () => {
|
|
|
9
7
|
describe('registration', () => {
|
|
10
8
|
it('code NOT able to create user with wrong email', async () => {
|
|
11
9
|
expect.assertions(1);
|
|
12
|
-
const { status } = await
|
|
13
|
-
.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
10
|
+
const { status } = await fetch(
|
|
11
|
+
global.server.testingGetUrl('/auth/register'),
|
|
12
|
+
{
|
|
13
|
+
method: 'POST',
|
|
14
|
+
headers: {
|
|
15
|
+
'Content-type': 'application/json',
|
|
16
|
+
},
|
|
17
|
+
body: JSON.stringify({
|
|
18
|
+
email: 'bad email',
|
|
19
|
+
password: userPassword,
|
|
20
|
+
nickName: 'test',
|
|
21
|
+
}),
|
|
22
|
+
},
|
|
23
|
+
).catch(() => {});
|
|
24
|
+
|
|
19
25
|
expect(status).toBe(400);
|
|
20
26
|
});
|
|
21
27
|
|
|
22
28
|
it('can create user', async () => {
|
|
23
29
|
expect.assertions(1);
|
|
24
|
-
const { status } = await
|
|
25
|
-
.
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
const { status } = await fetch(
|
|
31
|
+
global.server.testingGetUrl('/auth/register'),
|
|
32
|
+
{
|
|
33
|
+
method: 'POST',
|
|
34
|
+
headers: {
|
|
35
|
+
'Content-type': 'application/json',
|
|
36
|
+
},
|
|
37
|
+
body: JSON.stringify({
|
|
38
|
+
email: userEmail,
|
|
39
|
+
password: userPassword,
|
|
40
|
+
nickName: 'test',
|
|
41
|
+
}),
|
|
42
|
+
},
|
|
43
|
+
);
|
|
31
44
|
expect(status).toBe(201);
|
|
32
45
|
});
|
|
33
46
|
|
|
34
47
|
it('can not create user with the same nickname', async () => {
|
|
35
48
|
expect.assertions(1);
|
|
36
|
-
await
|
|
37
|
-
|
|
38
|
-
|
|
49
|
+
await fetch(global.server.testingGetUrl('/auth/register'), {
|
|
50
|
+
method: 'POST',
|
|
51
|
+
headers: {
|
|
52
|
+
'Content-type': 'application/json',
|
|
53
|
+
},
|
|
54
|
+
body: JSON.stringify({
|
|
39
55
|
email: userEmail,
|
|
40
56
|
password: userPassword,
|
|
41
57
|
nickName: 'test',
|
|
42
|
-
})
|
|
58
|
+
}),
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const { status } = await fetch(
|
|
62
|
+
global.server.testingGetUrl('/auth/register'),
|
|
63
|
+
{
|
|
64
|
+
method: 'POST',
|
|
65
|
+
headers: {
|
|
66
|
+
'Content-type': 'application/json',
|
|
67
|
+
},
|
|
68
|
+
body: JSON.stringify({
|
|
69
|
+
email: userEmail2,
|
|
70
|
+
password: '123',
|
|
71
|
+
nickName: 'test',
|
|
72
|
+
}),
|
|
73
|
+
},
|
|
74
|
+
).catch(() => {});
|
|
43
75
|
|
|
44
|
-
const { status } = await request(global.server.app.httpServer.express)
|
|
45
|
-
.post('/auth/register')
|
|
46
|
-
.send({
|
|
47
|
-
email: userEmail2,
|
|
48
|
-
password: '123',
|
|
49
|
-
nickName: 'test',
|
|
50
|
-
});
|
|
51
76
|
expect(status).toBe(400);
|
|
52
77
|
});
|
|
53
78
|
|
|
54
79
|
it('can NOT create SAME user', async () => {
|
|
55
80
|
expect.assertions(1);
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
.
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
81
|
+
|
|
82
|
+
const { status } = await fetch(
|
|
83
|
+
global.server.testingGetUrl('/auth/register'),
|
|
84
|
+
{
|
|
85
|
+
method: 'POST',
|
|
86
|
+
headers: {
|
|
87
|
+
'Content-type': 'application/json',
|
|
88
|
+
},
|
|
89
|
+
body: JSON.stringify({
|
|
90
|
+
email: userEmail,
|
|
91
|
+
password: userPassword,
|
|
92
|
+
nickName: 'test',
|
|
93
|
+
}),
|
|
94
|
+
},
|
|
95
|
+
);
|
|
96
|
+
|
|
63
97
|
expect(status).toBe(400);
|
|
64
98
|
});
|
|
65
99
|
});
|
|
66
100
|
|
|
67
101
|
describe('login', () => {
|
|
68
|
-
it('can NOT login with normal creds and not
|
|
102
|
+
it('can NOT login with normal creds and not verified email', async () => {
|
|
69
103
|
expect.assertions(1);
|
|
70
|
-
const { status } = await
|
|
71
|
-
.
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
104
|
+
const { status } = await fetch(
|
|
105
|
+
global.server.testingGetUrl('/auth/login'),
|
|
106
|
+
{
|
|
107
|
+
method: 'POST',
|
|
108
|
+
headers: {
|
|
109
|
+
'Content-type': 'application/json',
|
|
110
|
+
},
|
|
111
|
+
body: JSON.stringify({
|
|
112
|
+
email: userEmail,
|
|
113
|
+
password: userPassword,
|
|
114
|
+
}),
|
|
115
|
+
},
|
|
116
|
+
).catch(() => {});
|
|
117
|
+
|
|
76
118
|
expect(status).toBe(400);
|
|
77
119
|
});
|
|
78
120
|
|
|
79
121
|
it('can NOT login with WRONG creds', async () => {
|
|
80
122
|
expect.assertions(1);
|
|
81
|
-
const { status } = await
|
|
82
|
-
.
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
123
|
+
const { status } = await fetch(
|
|
124
|
+
global.server.testingGetUrl('/auth/login'),
|
|
125
|
+
{
|
|
126
|
+
method: 'POST',
|
|
127
|
+
headers: {
|
|
128
|
+
'Content-type': 'application/json',
|
|
129
|
+
},
|
|
130
|
+
body: JSON.stringify({
|
|
131
|
+
email: 'test@test.by',
|
|
132
|
+
password: 'noPassword$',
|
|
133
|
+
}),
|
|
134
|
+
},
|
|
135
|
+
).catch(() => {});
|
|
87
136
|
expect(status).toBe(400);
|
|
88
137
|
});
|
|
89
138
|
|
|
90
|
-
it('can login with normal creds and
|
|
139
|
+
it('can login with normal creds and verified email', async () => {
|
|
91
140
|
expect.assertions(2);
|
|
92
141
|
|
|
93
142
|
const user = await global.server.app
|
|
@@ -96,16 +145,21 @@ describe('auth', () => {
|
|
|
96
145
|
user.isVerified = true;
|
|
97
146
|
await user.save();
|
|
98
147
|
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
148
|
+
const response = await fetch(global.server.testingGetUrl('/auth/login'), {
|
|
149
|
+
method: 'POST',
|
|
150
|
+
headers: {
|
|
151
|
+
'Content-type': 'application/json',
|
|
152
|
+
},
|
|
153
|
+
body: JSON.stringify({
|
|
104
154
|
email: userEmail,
|
|
105
155
|
password: userPassword,
|
|
106
|
-
})
|
|
107
|
-
|
|
108
|
-
|
|
156
|
+
}),
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
const responseBody = await response.json();
|
|
160
|
+
|
|
161
|
+
expect(response.status).toBe(200);
|
|
162
|
+
expect(responseBody.token).toBeDefined();
|
|
109
163
|
});
|
|
110
164
|
});
|
|
111
165
|
|
|
@@ -126,9 +180,14 @@ describe('auth', () => {
|
|
|
126
180
|
|
|
127
181
|
await user.save();
|
|
128
182
|
|
|
129
|
-
const { status } = await
|
|
130
|
-
global.server.
|
|
131
|
-
|
|
183
|
+
const { status } = await fetch(
|
|
184
|
+
`${global.server.testingGetUrl(
|
|
185
|
+
'/auth/verify',
|
|
186
|
+
)}?verification_token=testToken`,
|
|
187
|
+
{
|
|
188
|
+
method: 'POST',
|
|
189
|
+
},
|
|
190
|
+
);
|
|
132
191
|
|
|
133
192
|
const { isVerified } = await global.server.app.getModel('User').findOne({
|
|
134
193
|
email: 'Test@gmail.com',
|
|
@@ -154,9 +213,14 @@ describe('auth', () => {
|
|
|
154
213
|
|
|
155
214
|
await user.save();
|
|
156
215
|
|
|
157
|
-
const { status } = await
|
|
158
|
-
global.server.
|
|
159
|
-
|
|
216
|
+
const { status } = await fetch(
|
|
217
|
+
`${global.server.testingGetUrl(
|
|
218
|
+
'/auth/verify',
|
|
219
|
+
)}?verification_token=testToken123wrong`,
|
|
220
|
+
{
|
|
221
|
+
method: 'POST',
|
|
222
|
+
},
|
|
223
|
+
);
|
|
160
224
|
|
|
161
225
|
const { isVerified } = await global.server.app.getModel('User').findOne({
|
|
162
226
|
email: 'Test423@gmail.com',
|
|
@@ -168,21 +232,37 @@ describe('auth', () => {
|
|
|
168
232
|
|
|
169
233
|
it('can NOT send recovery to not exist email', async () => {
|
|
170
234
|
expect.assertions(1);
|
|
171
|
-
const { status } = await
|
|
172
|
-
.
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
235
|
+
const { status } = await fetch(
|
|
236
|
+
global.server.testingGetUrl('/auth/send-recovery-email'),
|
|
237
|
+
{
|
|
238
|
+
method: 'POST',
|
|
239
|
+
headers: {
|
|
240
|
+
'Content-type': 'application/json',
|
|
241
|
+
},
|
|
242
|
+
body: JSON.stringify({
|
|
243
|
+
email: 'notExists@gmail.com',
|
|
244
|
+
}),
|
|
245
|
+
},
|
|
246
|
+
);
|
|
247
|
+
|
|
176
248
|
expect(status).toBe(400);
|
|
177
249
|
});
|
|
178
250
|
|
|
179
251
|
it('can send recovery to exist email', async () => {
|
|
180
252
|
expect.assertions(1);
|
|
181
|
-
const { status } = await
|
|
182
|
-
.
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
253
|
+
const { status } = await fetch(
|
|
254
|
+
global.server.testingGetUrl('/auth/send-recovery-email'),
|
|
255
|
+
{
|
|
256
|
+
method: 'POST',
|
|
257
|
+
headers: {
|
|
258
|
+
'Content-type': 'application/json',
|
|
259
|
+
},
|
|
260
|
+
body: JSON.stringify({
|
|
261
|
+
email: userEmail,
|
|
262
|
+
}),
|
|
263
|
+
},
|
|
264
|
+
);
|
|
265
|
+
|
|
186
266
|
expect(status).toBe(200);
|
|
187
267
|
});
|
|
188
268
|
|
|
@@ -203,12 +283,19 @@ describe('auth', () => {
|
|
|
203
283
|
|
|
204
284
|
await user.save();
|
|
205
285
|
|
|
206
|
-
const { status } = await
|
|
207
|
-
.
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
286
|
+
const { status } = await fetch(
|
|
287
|
+
global.server.testingGetUrl('/auth/recover-password'),
|
|
288
|
+
{
|
|
289
|
+
method: 'POST',
|
|
290
|
+
headers: {
|
|
291
|
+
'Content-type': 'application/json',
|
|
292
|
+
},
|
|
293
|
+
body: JSON.stringify({
|
|
294
|
+
password: 'newPass',
|
|
295
|
+
passwordRecoveryToken: 'superPassword',
|
|
296
|
+
}),
|
|
297
|
+
},
|
|
298
|
+
);
|
|
212
299
|
|
|
213
300
|
expect(status).toBe(200);
|
|
214
301
|
});
|
|
@@ -230,94 +317,133 @@ describe('auth', () => {
|
|
|
230
317
|
|
|
231
318
|
await user.save();
|
|
232
319
|
|
|
233
|
-
const { status } = await
|
|
234
|
-
.
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
320
|
+
const { status } = await fetch(
|
|
321
|
+
global.server.testingGetUrl('/auth/recover-password'),
|
|
322
|
+
{
|
|
323
|
+
method: 'POST',
|
|
324
|
+
headers: {
|
|
325
|
+
'Content-type': 'application/json',
|
|
326
|
+
},
|
|
327
|
+
body: JSON.stringify({
|
|
328
|
+
password: 'newPass',
|
|
329
|
+
passwordRecoveryToken: '13123',
|
|
330
|
+
}),
|
|
331
|
+
},
|
|
332
|
+
);
|
|
239
333
|
|
|
240
334
|
expect(status).toBe(400);
|
|
241
335
|
});
|
|
242
336
|
|
|
243
|
-
it('can login with normal creds and
|
|
337
|
+
it('can login with normal creds and NOT verifyed email if option isAuthWithVefificationFlow is set', async () => {
|
|
244
338
|
expect.assertions(4);
|
|
245
339
|
|
|
246
|
-
const { status } = await
|
|
247
|
-
.
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
340
|
+
const { status } = await fetch(
|
|
341
|
+
global.server.testingGetUrl('/auth/register'),
|
|
342
|
+
{
|
|
343
|
+
method: 'POST',
|
|
344
|
+
headers: {
|
|
345
|
+
'Content-type': 'application/json',
|
|
346
|
+
},
|
|
347
|
+
body: JSON.stringify({
|
|
348
|
+
email: userEmail2,
|
|
349
|
+
password: userPassword,
|
|
350
|
+
}),
|
|
351
|
+
},
|
|
352
|
+
);
|
|
353
|
+
|
|
354
|
+
const { status: status2 } = await fetch(
|
|
355
|
+
global.server.testingGetUrl('/auth/login'),
|
|
356
|
+
{
|
|
357
|
+
method: 'POST',
|
|
358
|
+
headers: {
|
|
359
|
+
'Content-type': 'application/json',
|
|
360
|
+
},
|
|
361
|
+
body: JSON.stringify({
|
|
362
|
+
email: userEmail2,
|
|
363
|
+
password: userPassword,
|
|
364
|
+
}),
|
|
365
|
+
},
|
|
366
|
+
);
|
|
261
367
|
|
|
262
368
|
global.server.app.updateConfig('auth', {
|
|
263
369
|
isAuthWithVefificationFlow: false,
|
|
264
370
|
});
|
|
265
371
|
|
|
266
|
-
const
|
|
267
|
-
global.server.
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
372
|
+
const response3 = await fetch(
|
|
373
|
+
global.server.testingGetUrl('/auth/login'),
|
|
374
|
+
{
|
|
375
|
+
method: 'POST',
|
|
376
|
+
headers: {
|
|
377
|
+
'Content-type': 'application/json',
|
|
378
|
+
},
|
|
379
|
+
body: JSON.stringify({
|
|
380
|
+
email: userEmail2,
|
|
381
|
+
password: userPassword,
|
|
382
|
+
}),
|
|
383
|
+
},
|
|
384
|
+
);
|
|
385
|
+
|
|
386
|
+
const responseBody3 = await response3.json();
|
|
274
387
|
|
|
275
388
|
expect(status).toBe(201);
|
|
276
389
|
expect(status2).toBe(400);
|
|
277
|
-
expect(
|
|
278
|
-
expect(
|
|
390
|
+
expect(response3.status).toBe(200);
|
|
391
|
+
expect(responseBody3.token).toBeDefined();
|
|
279
392
|
});
|
|
280
393
|
});
|
|
281
394
|
|
|
282
395
|
it('can user send verification', async () => {
|
|
283
396
|
expect.assertions(1);
|
|
284
397
|
|
|
285
|
-
const { status } = await
|
|
286
|
-
.
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
398
|
+
const { status } = await fetch(
|
|
399
|
+
global.server.testingGetUrl('/auth/send-verification'),
|
|
400
|
+
{
|
|
401
|
+
method: 'POST',
|
|
402
|
+
headers: {
|
|
403
|
+
'Content-type': 'application/json',
|
|
404
|
+
},
|
|
405
|
+
body: JSON.stringify({
|
|
406
|
+
email: userEmail2,
|
|
407
|
+
}),
|
|
408
|
+
},
|
|
409
|
+
);
|
|
410
|
+
|
|
290
411
|
expect(status).toBe(200);
|
|
291
412
|
});
|
|
292
413
|
|
|
293
414
|
it('can not user send verification to wrong email', async () => {
|
|
294
415
|
expect.assertions(1);
|
|
295
416
|
|
|
296
|
-
const { status } = await
|
|
297
|
-
.
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
417
|
+
const { status } = await fetch(
|
|
418
|
+
global.server.testingGetUrl('/auth/send-verification'),
|
|
419
|
+
{
|
|
420
|
+
method: 'POST',
|
|
421
|
+
headers: {
|
|
422
|
+
'Content-type': 'application/json',
|
|
423
|
+
},
|
|
424
|
+
body: JSON.stringify({
|
|
425
|
+
email: 'wrong@gmail.com',
|
|
426
|
+
}),
|
|
427
|
+
},
|
|
428
|
+
);
|
|
429
|
+
|
|
301
430
|
expect(status).toBe(400);
|
|
302
431
|
});
|
|
303
432
|
|
|
304
433
|
describe('rate limiter', () => {
|
|
305
|
-
it('
|
|
434
|
+
it('should receive 429 on rate limit exceeded', async () => {
|
|
306
435
|
expect.assertions(1);
|
|
307
|
-
const resultsPromise = [];
|
|
308
436
|
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
);
|
|
315
|
-
}
|
|
437
|
+
const requests = Array.from({ length: 11 }, () =>
|
|
438
|
+
fetch(global.server.testingGetUrl('/auth/logout'), {
|
|
439
|
+
method: 'POST',
|
|
440
|
+
}),
|
|
441
|
+
);
|
|
316
442
|
|
|
317
|
-
const
|
|
318
|
-
const
|
|
443
|
+
const responses = await Promise.all(requests);
|
|
444
|
+
const statusCodes = responses.map((response) => response.status);
|
|
319
445
|
|
|
320
|
-
expect(
|
|
446
|
+
expect(statusCodes).toContain(429);
|
|
321
447
|
});
|
|
322
448
|
});
|
|
323
449
|
});
|
package/controllers/Home.test.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
const request = require('supertest');
|
|
2
|
-
|
|
3
1
|
describe('home', () => {
|
|
4
2
|
it('can open home have', async () => {
|
|
5
3
|
expect.assertions(1);
|
|
6
|
-
const { status } = await
|
|
7
|
-
|
|
4
|
+
const { status } = await fetch(global.server.testingGetUrl('/')).catch(
|
|
5
|
+
() => {},
|
|
8
6
|
);
|
|
7
|
+
|
|
9
8
|
expect(status).toBe(200);
|
|
10
9
|
});
|
|
11
10
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adaptivestone/framework",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.4.0",
|
|
4
4
|
"description": "Adaptive stone node js framework",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"engines": {
|
|
@@ -22,7 +22,8 @@
|
|
|
22
22
|
"prepare": "husky install",
|
|
23
23
|
"cli": "node cliCommand",
|
|
24
24
|
"benchmark": "h2load -n 10000 -c 50 -p 'http/1.1' http://localhost:3300/",
|
|
25
|
-
"benchmark2": "h2load -n 10000 -c 50 https://localhost:3300/"
|
|
25
|
+
"benchmark2": "h2load -n 10000 -c 50 https://localhost:3300/",
|
|
26
|
+
"redis:docker": "docker run --rm -p 6379:6379 redis"
|
|
26
27
|
},
|
|
27
28
|
"jest": {
|
|
28
29
|
"setupFilesAfterEnv": [
|
|
@@ -71,8 +72,7 @@
|
|
|
71
72
|
"lint-staged": "^14.0.0",
|
|
72
73
|
"mongodb-memory-server": "^8.0.2",
|
|
73
74
|
"nodemon": "^3.0.1",
|
|
74
|
-
"prettier": "^3.0.0"
|
|
75
|
-
"supertest": "^6.1.4"
|
|
75
|
+
"prettier": "^3.0.0"
|
|
76
76
|
},
|
|
77
77
|
"lint-staged": {
|
|
78
78
|
"**/*.{js,jsx,ts,tsx,json,css,scss,md}": [
|
package/tests/setup.js
CHANGED
|
@@ -46,6 +46,8 @@ beforeAll(async () => {
|
|
|
46
46
|
if (!global.testSetup) {
|
|
47
47
|
global.testSetup = {};
|
|
48
48
|
}
|
|
49
|
+
global.server.testingGetUrl = (urlPart) =>
|
|
50
|
+
`http://127.0.0.1:${global.server.getConfig('http').port}${urlPart}`;
|
|
49
51
|
if (!global.testSetup.disableUserCreate) {
|
|
50
52
|
const User = global.server.app.getModel('User');
|
|
51
53
|
global.user = await User.create({
|