soul-cli 0.10.0 → 0.12.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/.env.sample +1 -0
- package/.github/workflows/ci.yml +2 -0
- package/.prettierrc +4 -0
- package/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +25 -0
- package/README.md +3 -0
- package/ROADMAP.md +83 -25
- package/docs/api/batch-examples.md +55 -0
- package/docs/api/rows-examples.md +10 -2
- package/docs/api-examples.md +1 -0
- package/docs/auth.md +10 -0
- package/package.json +1 -1
- package/src/cli.js +6 -0
- package/src/config/index.js +7 -0
- package/src/config/index.test.js +10 -0
- package/src/constants/messages.js +1 -1
- package/src/controllers/batch.js +128 -0
- package/src/controllers/batch.test.js +180 -0
- package/src/controllers/rows.js +223 -213
- package/src/controllers/rows.query.test.js +369 -0
- package/src/index.js +2 -0
- package/src/middlewares/api.js +6 -2
- package/src/middlewares/api.test.js +27 -13
- package/src/middlewares/auth.js +52 -33
- package/src/middlewares/auth.test.js +137 -3
- package/src/middlewares/broadcast.js +7 -11
- package/src/routes/batch.js +12 -0
- package/src/routes/rows.js +1 -0
- package/src/schemas/batch.js +27 -0
- package/src/schemas/rows.js +4 -1
- package/src/services/authService.js +31 -4
- package/src/services/authService.test.js +76 -0
- package/src/services/rowService.js +12 -6
- package/src/services/rowService.test.js +37 -0
- package/src/swagger/index.js +14 -16
- package/src/swagger/swagger.json +68 -59
- package/src/utils/query.js +285 -0
- package/src/utils/query.test.js +147 -0
- package/src/utils/sql.js +24 -0
- package/src/utils/sql.test.js +14 -0
- package/src/websocket.js +10 -8
- package/src/websocket.test.js +52 -0
- package/src/schemas/index.js +0 -30
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
const supertest = require('supertest');
|
|
2
|
+
|
|
3
|
+
const app = require('../index');
|
|
4
|
+
const db = require('../db/index');
|
|
5
|
+
const config = require('../config');
|
|
6
|
+
const { generateToken } = require('../utils');
|
|
7
|
+
|
|
8
|
+
const requestWithSupertest = supertest(app);
|
|
9
|
+
|
|
10
|
+
describe('Rows query params', () => {
|
|
11
|
+
let accessToken;
|
|
12
|
+
|
|
13
|
+
const get = (query) =>
|
|
14
|
+
requestWithSupertest
|
|
15
|
+
.get(`/api/tables/qtest/rows?${query}`)
|
|
16
|
+
.set('Cookie', [`accessToken=${accessToken}`]);
|
|
17
|
+
|
|
18
|
+
const filter = (filters, extra = '') =>
|
|
19
|
+
get(`_filters=${encodeURIComponent(filters)}&_ordering=id${extra}`);
|
|
20
|
+
|
|
21
|
+
const names = (res) => res.body.data.map((row) => row.name);
|
|
22
|
+
|
|
23
|
+
beforeAll(async () => {
|
|
24
|
+
accessToken = await generateToken(
|
|
25
|
+
{ username: 'John', isSuperuser: true },
|
|
26
|
+
config.tokenSecret,
|
|
27
|
+
'1H',
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
db.prepare(
|
|
31
|
+
'CREATE TABLE qtest (id INTEGER PRIMARY KEY, name TEXT, category TEXT, price INTEGER, note TEXT)',
|
|
32
|
+
).run();
|
|
33
|
+
const insert = db.prepare(
|
|
34
|
+
'INSERT INTO qtest (name, category, price, note) VALUES (?, ?, ?, ?)',
|
|
35
|
+
);
|
|
36
|
+
[
|
|
37
|
+
['Apple', 'fruit', 3, null],
|
|
38
|
+
['Banana', 'fruit', 5, 'a,b'],
|
|
39
|
+
['Carrot', 'veg', 2, 'x:y'],
|
|
40
|
+
['Date', 'fruit', 5, 'p|q'],
|
|
41
|
+
['Eggplant', 'veg', 7, '(raw)'],
|
|
42
|
+
['Fig', 'fruit', 9, ''],
|
|
43
|
+
['Grape', 'fruit', 4, '100%'],
|
|
44
|
+
].forEach((row) => insert.run(...row));
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
afterAll(() => {
|
|
48
|
+
db.prepare('DROP TABLE IF EXISTS qtest').run();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe('existing _filters behavior', () => {
|
|
52
|
+
it('ANDs comma-separated filters', async () => {
|
|
53
|
+
const res = await filter('category:fruit,price__gte:5');
|
|
54
|
+
expect(names(res)).toEqual(['Banana', 'Date', 'Fig']);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('turns a bracketed value into IN for any operator', async () => {
|
|
58
|
+
const res = await filter('id__neq:[1,2]');
|
|
59
|
+
expect(res.body.data.map((row) => row.id)).toEqual([1, 2]);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('keeps colons inside a value', async () => {
|
|
63
|
+
const res = await filter('note:x:y');
|
|
64
|
+
expect(names(res)).toEqual(['Carrot']);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('returns 400 for an empty value', async () => {
|
|
68
|
+
const res = await filter('note:');
|
|
69
|
+
expect(res.status).toEqual(400);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('returns 400 for an unknown operator and lists the valid ones', async () => {
|
|
73
|
+
const res = await filter('price__bogus:1');
|
|
74
|
+
expect(res.status).toEqual(400);
|
|
75
|
+
expect(res.body.message).toContain('__between');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('filters null values', async () => {
|
|
79
|
+
const res = await filter('note__null');
|
|
80
|
+
expect(names(res)).toEqual(['Apple']);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe('_ordering', () => {
|
|
85
|
+
it('orders by several columns', async () => {
|
|
86
|
+
const res = await get('_ordering=category,-price,name');
|
|
87
|
+
expect(names(res)).toEqual([
|
|
88
|
+
'Fig',
|
|
89
|
+
'Banana',
|
|
90
|
+
'Date',
|
|
91
|
+
'Grape',
|
|
92
|
+
'Apple',
|
|
93
|
+
'Eggplant',
|
|
94
|
+
'Carrot',
|
|
95
|
+
]);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('returns 400 when one of the columns does not exist', async () => {
|
|
99
|
+
const res = await get('_ordering=category,bogus');
|
|
100
|
+
expect(res.status).toEqual(400);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('joins a related table once when ordering by it twice', async () => {
|
|
104
|
+
const res = await requestWithSupertest
|
|
105
|
+
.get(
|
|
106
|
+
'/api/tables/posts/rows?_ordering=userId.firstName,-userId.lastName&_extend=userId',
|
|
107
|
+
)
|
|
108
|
+
.set('Cookie', [`accessToken=${accessToken}`]);
|
|
109
|
+
expect(res.status).toEqual(200);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe('_distinct', () => {
|
|
114
|
+
it('returns unique values and a matching total', async () => {
|
|
115
|
+
const res = await get('_distinct=category&_ordering=category&_limit=1');
|
|
116
|
+
expect(res.status).toEqual(200);
|
|
117
|
+
expect(res.body.data).toEqual([{ category: 'fruit' }]);
|
|
118
|
+
expect(res.body.total).toEqual(2);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('returns unique combinations of several columns', async () => {
|
|
122
|
+
const res = await get(
|
|
123
|
+
'_distinct=category,price&_ordering=category,price',
|
|
124
|
+
);
|
|
125
|
+
expect(res.body.data).toHaveLength(6);
|
|
126
|
+
expect(res.body.total).toEqual(6);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('returns 400 for an unknown column', async () => {
|
|
130
|
+
const res = await get('_distinct=bogus');
|
|
131
|
+
expect(res.status).toEqual(400);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('returns 400 when combined with _schema', async () => {
|
|
135
|
+
const res = await get('_distinct=category&_schema=name');
|
|
136
|
+
expect(res.status).toEqual(400);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('returns 400 when combined with _group_by', async () => {
|
|
140
|
+
const res = await get('_distinct=category&_group_by=category');
|
|
141
|
+
expect(res.status).toEqual(400);
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
describe('operators', () => {
|
|
146
|
+
it('like uses the given pattern', async () => {
|
|
147
|
+
const res = await filter('name__like:A%');
|
|
148
|
+
expect(names(res)).toEqual(['Apple']);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('contains matches anywhere', async () => {
|
|
152
|
+
const res = await filter('name__contains:an');
|
|
153
|
+
expect(names(res)).toEqual(['Banana', 'Eggplant']);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('startswith matches the beginning', async () => {
|
|
157
|
+
const res = await filter('name__startswith:E');
|
|
158
|
+
expect(names(res)).toEqual(['Eggplant']);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('contains treats % and _ literally', async () => {
|
|
162
|
+
expect(names(await filter('note__contains:%'))).toEqual(['Grape']);
|
|
163
|
+
expect(names(await filter('note__contains:_'))).toEqual([]);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('between is inclusive', async () => {
|
|
167
|
+
const res = await filter('price__between:[3,5]');
|
|
168
|
+
expect(names(res)).toEqual(['Apple', 'Banana', 'Date', 'Grape']);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('between needs exactly two values', async () => {
|
|
172
|
+
expect((await filter('price__between:5')).status).toEqual(400);
|
|
173
|
+
expect((await filter('price__between:[1,2,3]')).status).toEqual(400);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('in and notin take a list or a single value', async () => {
|
|
177
|
+
expect(names(await filter('id__in:[1,2]'))).toEqual(['Apple', 'Banana']);
|
|
178
|
+
expect(names(await filter('id__in:3'))).toEqual(['Carrot']);
|
|
179
|
+
expect(names(await filter('id__notin:[1,2,3,4,5,6]'))).toEqual(['Grape']);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('like operators reject a list', async () => {
|
|
183
|
+
const res = await filter('name__like:[a,b]');
|
|
184
|
+
expect(res.status).toEqual(400);
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
describe('quoted values', () => {
|
|
189
|
+
it('allows commas, colons and pipes inside quotes', async () => {
|
|
190
|
+
expect(names(await filter('note:"a,b"'))).toEqual(['Banana']);
|
|
191
|
+
expect(names(await filter('note:"x:y"'))).toEqual(['Carrot']);
|
|
192
|
+
expect(names(await filter('note:"p|q"'))).toEqual(['Date']);
|
|
193
|
+
expect(names(await filter('note:"(raw)"'))).toEqual(['Eggplant']);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it('allows quoted values inside a list and next to other filters', async () => {
|
|
197
|
+
expect(names(await filter('note:["a,b","x:y"]'))).toEqual([
|
|
198
|
+
'Banana',
|
|
199
|
+
'Carrot',
|
|
200
|
+
]);
|
|
201
|
+
expect(names(await filter('note:"a,b",category:fruit'))).toEqual([
|
|
202
|
+
'Banana',
|
|
203
|
+
]);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('matches an empty string with ""', async () => {
|
|
207
|
+
expect(names(await filter('note:""'))).toEqual(['Fig']);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it('treats an unclosed or trailing-text quote literally', async () => {
|
|
211
|
+
const unclosed = await filter('name:"Apple');
|
|
212
|
+
expect(unclosed.status).toEqual(200);
|
|
213
|
+
expect(unclosed.body.data).toEqual([]);
|
|
214
|
+
expect((await filter('name:"Apple"x')).body.data).toEqual([]);
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
describe('OR groups and NOT', () => {
|
|
219
|
+
it('ORs the filters inside a group', async () => {
|
|
220
|
+
const res = await filter('(category:veg|price__gte:9)');
|
|
221
|
+
expect(names(res)).toEqual(['Carrot', 'Eggplant', 'Fig']);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('ANDs a group with other filters', async () => {
|
|
225
|
+
const res = await filter('(category:veg|price__gte:9),price__lt:8');
|
|
226
|
+
expect(names(res)).toEqual(['Carrot', 'Eggplant']);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('protects | inside quoted values', async () => {
|
|
230
|
+
const res = await filter('(note:"p|q"|note:"a,b")');
|
|
231
|
+
expect(names(res)).toEqual(['Banana', 'Date']);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it('negates a filter with !', async () => {
|
|
235
|
+
const res = await filter('!category:veg');
|
|
236
|
+
expect(names(res)).toEqual(['Apple', 'Banana', 'Date', 'Fig', 'Grape']);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it('negates a group with !', async () => {
|
|
240
|
+
const res = await filter('!(category:veg|price__gte:9)');
|
|
241
|
+
expect(names(res)).toEqual(['Apple', 'Banana', 'Date', 'Grape']);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('negates inside a group', async () => {
|
|
245
|
+
const res = await filter('(!category:fruit|id:1)');
|
|
246
|
+
expect(names(res)).toEqual(['Apple', 'Carrot', 'Eggplant']);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it('excludes NULL rows when negating, like __neq', async () => {
|
|
250
|
+
const res = await filter('!note:x:y');
|
|
251
|
+
expect(names(res)).toEqual([
|
|
252
|
+
'Banana',
|
|
253
|
+
'Date',
|
|
254
|
+
'Eggplant',
|
|
255
|
+
'Fig',
|
|
256
|
+
'Grape',
|
|
257
|
+
]);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it('returns 400 for an unclosed group', async () => {
|
|
261
|
+
expect((await filter('(category:veg')).status).toEqual(400);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it('returns 400 for a comma inside a group', async () => {
|
|
265
|
+
expect((await filter('(category:veg,price:2)')).status).toEqual(400);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it('returns 400 for an unknown column inside a group', async () => {
|
|
269
|
+
expect((await filter('(bogus:1|id:2)')).status).toEqual(400);
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
describe('_group_by and _aggregate', () => {
|
|
274
|
+
it('groups rows and aggregates them', async () => {
|
|
275
|
+
const res = await get(
|
|
276
|
+
'_group_by=category&_aggregate=count,sum:price,avg:price,min:price,max:price&_ordering=category',
|
|
277
|
+
);
|
|
278
|
+
expect(res.status).toEqual(200);
|
|
279
|
+
expect(res.body.data).toEqual([
|
|
280
|
+
{
|
|
281
|
+
category: 'fruit',
|
|
282
|
+
count: 5,
|
|
283
|
+
sum_price: 26,
|
|
284
|
+
avg_price: 5.2,
|
|
285
|
+
min_price: 3,
|
|
286
|
+
max_price: 9,
|
|
287
|
+
},
|
|
288
|
+
{
|
|
289
|
+
category: 'veg',
|
|
290
|
+
count: 2,
|
|
291
|
+
sum_price: 9,
|
|
292
|
+
avg_price: 4.5,
|
|
293
|
+
min_price: 2,
|
|
294
|
+
max_price: 7,
|
|
295
|
+
},
|
|
296
|
+
]);
|
|
297
|
+
expect(res.body.total).toEqual(2);
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it('orders by an aggregate alias', async () => {
|
|
301
|
+
const res = await get(
|
|
302
|
+
'_group_by=category&_aggregate=sum:price&_ordering=-sum_price',
|
|
303
|
+
);
|
|
304
|
+
expect(res.body.data.map((row) => row.category)).toEqual([
|
|
305
|
+
'fruit',
|
|
306
|
+
'veg',
|
|
307
|
+
]);
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it('returns a single row when only _aggregate is given', async () => {
|
|
311
|
+
const res = await get('_aggregate=count');
|
|
312
|
+
expect(res.body.data).toEqual([{ count: 7 }]);
|
|
313
|
+
expect(res.body.total).toEqual(1);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it('supports count:column', async () => {
|
|
317
|
+
const res = await get('_aggregate=count:note');
|
|
318
|
+
expect(res.body.data).toEqual([{ count_note: 6 }]);
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
it('applies _filters before grouping', async () => {
|
|
322
|
+
const res = await get(
|
|
323
|
+
'_filters=category:fruit&_group_by=category&_aggregate=count',
|
|
324
|
+
);
|
|
325
|
+
expect(res.body.data).toEqual([{ category: 'fruit', count: 5 }]);
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
it('groups without an aggregate', async () => {
|
|
329
|
+
const res = await get('_group_by=category&_ordering=category');
|
|
330
|
+
expect(res.body.data).toEqual([
|
|
331
|
+
{ category: 'fruit' },
|
|
332
|
+
{ category: 'veg' },
|
|
333
|
+
]);
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
it('keeps total and pagination links correct', async () => {
|
|
337
|
+
const res = await get('_group_by=price&_ordering=price&_limit=2');
|
|
338
|
+
expect(res.body.data).toHaveLength(2);
|
|
339
|
+
expect(res.body.total).toEqual(6);
|
|
340
|
+
expect(res.body.next).toContain('_group_by=price');
|
|
341
|
+
expect(res.body.previous).toBeNull();
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
it('returns 400 for bad input', async () => {
|
|
345
|
+
const bad = [
|
|
346
|
+
'_group_by=bogus',
|
|
347
|
+
'_group_by=category&_ordering=name',
|
|
348
|
+
'_group_by=category&_schema=name',
|
|
349
|
+
'_aggregate=median:price',
|
|
350
|
+
'_aggregate=sum',
|
|
351
|
+
'_aggregate=count:bogus',
|
|
352
|
+
'_aggregate=sum:price:x',
|
|
353
|
+
`_aggregate=${encodeURIComponent('count:price) FROM qtest --')}`,
|
|
354
|
+
];
|
|
355
|
+
for (const query of bad) {
|
|
356
|
+
const res = await get(query);
|
|
357
|
+
expect([query, res.status]).toEqual([query, 400]);
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
it('does not run injected SQL in _group_by', async () => {
|
|
362
|
+
const res = await get(
|
|
363
|
+
`_group_by=${encodeURIComponent('category;DROP TABLE qtest')}`,
|
|
364
|
+
);
|
|
365
|
+
expect(res.status).toEqual(400);
|
|
366
|
+
expect((await get('_limit=1')).status).toEqual(200);
|
|
367
|
+
});
|
|
368
|
+
});
|
|
369
|
+
});
|
package/src/index.js
CHANGED
|
@@ -16,6 +16,7 @@ const logger = require('./utils/logger');
|
|
|
16
16
|
const rootRoutes = require('./routes/index');
|
|
17
17
|
const tablesRoutes = require('./routes/tables');
|
|
18
18
|
const rowsRoutes = require('./routes/rows');
|
|
19
|
+
const batchRoutes = require('./routes/batch');
|
|
19
20
|
const authRoutes = require('./routes/auth');
|
|
20
21
|
|
|
21
22
|
const swaggerFile = require('./swagger/swagger.json');
|
|
@@ -107,6 +108,7 @@ runCLICommands();
|
|
|
107
108
|
|
|
108
109
|
app.use('/api/docs', swaggerUi.serve, swaggerUi.setup(swaggerFile));
|
|
109
110
|
app.use('/api', rootRoutes);
|
|
111
|
+
app.use('/api', batchRoutes);
|
|
110
112
|
app.use('/api/tables', tablesRoutes);
|
|
111
113
|
app.use('/api/tables', rowsRoutes);
|
|
112
114
|
|
package/src/middlewares/api.js
CHANGED
|
@@ -22,8 +22,12 @@ const processRowRequest = async (req, res, next) => {
|
|
|
22
22
|
const resource = req.params.name;
|
|
23
23
|
const { method } = req;
|
|
24
24
|
|
|
25
|
-
//
|
|
26
|
-
if (
|
|
25
|
+
// Auth tables are read-only while AUTH is set to false
|
|
26
|
+
if (
|
|
27
|
+
apiConstants.authEndpoints.includes(resource) &&
|
|
28
|
+
!config.auth &&
|
|
29
|
+
method !== httpVerbs.GET
|
|
30
|
+
) {
|
|
27
31
|
return res.status(403).send({
|
|
28
32
|
message: errorMessage.AUTH_SET_TO_FALSE_ERROR,
|
|
29
33
|
});
|
|
@@ -10,20 +10,34 @@ jest.mock('../config', () => ({
|
|
|
10
10
|
|
|
11
11
|
const { processRowRequest } = require('./api');
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
const run = async (method) => {
|
|
14
|
+
const req = { params: { name: '_users' }, method };
|
|
15
|
+
const res = { status: jest.fn().mockReturnThis(), send: jest.fn() };
|
|
16
|
+
const next = jest.fn();
|
|
17
|
+
await processRowRequest(req, res, next);
|
|
18
|
+
return { res, next };
|
|
19
|
+
};
|
|
18
20
|
|
|
19
|
-
|
|
21
|
+
describe('processRowRequest', () => {
|
|
22
|
+
it('allows GET on an auth-endpoint table with AUTH disabled', async () => {
|
|
23
|
+
const { res, next } = await run('GET');
|
|
20
24
|
|
|
21
|
-
expect(res.status).
|
|
22
|
-
expect(
|
|
23
|
-
expect.objectContaining({
|
|
24
|
-
message: expect.stringContaining('AUTH'),
|
|
25
|
-
}),
|
|
26
|
-
);
|
|
27
|
-
expect(next).not.toHaveBeenCalled();
|
|
25
|
+
expect(res.status).not.toHaveBeenCalled();
|
|
26
|
+
expect(next).toHaveBeenCalled();
|
|
28
27
|
});
|
|
28
|
+
|
|
29
|
+
it.each(['POST', 'PUT', 'DELETE'])(
|
|
30
|
+
'returns 403 on %s to an auth-endpoint table with AUTH disabled',
|
|
31
|
+
async (method) => {
|
|
32
|
+
const { res, next } = await run(method);
|
|
33
|
+
|
|
34
|
+
expect(res.status).toHaveBeenCalledWith(403);
|
|
35
|
+
expect(res.send).toHaveBeenCalledWith(
|
|
36
|
+
expect.objectContaining({
|
|
37
|
+
message: expect.stringContaining('AUTH'),
|
|
38
|
+
}),
|
|
39
|
+
);
|
|
40
|
+
expect(next).not.toHaveBeenCalled();
|
|
41
|
+
},
|
|
42
|
+
);
|
|
29
43
|
});
|
package/src/middlewares/auth.js
CHANGED
|
@@ -6,48 +6,67 @@ const { authService } = require('../services');
|
|
|
6
6
|
|
|
7
7
|
const { errorMessage } = responseMessages;
|
|
8
8
|
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
);
|
|
23
|
-
req.user = payload;
|
|
24
|
-
} catch {
|
|
25
|
-
// an invalid/expired access token is a routine client-side
|
|
26
|
-
// condition, not an unexpected server error -- not logged as one.
|
|
27
|
-
return res
|
|
28
|
-
.status(401)
|
|
29
|
-
.send({ message: errorMessage.INVALID_ACCESS_TOKEN_ERROR });
|
|
30
|
-
}
|
|
9
|
+
const authenticate = async (req, res, next) => {
|
|
10
|
+
if (!config.auth) {
|
|
11
|
+
return next();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const accessToken = req.cookies.accessToken;
|
|
15
|
+
|
|
16
|
+
// no token: anonymous request, only allowed when an anon role is configured
|
|
17
|
+
if (!accessToken && !config.anonRole) {
|
|
18
|
+
return res
|
|
19
|
+
.status(401)
|
|
20
|
+
.send({ message: errorMessage.INVALID_ACCESS_TOKEN_ERROR });
|
|
21
|
+
}
|
|
31
22
|
|
|
32
|
-
|
|
33
|
-
|
|
23
|
+
if (accessToken) {
|
|
24
|
+
try {
|
|
25
|
+
req.user = await decodeToken(accessToken, config.tokenSecret);
|
|
26
|
+
} catch {
|
|
27
|
+
// an invalid/expired access token is a routine client-side
|
|
28
|
+
// condition, not an unexpected server error -- not logged as one.
|
|
29
|
+
return res
|
|
30
|
+
.status(401)
|
|
31
|
+
.send({ message: errorMessage.INVALID_ACCESS_TOKEN_ERROR });
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
next();
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const hasAccess = (req, res, next) =>
|
|
39
|
+
authenticate(req, res, () => {
|
|
40
|
+
if (!config.auth) {
|
|
41
|
+
return next();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
// endpoints open to any authenticated user regardless of roles
|
|
46
|
+
if (
|
|
47
|
+
req.user &&
|
|
48
|
+
apiConstants.universalAccessEndpoints.includes(req.originalUrl)
|
|
49
|
+
) {
|
|
34
50
|
return next();
|
|
35
51
|
}
|
|
36
52
|
|
|
37
|
-
if (
|
|
53
|
+
if (
|
|
54
|
+
authService.hasTablePermission({
|
|
55
|
+
payload: req.user || null,
|
|
56
|
+
tableName: req.params.name,
|
|
57
|
+
verb: req.method,
|
|
58
|
+
})
|
|
59
|
+
) {
|
|
38
60
|
return next();
|
|
39
61
|
}
|
|
40
62
|
|
|
41
63
|
return res
|
|
42
64
|
.status(403)
|
|
43
65
|
.send({ message: errorMessage.NOT_AUTHORIZED_ERROR });
|
|
44
|
-
}
|
|
45
|
-
|
|
66
|
+
} catch (error) {
|
|
67
|
+
logger.error(error.message, { stack: error.stack });
|
|
68
|
+
res.status(401).send({ message: error.message });
|
|
46
69
|
}
|
|
47
|
-
}
|
|
48
|
-
logger.error(error.message, { stack: error.stack });
|
|
49
|
-
res.status(401).send({ message: error.message });
|
|
50
|
-
}
|
|
51
|
-
};
|
|
70
|
+
});
|
|
52
71
|
|
|
53
|
-
module.exports = { hasAccess };
|
|
72
|
+
module.exports = { authenticate, hasAccess };
|