@terreno/api 0.30.0 → 56.0.0-beta.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 (226) hide show
  1. package/dist/api.d.ts +10 -0
  2. package/dist/api.js +110 -361
  3. package/dist/auth.d.ts +26 -0
  4. package/dist/auth.js +100 -1
  5. package/dist/betterAuth.d.ts +5 -0
  6. package/dist/betterAuthSetup.js +23 -4
  7. package/dist/docLoader.js +12 -9
  8. package/dist/errors.d.ts +13 -0
  9. package/dist/errors.js +29 -1
  10. package/dist/index.d.ts +17 -1
  11. package/dist/index.js +29 -1
  12. package/dist/middleware.d.ts +7 -0
  13. package/dist/middleware.js +16 -1
  14. package/dist/openApiCompat.d.ts +10 -1
  15. package/dist/openApiCompat.js +115 -47
  16. package/dist/permissions.d.ts +27 -0
  17. package/dist/permissions.js +42 -1
  18. package/dist/plugins.d.ts +13 -0
  19. package/dist/plugins.js +35 -1
  20. package/dist/realtime/changeStreamWatcher.d.ts +100 -3
  21. package/dist/realtime/changeStreamWatcher.js +542 -137
  22. package/dist/realtime/realtimeApp.d.ts +2 -0
  23. package/dist/realtime/realtimeApp.js +73 -21
  24. package/dist/realtime/sessionRevalidation.d.ts +90 -0
  25. package/dist/realtime/sessionRevalidation.js +438 -0
  26. package/dist/realtime/socketAuth.d.ts +97 -0
  27. package/dist/realtime/socketAuth.js +272 -0
  28. package/dist/realtime/socketUser.d.ts +54 -0
  29. package/dist/realtime/socketUser.js +75 -5
  30. package/dist/realtime/types.d.ts +40 -0
  31. package/dist/secretProviders.d.ts +23 -0
  32. package/dist/secretProviders.js +9 -3
  33. package/dist/sync/executors.d.ts +198 -0
  34. package/dist/sync/executors.js +670 -0
  35. package/dist/sync/loadHarness.js +807 -0
  36. package/dist/sync/models.d.ts +216 -0
  37. package/dist/sync/models.js +644 -0
  38. package/dist/sync/mutationHandler.d.ts +97 -0
  39. package/dist/sync/mutationHandler.js +886 -0
  40. package/dist/sync/registry.d.ts +73 -0
  41. package/dist/sync/registry.js +223 -0
  42. package/dist/sync/routes.d.ts +62 -0
  43. package/dist/sync/routes.js +936 -0
  44. package/dist/sync/scripts/compactTombstones.d.ts +47 -0
  45. package/dist/sync/scripts/compactTombstones.js +270 -0
  46. package/dist/sync/serialize.d.ts +20 -0
  47. package/dist/{tests/bunSetup.js → sync/serialize.js} +35 -68
  48. package/dist/sync/socketHandlers.d.ts +54 -0
  49. package/dist/sync/socketHandlers.js +470 -0
  50. package/dist/sync/streams.d.ts +85 -0
  51. package/dist/sync/streams.js +221 -0
  52. package/dist/sync/syncApp.d.ts +29 -0
  53. package/dist/sync/syncApp.js +44 -0
  54. package/dist/sync/syncBatch.d.ts +71 -0
  55. package/dist/sync/syncBatch.js +252 -0
  56. package/dist/sync/syncSeqPlugin.d.ts +8 -0
  57. package/dist/sync/syncSeqPlugin.js +903 -0
  58. package/dist/sync/types.d.ts +254 -0
  59. package/dist/sync/types.js +9 -0
  60. package/dist/terrenoApp.js +90 -27
  61. package/dist/vendor/wesleytodd-openapi/index.d.ts +1 -1
  62. package/dist/vendor/wesleytodd-openapi/lib/ui.d.ts +2 -1
  63. package/package.json +7 -6
  64. package/src/__tests__/openApiCompat.test.ts +20 -0
  65. package/src/actions.openApi.test.ts +7 -10
  66. package/src/api.test.ts +12 -0
  67. package/src/api.ts +87 -295
  68. package/src/auth.test.ts +128 -176
  69. package/src/auth.ts +109 -0
  70. package/src/betterAuth.ts +6 -0
  71. package/src/betterAuthSetup.test.ts +34 -0
  72. package/src/betterAuthSetup.ts +20 -1
  73. package/src/docLoader.ts +3 -1
  74. package/src/errors.ts +33 -0
  75. package/src/expressServer.test.ts +7 -1
  76. package/src/index.ts +42 -0
  77. package/src/middleware.test.ts +40 -1
  78. package/src/middleware.ts +15 -0
  79. package/src/openApiCompat.ts +99 -45
  80. package/src/permissions.test.ts +75 -1
  81. package/src/permissions.ts +49 -0
  82. package/src/plugins.test.ts +54 -0
  83. package/src/plugins.ts +43 -0
  84. package/src/realtime/changeStreamWatcher.ts +552 -114
  85. package/src/realtime/realtime.test.ts +157 -0
  86. package/src/realtime/realtimeApp.ts +75 -11
  87. package/src/realtime/sessionRevalidation.test.ts +423 -0
  88. package/src/realtime/sessionRevalidation.ts +273 -0
  89. package/src/realtime/socketAuth.test.ts +355 -0
  90. package/src/realtime/socketAuth.ts +228 -0
  91. package/src/realtime/socketUser.ts +69 -0
  92. package/src/realtime/types.ts +40 -0
  93. package/src/secretProviders.test.ts +7 -1
  94. package/src/secretProviders.ts +15 -3
  95. package/src/sync/ensureSyncIndexes.test.ts +144 -0
  96. package/src/sync/executors.test.ts +882 -0
  97. package/src/sync/executors.ts +703 -0
  98. package/src/sync/integration.test.ts +1160 -0
  99. package/src/sync/loadHarness.ts +618 -0
  100. package/src/sync/models.ts +640 -0
  101. package/src/sync/mutationHandler.test.ts +1252 -0
  102. package/src/sync/mutationHandler.ts +850 -0
  103. package/src/sync/registry.ts +232 -0
  104. package/src/sync/routes.ts +812 -0
  105. package/src/sync/scripts/compactTombstones.test.ts +249 -0
  106. package/src/sync/scripts/compactTombstones.ts +145 -0
  107. package/src/sync/serialize.ts +48 -0
  108. package/src/sync/sessionRevalidationIntegration.test.ts +281 -0
  109. package/src/sync/socketHandlers.ts +320 -0
  110. package/src/sync/streams.ts +188 -0
  111. package/src/sync/sync.test.ts +582 -0
  112. package/src/sync/syncApp.ts +44 -0
  113. package/src/sync/syncBatch.ts +219 -0
  114. package/src/sync/syncFrontier.test.ts +494 -0
  115. package/src/sync/syncPhaseC.test.ts +718 -0
  116. package/src/sync/syncRoutes.test.ts +865 -0
  117. package/src/sync/syncSeqPlugin.ts +738 -0
  118. package/src/sync/syncSocket.test.ts +1440 -0
  119. package/src/sync/types.ts +270 -0
  120. package/src/terrenoApp.ts +37 -17
  121. package/src/tests/bunSetup.ts +8 -0
  122. package/tsconfig.json +4 -4
  123. package/tsconfig.typedoc.json +7 -1
  124. package/dist/__tests__/openApiCompat.test.js +0 -317
  125. package/dist/__tests__/versionCheckPlugin.test.d.ts +0 -1
  126. package/dist/__tests__/versionCheckPlugin.test.js +0 -597
  127. package/dist/actions.openApi.test.d.ts +0 -1
  128. package/dist/actions.openApi.test.js +0 -255
  129. package/dist/actions.test.d.ts +0 -1
  130. package/dist/actions.test.js +0 -1089
  131. package/dist/api.arrayOperations.test.d.ts +0 -1
  132. package/dist/api.arrayOperations.test.js +0 -878
  133. package/dist/api.asyncHandler.test.d.ts +0 -1
  134. package/dist/api.asyncHandler.test.js +0 -236
  135. package/dist/api.coverage.test.d.ts +0 -1
  136. package/dist/api.coverage.test.js +0 -194
  137. package/dist/api.errorPaths.test.d.ts +0 -1
  138. package/dist/api.errorPaths.test.js +0 -801
  139. package/dist/api.errors.test.d.ts +0 -1
  140. package/dist/api.errors.test.js +0 -185
  141. package/dist/api.hooks.test.d.ts +0 -1
  142. package/dist/api.hooks.test.js +0 -950
  143. package/dist/api.query.test.d.ts +0 -1
  144. package/dist/api.query.test.js +0 -948
  145. package/dist/api.test.d.ts +0 -1
  146. package/dist/api.test.js +0 -2820
  147. package/dist/auth.compile.test.d.ts +0 -1
  148. package/dist/auth.compile.test.js +0 -201
  149. package/dist/auth.coverage.test.d.ts +0 -1
  150. package/dist/auth.coverage.test.js +0 -202
  151. package/dist/auth.test.d.ts +0 -1
  152. package/dist/auth.test.js +0 -2286
  153. package/dist/betterAuth.test.d.ts +0 -1
  154. package/dist/betterAuth.test.js +0 -181
  155. package/dist/betterAuthApp.test.d.ts +0 -1
  156. package/dist/betterAuthApp.test.js +0 -242
  157. package/dist/betterAuthSetup.test.d.ts +0 -1
  158. package/dist/betterAuthSetup.test.js +0 -762
  159. package/dist/config.test.d.ts +0 -1
  160. package/dist/config.test.js +0 -353
  161. package/dist/configuration.test.d.ts +0 -1
  162. package/dist/configuration.test.js +0 -980
  163. package/dist/configurationPlugin.test.d.ts +0 -1
  164. package/dist/configurationPlugin.test.js +0 -827
  165. package/dist/consentApp.test.d.ts +0 -1
  166. package/dist/consentApp.test.js +0 -1415
  167. package/dist/docLoader.test.d.ts +0 -1
  168. package/dist/docLoader.test.js +0 -137
  169. package/dist/envConfigurationPlugin.test.d.ts +0 -1
  170. package/dist/envConfigurationPlugin.test.js +0 -369
  171. package/dist/errors.test.d.ts +0 -1
  172. package/dist/errors.test.js +0 -607
  173. package/dist/expressServer.test.d.ts +0 -1
  174. package/dist/expressServer.test.js +0 -1249
  175. package/dist/githubAuth.test.d.ts +0 -1
  176. package/dist/githubAuth.test.js +0 -1142
  177. package/dist/httpClient.test.d.ts +0 -1
  178. package/dist/httpClient.test.js +0 -1471
  179. package/dist/logger.test.d.ts +0 -1
  180. package/dist/logger.test.js +0 -685
  181. package/dist/middleware.test.d.ts +0 -1
  182. package/dist/middleware.test.js +0 -241
  183. package/dist/notifiers/googleChatNotifier.test.d.ts +0 -1
  184. package/dist/notifiers/googleChatNotifier.test.js +0 -298
  185. package/dist/notifiers/slackNotifier.test.d.ts +0 -1
  186. package/dist/notifiers/slackNotifier.test.js +0 -297
  187. package/dist/notifiers/zoomNotifier.test.d.ts +0 -1
  188. package/dist/notifiers/zoomNotifier.test.js +0 -373
  189. package/dist/openApi.test.d.ts +0 -1
  190. package/dist/openApi.test.js +0 -620
  191. package/dist/openApiBuilder.test.d.ts +0 -1
  192. package/dist/openApiBuilder.test.js +0 -601
  193. package/dist/openApiEtag.test.d.ts +0 -1
  194. package/dist/openApiEtag.test.js +0 -86
  195. package/dist/openApiValidator.test.d.ts +0 -1
  196. package/dist/openApiValidator.test.js +0 -761
  197. package/dist/permissions.middleware.test.d.ts +0 -1
  198. package/dist/permissions.middleware.test.js +0 -308
  199. package/dist/permissions.test.d.ts +0 -1
  200. package/dist/permissions.test.js +0 -439
  201. package/dist/plugins.test.d.ts +0 -1
  202. package/dist/plugins.test.js +0 -890
  203. package/dist/populate.test.d.ts +0 -1
  204. package/dist/populate.test.js +0 -373
  205. package/dist/realtime/realtime.test.d.ts +0 -10
  206. package/dist/realtime/realtime.test.js +0 -3982
  207. package/dist/requestContext.test.d.ts +0 -1
  208. package/dist/requestContext.test.js +0 -412
  209. package/dist/scriptRunner.test.d.ts +0 -1
  210. package/dist/scriptRunner.test.js +0 -386
  211. package/dist/secretProviders.test.d.ts +0 -1
  212. package/dist/secretProviders.test.js +0 -726
  213. package/dist/syncConsents.test.d.ts +0 -1
  214. package/dist/syncConsents.test.js +0 -522
  215. package/dist/terrenoApp.test.d.ts +0 -1
  216. package/dist/terrenoApp.test.js +0 -489
  217. package/dist/tests/bunSetup.d.ts +0 -1
  218. package/dist/tests/fixtures/compileAuthEntry.d.ts +0 -1
  219. package/dist/tests/fixtures/compileAuthEntry.js +0 -37
  220. package/dist/tests/testEnv.d.ts +0 -0
  221. package/dist/tests/testEnv.js +0 -6
  222. package/dist/transformers.test.d.ts +0 -1
  223. package/dist/transformers.test.js +0 -423
  224. package/dist/utils.test.d.ts +0 -1
  225. package/dist/utils.test.js +0 -242
  226. /package/dist/{__tests__/openApiCompat.test.d.ts → sync/loadHarness.d.ts} +0 -0
