@saltcorn/data 1.6.0-rc.2 → 1.6.0-rc.4

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 (156) hide show
  1. package/dist/base-plugin/actions.d.ts +1 -1
  2. package/dist/base-plugin/actions.d.ts.map +1 -1
  3. package/dist/base-plugin/actions.js +9 -12
  4. package/dist/base-plugin/actions.js.map +1 -1
  5. package/dist/base-plugin/index.d.ts +83 -1
  6. package/dist/base-plugin/index.d.ts.map +1 -1
  7. package/dist/base-plugin/types.d.ts +82 -0
  8. package/dist/base-plugin/types.d.ts.map +1 -1
  9. package/dist/base-plugin/types.js +31 -2
  10. package/dist/base-plugin/types.js.map +1 -1
  11. package/dist/db/state.d.ts.map +1 -1
  12. package/dist/db/state.js +5 -1
  13. package/dist/db/state.js.map +1 -1
  14. package/dist/models/file.d.ts +30 -0
  15. package/dist/models/file.d.ts.map +1 -1
  16. package/dist/models/file.js +100 -16
  17. package/dist/models/file.js.map +1 -1
  18. package/dist/viewable_fields.js +1 -1
  19. package/dist/viewable_fields.js.map +1 -1
  20. package/package.json +8 -8
  21. package/dist/setup-test-schema.d.ts +0 -2
  22. package/dist/setup-test-schema.d.ts.map +0 -1
  23. package/dist/setup-test-schema.js +0 -34
  24. package/dist/setup-test-schema.js.map +0 -1
  25. package/dist/tests/actions.test.d.ts +0 -2
  26. package/dist/tests/actions.test.d.ts.map +0 -1
  27. package/dist/tests/actions.test.js +0 -1104
  28. package/dist/tests/actions.test.js.map +0 -1
  29. package/dist/tests/auth.test.d.ts +0 -2
  30. package/dist/tests/auth.test.d.ts.map +0 -1
  31. package/dist/tests/auth.test.js +0 -927
  32. package/dist/tests/auth.test.js.map +0 -1
  33. package/dist/tests/auxtest.test.d.ts +0 -2
  34. package/dist/tests/auxtest.test.d.ts.map +0 -1
  35. package/dist/tests/auxtest.test.js +0 -631
  36. package/dist/tests/auxtest.test.js.map +0 -1
  37. package/dist/tests/base.test.d.ts +0 -2
  38. package/dist/tests/base.test.d.ts.map +0 -1
  39. package/dist/tests/base.test.js +0 -27
  40. package/dist/tests/base.test.js.map +0 -1
  41. package/dist/tests/calc.test.d.ts +0 -2
  42. package/dist/tests/calc.test.d.ts.map +0 -1
  43. package/dist/tests/calc.test.js +0 -1465
  44. package/dist/tests/calc.test.js.map +0 -1
  45. package/dist/tests/composite_pk.test.d.ts +0 -2
  46. package/dist/tests/composite_pk.test.d.ts.map +0 -1
  47. package/dist/tests/composite_pk.test.js +0 -98
  48. package/dist/tests/composite_pk.test.js.map +0 -1
  49. package/dist/tests/config.test.d.ts +0 -2
  50. package/dist/tests/config.test.d.ts.map +0 -1
  51. package/dist/tests/config.test.js +0 -86
  52. package/dist/tests/config.test.js.map +0 -1
  53. package/dist/tests/db.test.d.ts +0 -2
  54. package/dist/tests/db.test.d.ts.map +0 -1
  55. package/dist/tests/db.test.js +0 -179
  56. package/dist/tests/db.test.js.map +0 -1
  57. package/dist/tests/discover.test.d.ts +0 -2
  58. package/dist/tests/discover.test.d.ts.map +0 -1
  59. package/dist/tests/discover.test.js +0 -330
  60. package/dist/tests/discover.test.js.map +0 -1
  61. package/dist/tests/edit.test.d.ts +0 -2
  62. package/dist/tests/edit.test.d.ts.map +0 -1
  63. package/dist/tests/edit.test.js +0 -1186
  64. package/dist/tests/edit.test.js.map +0 -1
  65. package/dist/tests/email.test.d.ts +0 -2
  66. package/dist/tests/email.test.d.ts.map +0 -1
  67. package/dist/tests/email.test.js +0 -255
  68. package/dist/tests/email.test.js.map +0 -1
  69. package/dist/tests/exact_views.test.d.ts +0 -2
  70. package/dist/tests/exact_views.test.d.ts.map +0 -1
  71. package/dist/tests/exact_views.test.js +0 -1363
  72. package/dist/tests/exact_views.test.js.map +0 -1
  73. package/dist/tests/field.test.d.ts +0 -2
  74. package/dist/tests/field.test.d.ts.map +0 -1
  75. package/dist/tests/field.test.js +0 -589
  76. package/dist/tests/field.test.js.map +0 -1
  77. package/dist/tests/fieldviews.test.d.ts +0 -2
  78. package/dist/tests/fieldviews.test.d.ts.map +0 -1
  79. package/dist/tests/fieldviews.test.js +0 -74
  80. package/dist/tests/fieldviews.test.js.map +0 -1
  81. package/dist/tests/file.test.d.ts +0 -2
  82. package/dist/tests/file.test.d.ts.map +0 -1
  83. package/dist/tests/file.test.js +0 -165
  84. package/dist/tests/file.test.js.map +0 -1
  85. package/dist/tests/filter.test.d.ts +0 -2
  86. package/dist/tests/filter.test.d.ts.map +0 -1
  87. package/dist/tests/filter.test.js +0 -499
  88. package/dist/tests/filter.test.js.map +0 -1
  89. package/dist/tests/form.test.d.ts +0 -2
  90. package/dist/tests/form.test.d.ts.map +0 -1
  91. package/dist/tests/form.test.js +0 -343
  92. package/dist/tests/form.test.js.map +0 -1
  93. package/dist/tests/list.test.d.ts +0 -2
  94. package/dist/tests/list.test.d.ts.map +0 -1
  95. package/dist/tests/list.test.js +0 -1236
  96. package/dist/tests/list.test.js.map +0 -1
  97. package/dist/tests/mail_queue.test.d.ts +0 -2
  98. package/dist/tests/mail_queue.test.d.ts.map +0 -1
  99. package/dist/tests/mail_queue.test.js +0 -122
  100. package/dist/tests/mail_queue.test.js.map +0 -1
  101. package/dist/tests/models.test.d.ts +0 -2
  102. package/dist/tests/models.test.d.ts.map +0 -1
  103. package/dist/tests/models.test.js +0 -417
  104. package/dist/tests/models.test.js.map +0 -1
  105. package/dist/tests/page.test.d.ts +0 -2
  106. package/dist/tests/page.test.d.ts.map +0 -1
  107. package/dist/tests/page.test.js +0 -91
  108. package/dist/tests/page.test.js.map +0 -1
  109. package/dist/tests/page_group.test.d.ts +0 -2
  110. package/dist/tests/page_group.test.d.ts.map +0 -1
  111. package/dist/tests/page_group.test.js +0 -52
  112. package/dist/tests/page_group.test.js.map +0 -1
  113. package/dist/tests/plugin.test.d.ts +0 -2
  114. package/dist/tests/plugin.test.d.ts.map +0 -1
  115. package/dist/tests/plugin.test.js +0 -66
  116. package/dist/tests/plugin.test.js.map +0 -1
  117. package/dist/tests/show.test.d.ts +0 -2
  118. package/dist/tests/show.test.d.ts.map +0 -1
  119. package/dist/tests/show.test.js +0 -613
  120. package/dist/tests/show.test.js.map +0 -1
  121. package/dist/tests/state.test.d.ts +0 -2
  122. package/dist/tests/state.test.d.ts.map +0 -1
  123. package/dist/tests/state.test.js +0 -82
  124. package/dist/tests/state.test.js.map +0 -1
  125. package/dist/tests/table.test.d.ts +0 -2
  126. package/dist/tests/table.test.d.ts.map +0 -1
  127. package/dist/tests/table.test.js +0 -3211
  128. package/dist/tests/table.test.js.map +0 -1
  129. package/dist/tests/table_history.test.d.ts +0 -2
  130. package/dist/tests/table_history.test.d.ts.map +0 -1
  131. package/dist/tests/table_history.test.js +0 -415
  132. package/dist/tests/table_history.test.js.map +0 -1
  133. package/dist/tests/table_sync_info.test.d.ts +0 -2
  134. package/dist/tests/table_sync_info.test.d.ts.map +0 -1
  135. package/dist/tests/table_sync_info.test.js +0 -62
  136. package/dist/tests/table_sync_info.test.js.map +0 -1
  137. package/dist/tests/tag.test.d.ts +0 -2
  138. package/dist/tests/tag.test.d.ts.map +0 -1
  139. package/dist/tests/tag.test.js +0 -97
  140. package/dist/tests/tag.test.js.map +0 -1
  141. package/dist/tests/user.test.d.ts +0 -2
  142. package/dist/tests/user.test.d.ts.map +0 -1
  143. package/dist/tests/user.test.js +0 -467
  144. package/dist/tests/user.test.js.map +0 -1
  145. package/dist/tests/view.test.d.ts +0 -2
  146. package/dist/tests/view.test.d.ts.map +0 -1
  147. package/dist/tests/view.test.js +0 -703
  148. package/dist/tests/view.test.js.map +0 -1
  149. package/dist/tests/workflow.test.d.ts +0 -2
  150. package/dist/tests/workflow.test.d.ts.map +0 -1
  151. package/dist/tests/workflow.test.js +0 -303
  152. package/dist/tests/workflow.test.js.map +0 -1
  153. package/dist/tests/workflow_run.test.d.ts +0 -2
  154. package/dist/tests/workflow_run.test.d.ts.map +0 -1
  155. package/dist/tests/workflow_run.test.js +0 -922
  156. package/dist/tests/workflow_run.test.js.map +0 -1
