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.
Files changed (43) hide show
  1. package/.env.sample +1 -0
  2. package/.github/workflows/ci.yml +2 -0
  3. package/.prettierrc +4 -0
  4. package/.release-please-manifest.json +1 -1
  5. package/CHANGELOG.md +25 -0
  6. package/README.md +3 -0
  7. package/ROADMAP.md +83 -25
  8. package/docs/api/batch-examples.md +55 -0
  9. package/docs/api/rows-examples.md +10 -2
  10. package/docs/api-examples.md +1 -0
  11. package/docs/auth.md +10 -0
  12. package/package.json +1 -1
  13. package/src/cli.js +6 -0
  14. package/src/config/index.js +7 -0
  15. package/src/config/index.test.js +10 -0
  16. package/src/constants/messages.js +1 -1
  17. package/src/controllers/batch.js +128 -0
  18. package/src/controllers/batch.test.js +180 -0
  19. package/src/controllers/rows.js +223 -213
  20. package/src/controllers/rows.query.test.js +369 -0
  21. package/src/index.js +2 -0
  22. package/src/middlewares/api.js +6 -2
  23. package/src/middlewares/api.test.js +27 -13
  24. package/src/middlewares/auth.js +52 -33
  25. package/src/middlewares/auth.test.js +137 -3
  26. package/src/middlewares/broadcast.js +7 -11
  27. package/src/routes/batch.js +12 -0
  28. package/src/routes/rows.js +1 -0
  29. package/src/schemas/batch.js +27 -0
  30. package/src/schemas/rows.js +4 -1
  31. package/src/services/authService.js +31 -4
  32. package/src/services/authService.test.js +76 -0
  33. package/src/services/rowService.js +12 -6
  34. package/src/services/rowService.test.js +37 -0
  35. package/src/swagger/index.js +14 -16
  36. package/src/swagger/swagger.json +68 -59
  37. package/src/utils/query.js +285 -0
  38. package/src/utils/query.test.js +147 -0
  39. package/src/utils/sql.js +24 -0
  40. package/src/utils/sql.test.js +14 -0
  41. package/src/websocket.js +10 -8
  42. package/src/websocket.test.js +52 -0
  43. package/src/schemas/index.js +0 -30