@@ -1,878 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
- var __read = (this && this.__read) || function (o, n) {
39
- var m = typeof Symbol === "function" && o[Symbol.iterator];
40
- if (!m) return o;
41
- var i = m.call(o), r, ar = [], e;
42
- try {
43
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
44
- }
45
- catch (error) { e = { error: error }; }
46
- finally {
47
- try {
48
- if (r && !r.done && (m = i["return"])) m.call(i);
49
- }
50
- finally { if (e) throw e.error; }
51
- }
52
- return ar;
53
- };
54
- var __importDefault = (this && this.__importDefault) || function (mod) {
55
- return (mod && mod.__esModule) ? mod : { "default": mod };
56
- };
57
- Object.defineProperty(exports, "__esModule", { value: true });
58
- // noExplicitAny: test mock typing
59
- // biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
60
- var bun_test_1 = require("bun:test");
61
- var supertest_1 = __importDefault(require("supertest"));
62
- var api_1 = require("./api");
63
- var auth_1 = require("./auth");
64
- var permissions_1 = require("./permissions");
65
- var tests_1 = require("./tests");
66
- var transformers_1 = require("./transformers");
67
- (0, bun_test_1.describe)("model array operations", function () {
68
- var _server;
69
- var app;
70
- var admin;
71
- var spinach;
72
- var apple;
73
- var agent;
74
- (0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
75
- var _a, _b;
76
- return __generator(this, function (_c) {
77
- switch (_c.label) {
78
- case 0:
79
- process.env.REFRESH_TOKEN_SECRET = "testsecret1234";
80
- return [4 /*yield*/, (0, tests_1.setupDb)()];
81
- case 1:
82
- _a = __read.apply(void 0, [_c.sent(), 1]), admin = _a[0];
83
- return [4 /*yield*/, Promise.all([
84
- tests_1.FoodModel.create({
85
- calories: 1,
86
- created: new Date("2021-12-03T00:00:20.000Z"),
87
- hidden: false,
88
- name: "Spinach",
89
- ownerId: admin._id,
90
- source: {
91
- name: "Brand",
92
- },
93
- }),
94
- tests_1.FoodModel.create({
95
- calories: 100,
96
- categories: [
97
- {
98
- name: "Fruit",
99
- show: true,
100
- },
101
- {
102
- name: "Popular",
103
- show: false,
104
- },
105
- ],
106
- created: new Date("2021-12-03T00:00:30.000Z"),
107
- hidden: false,
108
- name: "Apple",
109
- ownerId: admin._id,
110
- tags: ["healthy", "cheap"],
111
- }),
112
- ])];
113
- case 2:
114
- _b = __read.apply(void 0, [_c.sent(), 2]), spinach = _b[0], apple = _b[1];
115
- app = (0, tests_1.getBaseServer)();
116
- (0, auth_1.setupAuth)(app, tests_1.UserModel);
117
- (0, auth_1.addAuthRoutes)(app, tests_1.UserModel);
118
- app.use("/food", (0, api_1.modelRouter)(tests_1.FoodModel, {
119
- allowAnonymous: true,
120
- permissions: {
121
- create: [permissions_1.Permissions.IsAdmin],
122
- delete: [permissions_1.Permissions.IsAdmin],
123
- list: [permissions_1.Permissions.IsAdmin],
124
- read: [permissions_1.Permissions.IsAdmin],
125
- update: [permissions_1.Permissions.IsAdmin],
126
- },
127
- queryFields: ["hidden", "calories", "created", "source.name"],
128
- sort: { created: "descending" },
129
- }));
130
- _server = (0, supertest_1.default)(app);
131
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
132
- case 3:
133
- agent = _c.sent();
134
- return [2 /*return*/];
135
- }
136
- });
137
- }); });
138
- (0, bun_test_1.it)("add array sub-schema item", function () { return __awaiter(void 0, void 0, void 0, function () {
139
- var res;
140
- return __generator(this, function (_a) {
141
- switch (_a.label) {
142
- case 0: return [4 /*yield*/, agent
143
- .post("/food/".concat(apple._id, "/categories"))
144
- .send({ name: "Good Seller", show: false })
145
- .expect(400)];
146
- case 1:
147
- res = _a.sent();
148
- (0, bun_test_1.expect)(res.body.title).toBe("Malformed array operation body");
149
- (0, bun_test_1.expect)(res.body.detail).toBe("Malformed body, array operations should have a single, top level key, got: name,show");
150
- return [4 /*yield*/, agent
151
- .post("/food/".concat(apple._id, "/categories"))
152
- .send({ categories: { name: "Good Seller", show: false } })
153
- .expect(200)];
154
- case 2:
155
- res = _a.sent();
156
- (0, bun_test_1.expect)(res.body.data.categories).toHaveLength(3);
157
- (0, bun_test_1.expect)(res.body.data.categories[2].name).toBe("Good Seller");
158
- return [4 /*yield*/, agent
159
- .post("/food/".concat(spinach._id, "/categories"))
160
- .send({ categories: { name: "Good Seller", show: false } })
161
- .expect(200)];
162
- case 3:
163
- res = _a.sent();
164
- (0, bun_test_1.expect)(res.body.data.categories).toHaveLength(1);
165
- return [2 /*return*/];
166
- }
167
- });
168
- }); });
169
- (0, bun_test_1.it)("update array sub-schema item", function () { return __awaiter(void 0, void 0, void 0, function () {
170
- var res;
171
- return __generator(this, function (_a) {
172
- switch (_a.label) {
173
- case 0: return [4 /*yield*/, agent
174
- .patch("/food/".concat(apple._id, "/categories/xyz"))
175
- .send({ categories: { name: "Good Seller", show: false } })
176
- .expect(404)];
177
- case 1:
178
- res = _a.sent();
179
- (0, bun_test_1.expect)(res.body.title).toBe("Array item not found");
180
- (0, bun_test_1.expect)(res.body.detail).toBe("Could not find categories/xyz");
181
- return [4 /*yield*/, agent
182
- .patch("/food/".concat(apple._id, "/categories/").concat(apple.categories[1]._id))
183
- .send({ categories: { name: "Good Seller", show: false } })
184
- .expect(200)];
185
- case 2:
186
- res = _a.sent();
187
- (0, bun_test_1.expect)(res.body.data.categories).toHaveLength(2);
188
- (0, bun_test_1.expect)(res.body.data.categories[1].name).toBe("Good Seller");
189
- return [2 /*return*/];
190
- }
191
- });
192
- }); });
193
- (0, bun_test_1.it)("delete array sub-schema item", function () { return __awaiter(void 0, void 0, void 0, function () {
194
- var res;
195
- return __generator(this, function (_a) {
196
- switch (_a.label) {
197
- case 0: return [4 /*yield*/, agent.delete("/food/".concat(apple._id, "/categories/xyz")).expect(404)];
198
- case 1:
199
- res = _a.sent();
200
- (0, bun_test_1.expect)(res.body.title).toBe("Array item not found");
201
- (0, bun_test_1.expect)(res.body.detail).toBe("Could not find categories/xyz");
202
- return [4 /*yield*/, agent
203
- .delete("/food/".concat(apple._id, "/categories/").concat(apple.categories[0]._id))
204
- .expect(200)];
205
- case 2:
206
- res = _a.sent();
207
- (0, bun_test_1.expect)(res.body.data.categories).toHaveLength(1);
208
- (0, bun_test_1.expect)(res.body.data.categories[0].name).toBe("Popular");
209
- return [2 /*return*/];
210
- }
211
- });
212
- }); });
213
- (0, bun_test_1.it)("add array item", function () { return __awaiter(void 0, void 0, void 0, function () {
214
- var res;
215
- return __generator(this, function (_a) {
216
- switch (_a.label) {
217
- case 0: return [4 /*yield*/, agent.post("/food/".concat(apple._id, "/tags")).send({ tags: "popular" }).expect(200)];
218
- case 1:
219
- res = _a.sent();
220
- (0, bun_test_1.expect)(res.body.data.tags).toHaveLength(3);
221
- (0, bun_test_1.expect)(res.body.data.tags).toEqual(["healthy", "cheap", "popular"]);
222
- return [4 /*yield*/, agent.post("/food/".concat(spinach._id, "/tags")).send({ tags: "popular" }).expect(200)];
223
- case 2:
224
- res = _a.sent();
225
- (0, bun_test_1.expect)(res.body.data.tags).toEqual(["popular"]);
226
- return [2 /*return*/];
227
- }
228
- });
229
- }); });
230
- (0, bun_test_1.it)("update array item", function () { return __awaiter(void 0, void 0, void 0, function () {
231
- var res;
232
- return __generator(this, function (_a) {
233
- switch (_a.label) {
234
- case 0: return [4 /*yield*/, agent
235
- .patch("/food/".concat(apple._id, "/tags/xyz"))
236
- .send({ tags: "unhealthy" })
237
- .expect(404)];
238
- case 1:
239
- res = _a.sent();
240
- (0, bun_test_1.expect)(res.body.title).toBe("Array item not found");
241
- (0, bun_test_1.expect)(res.body.detail).toBe("Could not find tags/xyz");
242
- return [4 /*yield*/, agent
243
- .patch("/food/".concat(apple._id, "/tags/healthy"))
244
- .send({ tags: "unhealthy" })
245
- .expect(200)];
246
- case 2:
247
- res = _a.sent();
248
- (0, bun_test_1.expect)(res.body.data.tags).toEqual(["unhealthy", "cheap"]);
249
- return [2 /*return*/];
250
- }
251
- });
252
- }); });
253
- (0, bun_test_1.it)("delete array item", function () { return __awaiter(void 0, void 0, void 0, function () {
254
- var res;
255
- return __generator(this, function (_a) {
256
- switch (_a.label) {
257
- case 0: return [4 /*yield*/, agent.delete("/food/".concat(apple._id, "/tags/xyz")).expect(404)];
258
- case 1:
259
- res = _a.sent();
260
- (0, bun_test_1.expect)(res.body.title).toBe("Array item not found");
261
- (0, bun_test_1.expect)(res.body.detail).toBe("Could not find tags/xyz");
262
- return [4 /*yield*/, agent.delete("/food/".concat(apple._id, "/tags/healthy")).expect(200)];
263
- case 2:
264
- res = _a.sent();
265
- (0, bun_test_1.expect)(res.body.data.tags).toEqual(["cheap"]);
266
- return [2 /*return*/];
267
- }
268
- });
269
- }); });
270
- (0, bun_test_1.it)("updates timestamps on array subdocuments", function () { return __awaiter(void 0, void 0, void 0, function () {
271
- var foodWithTimestamps, firstCategoryId, secondCategoryId, res, updatedCategory, unchangedCategory;
272
- var _a, _b, _c, _d, _e, _f;
273
- return __generator(this, function (_g) {
274
- switch (_g.label) {
275
- case 0: return [4 /*yield*/, tests_1.FoodModel.create({
276
- calories: 100,
277
- categories: [
278
- {
279
- name: "Category 1",
280
- show: true,
281
- updated: new Date("2024-01-01T00:00:00.000Z"),
282
- },
283
- {
284
- name: "Category 2",
285
- show: true,
286
- updated: new Date("2024-01-01T00:00:00.000Z"),
287
- },
288
- ],
289
- created: new Date(),
290
- name: "Food with Timestamps",
291
- ownerId: admin._id,
292
- })];
293
- case 1:
294
- foodWithTimestamps = _g.sent();
295
- firstCategoryId = (_c = (_b = (_a = foodWithTimestamps.categories) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b._id) === null || _c === void 0 ? void 0 : _c.toString();
296
- secondCategoryId = (_f = (_e = (_d = foodWithTimestamps.categories) === null || _d === void 0 ? void 0 : _d[1]) === null || _e === void 0 ? void 0 : _e._id) === null || _f === void 0 ? void 0 : _f.toString();
297
- if (!firstCategoryId || !secondCategoryId) {
298
- throw new Error("Failed to create food with categories");
299
- }
300
- // Wait a moment to ensure timestamp difference
301
- return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 100); })];
302
- case 2:
303
- // Wait a moment to ensure timestamp difference
304
- _g.sent();
305
- return [4 /*yield*/, agent
306
- .patch("/food/".concat(foodWithTimestamps._id, "/categories/").concat(firstCategoryId))
307
- .send({ categories: { name: "Updated Category" } })
308
- .expect(200)];
309
- case 3:
310
- res = _g.sent();
311
- updatedCategory = res.body.data.categories.find(function (c) { return c._id === firstCategoryId; });
312
- unchangedCategory = res.body.data.categories.find(function (c) { return c._id === secondCategoryId; });
313
- if (!updatedCategory || !unchangedCategory) {
314
- throw new Error("Failed to find categories in response");
315
- }
316
- (0, bun_test_1.expect)(updatedCategory.updated).not.toBe(updatedCategory.created);
317
- (0, bun_test_1.expect)(unchangedCategory.updated).toBe(unchangedCategory.created);
318
- (0, bun_test_1.expect)(updatedCategory.name).toBe("Updated Category");
319
- // Unchanged.
320
- (0, bun_test_1.expect)(updatedCategory.show).toBe(true);
321
- (0, bun_test_1.expect)(unchangedCategory.show).toBe(true);
322
- return [2 /*return*/];
323
- }
324
- });
325
- }); });
326
- (0, bun_test_1.it)("array operations call postUpdate with different copy of document", function () { return __awaiter(void 0, void 0, void 0, function () {
327
- var postUpdateDoc, postUpdatePrevDoc, postUpdateCalled, categoryId, updatedCategory, prevCategory, remainingCategories, prevCategories;
328
- return __generator(this, function (_a) {
329
- switch (_a.label) {
330
- case 0:
331
- postUpdateCalled = false;
332
- app = (0, tests_1.getBaseServer)();
333
- (0, auth_1.setupAuth)(app, tests_1.UserModel);
334
- (0, auth_1.addAuthRoutes)(app, tests_1.UserModel);
335
- app.use("/food", (0, api_1.modelRouter)(tests_1.FoodModel, {
336
- allowAnonymous: true,
337
- permissions: {
338
- create: [permissions_1.Permissions.IsAdmin],
339
- delete: [permissions_1.Permissions.IsAdmin],
340
- list: [permissions_1.Permissions.IsAdmin],
341
- read: [permissions_1.Permissions.IsAdmin],
342
- update: [permissions_1.Permissions.IsAdmin],
343
- },
344
- postUpdate: function (doc, _cleanedBody, _request, prevValue) { return __awaiter(void 0, void 0, void 0, function () {
345
- return __generator(this, function (_a) {
346
- postUpdateDoc = doc;
347
- postUpdatePrevDoc = prevValue;
348
- postUpdateCalled = true;
349
- return [2 /*return*/];
350
- });
351
- }); },
352
- }));
353
- _server = (0, supertest_1.default)(app);
354
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
355
- case 1:
356
- agent = _a.sent();
357
- // Test POST operation (add to array)
358
- return [4 /*yield*/, agent
359
- .post("/food/".concat(apple._id, "/categories"))
360
- .send({ categories: { name: "New Category", show: true } })
361
- .expect(200)];
362
- case 2:
363
- // Test POST operation (add to array)
364
- _a.sent();
365
- (0, bun_test_1.expect)(postUpdateCalled).toBe(true);
366
- (0, bun_test_1.expect)(postUpdateDoc).toBeDefined();
367
- (0, bun_test_1.expect)(postUpdatePrevDoc).toBeDefined();
368
- // Verify they are different object references
369
- (0, bun_test_1.expect)(postUpdateDoc).not.toBe(postUpdatePrevDoc);
370
- // Verify the content is different (new category added)
371
- (0, bun_test_1.expect)(postUpdateDoc.categories).toHaveLength(3);
372
- (0, bun_test_1.expect)(postUpdatePrevDoc.categories).toHaveLength(2);
373
- // Reset for next test
374
- postUpdateCalled = false;
375
- postUpdateDoc = undefined;
376
- postUpdatePrevDoc = undefined;
377
- categoryId = apple.categories[0]._id;
378
- if (!categoryId) {
379
- throw new Error("Category ID is undefined");
380
- }
381
- return [4 /*yield*/, agent
382
- .patch("/food/".concat(apple._id, "/categories/").concat(categoryId))
383
- .send({ categories: { name: "Updated Category", show: false } })
384
- .expect(200)];
385
- case 3:
386
- _a.sent();
387
- (0, bun_test_1.expect)(postUpdateCalled).toBe(true);
388
- (0, bun_test_1.expect)(postUpdateDoc).toBeDefined();
389
- (0, bun_test_1.expect)(postUpdatePrevDoc).toBeDefined();
390
- // Verify they are different object references
391
- (0, bun_test_1.expect)(postUpdateDoc).not.toBe(postUpdatePrevDoc);
392
- updatedCategory = postUpdateDoc.categories.find(function (c) { return c._id.toString() === categoryId.toString(); });
393
- prevCategory = postUpdatePrevDoc.categories.find(function (c) { return c._id.toString() === categoryId.toString(); });
394
- (0, bun_test_1.expect)(updatedCategory.name).toBe("Updated Category");
395
- (0, bun_test_1.expect)(prevCategory.name).toBe("Fruit");
396
- // Reset for next test
397
- postUpdateCalled = false;
398
- postUpdateDoc = undefined;
399
- postUpdatePrevDoc = undefined;
400
- // Test DELETE operation (remove from array)
401
- return [4 /*yield*/, agent.delete("/food/".concat(apple._id, "/categories/").concat(categoryId)).expect(200)];
402
- case 4:
403
- // Test DELETE operation (remove from array)
404
- _a.sent();
405
- (0, bun_test_1.expect)(postUpdateCalled).toBe(true);
406
- (0, bun_test_1.expect)(postUpdateDoc).toBeDefined();
407
- (0, bun_test_1.expect)(postUpdatePrevDoc).toBeDefined();
408
- // Verify they are different object references
409
- (0, bun_test_1.expect)(postUpdateDoc).not.toBe(postUpdatePrevDoc);
410
- remainingCategories = postUpdateDoc.categories.filter(function (c) { return c._id.toString() === categoryId.toString(); });
411
- prevCategories = postUpdatePrevDoc.categories.filter(function (c) { return c._id.toString() === categoryId.toString(); });
412
- (0, bun_test_1.expect)(remainingCategories).toHaveLength(0);
413
- (0, bun_test_1.expect)(prevCategories).toHaveLength(1);
414
- return [2 /*return*/];
415
- }
416
- });
417
- }); });
418
- (0, bun_test_1.it)("array operations with string arrays call postUpdate with different copy", function () { return __awaiter(void 0, void 0, void 0, function () {
419
- var postUpdateDoc, postUpdatePrevDoc, postUpdateCalled;
420
- return __generator(this, function (_a) {
421
- switch (_a.label) {
422
- case 0:
423
- postUpdateCalled = false;
424
- app = (0, tests_1.getBaseServer)();
425
- (0, auth_1.setupAuth)(app, tests_1.UserModel);
426
- (0, auth_1.addAuthRoutes)(app, tests_1.UserModel);
427
- app.use("/food", (0, api_1.modelRouter)(tests_1.FoodModel, {
428
- allowAnonymous: true,
429
- permissions: {
430
- create: [permissions_1.Permissions.IsAdmin],
431
- delete: [permissions_1.Permissions.IsAdmin],
432
- list: [permissions_1.Permissions.IsAdmin],
433
- read: [permissions_1.Permissions.IsAdmin],
434
- update: [permissions_1.Permissions.IsAdmin],
435
- },
436
- postUpdate: function (doc, _cleanedBody, _request, prevValue) { return __awaiter(void 0, void 0, void 0, function () {
437
- return __generator(this, function (_a) {
438
- postUpdateDoc = doc;
439
- postUpdatePrevDoc = prevValue;
440
- postUpdateCalled = true;
441
- return [2 /*return*/];
442
- });
443
- }); },
444
- }));
445
- _server = (0, supertest_1.default)(app);
446
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
447
- case 1:
448
- agent = _a.sent();
449
- // Test POST operation with string array (add tag)
450
- return [4 /*yield*/, agent.post("/food/".concat(apple._id, "/tags")).send({ tags: "organic" }).expect(200)];
451
- case 2:
452
- // Test POST operation with string array (add tag)
453
- _a.sent();
454
- (0, bun_test_1.expect)(postUpdateCalled).toBe(true);
455
- (0, bun_test_1.expect)(postUpdateDoc).toBeDefined();
456
- (0, bun_test_1.expect)(postUpdatePrevDoc).toBeDefined();
457
- // Verify they are different object references
458
- (0, bun_test_1.expect)(postUpdateDoc).not.toBe(postUpdatePrevDoc);
459
- // Verify the content is different (new tag added)
460
- (0, bun_test_1.expect)(postUpdateDoc.tags).toHaveLength(3);
461
- (0, bun_test_1.expect)(postUpdatePrevDoc.tags).toHaveLength(2);
462
- (0, bun_test_1.expect)(postUpdateDoc.tags).toContain("organic");
463
- (0, bun_test_1.expect)(postUpdatePrevDoc.tags).not.toContain("organic");
464
- // Reset for next test
465
- postUpdateCalled = false;
466
- postUpdateDoc = undefined;
467
- postUpdatePrevDoc = undefined;
468
- // Test PATCH operation with string array (update tag)
469
- return [4 /*yield*/, agent.patch("/food/".concat(apple._id, "/tags/healthy")).send({ tags: "super-healthy" }).expect(200)];
470
- case 3:
471
- // Test PATCH operation with string array (update tag)
472
- _a.sent();
473
- (0, bun_test_1.expect)(postUpdateCalled).toBe(true);
474
- (0, bun_test_1.expect)(postUpdateDoc).not.toBe(postUpdatePrevDoc);
475
- // Verify the content is different (tag updated)
476
- (0, bun_test_1.expect)(postUpdateDoc.tags).toContain("super-healthy");
477
- (0, bun_test_1.expect)(postUpdatePrevDoc.tags).toContain("healthy");
478
- (0, bun_test_1.expect)(postUpdateDoc.tags).not.toContain("healthy");
479
- (0, bun_test_1.expect)(postUpdatePrevDoc.tags).not.toContain("super-healthy");
480
- return [2 /*return*/];
481
- }
482
- });
483
- }); });
484
- });
485
- (0, bun_test_1.describe)("array operation errors", function () {
486
- var _server;
487
- var app;
488
- var admin;
489
- var apple;
490
- var agent;
491
- (0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
492
- var _a;
493
- return __generator(this, function (_b) {
494
- switch (_b.label) {
495
- case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
496
- case 1:
497
- _a = __read.apply(void 0, [_b.sent(), 1]), admin = _a[0];
498
- return [4 /*yield*/, tests_1.FoodModel.create({
499
- calories: 100,
500
- categories: [
501
- { name: "Fruit", show: true },
502
- { name: "Popular", show: false },
503
- ],
504
- created: new Date("2021-12-03T00:00:30.000Z"),
505
- hidden: false,
506
- name: "Apple",
507
- ownerId: admin._id,
508
- tags: ["healthy", "cheap"],
509
- })];
510
- case 2:
511
- apple = _b.sent();
512
- app = (0, tests_1.getBaseServer)();
513
- (0, auth_1.setupAuth)(app, tests_1.UserModel);
514
- (0, auth_1.addAuthRoutes)(app, tests_1.UserModel);
515
- return [2 /*return*/];
516
- }
517
- });
518
- }); });
519
- (0, bun_test_1.it)("array operation preUpdate returning undefined throws error", function () { return __awaiter(void 0, void 0, void 0, function () {
520
- var res;
521
- return __generator(this, function (_a) {
522
- switch (_a.label) {
523
- case 0:
524
- app.use("/food", (0, api_1.modelRouter)(tests_1.FoodModel, {
525
- allowAnonymous: true,
526
- permissions: {
527
- create: [permissions_1.Permissions.IsAdmin],
528
- delete: [permissions_1.Permissions.IsAdmin],
529
- list: [permissions_1.Permissions.IsAdmin],
530
- read: [permissions_1.Permissions.IsAdmin],
531
- update: [permissions_1.Permissions.IsAdmin],
532
- },
533
- preUpdate: function () { return undefined; },
534
- }));
535
- _server = (0, supertest_1.default)(app);
536
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
537
- case 1:
538
- agent = _a.sent();
539
- return [4 /*yield*/, agent.post("/food/".concat(apple._id, "/tags")).send({ tags: "organic" }).expect(403)];
540
- case 2:
541
- res = _a.sent();
542
- (0, bun_test_1.expect)(res.body.title).toBe("Update not allowed");
543
- (0, bun_test_1.expect)(res.body.detail).toBe("A body must be returned from preUpdate");
544
- return [2 /*return*/];
545
- }
546
- });
547
- }); });
548
- (0, bun_test_1.it)("array operation preUpdate returning null throws error", function () { return __awaiter(void 0, void 0, void 0, function () {
549
- var res;
550
- return __generator(this, function (_a) {
551
- switch (_a.label) {
552
- case 0:
553
- app.use("/food", (0, api_1.modelRouter)(tests_1.FoodModel, {
554
- allowAnonymous: true,
555
- permissions: {
556
- create: [permissions_1.Permissions.IsAdmin],
557
- delete: [permissions_1.Permissions.IsAdmin],
558
- list: [permissions_1.Permissions.IsAdmin],
559
- read: [permissions_1.Permissions.IsAdmin],
560
- update: [permissions_1.Permissions.IsAdmin],
561
- },
562
- preUpdate: function () { return null; },
563
- }));
564
- _server = (0, supertest_1.default)(app);
565
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
566
- case 1:
567
- agent = _a.sent();
568
- return [4 /*yield*/, agent.post("/food/".concat(apple._id, "/tags")).send({ tags: "organic" }).expect(403)];
569
- case 2:
570
- res = _a.sent();
571
- (0, bun_test_1.expect)(res.body.title).toBe("Update not allowed");
572
- return [2 /*return*/];
573
- }
574
- });
575
- }); });
576
- (0, bun_test_1.it)("array operation preUpdate error is handled", function () { return __awaiter(void 0, void 0, void 0, function () {
577
- var res;
578
- return __generator(this, function (_a) {
579
- switch (_a.label) {
580
- case 0:
581
- app.use("/food", (0, api_1.modelRouter)(tests_1.FoodModel, {
582
- allowAnonymous: true,
583
- permissions: {
584
- create: [permissions_1.Permissions.IsAdmin],
585
- delete: [permissions_1.Permissions.IsAdmin],
586
- list: [permissions_1.Permissions.IsAdmin],
587
- read: [permissions_1.Permissions.IsAdmin],
588
- update: [permissions_1.Permissions.IsAdmin],
589
- },
590
- preUpdate: function () {
591
- throw new Error("preUpdate array failed");
592
- },
593
- }));
594
- _server = (0, supertest_1.default)(app);
595
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
596
- case 1:
597
- agent = _a.sent();
598
- return [4 /*yield*/, agent.post("/food/".concat(apple._id, "/tags")).send({ tags: "organic" }).expect(400)];
599
- case 2:
600
- res = _a.sent();
601
- (0, bun_test_1.expect)(res.body.title).toContain("preUpdate hook error");
602
- return [2 /*return*/];
603
- }
604
- });
605
- }); });
606
- (0, bun_test_1.it)("array operation postUpdate error is handled", function () { return __awaiter(void 0, void 0, void 0, function () {
607
- var res;
608
- return __generator(this, function (_a) {
609
- switch (_a.label) {
610
- case 0:
611
- app.use("/food", (0, api_1.modelRouter)(tests_1.FoodModel, {
612
- allowAnonymous: true,
613
- permissions: {
614
- create: [permissions_1.Permissions.IsAdmin],
615
- delete: [permissions_1.Permissions.IsAdmin],
616
- list: [permissions_1.Permissions.IsAdmin],
617
- read: [permissions_1.Permissions.IsAdmin],
618
- update: [permissions_1.Permissions.IsAdmin],
619
- },
620
- postUpdate: function () {
621
- throw new Error("postUpdate array failed");
622
- },
623
- }));
624
- _server = (0, supertest_1.default)(app);
625
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
626
- case 1:
627
- agent = _a.sent();
628
- return [4 /*yield*/, agent.post("/food/".concat(apple._id, "/tags")).send({ tags: "organic" }).expect(400)];
629
- case 2:
630
- res = _a.sent();
631
- (0, bun_test_1.expect)(res.body.title).toContain("PATCH Post Update error");
632
- return [2 /*return*/];
633
- }
634
- });
635
- }); });
636
- (0, bun_test_1.it)("array operation denied without update permission", function () { return __awaiter(void 0, void 0, void 0, function () {
637
- var res;
638
- return __generator(this, function (_a) {
639
- switch (_a.label) {
640
- case 0:
641
- app.use("/food", (0, api_1.modelRouter)(tests_1.FoodModel, {
642
- allowAnonymous: true,
643
- permissions: {
644
- create: [permissions_1.Permissions.IsAdmin],
645
- delete: [permissions_1.Permissions.IsAdmin],
646
- list: [permissions_1.Permissions.IsAny],
647
- read: [permissions_1.Permissions.IsAny],
648
- update: [permissions_1.Permissions.IsAdmin],
649
- },
650
- }));
651
- _server = (0, supertest_1.default)(app);
652
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "notAdmin")];
653
- case 1:
654
- agent = _a.sent();
655
- return [4 /*yield*/, agent.post("/food/".concat(apple._id, "/tags")).send({ tags: "organic" }).expect(405)];
656
- case 2:
657
- res = _a.sent();
658
- (0, bun_test_1.expect)(res.body.title).toBe("Access denied");
659
- (0, bun_test_1.expect)(res.body.detail).toContain("Access to PATCH");
660
- return [2 /*return*/];
661
- }
662
- });
663
- }); });
664
- (0, bun_test_1.it)("array operation on non-existent document returns 404", function () { return __awaiter(void 0, void 0, void 0, function () {
665
- var fakeId, res;
666
- return __generator(this, function (_a) {
667
- switch (_a.label) {
668
- case 0:
669
- app.use("/food", (0, api_1.modelRouter)(tests_1.FoodModel, {
670
- allowAnonymous: true,
671
- permissions: {
672
- create: [permissions_1.Permissions.IsAdmin],
673
- delete: [permissions_1.Permissions.IsAdmin],
674
- list: [permissions_1.Permissions.IsAdmin],
675
- read: [permissions_1.Permissions.IsAdmin],
676
- update: [permissions_1.Permissions.IsAdmin],
677
- },
678
- }));
679
- _server = (0, supertest_1.default)(app);
680
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
681
- case 1:
682
- agent = _a.sent();
683
- fakeId = "000000000000000000000000";
684
- return [4 /*yield*/, agent.post("/food/".concat(fakeId, "/tags")).send({ tags: "organic" }).expect(404)];
685
- case 2:
686
- res = _a.sent();
687
- (0, bun_test_1.expect)(res.body.title).toBe("Document not found");
688
- (0, bun_test_1.expect)(res.body.detail).toContain("Could not find document to PATCH");
689
- return [2 /*return*/];
690
- }
691
- });
692
- }); });
693
- (0, bun_test_1.it)("array operation denied when user cannot update specific doc", function () { return __awaiter(void 0, void 0, void 0, function () {
694
- var res;
695
- return __generator(this, function (_a) {
696
- switch (_a.label) {
697
- case 0:
698
- // Create food owned by admin, then try to update as notAdmin
699
- app.use("/food", (0, api_1.modelRouter)(tests_1.FoodModel, {
700
- allowAnonymous: true,
701
- permissions: {
702
- create: [permissions_1.Permissions.IsAuthenticated],
703
- delete: [permissions_1.Permissions.IsAuthenticated],
704
- list: [permissions_1.Permissions.IsAuthenticated],
705
- read: [permissions_1.Permissions.IsAuthenticated],
706
- update: [permissions_1.Permissions.IsOwner],
707
- },
708
- }));
709
- _server = (0, supertest_1.default)(app);
710
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "notAdmin")];
711
- case 1:
712
- // Login as notAdmin and try to update admin's food (apple)
713
- agent = _a.sent();
714
- return [4 /*yield*/, agent.post("/food/".concat(apple._id, "/tags")).send({ tags: "organic" }).expect(403)];
715
- case 2:
716
- res = _a.sent();
717
- (0, bun_test_1.expect)(res.body.title).toBe("Update not allowed");
718
- (0, bun_test_1.expect)(res.body.detail).toContain("Patch not allowed");
719
- return [2 /*return*/];
720
- }
721
- });
722
- }); });
723
- (0, bun_test_1.it)("array operation transform error is handled", function () { return __awaiter(void 0, void 0, void 0, function () {
724
- var res;
725
- return __generator(this, function (_a) {
726
- switch (_a.label) {
727
- case 0:
728
- app.use("/food", (0, api_1.modelRouter)(tests_1.FoodModel, {
729
- allowAnonymous: true,
730
- permissions: {
731
- create: [permissions_1.Permissions.IsAdmin],
732
- delete: [permissions_1.Permissions.IsAdmin],
733
- list: [permissions_1.Permissions.IsAdmin],
734
- read: [permissions_1.Permissions.IsAdmin],
735
- update: [permissions_1.Permissions.IsAdmin],
736
- },
737
- transformer: (0, transformers_1.AdminOwnerTransformer)({
738
- adminWriteFields: ["name"],
739
- }),
740
- }));
741
- _server = (0, supertest_1.default)(app);
742
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
743
- case 1:
744
- agent = _a.sent();
745
- return [4 /*yield*/, agent.post("/food/".concat(apple._id, "/tags")).send({ tags: "organic" }).expect(403)];
746
- case 2:
747
- res = _a.sent();
748
- (0, bun_test_1.expect)(res.body.title).toContain("cannot write fields");
749
- return [2 /*return*/];
750
- }
751
- });
752
- }); });
753
- });
754
- (0, bun_test_1.describe)("array operation with undefined preUpdate return", function () {
755
- var _server;
756
- var app;
757
- var admin;
758
- var apple;
759
- var agent;
760
- (0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
761
- var _a;
762
- return __generator(this, function (_b) {
763
- switch (_b.label) {
764
- case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
765
- case 1:
766
- _a = __read.apply(void 0, [_b.sent(), 1]), admin = _a[0];
767
- return [4 /*yield*/, tests_1.FoodModel.create({
768
- calories: 100,
769
- categories: [
770
- { name: "Fruit", show: true },
771
- { name: "Popular", show: false },
772
- ],
773
- created: new Date("2021-12-03T00:00:30.000Z"),
774
- hidden: false,
775
- name: "Apple",
776
- ownerId: admin._id,
777
- tags: ["healthy", "cheap"],
778
- })];
779
- case 2:
780
- apple = _b.sent();
781
- app = (0, tests_1.getBaseServer)();
782
- (0, auth_1.setupAuth)(app, tests_1.UserModel);
783
- (0, auth_1.addAuthRoutes)(app, tests_1.UserModel);
784
- return [2 /*return*/];
785
- }
786
- });
787
- }); });
788
- (0, bun_test_1.it)("array operation preUpdate returning undefined for array POST throws error", function () { return __awaiter(void 0, void 0, void 0, function () {
789
- var res;
790
- return __generator(this, function (_a) {
791
- switch (_a.label) {
792
- case 0:
793
- app.use("/food", (0, api_1.modelRouter)(tests_1.FoodModel, {
794
- allowAnonymous: true,
795
- permissions: {
796
- create: [permissions_1.Permissions.IsAdmin],
797
- delete: [permissions_1.Permissions.IsAdmin],
798
- list: [permissions_1.Permissions.IsAdmin],
799
- read: [permissions_1.Permissions.IsAdmin],
800
- update: [permissions_1.Permissions.IsAdmin],
801
- },
802
- preUpdate: function () { return undefined; },
803
- }));
804
- _server = (0, supertest_1.default)(app);
805
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
806
- case 1:
807
- agent = _a.sent();
808
- return [4 /*yield*/, agent.post("/food/".concat(apple._id, "/tags")).send({ tags: "organic" }).expect(403)];
809
- case 2:
810
- res = _a.sent();
811
- (0, bun_test_1.expect)(res.body.title).toBe("Update not allowed");
812
- (0, bun_test_1.expect)(res.body.detail).toBe("A body must be returned from preUpdate");
813
- return [2 /*return*/];
814
- }
815
- });
816
- }); });
817
- (0, bun_test_1.it)("array operation preUpdate returning null for array PATCH throws error", function () { return __awaiter(void 0, void 0, void 0, function () {
818
- var res;
819
- return __generator(this, function (_a) {
820
- switch (_a.label) {
821
- case 0:
822
- app.use("/food", (0, api_1.modelRouter)(tests_1.FoodModel, {
823
- allowAnonymous: true,
824
- permissions: {
825
- create: [permissions_1.Permissions.IsAdmin],
826
- delete: [permissions_1.Permissions.IsAdmin],
827
- list: [permissions_1.Permissions.IsAdmin],
828
- read: [permissions_1.Permissions.IsAdmin],
829
- update: [permissions_1.Permissions.IsAdmin],
830
- },
831
- preUpdate: function () { return null; },
832
- }));
833
- _server = (0, supertest_1.default)(app);
834
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
835
- case 1:
836
- agent = _a.sent();
837
- return [4 /*yield*/, agent
838
- .patch("/food/".concat(apple._id, "/tags/healthy"))
839
- .send({ tags: "unhealthy" })
840
- .expect(403)];
841
- case 2:
842
- res = _a.sent();
843
- (0, bun_test_1.expect)(res.body.title).toBe("Update not allowed");
844
- return [2 /*return*/];
845
- }
846
- });
847
- }); });
848
- (0, bun_test_1.it)("array operation preUpdate error for array DELETE is handled", function () { return __awaiter(void 0, void 0, void 0, function () {
849
- var res;
850
- return __generator(this, function (_a) {
851
- switch (_a.label) {
852
- case 0:
853
- app.use("/food", (0, api_1.modelRouter)(tests_1.FoodModel, {
854
- allowAnonymous: true,
855
- permissions: {
856
- create: [permissions_1.Permissions.IsAdmin],
857
- delete: [permissions_1.Permissions.IsAdmin],
858
- list: [permissions_1.Permissions.IsAdmin],
859
- read: [permissions_1.Permissions.IsAdmin],
860
- update: [permissions_1.Permissions.IsAdmin],
861
- },
862
- preUpdate: function () {
863
- throw new Error("preUpdate error during delete");
864
- },
865
- }));
866
- _server = (0, supertest_1.default)(app);
867
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
868
- case 1:
869
- agent = _a.sent();
870
- return [4 /*yield*/, agent.delete("/food/".concat(apple._id, "/tags/healthy")).expect(400)];
871
- case 2:
872
- res = _a.sent();
873
- (0, bun_test_1.expect)(res.body.title).toContain("preUpdate hook error");
874
- return [2 /*return*/];
875
- }
876
- });
877
- }); });
878
- });