@adaptivestone/framework 5.0.0-alpha.10 → 5.0.0-alpha.12
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 +8 -0
- package/package.json +3 -3
- 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/IpDetector.test.js +0 -143
- package/services/http/middleware/PrepareAppInfo.test.js +0 -26
- package/services/http/middleware/RateLimiter.test.js +0 -235
- package/services/http/middleware/RequestParser.test.js +0 -121
- package/services/http/middleware/Role.test.js +0 -93
- package/services/validate/ValidateService.test.js +0 -107
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import IpDetector from './IpDetector.js';
|
|
3
|
-
|
|
4
|
-
const testVectors = [
|
|
5
|
-
// IPv4 CIDR blocks
|
|
6
|
-
{
|
|
7
|
-
cidr: '192.168.0.0/16',
|
|
8
|
-
tests: [
|
|
9
|
-
{ ip: '192.168.1.1', matches: true },
|
|
10
|
-
{ ip: '192.169.1.1', matches: false },
|
|
11
|
-
],
|
|
12
|
-
},
|
|
13
|
-
{
|
|
14
|
-
cidr: '10.0.0.0/8',
|
|
15
|
-
tests: [
|
|
16
|
-
{ ip: '10.0.0.1', matches: true },
|
|
17
|
-
{ ip: '11.0.0.1', matches: false },
|
|
18
|
-
],
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
cidr: '172.16.0.0/12',
|
|
22
|
-
tests: [
|
|
23
|
-
{ ip: '172.16.0.1', matches: true },
|
|
24
|
-
{ ip: '172.32.0.1', matches: false },
|
|
25
|
-
],
|
|
26
|
-
},
|
|
27
|
-
|
|
28
|
-
// // IPv6 CIDR blocks
|
|
29
|
-
{
|
|
30
|
-
cidr: '2001:db8::/32',
|
|
31
|
-
tests: [
|
|
32
|
-
{ ip: '2001:db8::1', matches: true },
|
|
33
|
-
{ ip: '2001:db9::1', matches: false },
|
|
34
|
-
],
|
|
35
|
-
},
|
|
36
|
-
{
|
|
37
|
-
cidr: 'fe80::/10',
|
|
38
|
-
tests: [
|
|
39
|
-
{ ip: 'fe80::1', matches: true },
|
|
40
|
-
{ ip: 'fec0::1', matches: false },
|
|
41
|
-
],
|
|
42
|
-
},
|
|
43
|
-
{
|
|
44
|
-
cidr: '::ffff:0:0/96',
|
|
45
|
-
tests: [
|
|
46
|
-
{ ip: '::ffff:192.0.2.1', matches: true },
|
|
47
|
-
{ ip: '2001:db8::1', matches: false },
|
|
48
|
-
],
|
|
49
|
-
},
|
|
50
|
-
|
|
51
|
-
// // Specific IPv4 addresses
|
|
52
|
-
{
|
|
53
|
-
cidr: '203.0.113.1/32',
|
|
54
|
-
tests: [
|
|
55
|
-
{ ip: '203.0.113.1', matches: true },
|
|
56
|
-
{ ip: '203.0.113.2', matches: false },
|
|
57
|
-
],
|
|
58
|
-
},
|
|
59
|
-
|
|
60
|
-
// // Specific IPv6 addresses
|
|
61
|
-
{
|
|
62
|
-
cidr: '2001:db8:85a3::8a2e:370:7334/128',
|
|
63
|
-
tests: [
|
|
64
|
-
{ ip: '2001:db8:85a3::8a2e:370:7334', matches: true },
|
|
65
|
-
{ ip: '2001:db8:85a3::8a2e:370:7335', matches: false },
|
|
66
|
-
],
|
|
67
|
-
},
|
|
68
|
-
|
|
69
|
-
// // Mixed scenarios
|
|
70
|
-
{
|
|
71
|
-
cidr: '::ffff:192.0.2.0/120',
|
|
72
|
-
tests: [
|
|
73
|
-
{ ip: '::ffff:192.0.2.1', matches: true },
|
|
74
|
-
{ ip: '192.0.2.1', matches: true }, // IPv4-mapped addresses should match their IPv4 equivalents
|
|
75
|
-
{ ip: '::ffff:192.0.3.1', matches: false },
|
|
76
|
-
],
|
|
77
|
-
},
|
|
78
|
-
|
|
79
|
-
// // Edge cases
|
|
80
|
-
{
|
|
81
|
-
cidr: '0.0.0.0/0',
|
|
82
|
-
tests: [
|
|
83
|
-
{ ip: '0.0.0.0', matches: true },
|
|
84
|
-
{ ip: '255.255.255.255', matches: true },
|
|
85
|
-
{ ip: '2001:db8::1', matches: false }, // Matches any IPv4 but not IPv6
|
|
86
|
-
],
|
|
87
|
-
},
|
|
88
|
-
{
|
|
89
|
-
cidr: '::/0',
|
|
90
|
-
tests: [
|
|
91
|
-
{ ip: '::1', matches: true },
|
|
92
|
-
{ ip: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', matches: true },
|
|
93
|
-
{ ip: '192.168.1.1', matches: true },
|
|
94
|
-
],
|
|
95
|
-
},
|
|
96
|
-
{
|
|
97
|
-
cidr: '8.8.8.8-8.8.8.10', // our feature range
|
|
98
|
-
tests: [
|
|
99
|
-
{ ip: '8.8.8.7', matches: false },
|
|
100
|
-
{ ip: '8.8.8.8', matches: true },
|
|
101
|
-
{ ip: '8.8.8.9', matches: true },
|
|
102
|
-
{ ip: '8.8.8.10', matches: true },
|
|
103
|
-
{ ip: '8.8.8.11', matches: false },
|
|
104
|
-
],
|
|
105
|
-
},
|
|
106
|
-
{
|
|
107
|
-
cidr: '1.1.1.1', // one ip
|
|
108
|
-
tests: [
|
|
109
|
-
{ ip: '8.8.8.7', matches: false },
|
|
110
|
-
{ ip: '1.1.1.1', matches: true },
|
|
111
|
-
],
|
|
112
|
-
},
|
|
113
|
-
];
|
|
114
|
-
|
|
115
|
-
describe('ipDetector methods', () => {
|
|
116
|
-
it('have description fields', async () => {
|
|
117
|
-
expect.assertions(1);
|
|
118
|
-
const middleware = new IpDetector(global.server.app);
|
|
119
|
-
expect(middleware.constructor.description).toBeDefined();
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
it('middleware that works', async () => {
|
|
123
|
-
expect.hasAssertions();
|
|
124
|
-
const nextFunction = () => {};
|
|
125
|
-
for (const vector of testVectors) {
|
|
126
|
-
global.server.app.updateConfig('ipDetector', {
|
|
127
|
-
trustedProxy: [vector.cidr],
|
|
128
|
-
});
|
|
129
|
-
const middleware = new IpDetector(global.server.app);
|
|
130
|
-
for (const test of vector.tests) {
|
|
131
|
-
const req = {
|
|
132
|
-
appInfo: {},
|
|
133
|
-
headers: { 'x-forwarded-for': 'notAnIP' },
|
|
134
|
-
socket: { remoteAddress: test.ip },
|
|
135
|
-
};
|
|
136
|
-
// eslint-disable-next-line no-await-in-loop
|
|
137
|
-
await middleware.middleware(req, {}, nextFunction);
|
|
138
|
-
const result = req.appInfo.ip === 'notAnIP';
|
|
139
|
-
expect(result).toBe(test.matches);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
});
|
|
143
|
-
});
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import PrepareAppInfo from './PrepareAppInfo.js';
|
|
3
|
-
|
|
4
|
-
describe('prepareAppInfo methods', () => {
|
|
5
|
-
it('have description fields', async () => {
|
|
6
|
-
expect.assertions(1);
|
|
7
|
-
const middleware = new PrepareAppInfo(global.server.app);
|
|
8
|
-
expect(middleware.constructor.description).toBeDefined();
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
it('middleware that works', async () => {
|
|
12
|
-
expect.assertions(3);
|
|
13
|
-
const middleware = new PrepareAppInfo(global.server.app);
|
|
14
|
-
let isCalled = false;
|
|
15
|
-
const nextFunction = () => {
|
|
16
|
-
isCalled = true;
|
|
17
|
-
};
|
|
18
|
-
const req = {};
|
|
19
|
-
await middleware.middleware(req, {}, nextFunction);
|
|
20
|
-
expect(isCalled).toBeTruthy();
|
|
21
|
-
expect(req.appInfo).toBeDefined();
|
|
22
|
-
req.appInfo.test = 5;
|
|
23
|
-
await middleware.middleware(req, {}, nextFunction);
|
|
24
|
-
expect(req.appInfo.test).toBe(5);
|
|
25
|
-
});
|
|
26
|
-
});
|
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
import { setTimeout } from 'node:timers/promises';
|
|
2
|
-
import crypto from 'node:crypto';
|
|
3
|
-
import { beforeAll, afterAll, describe, it, expect } from 'vitest';
|
|
4
|
-
|
|
5
|
-
import RateLimiter from './RateLimiter.js';
|
|
6
|
-
|
|
7
|
-
let mongoRateLimiter;
|
|
8
|
-
|
|
9
|
-
describe('rate limiter methods', () => {
|
|
10
|
-
beforeAll(async () => {
|
|
11
|
-
await setTimeout(20);
|
|
12
|
-
|
|
13
|
-
mongoRateLimiter = new RateLimiter(global.server.app, {
|
|
14
|
-
driver: 'mongo',
|
|
15
|
-
limiterOptions: {
|
|
16
|
-
keyPrefix: `mongo_${Date.now()}_${crypto.randomUUID()}}`,
|
|
17
|
-
},
|
|
18
|
-
});
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
afterAll(async () => {
|
|
22
|
-
// we need to wait because redis mongo ask mongo to create indexes
|
|
23
|
-
await setTimeout(200);
|
|
24
|
-
});
|
|
25
|
-
it('have description fields', async () => {
|
|
26
|
-
expect.assertions(1);
|
|
27
|
-
const middleware = new RateLimiter(global.server.app, {
|
|
28
|
-
driver: 'redis',
|
|
29
|
-
});
|
|
30
|
-
expect(middleware.constructor.description).toBeDefined();
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('can create redis rateLimiter', async () => {
|
|
34
|
-
expect.assertions(1);
|
|
35
|
-
|
|
36
|
-
const redisRateLimiter = new RateLimiter(global.server.app, {
|
|
37
|
-
driver: 'redis',
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
expect(redisRateLimiter.limiter).toBeDefined();
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('can not create rateLimiter with unknown driver', async () => {
|
|
44
|
-
expect.assertions(1);
|
|
45
|
-
|
|
46
|
-
const rateLimiter = new RateLimiter(global.server.app, {
|
|
47
|
-
driver: 'unknown',
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
expect(rateLimiter.limiter).toBeNull();
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it('generateConsumeKey works correctly', async () => {
|
|
54
|
-
expect.assertions(1);
|
|
55
|
-
|
|
56
|
-
const redisRateLimiter = new RateLimiter(global.server.app, {
|
|
57
|
-
driver: 'redis',
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
const res = await redisRateLimiter.gerenateConsumeKey({
|
|
61
|
-
appInfo: {
|
|
62
|
-
ip: '192.168.0.0',
|
|
63
|
-
user: {
|
|
64
|
-
id: 'someId',
|
|
65
|
-
},
|
|
66
|
-
},
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
expect(res).toBe('192.168.0.0__someId');
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('generateConsumeKey with request works correctly', async () => {
|
|
73
|
-
expect.assertions(1);
|
|
74
|
-
|
|
75
|
-
const redisRateLimiter = new RateLimiter(global.server.app, {
|
|
76
|
-
driver: 'redis',
|
|
77
|
-
consumeKeyComponents: {
|
|
78
|
-
request: ['email'],
|
|
79
|
-
},
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
const res = await redisRateLimiter.gerenateConsumeKey({
|
|
83
|
-
appInfo: {
|
|
84
|
-
ip: '192.168.0.0',
|
|
85
|
-
},
|
|
86
|
-
body: {
|
|
87
|
-
email: 'foo@example.com',
|
|
88
|
-
},
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
expect(res).toBe('192.168.0.0__foo@example.com');
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it('middleware without driver should fail', async () => {
|
|
95
|
-
expect.assertions(2);
|
|
96
|
-
const rateLimiter = new RateLimiter(global.server.app, {
|
|
97
|
-
driver: 'unknown',
|
|
98
|
-
});
|
|
99
|
-
const req = {
|
|
100
|
-
appInfo: {},
|
|
101
|
-
};
|
|
102
|
-
let status;
|
|
103
|
-
let isSend;
|
|
104
|
-
await rateLimiter.middleware(
|
|
105
|
-
req,
|
|
106
|
-
{
|
|
107
|
-
status(statusCode) {
|
|
108
|
-
status = statusCode;
|
|
109
|
-
return this;
|
|
110
|
-
},
|
|
111
|
-
json() {
|
|
112
|
-
isSend = true;
|
|
113
|
-
},
|
|
114
|
-
},
|
|
115
|
-
() => {},
|
|
116
|
-
);
|
|
117
|
-
expect(status).toBe(500);
|
|
118
|
-
expect(isSend).toBeTruthy();
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
const makeOneRequest = async ({ rateLimiter, driver, request }) => {
|
|
122
|
-
let realRateLimiter = rateLimiter;
|
|
123
|
-
if (!realRateLimiter) {
|
|
124
|
-
realRateLimiter = new RateLimiter(global.server.app, {
|
|
125
|
-
driver,
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
const req = {
|
|
129
|
-
appInfo: {},
|
|
130
|
-
...request,
|
|
131
|
-
};
|
|
132
|
-
let status;
|
|
133
|
-
let isSend = false;
|
|
134
|
-
let isNextCalled = false;
|
|
135
|
-
await realRateLimiter.middleware(
|
|
136
|
-
req,
|
|
137
|
-
{
|
|
138
|
-
status(statusCode) {
|
|
139
|
-
status = statusCode;
|
|
140
|
-
return this;
|
|
141
|
-
},
|
|
142
|
-
json() {
|
|
143
|
-
isSend = true;
|
|
144
|
-
},
|
|
145
|
-
},
|
|
146
|
-
() => {
|
|
147
|
-
isNextCalled = true;
|
|
148
|
-
},
|
|
149
|
-
);
|
|
150
|
-
return { status, isSend, isNextCalled };
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
it('middleware should works with a mongo drivers', async () => {
|
|
154
|
-
expect.assertions(1);
|
|
155
|
-
const { isNextCalled } = await makeOneRequest({
|
|
156
|
-
rateLimiter: mongoRateLimiter,
|
|
157
|
-
request: { ip: '10.10.0.1' },
|
|
158
|
-
});
|
|
159
|
-
expect(isNextCalled).toBeTruthy();
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
it('middleware should works with a memory drivers', async () => {
|
|
163
|
-
expect.assertions(1);
|
|
164
|
-
const { isNextCalled } = await makeOneRequest({
|
|
165
|
-
driver: 'memory',
|
|
166
|
-
request: { ip: '10.10.0.1' },
|
|
167
|
-
});
|
|
168
|
-
expect(isNextCalled).toBeTruthy();
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
it('middleware should works with a redis drivers', async () => {
|
|
172
|
-
expect.assertions(1);
|
|
173
|
-
const { isNextCalled } = await makeOneRequest({
|
|
174
|
-
driver: 'redis',
|
|
175
|
-
request: { ip: '10.10.0.1' },
|
|
176
|
-
});
|
|
177
|
-
expect(isNextCalled).toBeTruthy();
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
it('middleware should rate limits for us. mongo driver', async () => {
|
|
181
|
-
expect.assertions(2);
|
|
182
|
-
|
|
183
|
-
const middlewares = Array.from({ length: 20 }, () =>
|
|
184
|
-
makeOneRequest({ rateLimiter: mongoRateLimiter }),
|
|
185
|
-
);
|
|
186
|
-
|
|
187
|
-
const data = await Promise.all(middlewares);
|
|
188
|
-
|
|
189
|
-
const status = data.find((obj) => obj.status === 429);
|
|
190
|
-
const isSend = data.find((obj) => obj.isSend);
|
|
191
|
-
|
|
192
|
-
expect(status.status).toBe(429);
|
|
193
|
-
expect(isSend.isSend).toBeTruthy();
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
it('middleware should rate limits for us. memory driver', async () => {
|
|
197
|
-
expect.assertions(2);
|
|
198
|
-
|
|
199
|
-
const rateLimiter = new RateLimiter(global.server.app, {
|
|
200
|
-
driver: 'memory',
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
const middlewares = Array.from({ length: 20 }, () =>
|
|
204
|
-
makeOneRequest({ rateLimiter }),
|
|
205
|
-
);
|
|
206
|
-
|
|
207
|
-
const data = await Promise.all(middlewares);
|
|
208
|
-
|
|
209
|
-
const status = data.find((obj) => obj.status === 429);
|
|
210
|
-
const isSend = data.find((obj) => obj.isSend);
|
|
211
|
-
|
|
212
|
-
expect(status.status).toBe(429);
|
|
213
|
-
expect(isSend.isSend).toBeTruthy();
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
it('middleware should rate limits for us. redis driver', async () => {
|
|
217
|
-
expect.assertions(2);
|
|
218
|
-
|
|
219
|
-
const rateLimiter = new RateLimiter(global.server.app, {
|
|
220
|
-
driver: 'redis',
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
const middlewares = Array.from({ length: 20 }, () =>
|
|
224
|
-
makeOneRequest({ rateLimiter }),
|
|
225
|
-
);
|
|
226
|
-
|
|
227
|
-
const data = await Promise.all(middlewares);
|
|
228
|
-
|
|
229
|
-
const status = data.find((obj) => obj.status === 429);
|
|
230
|
-
const isSend = data.find((obj) => obj.isSend);
|
|
231
|
-
|
|
232
|
-
expect(status.status).toBe(429);
|
|
233
|
-
expect(isSend.isSend).toBeTruthy();
|
|
234
|
-
});
|
|
235
|
-
});
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
import { createServer } from 'node:http';
|
|
2
|
-
import { describe, it, expect } from 'vitest';
|
|
3
|
-
import { PersistentFile } from 'formidable';
|
|
4
|
-
|
|
5
|
-
import RequestParser from './RequestParser.js';
|
|
6
|
-
|
|
7
|
-
describe('reqest parser limiter methods', () => {
|
|
8
|
-
it('have description fields', async () => {
|
|
9
|
-
expect.assertions(1);
|
|
10
|
-
const middleware = new RequestParser(global.server.app);
|
|
11
|
-
expect(middleware.constructor.description).toBeDefined();
|
|
12
|
-
});
|
|
13
|
-
it('middleware that works', async () => {
|
|
14
|
-
expect.assertions(4);
|
|
15
|
-
|
|
16
|
-
await new Promise((done) => {
|
|
17
|
-
// from https://github.com/node-formidable/formidable/blob/master/test-node/standalone/promise.test.js
|
|
18
|
-
|
|
19
|
-
const server = createServer(async (req, res) => {
|
|
20
|
-
req.appInfo = {};
|
|
21
|
-
const middleware = new RequestParser(global.server.app);
|
|
22
|
-
middleware.middleware(req, {}, (err) => {
|
|
23
|
-
expect(err).toBeUndefined();
|
|
24
|
-
expect(req.body.title).toBeDefined();
|
|
25
|
-
expect(req.body.multipleFiles).toBeDefined();
|
|
26
|
-
expect(
|
|
27
|
-
req.body.multipleFiles[0] instanceof PersistentFile,
|
|
28
|
-
).toBeTruthy();
|
|
29
|
-
|
|
30
|
-
res.writeHead(200);
|
|
31
|
-
res.end('ok');
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
server.listen(null, async () => {
|
|
35
|
-
const chosenPort = server.address().port;
|
|
36
|
-
const body = `----13068458571765726332503797717\r
|
|
37
|
-
Content-Disposition: form-data; name="title"\r
|
|
38
|
-
\r
|
|
39
|
-
a\r
|
|
40
|
-
----13068458571765726332503797717\r
|
|
41
|
-
Content-Disposition: form-data; name="multipleFiles"; filename="x.txt"\r
|
|
42
|
-
Content-Type: application/x-javascript\r
|
|
43
|
-
\r
|
|
44
|
-
\r
|
|
45
|
-
\r
|
|
46
|
-
a\r
|
|
47
|
-
b\r
|
|
48
|
-
c\r
|
|
49
|
-
d\r
|
|
50
|
-
\r
|
|
51
|
-
----13068458571765726332503797717--\r
|
|
52
|
-
`;
|
|
53
|
-
await fetch(String(new URL(`http:localhost:${chosenPort}/`)), {
|
|
54
|
-
method: 'POST',
|
|
55
|
-
|
|
56
|
-
headers: {
|
|
57
|
-
'Content-Length': body.length,
|
|
58
|
-
Host: `localhost:${chosenPort}`,
|
|
59
|
-
'Content-Type':
|
|
60
|
-
'multipart/form-data; boundary=--13068458571765726332503797717',
|
|
61
|
-
},
|
|
62
|
-
body,
|
|
63
|
-
}).catch((err) => {
|
|
64
|
-
console.error(err);
|
|
65
|
-
done(err);
|
|
66
|
-
});
|
|
67
|
-
server.close(() => {
|
|
68
|
-
done();
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
it('middleware with a problem', async () => {
|
|
74
|
-
expect.assertions(1);
|
|
75
|
-
|
|
76
|
-
await new Promise((done) => {
|
|
77
|
-
// from https://github.com/node-formidable/formidable/blob/master/test-node/standalone/promise.test.js
|
|
78
|
-
|
|
79
|
-
const server = createServer(async (req, res) => {
|
|
80
|
-
req.appInfo = {};
|
|
81
|
-
const middleware = new RequestParser(global.server.app);
|
|
82
|
-
let status;
|
|
83
|
-
|
|
84
|
-
const resp = {
|
|
85
|
-
status: (code) => {
|
|
86
|
-
status = code;
|
|
87
|
-
return resp;
|
|
88
|
-
},
|
|
89
|
-
json: () => resp,
|
|
90
|
-
};
|
|
91
|
-
await middleware.middleware(req, resp, () => {});
|
|
92
|
-
expect(status).toBe(400);
|
|
93
|
-
// expect(err).toBeDefined();
|
|
94
|
-
|
|
95
|
-
res.writeHead(200);
|
|
96
|
-
res.end('ok');
|
|
97
|
-
});
|
|
98
|
-
server.listen(null, async () => {
|
|
99
|
-
const chosenPort = server.address().port;
|
|
100
|
-
const body = 'someBadBody';
|
|
101
|
-
|
|
102
|
-
await fetch(String(new URL(`http:localhost:${chosenPort}/`)), {
|
|
103
|
-
method: 'POST',
|
|
104
|
-
|
|
105
|
-
headers: {
|
|
106
|
-
'Content-Length': body.length,
|
|
107
|
-
Host: `localhost:${chosenPort}`,
|
|
108
|
-
'Content-Type': 'badContentType',
|
|
109
|
-
},
|
|
110
|
-
body,
|
|
111
|
-
}).catch((err) => {
|
|
112
|
-
console.error(err);
|
|
113
|
-
done(err);
|
|
114
|
-
});
|
|
115
|
-
server.close(() => {
|
|
116
|
-
done();
|
|
117
|
-
});
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
});
|
|
121
|
-
});
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import Role from './Role.js';
|
|
3
|
-
|
|
4
|
-
describe('role middleware methods', () => {
|
|
5
|
-
it('have description fields', async () => {
|
|
6
|
-
expect.assertions(1);
|
|
7
|
-
const middleware = new Role(global.server.app);
|
|
8
|
-
expect(middleware.constructor.description).toBeDefined();
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
it('middleware pass when user presented with a right role', async () => {
|
|
12
|
-
expect.assertions(1);
|
|
13
|
-
let isCalled = false;
|
|
14
|
-
const nextFunction = () => {
|
|
15
|
-
isCalled = true;
|
|
16
|
-
};
|
|
17
|
-
const req = {
|
|
18
|
-
appInfo: {
|
|
19
|
-
user: {
|
|
20
|
-
roles: ['role1', 'role2'],
|
|
21
|
-
},
|
|
22
|
-
},
|
|
23
|
-
};
|
|
24
|
-
const middleware = new Role(global.server.app, {
|
|
25
|
-
roles: ['admin', 'role1'],
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
await middleware.middleware(req, {}, nextFunction);
|
|
29
|
-
expect(isCalled).toBeTruthy();
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('middleware NOT pass when user NOT presented', async () => {
|
|
33
|
-
expect.assertions(3);
|
|
34
|
-
let isCalled = false;
|
|
35
|
-
let status;
|
|
36
|
-
let isSend;
|
|
37
|
-
const nextFunction = () => {
|
|
38
|
-
isCalled = true;
|
|
39
|
-
};
|
|
40
|
-
const req = {
|
|
41
|
-
appInfo: {}, // no user
|
|
42
|
-
};
|
|
43
|
-
const middleware = new Role(global.server.app);
|
|
44
|
-
await middleware.middleware(
|
|
45
|
-
req,
|
|
46
|
-
{
|
|
47
|
-
status(statusCode) {
|
|
48
|
-
status = statusCode;
|
|
49
|
-
return this;
|
|
50
|
-
},
|
|
51
|
-
json() {
|
|
52
|
-
isSend = true;
|
|
53
|
-
},
|
|
54
|
-
},
|
|
55
|
-
nextFunction,
|
|
56
|
-
);
|
|
57
|
-
expect(isCalled).toBeFalsy();
|
|
58
|
-
expect(status).toBe(401);
|
|
59
|
-
expect(isSend).toBeTruthy();
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it('middleware NOT pass when user have a wrong role', async () => {
|
|
63
|
-
expect.assertions(3);
|
|
64
|
-
let isCalled = false;
|
|
65
|
-
let status;
|
|
66
|
-
let isSend;
|
|
67
|
-
const nextFunction = () => {
|
|
68
|
-
isCalled = true;
|
|
69
|
-
};
|
|
70
|
-
const req = {
|
|
71
|
-
appInfo: {
|
|
72
|
-
user: { roles: ['role1', 'role2'] },
|
|
73
|
-
},
|
|
74
|
-
};
|
|
75
|
-
const middleware = new Role(global.server.app, { roles: ['admin'] });
|
|
76
|
-
await middleware.middleware(
|
|
77
|
-
req,
|
|
78
|
-
{
|
|
79
|
-
status(statusCode) {
|
|
80
|
-
status = statusCode;
|
|
81
|
-
return this;
|
|
82
|
-
},
|
|
83
|
-
json() {
|
|
84
|
-
isSend = true;
|
|
85
|
-
},
|
|
86
|
-
},
|
|
87
|
-
nextFunction,
|
|
88
|
-
);
|
|
89
|
-
expect(isCalled).toBeFalsy();
|
|
90
|
-
expect(status).toBe(403);
|
|
91
|
-
expect(isSend).toBeTruthy();
|
|
92
|
-
});
|
|
93
|
-
});
|