@@ -0,0 +1,180 @@
1
+ const supertest = require('supertest');
2
+
3
+ const app = require('../index');
4
+ const config = require('../config');
5
+ const db = require('../db/index');
6
+ const { generateToken } = require('../utils');
7
+ const { websocketSubscribers } = require('../websocket');
8
+
9
+ const requestWithSupertest = supertest(app);
10
+
11
+ const tokenFor = (payload) => generateToken(payload, config.tokenSecret, '1H');
12
+
13
+ const postBatch = async (operations, payload = { isSuperuser: true }) =>
14
+ requestWithSupertest
15
+ .post('/api/batch')
16
+ .set('Cookie', [`accessToken=${await tokenFor(payload)}`])
17
+ .send({ operations });
18
+
19
+ const countUsers = (firstName) =>
20
+ db
21
+ .prepare('SELECT COUNT(*) AS total FROM users WHERE firstName = ?')
22
+ .get(firstName).total;
23
+
24
+ const insertUser = (firstName) => ({
25
+ method: 'POST',
26
+ path: '/api/tables/users/rows',
27
+ body: { fields: { firstName } },
28
+ });
29
+
30
+ describe('POST /api/batch', () => {
31
+ it('applies every operation and returns results in order', async () => {
32
+ const res = await postBatch([
33
+ insertUser('BatchOk'),
34
+ {
35
+ method: 'PUT',
36
+ path: '/api/tables/users/rows/1',
37
+ body: { fields: { lastName: 'BatchUpdated' } },
38
+ },
39
+ insertUser('BatchOkTwo'),
40
+ ]);
41
+
42
+ expect(res.status).toEqual(200);
43
+ expect(res.body.results.map((r) => r.status)).toEqual([201, 200, 201]);
44
+ expect(countUsers('BatchOk')).toEqual(1);
45
+ expect(countUsers('BatchOkTwo')).toEqual(1);
46
+ expect(
47
+ db.prepare('SELECT lastName FROM users WHERE id = 1').get().lastName,
48
+ ).toEqual('BatchUpdated');
49
+ });
50
+
51
+ it('supports delete and _lookup_field', async () => {
52
+ db.prepare("INSERT INTO users (firstName) VALUES ('BatchDelete')").run();
53
+
54
+ const res = await postBatch([
55
+ {
56
+ method: 'DELETE',
57
+ path: '/api/tables/users/rows/BatchDelete?_lookup_field=firstName',
58
+ },
59
+ ]);
60
+
61
+ expect(res.status).toEqual(200);
62
+ expect(countUsers('BatchDelete')).toEqual(0);
63
+ });
64
+
65
+ it('rolls back everything when an operation fails', async () => {
66
+ const res = await postBatch([
67
+ insertUser('BatchRollback'),
68
+ {
69
+ method: 'PUT',
70
+ path: '/api/tables/users/rows/1',
71
+ body: { fields: { nope: 'x' } },
72
+ },
73
+ ]);
74
+
75
+ expect(res.status).toEqual(400);
76
+ expect(res.body.index).toEqual(1);
77
+ expect(countUsers('BatchRollback')).toEqual(0);
78
+ });
79
+
80
+ it('rolls back when a delete matches no rows', async () => {
81
+ const res = await postBatch([
82
+ insertUser('BatchRollbackDelete'),
83
+ { method: 'DELETE', path: '/api/tables/users/rows/999999' },
84
+ ]);
85
+
86
+ expect(res.status).toEqual(404);
87
+ expect(res.body).toMatchObject({ index: 1, error: 'not_found' });
88
+ expect(countUsers('BatchRollbackDelete')).toEqual(0);
89
+ });
90
+
91
+ it.each([
92
+ ['an unsupported path', { method: 'POST', path: '/api/other' }],
93
+ [
94
+ 'a reserved table',
95
+ {
96
+ method: 'POST',
97
+ path: '/api/tables/_users/rows',
98
+ body: { fields: { username: 'x' } },
99
+ },
100
+ ],
101
+ ['a missing pk', { method: 'PUT', path: '/api/tables/users/rows' }],
102
+ [
103
+ 'an unknown table',
104
+ insertUser('x') && {
105
+ method: 'POST',
106
+ path: '/api/tables/missing_table/rows',
107
+ body: { fields: { a: 1 } },
108
+ },
109
+ ],
110
+ ])('rejects %s', async (_, operation) => {
111
+ const res = await postBatch([insertUser('BatchRejected'), operation]);
112
+
113
+ expect(res.status).toBeGreaterThanOrEqual(400);
114
+ expect(res.body.index).toEqual(1);
115
+ expect(countUsers('BatchRejected')).toEqual(0);
116
+ });
117
+
118
+ it('rejects an invalid body', async () => {
119
+ const empty = await postBatch([]);
120
+ const badMethod = await postBatch([{ method: 'GET', path: '/x' }]);
121
+
122
+ expect(empty.status).toEqual(400);
123
+ expect(badMethod.status).toEqual(400);
124
+ });
125
+
126
+ it('rejects more than 100 operations', async () => {
127
+ const res = await postBatch(
128
+ Array.from({ length: 101 }, () => insertUser('BatchTooMany')),
129
+ );
130
+
131
+ expect(res.status).toEqual(400);
132
+ expect(countUsers('BatchTooMany')).toEqual(0);
133
+ });
134
+
135
+ it('returns 401 without a token', async () => {
136
+ const res = await requestWithSupertest
137
+ .post('/api/batch')
138
+ .send({ operations: [insertUser('BatchNoToken')] });
139
+
140
+ expect(res.status).toEqual(401);
141
+ });
142
+
143
+ it('returns 403 and writes nothing when a table is not permitted', async () => {
144
+ const res = await postBatch([insertUser('BatchForbidden')], {
145
+ isSuperuser: false,
146
+ roleIds: [],
147
+ });
148
+
149
+ expect(res.status).toEqual(403);
150
+ expect(res.body.index).toEqual(0);
151
+ expect(countUsers('BatchForbidden')).toEqual(0);
152
+ });
153
+
154
+ describe('broadcasts', () => {
155
+ const send = jest.fn();
156
+
157
+ beforeEach(() => {
158
+ send.mockClear();
159
+ websocketSubscribers.set('users', new Set([{ ws: { send } }]));
160
+ });
161
+
162
+ afterEach(() => websocketSubscribers.delete('users'));
163
+
164
+ it('sends events after a successful commit', async () => {
165
+ await postBatch([insertUser('BatchEvent')]);
166
+
167
+ expect(send).toHaveBeenCalledTimes(1);
168
+ expect(JSON.parse(send.mock.calls[0][0]).type).toEqual('INSERT');
169
+ });
170
+
171
+ it('sends nothing when the batch is rolled back', async () => {
172
+ await postBatch([
173
+ insertUser('BatchNoEvent'),
174
+ { method: 'DELETE', path: '/api/tables/users/rows/999999' },
175
+ ]);
176
+
177
+ expect(send).not.toHaveBeenCalled();
178
+ });
179
+ });
180
+ });