@@ -1,1104 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const trigger_1 = __importDefault(require("../models/trigger"));
7
- const table_1 = __importDefault(require("../models/table"));
8
- const field_1 = __importDefault(require("../models/field"));
9
- const user_1 = __importDefault(require("../models/user"));
10
- const eventlog_1 = __importDefault(require("../models/eventlog"));
11
- const scheduler_1 = __importDefault(require("../models/scheduler"));
12
- const { runScheduler } = scheduler_1.default;
13
- const db_1 = __importDefault(require("../db"));
14
- const { getState } = require("../db/state");
15
- const mocks_1 = __importDefault(require("./mocks"));
16
- const { plugin_with_routes, getActionCounter, resetActionCounter, mockReqRes, sleep, } = mocks_1.default;
17
- const assertions_1 = require("../tests/assertions");
18
- const test_expect_1 = require("@saltcorn/db-common/test_expect");
19
- const actions_1 = __importDefault(require("../base-plugin/actions"));
20
- const { duplicate_row, insert_any_row, insert_joined_row, modify_row, delete_rows, emit_event, notify_user, run_js_code, } = actions_1.default;
21
- const utils_1 = __importDefault(require("../utils"));
22
- const notification_1 = __importDefault(require("../models/notification"));
23
- const plugin_helper_1 = require("../plugin-helper");
24
- const { applyAsync, mergeActionResults } = utils_1.default;
25
- (0, test_expect_1.afterAll)(db_1.default.close);
26
- (0, test_expect_1.beforeAll)(async () => {
27
- await require("../db/reset_schema")();
28
- await require("../db/fixtures")();
29
- });
30
- test_expect_1.jest.setTimeout(20000);
31
- (0, test_expect_1.describe)("Action and Trigger model", () => {
32
- (0, test_expect_1.it)("should add insert trigger", async () => {
33
- getState().registerPlugin("mock_plugin", plugin_with_routes());
34
- resetActionCounter();
35
- (0, test_expect_1.expect)(getActionCounter()).toBe(0);
36
- const table = table_1.default.findOne({ name: "patients" });
37
- (0, assertions_1.assertIsSet)(table);
38
- const trigger = await trigger_1.default.create({
39
- action: "incrementCounter",
40
- table_id: table.id,
41
- when_trigger: "Insert",
42
- name: "incCount",
43
- });
44
- (0, test_expect_1.expect)(getActionCounter()).toBe(0);
45
- await table.insertRow({ name: "Don Fabrizio" });
46
- await sleep(10);
47
- (0, test_expect_1.expect)(getActionCounter()).toBe(1);
48
- const trigger1 = await trigger_1.default.findOne({ id: trigger.id });
49
- (0, test_expect_1.expect)(!!trigger1).toBe(true);
50
- (0, test_expect_1.expect)(trigger1.id).toBe(trigger.id);
51
- (0, test_expect_1.expect)(trigger1.toJson).toStrictEqual({
52
- action: "incrementCounter",
53
- channel: null,
54
- configuration: {},
55
- description: null,
56
- min_role: 100,
57
- name: "incCount",
58
- table_name: "patients",
59
- when_trigger: "Insert",
60
- });
61
- });
62
- (0, test_expect_1.it)("should clone trigger", async () => {
63
- const trig = await trigger_1.default.findOne({ name: "incCount" });
64
- (0, assertions_1.assertIsSet)(trig);
65
- await trig.clone();
66
- await trig.clone();
67
- const trig1 = await trigger_1.default.findOne({ name: "incCount-copy" });
68
- (0, assertions_1.assertIsSet)(trig1);
69
- const trig2 = await trigger_1.default.findOne({ name: "incCount-copy-1" });
70
- (0, assertions_1.assertIsSet)(trig2);
71
- });
72
- (0, test_expect_1.it)("should add update trigger", async () => {
73
- (0, test_expect_1.expect)(getActionCounter()).toBe(1);
74
- const table = table_1.default.findOne({ name: "patients" });
75
- (0, assertions_1.assertIsSet)(table);
76
- await trigger_1.default.create({
77
- action: "setCounter",
78
- table_id: table.id,
79
- when_trigger: "Update",
80
- configuration: { number: 17 },
81
- });
82
- (0, test_expect_1.expect)(getActionCounter()).toBe(1);
83
- const don = await table.getRow({ name: "Don Fabrizio" });
84
- (0, assertions_1.assertIsSet)(don);
85
- await table.updateRow({ name: "Don Fabrizio II" }, don.id);
86
- (0, test_expect_1.expect)(getActionCounter()).toBe(17);
87
- });
88
- (0, test_expect_1.it)("should add update trigger", async () => {
89
- (0, test_expect_1.expect)(getActionCounter()).toBe(17);
90
- const table = table_1.default.findOne({ name: "patients" });
91
- (0, assertions_1.assertIsSet)(table);
92
- await trigger_1.default.create({
93
- action: "setCounter",
94
- table_id: table.id,
95
- when_trigger: "Delete",
96
- configuration: { number: 37 },
97
- });
98
- (0, test_expect_1.expect)(getActionCounter()).toBe(17);
99
- await table.deleteRows({ name: "Don Fabrizio" });
100
- (0, test_expect_1.expect)(getActionCounter()).toBe(17);
101
- await table.deleteRows({ name: "Don Fabrizio II" });
102
- (0, test_expect_1.expect)(getActionCounter()).toBe(37);
103
- });
104
- (0, test_expect_1.it)("should run js code", async () => {
105
- const table = table_1.default.findOne({ name: "books" });
106
- (0, assertions_1.assertIsSet)(table);
107
- await trigger_1.default.create({
108
- action: "run_js_code",
109
- table_id: table.id,
110
- when_trigger: "Insert",
111
- configuration: {
112
- code: `
113
- const table = Table.findOne({ name: "patients" });
114
- await table.insertRow({ name: "TriggeredInsert" });
115
- `,
116
- },
117
- });
118
- await table.insertRow({ author: "Giuseppe Tomasi", pages: 209 });
119
- const patients = table_1.default.findOne({ name: "patients" });
120
- (0, assertions_1.assertIsSet)(patients);
121
- await sleep(10);
122
- const rows = await patients.getRows({ name: "TriggeredInsert" });
123
- (0, test_expect_1.expect)(rows.length).toBe(1);
124
- });
125
- (0, test_expect_1.it)("should run webhook", async () => {
126
- const table = table_1.default.findOne({ name: "books" });
127
- (0, assertions_1.assertIsSet)(table);
128
- await trigger_1.default.create({
129
- action: "webhook",
130
- table_id: table.id,
131
- when_trigger: "Update",
132
- configuration: {
133
- // from https://requestbin.com/
134
- // to inspect https://pipedream.com/sources/dc_jku44wk
135
- url: "https://b6af540a71dce96ec130de5a0c47ada6.m.pipedream.net",
136
- },
137
- });
138
- const row = await table.getRow({ author: "Giuseppe Tomasi" });
139
- (0, assertions_1.assertIsSet)(row);
140
- await table.updateRow({ pages: 210 }, row.id);
141
- });
142
- (0, test_expect_1.it)("should list triggers", async () => {
143
- //const table = Table.findOne({ name: "books" });
144
- const triggers = await trigger_1.default.findAllWithTableName();
145
- const trigger = triggers.find((tr) => tr && tr.table_name === "books" && tr.when_trigger === "Update");
146
- (0, assertions_1.assertIsSet)(trigger);
147
- (0, test_expect_1.expect)(trigger.action).toBe("webhook");
148
- });
149
- (0, test_expect_1.it)("should have options", async () => {
150
- (0, test_expect_1.expect)(trigger_1.default.when_options).toContain("Insert");
151
- });
152
- (0, test_expect_1.it)("should get triggers", async () => {
153
- const table = table_1.default.findOne({ name: "books" });
154
- (0, assertions_1.assertIsSet)(table);
155
- const trigger = await trigger_1.default.findOne({
156
- table_id: table.id,
157
- when_trigger: "Update",
158
- });
159
- (0, test_expect_1.expect)(trigger.action).toBe("webhook");
160
- await trigger_1.default.update(trigger.id, { when_trigger: "Insert" });
161
- const ins_trigger = trigger_1.default.find({
162
- table_id: table.id,
163
- when_trigger: "Insert",
164
- });
165
- (0, test_expect_1.expect)(ins_trigger.length).toBe(2);
166
- await trigger.delete();
167
- const ins_trigger1 = trigger_1.default.find({
168
- table_id: table.id,
169
- when_trigger: "Insert",
170
- });
171
- (0, test_expect_1.expect)(ins_trigger1.length).toBe(1);
172
- });
173
- (0, test_expect_1.it)("should run webhook on insert", async () => {
174
- const table = table_1.default.findOne({ name: "books" });
175
- (0, assertions_1.assertIsSet)(table);
176
- await trigger_1.default.create({
177
- action: "webhook",
178
- table_id: table.id,
179
- when_trigger: "Insert",
180
- configuration: {
181
- // from https://requestbin.com/
182
- // to inspect https://pipedream.com/sources/dc_jku44wk
183
- url: "https://b6af540a71dce96ec130de5a0c47ada6.m.pipedream.net",
184
- body: "{foo: author}",
185
- response_field: "author",
186
- },
187
- });
188
- const id = await table.insertRow({ author: "NK Jemisin", pages: 901 }, undefined, {});
189
- const row = await table.getRow({ id });
190
- (0, test_expect_1.expect)(['{"success":true}', "Error in workflow"]).toContain(row?.author);
191
- });
192
- (0, test_expect_1.it)("should run triggerwith table.run_trigger", async () => {
193
- getState().registerPlugin("mock_plugin", plugin_with_routes());
194
- resetActionCounter();
195
- (0, test_expect_1.expect)(getActionCounter()).toBe(0);
196
- const table = table_1.default.findOne({ name: "patients" });
197
- (0, assertions_1.assertIsSet)(table);
198
- await table.run_trigger("incCount", { name: "Mary Boas" });
199
- (0, test_expect_1.expect)(getActionCounter()).toBe(1);
200
- });
201
- });
202
- (0, test_expect_1.describe)("base plugin actions", () => {
203
- (0, test_expect_1.it)("should insert_any_row", async () => {
204
- const action = insert_any_row;
205
- const result = await action.run({
206
- row: { x: 3, y: 7 },
207
- configuration: { table: "patients", row_expr: '{name:"Simon1"}' },
208
- user: { id: 1, role_id: 1 },
209
- });
210
- (0, test_expect_1.expect)(result).toStrictEqual({});
211
- const patients = table_1.default.findOne({ name: "patients" });
212
- (0, assertions_1.assertIsSet)(patients);
213
- const rows = await patients.getRows({ name: "Simon1" });
214
- (0, test_expect_1.expect)(rows.length).toBe(1);
215
- });
216
- (0, test_expect_1.it)("should insert_any_row and return id", async () => {
217
- const action = insert_any_row;
218
- const result = await action.run({
219
- row: { x: 3, y: 7 },
220
- configuration: {
221
- table: "patients",
222
- row_expr: '{name:"Simon9"}',
223
- id_variable: "myid",
224
- },
225
- user: { id: 1, role_id: 1 },
226
- });
227
- (0, assertions_1.assertIsRow)(result);
228
- (0, test_expect_1.expect)(typeof result.myid).toBe("number");
229
- const patients = table_1.default.findOne({ name: "patients" });
230
- (0, assertions_1.assertIsSet)(patients);
231
- const rows = await patients.getRows({ name: "Simon9" });
232
- (0, test_expect_1.expect)(rows.length).toBe(1);
233
- });
234
- (0, test_expect_1.it)("insert_any_row should upsert", async () => {
235
- const exrow = await table_1.default.findOne("patients")?.getRow({ name: "Simon9" });
236
- const id = exrow?.id;
237
- const action = insert_any_row;
238
- const result = await action.run({
239
- row: { x: 3, y: 7 },
240
- configuration: {
241
- table: "patients",
242
- row_expr: `{name:"Simon99", id:${id}}`,
243
- id_variable: "myid",
244
- },
245
- user: { id: 1, role_id: 1 },
246
- });
247
- (0, test_expect_1.expect)(result).toStrictEqual({ myid: id });
248
- const patients = table_1.default.findOne({ name: "patients" });
249
- (0, assertions_1.assertIsSet)(patients);
250
- const rows = await patients.getRows({ name: "Simon99" });
251
- (0, test_expect_1.expect)(rows.length).toBe(1);
252
- (0, test_expect_1.expect)(rows[0].id).toBe(id);
253
- const rows1 = await patients.getRows({ name: "Simon9" });
254
- (0, test_expect_1.expect)(rows1.length).toBe(0);
255
- });
256
- (0, test_expect_1.it)("should insert_any_row on arrays", async () => {
257
- const action = insert_any_row;
258
- const result = await action.run({
259
- row: { x: 3, y: 7 },
260
- configuration: {
261
- table: "patients",
262
- row_expr: '[{name:"Simon2"}, {name:"Simon2"}]',
263
- },
264
- user: { id: 1, role_id: 1 },
265
- });
266
- (0, test_expect_1.expect)(result).toStrictEqual({});
267
- const patients = table_1.default.findOne({ name: "patients" });
268
- (0, assertions_1.assertIsSet)(patients);
269
- const rows = await patients.getRows({ name: "Simon2" });
270
- (0, test_expect_1.expect)(rows.length).toBe(2);
271
- });
272
- (0, test_expect_1.it)("should insert_any_row on arrays and return ids", async () => {
273
- const action = insert_any_row;
274
- const result = await action.run({
275
- row: { x: 3, y: 7 },
276
- configuration: {
277
- table: "patients",
278
- row_expr: '[{name:"Simon10"}, {name:"Simon11"}]',
279
- id_variable: "myids",
280
- },
281
- user: { id: 1, role_id: 1 },
282
- });
283
- (0, assertions_1.assertIsRow)(result);
284
- (0, test_expect_1.expect)(result.myids.length).toBe(2);
285
- (0, test_expect_1.expect)(typeof result.myids[0]).toBe("number");
286
- (0, test_expect_1.expect)(result.myids[0]).toBeGreaterThan(2);
287
- const patients = table_1.default.findOne({ name: "patients" });
288
- (0, assertions_1.assertIsSet)(patients);
289
- const rows = await patients.getRows({ name: "Simon2" });
290
- (0, test_expect_1.expect)(rows.length).toBe(2);
291
- });
292
- (0, test_expect_1.it)("should insert_any_row with field", async () => {
293
- const patients = table_1.default.findOne({ name: "patients" });
294
- (0, assertions_1.assertIsSet)(patients);
295
- const books = table_1.default.findOne({ name: "books" });
296
- (0, assertions_1.assertIsSet)(books);
297
- const action = insert_any_row;
298
- const result = await action.run({
299
- row: { pages: 3, author: "Joe" },
300
- table: books,
301
- configuration: {
302
- table: "patients",
303
- row_expr: '{name:"Si"+row.pages+"mon"+author}',
304
- },
305
- user: { id: 1, role_id: 1 },
306
- });
307
- (0, test_expect_1.expect)(result).toStrictEqual({});
308
- await sleep(10);
309
- const rows = await patients.getRows({ name: "Si3monJoe" });
310
- (0, test_expect_1.expect)(rows.length).toBe(1);
311
- });
312
- (0, test_expect_1.it)("should modify_row", async () => {
313
- const patients = table_1.default.findOne({ name: "patients" });
314
- (0, assertions_1.assertIsSet)(patients);
315
- const row = await patients.getRow({ name: "Simon1" });
316
- (0, assertions_1.assertIsSet)(row);
317
- (0, test_expect_1.expect)(row.favbook).toBe(null);
318
- const result = await modify_row.run({
319
- row,
320
- table: patients,
321
- configuration: { row_expr: "{favbook:1}", where: "Database" },
322
- user: { id: 1, role_id: 1 },
323
- });
324
- (0, test_expect_1.expect)(result).toStrictEqual(undefined);
325
- const row1 = await patients.getRow({ name: "Simon1" });
326
- (0, assertions_1.assertIsSet)(row1);
327
- (0, test_expect_1.expect)(row1.favbook).toBe(1);
328
- });
329
- (0, test_expect_1.it)("should delete_rows", async () => {
330
- const patients = table_1.default.findOne({ name: "patients" });
331
- (0, assertions_1.assertIsSet)(patients);
332
- const id1 = await patients.insertRow({ name: "Del1" });
333
- await patients.insertRow({ name: "Del2" });
334
- const row = await patients.getRow({ id: id1 });
335
- (0, assertions_1.assertIsSet)(row);
336
- const result = await delete_rows.run({
337
- row,
338
- table: patients,
339
- configuration: { delete_triggering_row: true },
340
- user: { id: 1, role_id: 1 },
341
- });
342
- (0, test_expect_1.expect)(result).toStrictEqual({});
343
- const row1 = await patients.getRow({ name: "Del1" });
344
- (0, test_expect_1.expect)(row1).toBe(null);
345
- const row1a = await patients.getRow({ name: "Del2" });
346
- (0, test_expect_1.expect)(!!row1a).toBe(true);
347
- const result1 = await delete_rows.run({
348
- configuration: {
349
- delete_triggering_row: false,
350
- delete_where: "{name: 'Del2'}",
351
- table_name: "patients",
352
- },
353
- user: { id: 1, role_id: 1 },
354
- });
355
- (0, test_expect_1.expect)(result1).toStrictEqual({});
356
- const row2 = await patients.getRow({ name: "Del2" });
357
- (0, test_expect_1.expect)(row2).toBe(null);
358
- });
359
- (0, test_expect_1.it)("should duplicate_row", async () => {
360
- const patients = table_1.default.findOne({ name: "patients" });
361
- (0, assertions_1.assertIsSet)(patients);
362
- const rows = await patients.getRows({ name: "Simon1" });
363
- (0, test_expect_1.expect)(rows.length).toBe(1);
364
- const result = await duplicate_row.run({
365
- row: rows[0],
366
- table: patients,
367
- user: { id: 1, role_id: 1 },
368
- });
369
- const rows1 = await patients.getRows({ name: "Simon1" });
370
- (0, test_expect_1.expect)(rows1.length).toBe(2);
371
- });
372
- (0, test_expect_1.it)("should insert_joined_row", async () => {
373
- const books = table_1.default.findOne({ name: "books" });
374
- (0, assertions_1.assertIsSet)(books);
375
- const book = await books.getRow({ id: 1 });
376
- (0, assertions_1.assertIsSet)(book);
377
- const discusses_books = table_1.default.findOne({ name: "discusses_books" });
378
- (0, assertions_1.assertIsSet)(discusses_books);
379
- const npats_before = await discusses_books.countRows({});
380
- const result = await insert_joined_row.run({
381
- table: discusses_books,
382
- row: book,
383
- configuration: { joined_table: `discusses_books.book` },
384
- user: { id: 1, role_id: 1 },
385
- });
386
- const npats_after = await discusses_books.countRows({});
387
- (0, test_expect_1.expect)(npats_after).toBe(npats_before + 1);
388
- });
389
- (0, test_expect_1.it)("should notify_user", async () => {
390
- const books = table_1.default.findOne({ name: "books" });
391
- (0, assertions_1.assertIsSet)(books);
392
- const book = await books.getRow({ id: 1 });
393
- (0, assertions_1.assertIsSet)(book);
394
- await notify_user.run({
395
- row: book,
396
- configuration: {
397
- user_spec: "{id:1}",
398
- title: "Hello",
399
- body: "World",
400
- link: "https://saltcorn.com",
401
- },
402
- user: { id: 1, role_id: 1 },
403
- });
404
- const notif = await notification_1.default.findOne({ title: "Hello" });
405
- (0, assertions_1.assertIsSet)(notif);
406
- (0, test_expect_1.expect)(notif.user_id).toBe(1);
407
- (0, test_expect_1.expect)(notif.body).toBe("World");
408
- });
409
- (0, test_expect_1.it)("should have valid configFields", async () => {
410
- const books = table_1.default.findOne({ name: "books" });
411
- (0, assertions_1.assertIsSet)(books);
412
- for (const [name, action] of Object.entries(actions_1.default)) {
413
- // @ts-ignore
414
- if (!action.configFields)
415
- continue;
416
- // @ts-ignore
417
- const configFields = await applyAsync(action.configFields, {
418
- table: books,
419
- });
420
- (0, test_expect_1.expect)(Array.isArray(configFields)).toBe(true);
421
- }
422
- });
423
- //TODO recalculate_stored_fields, set_user_language
424
- });
425
- (0, test_expect_1.describe)("run_js_code", () => {
426
- (0, test_expect_1.it)("should return value", async () => {
427
- const rres = await run_js_code.run({
428
- configuration: {
429
- code: "return 5",
430
- run_where: "Server",
431
- },
432
- user: { id: 1, role_id: 1 },
433
- });
434
- (0, test_expect_1.expect)(rres).toBe(5);
435
- });
436
- (0, test_expect_1.it)("should assert in run_js_code", async () => {
437
- const rres = await run_js_code.run({
438
- configuration: {
439
- code: `assert(1);return 5`,
440
- run_where: "Server",
441
- },
442
- user: { id: 1, role_id: 1 },
443
- });
444
- (0, test_expect_1.expect)(rres).toBe(5);
445
- const rres1 = await run_js_code.run({
446
- configuration: {
447
- code: `assert.ok(1);return 5`,
448
- run_where: "Server",
449
- },
450
- user: { id: 1, role_id: 1 },
451
- });
452
- (0, test_expect_1.expect)(rres1).toBe(5);
453
- });
454
- (0, test_expect_1.it)("should fail assert in run_js_code", async () => {
455
- await (0, test_expect_1.expect)((async () => await run_js_code.run({
456
- configuration: {
457
- code: `assert(0);return 5`,
458
- run_where: "Server",
459
- },
460
- user: { id: 1, role_id: 1 },
461
- }))()).rejects.toThrow();
462
- });
463
- });
464
- (0, test_expect_1.describe)("Events and eventlog", () => {
465
- (0, test_expect_1.it)("should add custom event", async () => {
466
- await getState().setConfig("custom_events", [
467
- {
468
- name: "FooHappened",
469
- hasChannel: false,
470
- },
471
- {
472
- name: "BarWasHere",
473
- hasChannel: true,
474
- },
475
- ]);
476
- await getState().setConfig("event_log_settings", {
477
- FooHappened: true,
478
- BarWasHere: true,
479
- BarWasHere_channel: "Baz,oldbooks",
480
- Insert: true,
481
- Insert_readings: true,
482
- });
483
- await getState().refresh_config();
484
- });
485
- (0, test_expect_1.it)("should emit custom event", async () => {
486
- const evs = await eventlog_1.default.find({ event_type: "FooHappened" });
487
- (0, test_expect_1.expect)(evs.length).toBe(0);
488
- await trigger_1.default.emitEvent("FooHappened");
489
- await sleep(200);
490
- const evs1 = await eventlog_1.default.find({ event_type: "FooHappened" });
491
- (0, test_expect_1.expect)(evs1.length).toBe(1);
492
- });
493
- (0, test_expect_1.it)("should find with user", async () => {
494
- const ev = await eventlog_1.default.findOne({ event_type: "FooHappened" });
495
- (0, assertions_1.assertIsSet)(ev.id);
496
- const evlog_w_user = await eventlog_1.default.findOneWithUser(ev.id);
497
- (0, test_expect_1.expect)(evlog_w_user?.event_type).toBe("FooHappened");
498
- });
499
- (0, test_expect_1.it)("should emit custom event with channel", async () => {
500
- const evs = await eventlog_1.default.find({ event_type: "BarWasHere" });
501
- (0, test_expect_1.expect)(evs.length).toBe(0);
502
- await trigger_1.default.emitEvent("BarWasHere");
503
- await trigger_1.default.emitEvent("BarWasHere", "Zap");
504
- await trigger_1.default.emitEvent("BarWasHere", "Baz");
505
- await sleep(100);
506
- const evs1 = await eventlog_1.default.find({ event_type: "BarWasHere" });
507
- (0, test_expect_1.expect)(evs1.length).toBe(1);
508
- });
509
- (0, test_expect_1.it)("should emit custom event with array payload", async () => {
510
- const evs = await eventlog_1.default.find({ event_type: "BarWasHere" });
511
- trigger_1.default.emitEvent("BarWasHere", "Baz", {}, [{ x: 1 }, { x: 2 }]);
512
- await sleep(200);
513
- const evs1 = await eventlog_1.default.find({ event_type: "BarWasHere" }, { orderBy: "id" });
514
- (0, test_expect_1.expect)(evs1.length).toBe(evs.length + 1);
515
- (0, test_expect_1.expect)(Array.isArray(evs1[evs1.length - 1].payload)).toBe(true);
516
- });
517
- (0, test_expect_1.it)("should emit custom event with object payload", async () => {
518
- const evs = await eventlog_1.default.find({ event_type: "BarWasHere" });
519
- trigger_1.default.emitEvent("BarWasHere", "Baz", {}, { x: 1 });
520
- await sleep(200);
521
- const evs1 = await eventlog_1.default.find({ event_type: "BarWasHere" }, { orderBy: "id" });
522
- (0, test_expect_1.expect)(evs1.length).toBe(evs.length + 1);
523
- (0, test_expect_1.expect)(evs1[evs1.length - 1].payload.x).toBe(1);
524
- });
525
- (0, test_expect_1.it)("should emit custom event with string payload", async () => {
526
- const evs = await eventlog_1.default.find({ event_type: "BarWasHere" });
527
- trigger_1.default.emitEvent("BarWasHere", "Baz", {}, "Hello!");
528
- await sleep(200);
529
- const evs1 = await eventlog_1.default.find({ event_type: "BarWasHere" }, { orderBy: "id" });
530
- (0, test_expect_1.expect)(evs1.length).toBe(evs.length + 1);
531
- (0, test_expect_1.expect)(evs1[evs1.length - 1].payload).toBe("Hello!");
532
- });
533
- (0, test_expect_1.it)("should emit custom event with null payload", async () => {
534
- const evs = await eventlog_1.default.find({ event_type: "BarWasHere" });
535
- trigger_1.default.emitEvent("BarWasHere", "Baz", {}, null);
536
- await sleep(200);
537
- const evs1 = await eventlog_1.default.find({ event_type: "BarWasHere" }, { orderBy: "id" });
538
- (0, test_expect_1.expect)(evs1.length).toBe(evs.length + 1);
539
- (0, test_expect_1.expect)(evs1[evs1.length - 1].payload).toBe(null);
540
- });
541
- (0, test_expect_1.it)("should emit custom event with bool payload", async () => {
542
- const evs = await eventlog_1.default.find({ event_type: "BarWasHere" });
543
- trigger_1.default.emitEvent("BarWasHere", "Baz", {}, true);
544
- await sleep(200);
545
- const evs1 = await eventlog_1.default.find({ event_type: "BarWasHere" }, { orderBy: "id" });
546
- (0, test_expect_1.expect)(evs1.length).toBe(evs.length + 1);
547
- (0, test_expect_1.expect)(evs1[evs1.length - 1].payload).toBe(true);
548
- });
549
- (0, test_expect_1.it)("should emit table event", async () => {
550
- await trigger_1.default.emitEvent("Insert", "readings");
551
- const evs = await eventlog_1.default.find({ event_type: "Insert" });
552
- (0, test_expect_1.expect)(evs.length).toBe(0);
553
- await sleep(100);
554
- const evs1 = await eventlog_1.default.find({ event_type: "Insert" });
555
- (0, test_expect_1.expect)(evs1.length).toBe(1);
556
- });
557
- (0, test_expect_1.it)("should run emit_event action", async () => {
558
- const books = table_1.default.findOne({ name: "books" });
559
- (0, assertions_1.assertIsSet)(books);
560
- const book = await books.getRow({ id: 1 });
561
- (0, assertions_1.assertIsSet)(book);
562
- const r = await emit_event.run({
563
- row: book,
564
- configuration: {
565
- eventType: "BarWasHere",
566
- channel: "oldbooks",
567
- },
568
- user: { id: 1, role_id: 1 },
569
- });
570
- await sleep(100);
571
- const ev = await eventlog_1.default.findOne({
572
- event_type: "BarWasHere",
573
- channel: "oldbooks",
574
- });
575
- (0, assertions_1.assertIsSet)(ev);
576
- (0, test_expect_1.expect)(ev.payload.pages).toBe(967);
577
- });
578
- });
579
- (0, test_expect_1.describe)("Scheduler", () => {
580
- (0, test_expect_1.it)("should run and tick", async () => {
581
- getState().registerPlugin("mock_plugin", plugin_with_routes());
582
- resetActionCounter();
583
- (0, test_expect_1.expect)(getActionCounter()).toBe(0);
584
- await trigger_1.default.create({
585
- action: "incrementCounter",
586
- when_trigger: "Often",
587
- });
588
- let stopSched = false;
589
- runScheduler({
590
- stop_when: () => stopSched,
591
- tickSeconds: 1,
592
- watchReaper: undefined,
593
- port: undefined,
594
- disableScheduler: undefined,
595
- });
596
- await sleep(500);
597
- (0, test_expect_1.expect)(getActionCounter()).toBe(1);
598
- await sleep(1200);
599
- (0, test_expect_1.expect)(getActionCounter() > 1).toBe(true);
600
- stopSched = true;
601
- await sleep(1200);
602
- });
603
- });
604
- (0, test_expect_1.describe)("Validate action", () => {
605
- (0, test_expect_1.it)("it should setup", async () => {
606
- const persons = await table_1.default.create("ValidatedTable");
607
- await field_1.default.create({
608
- table: persons,
609
- name: "name",
610
- type: "String",
611
- });
612
- await field_1.default.create({
613
- table: persons,
614
- name: "age",
615
- type: "Integer",
616
- });
617
- await trigger_1.default.create({
618
- action: "run_js_code",
619
- table_id: persons.id,
620
- when_trigger: "Validate",
621
- configuration: {
622
- code: `
623
- if(age && age<16) return {error: "Must be 16+ to qualify"}
624
- if(!row.name) return {set_fields: {name: "PersonAged"+age}}
625
- `,
626
- },
627
- });
628
- });
629
- (0, test_expect_1.it)("it should insert valid rows", async () => {
630
- const table = table_1.default.findOne({ name: "ValidatedTable" });
631
- (0, assertions_1.assertIsSet)(table);
632
- await table.insertRow({ name: "Mike", age: 19 });
633
- const row = await table.getRow({ name: "Mike" });
634
- (0, assertions_1.assertIsSet)(row);
635
- (0, test_expect_1.expect)(row.age).toBe(19);
636
- });
637
- (0, test_expect_1.it)("it should not insert invalid rows", async () => {
638
- const table = table_1.default.findOne({ name: "ValidatedTable" });
639
- (0, assertions_1.assertIsSet)(table);
640
- await table.insertRow({ name: "Fred", age: 14 });
641
- const row = await table.getRow({ name: "Fred" });
642
- (0, test_expect_1.expect)(row).toBe(null);
643
- });
644
- (0, test_expect_1.it)("it should set fields", async () => {
645
- const table = table_1.default.findOne({ name: "ValidatedTable" });
646
- (0, assertions_1.assertIsSet)(table);
647
- await table.insertRow({ age: 25 });
648
- const row = await table.getRow({ age: 25 });
649
- (0, assertions_1.assertIsSet)(row);
650
- (0, test_expect_1.expect)(row.name).toBe("PersonAged25");
651
- });
652
- (0, test_expect_1.it)("it should not update to invalid row", async () => {
653
- const table = table_1.default.findOne({ name: "ValidatedTable" });
654
- (0, assertions_1.assertIsSet)(table);
655
- const row = await table.getRow({ name: "Mike" });
656
- (0, assertions_1.assertIsSet)(row);
657
- const upres = await table.updateRow({ name: "Mike", age: 12 }, row.id);
658
- (0, test_expect_1.expect)(upres).toBe("Must be 16+ to qualify");
659
- const row1 = await table.getRow({ id: row.id });
660
- (0, assertions_1.assertIsSet)(row1);
661
- (0, test_expect_1.expect)(row1.age).toBe(19);
662
- (0, test_expect_1.expect)(row1.name).toBe("Mike");
663
- });
664
- (0, test_expect_1.it)("it should update to valid row", async () => {
665
- const table = table_1.default.findOne({ name: "ValidatedTable" });
666
- (0, assertions_1.assertIsSet)(table);
667
- const row = await table.getRow({ name: "Mike" });
668
- (0, assertions_1.assertIsSet)(row);
669
- const upres = await table.updateRow({ name: "Mike", age: 29 }, row.id);
670
- (0, test_expect_1.expect)(upres).toBe(undefined);
671
- const row1 = await table.getRow({ id: row.id });
672
- (0, assertions_1.assertIsSet)(row1);
673
- (0, test_expect_1.expect)(row1.age).toBe(29);
674
- (0, test_expect_1.expect)(row1.name).toBe("Mike");
675
- });
676
- (0, test_expect_1.it)("it should not change missing fields on update", async () => {
677
- const table = table_1.default.findOne({ name: "ValidatedTable" });
678
- (0, assertions_1.assertIsSet)(table);
679
- const row = await table.getRow({ name: "Mike" });
680
- (0, assertions_1.assertIsSet)(row);
681
- const upres = await table.updateRow({ age: 31 }, row.id);
682
- (0, test_expect_1.expect)(upres).toBe(undefined);
683
- const row1 = await table.getRow({ id: row.id });
684
- (0, assertions_1.assertIsSet)(row1);
685
- (0, test_expect_1.expect)(row1.age).toBe(31);
686
- (0, test_expect_1.expect)(row1.name).toBe("Mike");
687
- });
688
- });
689
- (0, test_expect_1.describe)("Validate to create email", () => {
690
- (0, test_expect_1.it)("it should setup field", async () => {
691
- await field_1.default.create({
692
- table: user_1.default.table,
693
- name: "username",
694
- type: "String",
695
- });
696
- });
697
- /*it("it should not create user without email", async () => {
698
- async function create_user() {
699
- await User.create({
700
- username: "tomn18",
701
- password: "s3cr3t893",
702
- });
703
- }
704
- expect(create_user).rejects.toThrow();
705
- const u = await User.findOne({ username: "tomn18" });
706
- expect(u).toBe(null);
707
- }); */
708
- (0, test_expect_1.it)("it should setup", async () => {
709
- await trigger_1.default.create({
710
- action: "run_js_code",
711
- table_id: user_1.default.table.id,
712
- when_trigger: "Validate",
713
- configuration: {
714
- code: `if(!row.email) return {set_fields: {email: row.username+"@anonymous.com"}}; else return {}`,
715
- },
716
- });
717
- });
718
- (0, test_expect_1.it)("it should set new user email in Validate", async () => {
719
- await user_1.default.create({
720
- username: "tomn19",
721
- password: "s3cr3t893",
722
- });
723
- const u = await user_1.default.findOne({ username: "tomn19" });
724
- (0, assertions_1.assertIsSet)(u);
725
- (0, test_expect_1.expect)(u.username).toBe("tomn19");
726
- (0, test_expect_1.expect)(u.email).toBe("tomn19@anonymous.com");
727
- });
728
- });
729
- (0, test_expect_1.describe)("mergeActionResults", () => {
730
- (0, test_expect_1.it)("it should merge errors", async () => {
731
- const result = { error: "Foo" };
732
- mergeActionResults(result, { error: "Bar" });
733
- (0, test_expect_1.expect)(result.error).toStrictEqual(["Foo", "Bar"]);
734
- });
735
- (0, test_expect_1.it)("it should overwrite other keys", async () => {
736
- const result = { error0: "Foo" };
737
- mergeActionResults(result, { error0: "Bar" });
738
- (0, test_expect_1.expect)(result.error0).toStrictEqual("Bar");
739
- });
740
- (0, test_expect_1.it)("it should merge set_fields", async () => {
741
- const result = {};
742
- mergeActionResults(result, { set_fields: { y: 2 } });
743
- mergeActionResults(result, { set_fields: { z: 3 } });
744
- (0, test_expect_1.expect)(result).toStrictEqual({ set_fields: { y: 2, z: 3 } });
745
- });
746
- });
747
- (0, test_expect_1.describe)("multistep triggers", () => {
748
- (0, test_expect_1.it)("should run", async () => {
749
- const trigger = await trigger_1.default.findOne({ name: "MySteps" });
750
- const runres = await trigger.runWithoutRow({});
751
- (0, test_expect_1.expect)(runres.error).toBe("errrr");
752
- (0, test_expect_1.expect)(runres.notify).toBe("note");
753
- (0, test_expect_1.expect)(runres.notify_success).toBe("fooo");
754
- });
755
- });
756
- (0, test_expect_1.describe)("run_action_column", () => {
757
- (0, test_expect_1.it)("should run state action", async () => {
758
- const runres = await (0, plugin_helper_1.run_action_column)({
759
- req: mockReqRes.req,
760
- col: {
761
- type: "action",
762
- block: false,
763
- rndid: "2d6f57",
764
- nsteps: 1,
765
- confirm: false,
766
- minRole: 100,
767
- isFormula: {},
768
- action_icon: "",
769
- action_name: "toast",
770
- action_label: "",
771
- configuration: {
772
- text: "note2",
773
- run_where: "Server",
774
- notify_type: "Notify",
775
- },
776
- },
777
- });
778
- (0, test_expect_1.expect)(runres).toStrictEqual({ notify: "note2" });
779
- });
780
- (0, test_expect_1.it)("should run trigger action", async () => {
781
- const runres = await (0, plugin_helper_1.run_action_column)({
782
- req: mockReqRes.req,
783
- col: {
784
- type: "action",
785
- block: false,
786
- rndid: "2d6f57",
787
- nsteps: 1,
788
- confirm: false,
789
- minRole: 100,
790
- isFormula: {},
791
- action_icon: "",
792
- action_name: "Toast1",
793
- action_label: "",
794
- configuration: {},
795
- },
796
- });
797
- (0, test_expect_1.expect)(runres).toStrictEqual({ notify_success: "fooo" });
798
- });
799
- (0, test_expect_1.it)("should run multistep builder", async () => {
800
- const runres = await (0, plugin_helper_1.run_action_column)({
801
- req: mockReqRes.req,
802
- col: {
803
- type: "action",
804
- block: false,
805
- rndid: "5f990e",
806
- nsteps: "2",
807
- confirm: false,
808
- minRole: 100,
809
- isFormula: {},
810
- action_icon: "",
811
- action_name: "Multi-step action",
812
- action_label: "",
813
- configuration: {
814
- steps: [
815
- {
816
- code: "1;",
817
- run_where: "Server",
818
- },
819
- {
820
- text: "note3",
821
- notify_type: "Notify",
822
- },
823
- {
824
- text: "succ3",
825
- notify_type: "Success",
826
- },
827
- ],
828
- },
829
- step_action_names: ["run_js_code", "toast", "toast"],
830
- },
831
- });
832
- (0, test_expect_1.expect)(runres).toStrictEqual({ notify: "note3", notify_success: "succ3" });
833
- });
834
- (0, test_expect_1.it)("should run multistep builder with trigger step", async () => {
835
- const runres = await (0, plugin_helper_1.run_action_column)({
836
- req: mockReqRes.req,
837
- col: {
838
- type: "action",
839
- block: false,
840
- rndid: "45a31c",
841
- nsteps: "2",
842
- confirm: false,
843
- minRole: 100,
844
- isFormula: {},
845
- action_icon: "",
846
- action_name: "Multi-step action",
847
- action_label: "",
848
- configuration: {
849
- steps: [
850
- {
851
- text: "note3",
852
- notify_type: "Notify",
853
- },
854
- {
855
- text: "succ3",
856
- notify_type: "Success",
857
- },
858
- ],
859
- },
860
- step_action_names: ["toast", "Toast1"],
861
- },
862
- });
863
- (0, test_expect_1.expect)(runres).toStrictEqual({ notify: "note3", notify_success: "fooo" });
864
- });
865
- (0, test_expect_1.it)("should run multitrigger step", async () => {
866
- const runres = await (0, plugin_helper_1.run_action_column)({
867
- req: mockReqRes.req,
868
- col: {
869
- type: "action",
870
- block: false,
871
- rndid: "cd9965",
872
- nsteps: 1,
873
- confirm: false,
874
- minRole: 100,
875
- isFormula: {},
876
- action_icon: "",
877
- action_name: "MySteps",
878
- action_label: "",
879
- configuration: {},
880
- },
881
- });
882
- (0, test_expect_1.expect)(runres.error).toBe("errrr");
883
- (0, test_expect_1.expect)(runres.notify).toBe("note");
884
- (0, test_expect_1.expect)(runres.notify_success).toBe("fooo");
885
- });
886
- });
887
- (0, test_expect_1.describe)("_only_if with old_row on Update trigger", () => {
888
- (0, test_expect_1.it)("should set up trigger with _only_if using old_row", async () => {
889
- getState().registerPlugin("mock_plugin", plugin_with_routes());
890
- resetActionCounter();
891
- const table = table_1.default.findOne({ name: "patients" });
892
- (0, assertions_1.assertIsSet)(table);
893
- await trigger_1.default.create({
894
- action: "setCounter",
895
- table_id: table.id,
896
- when_trigger: "Update",
897
- name: "onlyIfOldRow",
898
- configuration: {
899
- number: 99,
900
- _only_if: "row.old_row.name !== row.name",
901
- },
902
- });
903
- });
904
- (0, test_expect_1.it)("should fire trigger when name changes (row.old_row.name !== row.name)", async () => {
905
- resetActionCounter();
906
- const table = table_1.default.findOne({ name: "patients" });
907
- (0, assertions_1.assertIsSet)(table);
908
- const row = await table.getRow({ name: "Kirk Douglas" });
909
- (0, assertions_1.assertIsSet)(row);
910
- await table.updateRow({ name: "Kirk Douglas Updated" }, row.id);
911
- (0, test_expect_1.expect)(getActionCounter()).toBe(99);
912
- });
913
- (0, test_expect_1.it)("should not fire trigger when name is unchanged", async () => {
914
- resetActionCounter();
915
- const table = table_1.default.findOne({ name: "patients" });
916
- (0, assertions_1.assertIsSet)(table);
917
- const row = await table.getRow({ name: "Kirk Douglas Updated" });
918
- (0, assertions_1.assertIsSet)(row);
919
- await table.updateRow({ name: "Kirk Douglas Updated" }, row.id);
920
- (0, test_expect_1.expect)(getActionCounter()).toBe(17);
921
- });
922
- (0, test_expect_1.it)("should clean up", async () => {
923
- const trigger = await trigger_1.default.findOne({ name: "onlyIfOldRow" });
924
- (0, assertions_1.assertIsSet)(trigger);
925
- await trigger.delete();
926
- const table = table_1.default.findOne({ name: "patients" });
927
- (0, assertions_1.assertIsSet)(table);
928
- const row = await table.getRow({ name: "Kirk Douglas Updated" });
929
- (0, assertions_1.assertIsSet)(row);
930
- await table.updateRow({ name: "Kirk Douglas" }, row.id);
931
- });
932
- });
933
- (0, test_expect_1.describe)("trigger as action", () => {
934
- (0, test_expect_1.it)("should run with standard action as referenced trigger via runWithoutRow", async () => {
935
- getState().registerPlugin("mock_plugin", plugin_with_routes());
936
- resetActionCounter();
937
- await trigger_1.default.create({
938
- name: "SharedCounter",
939
- action: "incrementCounter",
940
- when_trigger: "Never",
941
- });
942
- await trigger_1.default.create({
943
- name: "ProxyToShared",
944
- action: "SharedCounter",
945
- when_trigger: "Never",
946
- });
947
- (0, test_expect_1.expect)(getActionCounter()).toBe(0);
948
- const proxyTrig = trigger_1.default.findOne({ name: "ProxyToShared" });
949
- (0, assertions_1.assertIsSet)(proxyTrig);
950
- await proxyTrig.runWithoutRow({});
951
- (0, test_expect_1.expect)(getActionCounter()).toBe(1);
952
- await proxyTrig.runWithoutRow({});
953
- (0, test_expect_1.expect)(getActionCounter()).toBe(2);
954
- await proxyTrig.delete();
955
- const shared = trigger_1.default.findOne({ name: "SharedCounter" });
956
- (0, assertions_1.assertIsSet)(shared);
957
- await shared.delete();
958
- });
959
- (0, test_expect_1.it)("should fire via table insert using shared Never trigger", async () => {
960
- getState().registerPlugin("mock_plugin", plugin_with_routes());
961
- resetActionCounter();
962
- const freshTable = await table_1.default.create("ProxyTriggerTable");
963
- await field_1.default.create({ table: freshTable, name: "val", type: "Integer" });
964
- await trigger_1.default.create({
965
- name: "SharedInsUpd",
966
- action: "incrementCounter",
967
- when_trigger: "Never",
968
- });
969
- await trigger_1.default.create({
970
- name: "ProxyInsert",
971
- action: "SharedInsUpd",
972
- table_id: freshTable.id,
973
- when_trigger: "Insert",
974
- });
975
- await trigger_1.default.create({
976
- name: "ProxyUpdate",
977
- action: "SharedInsUpd",
978
- table_id: freshTable.id,
979
- when_trigger: "Update",
980
- });
981
- (0, test_expect_1.expect)(getActionCounter()).toBe(0);
982
- const rowId = await freshTable.insertRow({ val: 1 });
983
- await new Promise((r) => setTimeout(r, 50));
984
- (0, test_expect_1.expect)(getActionCounter()).toBe(1);
985
- await freshTable.updateRow({ val: 2 }, rowId);
986
- await new Promise((r) => setTimeout(r, 50));
987
- (0, test_expect_1.expect)(getActionCounter()).toBe(2);
988
- const insT = trigger_1.default.findOne({ name: "ProxyInsert" });
989
- (0, assertions_1.assertIsSet)(insT);
990
- await insT.delete();
991
- const updT = trigger_1.default.findOne({ name: "ProxyUpdate" });
992
- (0, assertions_1.assertIsSet)(updT);
993
- await updT.delete();
994
- const shared = trigger_1.default.findOne({ name: "SharedInsUpd" });
995
- (0, assertions_1.assertIsSet)(shared);
996
- await shared.delete();
997
- await freshTable.delete();
998
- });
999
- (0, test_expect_1.it)("should run with multi-step action as referenced trigger", async () => {
1000
- const myStepsTrig = trigger_1.default.findOne({ name: "MySteps" });
1001
- (0, assertions_1.assertIsSet)(myStepsTrig);
1002
- const table = table_1.default.findOne({ name: "patients" });
1003
- (0, assertions_1.assertIsSet)(table);
1004
- await trigger_1.default.create({
1005
- name: "MultiStepProxy",
1006
- action: "MySteps",
1007
- table_id: table.id,
1008
- when_trigger: "Insert",
1009
- });
1010
- const proxyTrig = trigger_1.default.findOne({ name: "MultiStepProxy" });
1011
- (0, assertions_1.assertIsSet)(proxyTrig);
1012
- const result = await proxyTrig.runWithoutRow({ row: { name: "testrow" } });
1013
- (0, test_expect_1.expect)(result.error).toBe("errrr");
1014
- (0, test_expect_1.expect)(result.notify).toBe("note");
1015
- (0, test_expect_1.expect)(result.notify_success).toBe("fooo");
1016
- await proxyTrig.delete();
1017
- });
1018
- (0, test_expect_1.it)("should run with workflow as referenced trigger", async () => {
1019
- const wfTrig = await trigger_1.default.create({
1020
- name: "SharedWorkflow",
1021
- action: "Workflow",
1022
- when_trigger: "Never",
1023
- configuration: {},
1024
- });
1025
- (0, assertions_1.assertIsSet)(wfTrig);
1026
- const proxyTrig = await trigger_1.default.create({
1027
- name: "WorkflowProxy",
1028
- action: "SharedWorkflow",
1029
- when_trigger: "Never",
1030
- });
1031
- (0, assertions_1.assertIsSet)(proxyTrig);
1032
- const freshProxy = trigger_1.default.findOne({ name: "WorkflowProxy" });
1033
- (0, assertions_1.assertIsSet)(freshProxy);
1034
- const result = await freshProxy.runWithoutRow({});
1035
- (0, test_expect_1.expect)(typeof result === "object" || result === undefined).toBe(true);
1036
- await proxyTrig.delete();
1037
- await wfTrig.delete();
1038
- });
1039
- });
1040
- (0, test_expect_1.describe)("plain_password_triggers", () => {
1041
- const secret = "fw78fgfw$Efgy";
1042
- (0, test_expect_1.it)("should set up trigger", async () => {
1043
- getState().registerPlugin("mock_plugin", plugin_with_routes());
1044
- resetActionCounter();
1045
- (0, test_expect_1.expect)(getActionCounter()).toBe(0);
1046
- await trigger_1.default.create({
1047
- action: "evalCounter",
1048
- table_id: user_1.default.table.id,
1049
- when_trigger: "Insert",
1050
- name: "incCountIfPlainPassIns",
1051
- configuration: {
1052
- number_expr: `row.plain_password==="${secret}" ? 1 : 0`,
1053
- },
1054
- });
1055
- await trigger_1.default.create({
1056
- action: "evalCounter",
1057
- table_id: user_1.default.table.id,
1058
- when_trigger: "Update",
1059
- name: "incCountIfPlainPassUpd",
1060
- configuration: {
1061
- number_expr: `row.plain_password==="${secret}" ? 1 : 0`,
1062
- },
1063
- });
1064
- });
1065
- (0, test_expect_1.it)("should not pass password on update without setting", async () => {
1066
- const u = await user_1.default.findOne({ email: "staff@foo.com" });
1067
- (0, assertions_1.assertIsSet)(u);
1068
- resetActionCounter();
1069
- (0, test_expect_1.expect)(getActionCounter()).toBe(0);
1070
- await u.changePasswordTo(secret);
1071
- (0, test_expect_1.expect)(getActionCounter()).toBe(0);
1072
- });
1073
- (0, test_expect_1.it)("should not pass password on create without setting", async () => {
1074
- resetActionCounter();
1075
- (0, test_expect_1.expect)(getActionCounter()).toBe(0);
1076
- await user_1.default.create({
1077
- email: "user1@foo.com",
1078
- password: secret,
1079
- role_id: 80,
1080
- });
1081
- (0, test_expect_1.expect)(getActionCounter()).toBe(0);
1082
- });
1083
- (0, test_expect_1.it)("should pass password on update with setting", async () => {
1084
- await getState().setConfig("plain_password_triggers", true);
1085
- const u = await user_1.default.findOne({ email: "staff@foo.com" });
1086
- (0, assertions_1.assertIsSet)(u);
1087
- resetActionCounter();
1088
- (0, test_expect_1.expect)(getActionCounter()).toBe(0);
1089
- await u.changePasswordTo(secret);
1090
- (0, test_expect_1.expect)(getActionCounter()).toBe(1);
1091
- });
1092
- (0, test_expect_1.it)("should pass password on create with setting", async () => {
1093
- await getState().setConfig("plain_password_triggers", true);
1094
- resetActionCounter();
1095
- (0, test_expect_1.expect)(getActionCounter()).toBe(0);
1096
- await user_1.default.create({
1097
- email: "user2@foo.com",
1098
- password: secret,
1099
- role_id: 80,
1100
- });
1101
- (0, test_expect_1.expect)(getActionCounter()).toBe(1);
1102
- });
1103
- });
1104
- //# sourceMappingURL=actions.test.js.map