soul-cli 0.11.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/.prettierrc +4 -0
- package/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +13 -0
- package/README.md +3 -0
- package/docs/api/batch-examples.md +55 -0
- 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 +108 -121
- 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/services/authService.js +31 -4
- package/src/services/authService.test.js +76 -0
- package/src/swagger/index.js +14 -16
- package/src/swagger/swagger.json +48 -57
- package/src/websocket.js +10 -8
- package/src/websocket.test.js +52 -0
- package/src/schemas/index.js +0 -30
package/src/controllers/rows.js
CHANGED
|
@@ -12,6 +12,92 @@ const {
|
|
|
12
12
|
buildGroupedSelect,
|
|
13
13
|
} = require('../utils/query');
|
|
14
14
|
|
|
15
|
+
const failure = (status, body) =>
|
|
16
|
+
Object.assign(new Error(body.message || body.error), { status, body });
|
|
17
|
+
|
|
18
|
+
const sendError = (res, error) =>
|
|
19
|
+
res
|
|
20
|
+
.status(error.status || 400)
|
|
21
|
+
.json(error.body || { message: error.message, error });
|
|
22
|
+
|
|
23
|
+
const resolveLookupField = (tableName, lookupField) => {
|
|
24
|
+
if (lookupField) {
|
|
25
|
+
assertValidColumnName(db, tableName, lookupField);
|
|
26
|
+
return lookupField;
|
|
27
|
+
}
|
|
28
|
+
return db
|
|
29
|
+
.prepare('SELECT * FROM pragma_table_info(?)')
|
|
30
|
+
.all(tableName)
|
|
31
|
+
.find((field) => field.pk === 1).name;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// Core row writes: sync, throw on failure, shared with the batch endpoint.
|
|
35
|
+
const insertRow = ({ tableName, fields: queryFields }) => {
|
|
36
|
+
assertValidTableName(db, tableName);
|
|
37
|
+
|
|
38
|
+
// Remove null values from fields for accurate query construction.
|
|
39
|
+
const fields = Object.fromEntries(
|
|
40
|
+
Object.entries(queryFields).filter(([, value]) => value !== null),
|
|
41
|
+
);
|
|
42
|
+
const data = rowService.save({ tableName, fields });
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
status: 201,
|
|
46
|
+
body: { message: 'Row inserted', data },
|
|
47
|
+
broadcast: {
|
|
48
|
+
type: 'INSERT',
|
|
49
|
+
data: { pk: data.lastInsertRowid, ...fields },
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const updateRows = ({ tableName, pks, fields, lookupField: lookup }) => {
|
|
55
|
+
assertValidTableName(db, tableName);
|
|
56
|
+
const lookupField = resolveLookupField(tableName, lookup);
|
|
57
|
+
Object.keys(fields).forEach((field) =>
|
|
58
|
+
assertValidColumnName(db, tableName, field),
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
if (Object.keys(fields).length === 0) {
|
|
62
|
+
throw failure(400, {
|
|
63
|
+
message: 'No fields provided',
|
|
64
|
+
error: 'no_fields_provided',
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const data = rowService.update({ tableName, fields, lookupField, pks });
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
status: 200,
|
|
72
|
+
body: { message: 'Row updated', data },
|
|
73
|
+
broadcast: {
|
|
74
|
+
type: 'UPDATE',
|
|
75
|
+
_lookup_field: lookupField,
|
|
76
|
+
data: { pks: pks.split(','), ...fields },
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const deleteRows = ({ tableName, pks, lookupField: lookup }) => {
|
|
82
|
+
assertValidTableName(db, tableName);
|
|
83
|
+
const lookupField = resolveLookupField(tableName, lookup);
|
|
84
|
+
const data = rowService.delete({ tableName, lookupField, pks });
|
|
85
|
+
|
|
86
|
+
if (data.changes === 0) {
|
|
87
|
+
throw failure(404, { error: 'not_found' });
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
status: 200,
|
|
92
|
+
body: { message: 'Row deleted', data },
|
|
93
|
+
broadcast: {
|
|
94
|
+
type: 'DELETE',
|
|
95
|
+
_lookup_field: lookupField,
|
|
96
|
+
data: { pks: pks.split(',') },
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
|
|
15
101
|
// Return paginated rows of a table
|
|
16
102
|
const listTableRows = async (req, res, next) => {
|
|
17
103
|
/*
|
|
@@ -427,17 +513,10 @@ const insertRowInTable = async (req, res, next) => {
|
|
|
427
513
|
*/
|
|
428
514
|
|
|
429
515
|
const { name: tableName } = req.params;
|
|
430
|
-
const { fields
|
|
431
|
-
|
|
432
|
-
// Remove null values from fields for accurate query construction.
|
|
433
|
-
const fields = Object.fromEntries(
|
|
434
|
-
Object.entries(queryFields).filter(([, value]) => value !== null),
|
|
435
|
-
);
|
|
516
|
+
const { fields } = req.body;
|
|
436
517
|
|
|
437
518
|
try {
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
const data = rowService.save({ tableName, fields });
|
|
519
|
+
const { status, body, broadcast } = insertRow({ tableName, fields });
|
|
441
520
|
|
|
442
521
|
/*
|
|
443
522
|
#swagger.responses[201] = {
|
|
@@ -447,18 +526,8 @@ const insertRowInTable = async (req, res, next) => {
|
|
|
447
526
|
}
|
|
448
527
|
}
|
|
449
528
|
*/
|
|
450
|
-
res.status(
|
|
451
|
-
|
|
452
|
-
data,
|
|
453
|
-
});
|
|
454
|
-
|
|
455
|
-
req.broadcast = {
|
|
456
|
-
type: 'INSERT',
|
|
457
|
-
data: {
|
|
458
|
-
pk: data.lastInsertRowid,
|
|
459
|
-
...fields,
|
|
460
|
-
},
|
|
461
|
-
};
|
|
529
|
+
res.status(status).json(body);
|
|
530
|
+
req.broadcast = broadcast;
|
|
462
531
|
next();
|
|
463
532
|
} catch (error) {
|
|
464
533
|
/*
|
|
@@ -469,10 +538,7 @@ const insertRowInTable = async (req, res, next) => {
|
|
|
469
538
|
}
|
|
470
539
|
}
|
|
471
540
|
*/
|
|
472
|
-
res
|
|
473
|
-
message: error.message,
|
|
474
|
-
error: error,
|
|
475
|
-
});
|
|
541
|
+
sendError(res, error);
|
|
476
542
|
}
|
|
477
543
|
};
|
|
478
544
|
|
|
@@ -718,64 +784,18 @@ const updateRowInTableByPK = async (req, res, next) => {
|
|
|
718
784
|
const { fields } = req.body;
|
|
719
785
|
const { _lookup_field } = req.query;
|
|
720
786
|
|
|
721
|
-
let lookupField = _lookup_field;
|
|
722
|
-
|
|
723
|
-
try {
|
|
724
|
-
assertValidTableName(db, tableName);
|
|
725
|
-
|
|
726
|
-
if (!_lookup_field) {
|
|
727
|
-
// find the primary key of the table
|
|
728
|
-
lookupField = db
|
|
729
|
-
.prepare('SELECT * FROM pragma_table_info(?)')
|
|
730
|
-
.all(tableName)
|
|
731
|
-
.find((field) => field.pk === 1).name;
|
|
732
|
-
} else {
|
|
733
|
-
assertValidColumnName(db, tableName, _lookup_field);
|
|
734
|
-
}
|
|
735
|
-
|
|
736
|
-
Object.keys(fields).forEach((field) =>
|
|
737
|
-
assertValidColumnName(db, tableName, field),
|
|
738
|
-
);
|
|
739
|
-
} catch (error) {
|
|
740
|
-
return res.status(error.status || 400).json({
|
|
741
|
-
message: error.message,
|
|
742
|
-
error: error,
|
|
743
|
-
});
|
|
744
|
-
}
|
|
745
|
-
|
|
746
|
-
if (Object.keys(fields).length === 0) {
|
|
747
|
-
return res.status(400).json({
|
|
748
|
-
message: 'No fields provided',
|
|
749
|
-
error: 'no_fields_provided',
|
|
750
|
-
});
|
|
751
|
-
}
|
|
752
|
-
|
|
753
787
|
try {
|
|
754
|
-
const
|
|
788
|
+
const { status, body, broadcast } = updateRows({
|
|
755
789
|
tableName,
|
|
756
|
-
fields,
|
|
757
|
-
lookupField,
|
|
758
790
|
pks,
|
|
791
|
+
fields,
|
|
792
|
+
lookupField: _lookup_field,
|
|
759
793
|
});
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
message: 'Row updated',
|
|
763
|
-
data,
|
|
764
|
-
});
|
|
765
|
-
req.broadcast = {
|
|
766
|
-
type: 'UPDATE',
|
|
767
|
-
_lookup_field: lookupField,
|
|
768
|
-
data: {
|
|
769
|
-
pks: pks.split(','),
|
|
770
|
-
...fields,
|
|
771
|
-
},
|
|
772
|
-
};
|
|
794
|
+
res.status(status).json(body);
|
|
795
|
+
req.broadcast = broadcast;
|
|
773
796
|
next();
|
|
774
797
|
} catch (error) {
|
|
775
|
-
res
|
|
776
|
-
message: error.message,
|
|
777
|
-
error: error,
|
|
778
|
-
});
|
|
798
|
+
sendError(res, error);
|
|
779
799
|
}
|
|
780
800
|
};
|
|
781
801
|
|
|
@@ -807,57 +827,24 @@ const deleteRowInTableByPK = async (req, res, next) => {
|
|
|
807
827
|
const { name: tableName, pks } = req.params;
|
|
808
828
|
const { _lookup_field } = req.query;
|
|
809
829
|
|
|
810
|
-
let lookupField = _lookup_field;
|
|
811
|
-
|
|
812
830
|
try {
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
lookupField = db
|
|
818
|
-
.prepare('SELECT * FROM pragma_table_info(?)')
|
|
819
|
-
.all(tableName)
|
|
820
|
-
.find((field) => field.pk === 1).name;
|
|
821
|
-
} else {
|
|
822
|
-
assertValidColumnName(db, tableName, _lookup_field);
|
|
823
|
-
}
|
|
824
|
-
} catch (error) {
|
|
825
|
-
return res.status(error.status || 400).json({
|
|
826
|
-
message: error.message,
|
|
827
|
-
error: error,
|
|
831
|
+
const { status, body, broadcast } = deleteRows({
|
|
832
|
+
tableName,
|
|
833
|
+
pks,
|
|
834
|
+
lookupField: _lookup_field,
|
|
828
835
|
});
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
const data = rowService.delete({ tableName, lookupField, pks });
|
|
833
|
-
|
|
834
|
-
if (data.changes === 0) {
|
|
835
|
-
res.status(404).json({
|
|
836
|
-
error: 'not_found',
|
|
837
|
-
});
|
|
838
|
-
} else {
|
|
839
|
-
res.json({
|
|
840
|
-
message: 'Row deleted',
|
|
841
|
-
data,
|
|
842
|
-
});
|
|
843
|
-
req.broadcast = {
|
|
844
|
-
type: 'DELETE',
|
|
845
|
-
_lookup_field: lookupField,
|
|
846
|
-
data: {
|
|
847
|
-
pks: pks.split(','),
|
|
848
|
-
},
|
|
849
|
-
};
|
|
850
|
-
next();
|
|
851
|
-
}
|
|
836
|
+
res.status(status).json(body);
|
|
837
|
+
req.broadcast = broadcast;
|
|
838
|
+
next();
|
|
852
839
|
} catch (error) {
|
|
853
|
-
res
|
|
854
|
-
message: error.message,
|
|
855
|
-
error: error,
|
|
856
|
-
});
|
|
840
|
+
sendError(res, error);
|
|
857
841
|
}
|
|
858
842
|
};
|
|
859
843
|
|
|
860
844
|
module.exports = {
|
|
845
|
+
insertRow,
|
|
846
|
+
updateRows,
|
|
847
|
+
deleteRows,
|
|
861
848
|
listTableRows,
|
|
862
849
|
insertRowInTable,
|
|
863
850
|
getRowInTableByPK,
|
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 };
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
jest.mock('../config', () => ({
|
|
1
|
+
jest.mock('../config', () => ({
|
|
2
|
+
auth: false,
|
|
3
|
+
tokenSecret: 'test-secret',
|
|
4
|
+
anonRole: null,
|
|
5
|
+
}));
|
|
2
6
|
jest.mock('../utils/index', () => ({ decodeToken: jest.fn() }));
|
|
3
7
|
jest.mock('../utils/logger', () => ({ error: jest.fn() }));
|
|
4
8
|
jest.mock('../services', () => ({
|
|
@@ -8,7 +12,7 @@ jest.mock('../services', () => ({
|
|
|
8
12
|
const config = require('../config');
|
|
9
13
|
const { decodeToken } = require('../utils/index');
|
|
10
14
|
const { authService } = require('../services');
|
|
11
|
-
const { hasAccess } = require('./auth');
|
|
15
|
+
const { authenticate, hasAccess } = require('./auth');
|
|
12
16
|
|
|
13
17
|
describe('hasAccess', () => {
|
|
14
18
|
let req, res, next;
|
|
@@ -16,11 +20,12 @@ describe('hasAccess', () => {
|
|
|
16
20
|
beforeEach(() => {
|
|
17
21
|
jest.clearAllMocks();
|
|
18
22
|
config.auth = false;
|
|
23
|
+
config.anonRole = null;
|
|
19
24
|
req = {
|
|
20
25
|
params: {},
|
|
21
26
|
method: 'GET',
|
|
22
27
|
originalUrl: '/api/tables/items/rows',
|
|
23
|
-
cookies: {},
|
|
28
|
+
cookies: { accessToken: 'token' },
|
|
24
29
|
};
|
|
25
30
|
res = { status: jest.fn().mockReturnThis(), send: jest.fn() };
|
|
26
31
|
next = jest.fn();
|
|
@@ -90,6 +95,72 @@ describe('hasAccess', () => {
|
|
|
90
95
|
expect(next).not.toHaveBeenCalled();
|
|
91
96
|
});
|
|
92
97
|
|
|
98
|
+
it('returns 401 when there is no token and no anon role is configured', async () => {
|
|
99
|
+
config.auth = true;
|
|
100
|
+
req.cookies = {};
|
|
101
|
+
|
|
102
|
+
await hasAccess(req, res, next);
|
|
103
|
+
|
|
104
|
+
expect(res.status).toHaveBeenCalledWith(401);
|
|
105
|
+
expect(decodeToken).not.toHaveBeenCalled();
|
|
106
|
+
expect(next).not.toHaveBeenCalled();
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('checks permissions with a null payload when there is no token but an anon role is configured', async () => {
|
|
110
|
+
config.auth = true;
|
|
111
|
+
req.cookies = {};
|
|
112
|
+
config.anonRole = 'public';
|
|
113
|
+
authService.hasTablePermission.mockReturnValue(true);
|
|
114
|
+
req.params.name = 'items';
|
|
115
|
+
|
|
116
|
+
await hasAccess(req, res, next);
|
|
117
|
+
|
|
118
|
+
expect(decodeToken).not.toHaveBeenCalled();
|
|
119
|
+
expect(authService.hasTablePermission).toHaveBeenCalledWith({
|
|
120
|
+
payload: null,
|
|
121
|
+
tableName: 'items',
|
|
122
|
+
verb: 'GET',
|
|
123
|
+
});
|
|
124
|
+
expect(next).toHaveBeenCalledTimes(1);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('returns 403 when the anon role does not allow the request', async () => {
|
|
128
|
+
config.auth = true;
|
|
129
|
+
req.cookies = {};
|
|
130
|
+
config.anonRole = 'public';
|
|
131
|
+
authService.hasTablePermission.mockReturnValue(false);
|
|
132
|
+
req.params.name = 'items';
|
|
133
|
+
|
|
134
|
+
await hasAccess(req, res, next);
|
|
135
|
+
|
|
136
|
+
expect(res.status).toHaveBeenCalledWith(403);
|
|
137
|
+
expect(next).not.toHaveBeenCalled();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('does not let anonymous requests use universal endpoints', async () => {
|
|
141
|
+
config.auth = true;
|
|
142
|
+
req.cookies = {};
|
|
143
|
+
config.anonRole = 'public';
|
|
144
|
+
authService.hasTablePermission.mockReturnValue(false);
|
|
145
|
+
req.originalUrl = '/api/auth/change-password';
|
|
146
|
+
|
|
147
|
+
await hasAccess(req, res, next);
|
|
148
|
+
|
|
149
|
+
expect(res.status).toHaveBeenCalledWith(403);
|
|
150
|
+
expect(next).not.toHaveBeenCalled();
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('still returns 401 for an invalid token when an anon role is configured', async () => {
|
|
154
|
+
config.auth = true;
|
|
155
|
+
config.anonRole = 'public';
|
|
156
|
+
decodeToken.mockRejectedValue(new Error('invalid token'));
|
|
157
|
+
|
|
158
|
+
await hasAccess(req, res, next);
|
|
159
|
+
|
|
160
|
+
expect(res.status).toHaveBeenCalledWith(401);
|
|
161
|
+
expect(authService.hasTablePermission).not.toHaveBeenCalled();
|
|
162
|
+
});
|
|
163
|
+
|
|
93
164
|
it('returns 401 and logs when an unexpected error is thrown', async () => {
|
|
94
165
|
config.auth = true;
|
|
95
166
|
decodeToken.mockResolvedValue({ userId: 1, isSuperuser: true });
|
|
@@ -103,3 +174,66 @@ describe('hasAccess', () => {
|
|
|
103
174
|
expect(res.send).toHaveBeenCalledWith({ message: 'boom' });
|
|
104
175
|
});
|
|
105
176
|
});
|
|
177
|
+
|
|
178
|
+
describe('authenticate', () => {
|
|
179
|
+
let req, res, next;
|
|
180
|
+
|
|
181
|
+
beforeEach(() => {
|
|
182
|
+
jest.clearAllMocks();
|
|
183
|
+
req = { cookies: { accessToken: 'token' } };
|
|
184
|
+
res = { status: jest.fn().mockReturnThis(), send: jest.fn() };
|
|
185
|
+
next = jest.fn();
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('skips authentication when auth is disabled', async () => {
|
|
189
|
+
config.auth = false;
|
|
190
|
+
|
|
191
|
+
await authenticate(req, res, next);
|
|
192
|
+
|
|
193
|
+
expect(next).toHaveBeenCalledTimes(1);
|
|
194
|
+
expect(decodeToken).not.toHaveBeenCalled();
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('sets req.user from a valid token', async () => {
|
|
198
|
+
config.auth = true;
|
|
199
|
+
decodeToken.mockResolvedValue({ username: 'john' });
|
|
200
|
+
|
|
201
|
+
await authenticate(req, res, next);
|
|
202
|
+
|
|
203
|
+
expect(req.user).toEqual({ username: 'john' });
|
|
204
|
+
expect(next).toHaveBeenCalledTimes(1);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it('returns 401 without a token when no anon role is configured', async () => {
|
|
208
|
+
config.auth = true;
|
|
209
|
+
config.anonRole = undefined;
|
|
210
|
+
req.cookies = {};
|
|
211
|
+
|
|
212
|
+
await authenticate(req, res, next);
|
|
213
|
+
|
|
214
|
+
expect(res.status).toHaveBeenCalledWith(401);
|
|
215
|
+
expect(next).not.toHaveBeenCalled();
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it('lets a request without a token through when an anon role is configured', async () => {
|
|
219
|
+
config.auth = true;
|
|
220
|
+
config.anonRole = 'anon';
|
|
221
|
+
req.cookies = {};
|
|
222
|
+
|
|
223
|
+
await authenticate(req, res, next);
|
|
224
|
+
|
|
225
|
+
expect(next).toHaveBeenCalledTimes(1);
|
|
226
|
+
expect(req.user).toBeUndefined();
|
|
227
|
+
config.anonRole = undefined;
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it('returns 401 for an invalid token', async () => {
|
|
231
|
+
config.auth = true;
|
|
232
|
+
decodeToken.mockRejectedValue(new Error('bad'));
|
|
233
|
+
|
|
234
|
+
await authenticate(req, res, next);
|
|
235
|
+
|
|
236
|
+
expect(res.status).toHaveBeenCalledWith(401);
|
|
237
|
+
expect(next).not.toHaveBeenCalled();
|
|
238
|
+
});
|
|
239
|
+
});
|
|
@@ -1,18 +1,14 @@
|
|
|
1
1
|
const { websocketSubscribers } = require('../websocket');
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const subscribers = websocketSubscribers?.get(tableName);
|
|
8
|
-
|
|
9
|
-
if (subscribers) {
|
|
10
|
-
subscribers.forEach(({ ws }) => {
|
|
11
|
-
ws.send(JSON.stringify(data));
|
|
12
|
-
});
|
|
13
|
-
}
|
|
3
|
+
const broadcastToTable = (tableName, data) => {
|
|
4
|
+
websocketSubscribers?.get(tableName)?.forEach(({ ws }) => {
|
|
5
|
+
ws.send(JSON.stringify(data));
|
|
6
|
+
});
|
|
14
7
|
};
|
|
15
8
|
|
|
9
|
+
const broadcast = (req) => broadcastToTable(req.params.name, req.broadcast);
|
|
10
|
+
|
|
16
11
|
module.exports = {
|
|
17
12
|
broadcast,
|
|
13
|
+
broadcastToTable,
|
|
18
14
|
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const express = require('express');
|
|
2
|
+
|
|
3
|
+
const controllers = require('../controllers/batch');
|
|
4
|
+
const { validator } = require('../middlewares/validation');
|
|
5
|
+
const { authenticate } = require('../middlewares/auth');
|
|
6
|
+
const schema = require('../schemas/batch');
|
|
7
|
+
|
|
8
|
+
const router = express.Router();
|
|
9
|
+
|
|
10
|
+
router.post('/batch', authenticate, validator(schema.batch), controllers.batch);
|
|
11
|
+
|
|
12
|
+
module.exports = router;
|