@terreno/admin-backend 0.21.0 → 0.22.2

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.
@@ -1,1498 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
- if (k2 === undefined) k2 = k;
15
- var desc = Object.getOwnPropertyDescriptor(m, k);
16
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
17
- desc = { enumerable: true, get: function() { return m[k]; } };
18
- }
19
- Object.defineProperty(o, k2, desc);
20
- }) : (function(o, m, k, k2) {
21
- if (k2 === undefined) k2 = k;
22
- o[k2] = m[k];
23
- }));
24
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
25
- Object.defineProperty(o, "default", { enumerable: true, value: v });
26
- }) : function(o, v) {
27
- o["default"] = v;
28
- });
29
- var __importStar = (this && this.__importStar) || (function () {
30
- var ownKeys = function(o) {
31
- ownKeys = Object.getOwnPropertyNames || function (o) {
32
- var ar = [];
33
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
34
- return ar;
35
- };
36
- return ownKeys(o);
37
- };
38
- return function (mod) {
39
- if (mod && mod.__esModule) return mod;
40
- var result = {};
41
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
42
- __setModuleDefault(result, mod);
43
- return result;
44
- };
45
- })();
46
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
47
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
48
- return new (P || (P = Promise))(function (resolve, reject) {
49
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
50
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
51
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
52
- step((generator = generator.apply(thisArg, _arguments || [])).next());
53
- });
54
- };
55
- var __generator = (this && this.__generator) || function (thisArg, body) {
56
- 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);
57
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
58
- function verb(n) { return function (v) { return step([n, v]); }; }
59
- function step(op) {
60
- if (f) throw new TypeError("Generator is already executing.");
61
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
62
- 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;
63
- if (y = 0, t) op = [op[0] & 2, t.value];
64
- switch (op[0]) {
65
- case 0: case 1: t = op; break;
66
- case 4: _.label++; return { value: op[1], done: false };
67
- case 5: _.label++; y = op[1]; op = [0]; continue;
68
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
69
- default:
70
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
71
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
72
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
73
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
74
- if (t[2]) _.ops.pop();
75
- _.trys.pop(); continue;
76
- }
77
- op = body.call(thisArg, _);
78
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
79
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
80
- }
81
- };
82
- var __read = (this && this.__read) || function (o, n) {
83
- var m = typeof Symbol === "function" && o[Symbol.iterator];
84
- if (!m) return o;
85
- var i = m.call(o), r, ar = [], e;
86
- try {
87
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
88
- }
89
- catch (error) { e = { error: error }; }
90
- finally {
91
- try {
92
- if (r && !r.done && (m = i["return"])) m.call(i);
93
- }
94
- finally { if (e) throw e.error; }
95
- }
96
- return ar;
97
- };
98
- var __values = (this && this.__values) || function(o) {
99
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
100
- if (m) return m.call(o);
101
- if (o && typeof o.length === "number") return {
102
- next: function () {
103
- if (o && i >= o.length) o = void 0;
104
- return { value: o && o[i++], done: !o };
105
- }
106
- };
107
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
108
- };
109
- var __importDefault = (this && this.__importDefault) || function (mod) {
110
- return (mod && mod.__esModule) ? mod : { "default": mod };
111
- };
112
- Object.defineProperty(exports, "__esModule", { value: true });
113
- var bun_test_1 = require("bun:test");
114
- var api_1 = require("@terreno/api");
115
- var tests_1 = require("@terreno/api/src/tests");
116
- var supertest_1 = __importDefault(require("supertest"));
117
- var adminApp_1 = require("./adminApp");
118
- var buildApp = function (models, adminOverrides) {
119
- if (models === void 0) { models = []; }
120
- var app = (0, tests_1.getBaseServer)();
121
- (0, api_1.setupAuth)(app, tests_1.UserModel);
122
- (0, api_1.addAuthRoutes)(app, tests_1.UserModel);
123
- var admin = new adminApp_1.AdminApp(__assign({ basePath: "/admin", models: models }, adminOverrides));
124
- admin.register(app);
125
- app.use(api_1.apiUnauthorizedMiddleware);
126
- app.use(api_1.apiErrorMiddleware);
127
- return app;
128
- };
129
- var foodModelConfig = {
130
- displayName: "Foods",
131
- listFields: ["name", "calories"],
132
- model: tests_1.FoodModel,
133
- routePath: "/foods",
134
- };
135
- (0, bun_test_1.describe)("AdminApp /admin/config", function () {
136
- var app;
137
- var adminAgent;
138
- var notAdminAgent;
139
- (0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
140
- return __generator(this, function (_a) {
141
- switch (_a.label) {
142
- case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
143
- case 1:
144
- _a.sent();
145
- app = buildApp([
146
- __assign(__assign({}, foodModelConfig), { fieldOrder: ["name", "calories", "tags"], fieldOverrides: { name: { widget: "markdown" } }, hiddenFields: ["hidden"] }),
147
- ]);
148
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
149
- case 2:
150
- adminAgent = _a.sent();
151
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "notAdmin")];
152
- case 3:
153
- notAdminAgent = _a.sent();
154
- return [2 /*return*/];
155
- }
156
- });
157
- }); });
158
- (0, bun_test_1.afterEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
159
- return __generator(this, function (_a) {
160
- switch (_a.label) {
161
- case 0: return [4 /*yield*/, tests_1.FoodModel.deleteMany({})];
162
- case 1:
163
- _a.sent();
164
- return [4 /*yield*/, api_1.VersionConfig.deleteMany({})];
165
- case 2:
166
- _a.sent();
167
- return [2 /*return*/];
168
- }
169
- });
170
- }); });
171
- (0, bun_test_1.it)("returns metadata for configured models to admins", function () { return __awaiter(void 0, void 0, void 0, function () {
172
- var res, _a, foodMeta;
173
- var _b, _c, _d;
174
- return __generator(this, function (_e) {
175
- switch (_e.label) {
176
- case 0: return [4 /*yield*/, adminAgent.get("/admin/config").expect(200)];
177
- case 1:
178
- res = _e.sent();
179
- (0, bun_test_1.expect)(res.body.schemaVersion).toBe(2);
180
- (0, bun_test_1.expect)((_b = res.body.home) === null || _b === void 0 ? void 0 : _b.title).toBe("Administration");
181
- (0, bun_test_1.expect)((_d = (_c = res.body.home) === null || _c === void 0 ? void 0 : _c.slots) === null || _d === void 0 ? void 0 : _d.main).toEqual(["modelsGrid"]);
182
- (0, bun_test_1.expect)(res.body.models).toHaveLength(1);
183
- _a = __read(res.body.models, 1), foodMeta = _a[0];
184
- (0, bun_test_1.expect)(foodMeta.name).toBe("Food");
185
- (0, bun_test_1.expect)(foodMeta.displayName).toBe("Foods");
186
- (0, bun_test_1.expect)(foodMeta.listFields).toEqual(["name", "calories"]);
187
- (0, bun_test_1.expect)(foodMeta.routePath).toBe("/admin/foods");
188
- (0, bun_test_1.expect)(foodMeta.defaultSort).toBe("-created");
189
- (0, bun_test_1.expect)(foodMeta.fieldOrder).toEqual(["name", "calories", "tags"]);
190
- return [2 /*return*/];
191
- }
192
- });
193
- }); });
194
- (0, bun_test_1.it)("includes recordTitleField in config when set on the model", function () { return __awaiter(void 0, void 0, void 0, function () {
195
- var appWithTitle, agent, res;
196
- return __generator(this, function (_a) {
197
- switch (_a.label) {
198
- case 0:
199
- appWithTitle = buildApp([__assign(__assign({}, foodModelConfig), { recordTitleField: "name" })]);
200
- return [4 /*yield*/, (0, tests_1.authAsUser)(appWithTitle, "admin")];
201
- case 1:
202
- agent = _a.sent();
203
- return [4 /*yield*/, agent.get("/admin/config").expect(200)];
204
- case 2:
205
- res = _a.sent();
206
- (0, bun_test_1.expect)(res.body.models[0].recordTitleField).toBe("name");
207
- return [2 /*return*/];
208
- }
209
- });
210
- }); });
211
- (0, bun_test_1.it)("applies fieldOverrides to generated config", function () { return __awaiter(void 0, void 0, void 0, function () {
212
- var res, _a, foodMeta;
213
- return __generator(this, function (_b) {
214
- switch (_b.label) {
215
- case 0: return [4 /*yield*/, adminAgent.get("/admin/config").expect(200)];
216
- case 1:
217
- res = _b.sent();
218
- _a = __read(res.body.models, 1), foodMeta = _a[0];
219
- (0, bun_test_1.expect)(foodMeta.fields.name.widget).toBe("markdown");
220
- return [2 /*return*/];
221
- }
222
- });
223
- }); });
224
- (0, bun_test_1.it)("removes hidden fields from config and listFields", function () { return __awaiter(void 0, void 0, void 0, function () {
225
- var agent, res, _a, foodMeta;
226
- return __generator(this, function (_b) {
227
- switch (_b.label) {
228
- case 0:
229
- app = buildApp([
230
- __assign(__assign({}, foodModelConfig), { hiddenFields: ["hidden"], listFields: ["name", "hidden", "calories"] }),
231
- ]);
232
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
233
- case 1:
234
- agent = _b.sent();
235
- return [4 /*yield*/, agent.get("/admin/config").expect(200)];
236
- case 2:
237
- res = _b.sent();
238
- _a = __read(res.body.models, 1), foodMeta = _a[0];
239
- (0, bun_test_1.expect)(foodMeta.fields.hidden).toBeUndefined();
240
- (0, bun_test_1.expect)(foodMeta.listFields).toEqual(["name", "calories"]);
241
- return [2 /*return*/];
242
- }
243
- });
244
- }); });
245
- (0, bun_test_1.it)("extracts item sub-field metadata for array fields", function () { return __awaiter(void 0, void 0, void 0, function () {
246
- var res, _a, foodMeta;
247
- return __generator(this, function (_b) {
248
- switch (_b.label) {
249
- case 0: return [4 /*yield*/, adminAgent.get("/admin/config").expect(200)];
250
- case 1:
251
- res = _b.sent();
252
- _a = __read(res.body.models, 1), foodMeta = _a[0];
253
- // likesIds is an array of subdocuments with likes + userId sub-fields
254
- (0, bun_test_1.expect)(foodMeta.fields.likesIds.type).toBe("array");
255
- (0, bun_test_1.expect)(foodMeta.fields.likesIds.items).toBeDefined();
256
- (0, bun_test_1.expect)(foodMeta.fields.likesIds.items.userId).toBeDefined();
257
- (0, bun_test_1.expect)(foodMeta.fields.likesIds.items.likes).toBeDefined();
258
- return [2 /*return*/];
259
- }
260
- });
261
- }); });
262
- (0, bun_test_1.it)("extracts ref information for ObjectId fields", function () { return __awaiter(void 0, void 0, void 0, function () {
263
- var res, _a, foodMeta;
264
- return __generator(this, function (_b) {
265
- switch (_b.label) {
266
- case 0: return [4 /*yield*/, adminAgent.get("/admin/config").expect(200)];
267
- case 1:
268
- res = _b.sent();
269
- _a = __read(res.body.models, 1), foodMeta = _a[0];
270
- (0, bun_test_1.expect)(foodMeta.fields.ownerId.ref).toBe("User");
271
- return [2 /*return*/];
272
- }
273
- });
274
- }); });
275
- (0, bun_test_1.it)("extracts ref information for array of ObjectId references", function () { return __awaiter(void 0, void 0, void 0, function () {
276
- var res, _a, foodMeta;
277
- return __generator(this, function (_b) {
278
- switch (_b.label) {
279
- case 0: return [4 /*yield*/, adminAgent.get("/admin/config").expect(200)];
280
- case 1:
281
- res = _b.sent();
282
- _a = __read(res.body.models, 1), foodMeta = _a[0];
283
- (0, bun_test_1.expect)(foodMeta.fields.eatenBy.ref).toBe("User");
284
- return [2 /*return*/];
285
- }
286
- });
287
- }); });
288
- (0, bun_test_1.it)("extracts itemType for primitive string arrays", function () { return __awaiter(void 0, void 0, void 0, function () {
289
- var res, _a, foodMeta;
290
- return __generator(this, function (_b) {
291
- switch (_b.label) {
292
- case 0: return [4 /*yield*/, adminAgent.get("/admin/config").expect(200)];
293
- case 1:
294
- res = _b.sent();
295
- _a = __read(res.body.models, 1), foodMeta = _a[0];
296
- // tags is [String] — should expose itemType so the frontend renders a primitive list
297
- (0, bun_test_1.expect)(foodMeta.fields.tags.type).toBe("array");
298
- (0, bun_test_1.expect)(foodMeta.fields.tags.itemType).toBe("string");
299
- (0, bun_test_1.expect)(foodMeta.fields.tags.items).toBeUndefined();
300
- return [2 /*return*/];
301
- }
302
- });
303
- }); });
304
- (0, bun_test_1.it)("extracts itemType and itemRef for arrays of ObjectId references", function () { return __awaiter(void 0, void 0, void 0, function () {
305
- var res, _a, foodMeta;
306
- return __generator(this, function (_b) {
307
- switch (_b.label) {
308
- case 0: return [4 /*yield*/, adminAgent.get("/admin/config").expect(200)];
309
- case 1:
310
- res = _b.sent();
311
- _a = __read(res.body.models, 1), foodMeta = _a[0];
312
- // eatenBy is [{type: ObjectId, ref: "User"}] — should expose both itemType and itemRef
313
- (0, bun_test_1.expect)(foodMeta.fields.eatenBy.type).toBe("array");
314
- (0, bun_test_1.expect)(foodMeta.fields.eatenBy.itemType).toBe("objectid");
315
- (0, bun_test_1.expect)(foodMeta.fields.eatenBy.itemRef).toBe("User");
316
- return [2 /*return*/];
317
- }
318
- });
319
- }); });
320
- (0, bun_test_1.it)("returns 403 for non-admin users", function () { return __awaiter(void 0, void 0, void 0, function () {
321
- var res;
322
- return __generator(this, function (_a) {
323
- switch (_a.label) {
324
- case 0: return [4 /*yield*/, notAdminAgent.get("/admin/config").expect(403)];
325
- case 1:
326
- res = _a.sent();
327
- (0, bun_test_1.expect)(res.body.title).toInclude("Admin access required");
328
- return [2 /*return*/];
329
- }
330
- });
331
- }); });
332
- (0, bun_test_1.it)("returns 401 for unauthenticated users", function () { return __awaiter(void 0, void 0, void 0, function () {
333
- return __generator(this, function (_a) {
334
- switch (_a.label) {
335
- case 0: return [4 /*yield*/, (0, supertest_1.default)(app).get("/admin/config").expect(401)];
336
- case 1:
337
- _a.sent();
338
- return [2 /*return*/];
339
- }
340
- });
341
- }); });
342
- (0, bun_test_1.it)("defaults basePath to /admin when not provided", function () { return __awaiter(void 0, void 0, void 0, function () {
343
- var altApp, agent, res;
344
- return __generator(this, function (_a) {
345
- switch (_a.label) {
346
- case 0:
347
- altApp = (0, tests_1.getBaseServer)();
348
- (0, api_1.setupAuth)(altApp, tests_1.UserModel);
349
- (0, api_1.addAuthRoutes)(altApp, tests_1.UserModel);
350
- new adminApp_1.AdminApp({ models: [foodModelConfig] }).register(altApp);
351
- altApp.use(api_1.apiUnauthorizedMiddleware);
352
- altApp.use(api_1.apiErrorMiddleware);
353
- return [4 /*yield*/, (0, tests_1.authAsUser)(altApp, "admin")];
354
- case 1:
355
- agent = _a.sent();
356
- return [4 /*yield*/, agent.get("/admin/config").expect(200)];
357
- case 2:
358
- res = _a.sent();
359
- (0, bun_test_1.expect)(res.body.models[0].routePath).toBe("/admin/foods");
360
- return [2 /*return*/];
361
- }
362
- });
363
- }); });
364
- (0, bun_test_1.it)("uses a custom defaultSort when provided", function () { return __awaiter(void 0, void 0, void 0, function () {
365
- var agent, res;
366
- return __generator(this, function (_a) {
367
- switch (_a.label) {
368
- case 0:
369
- app = buildApp([__assign(__assign({}, foodModelConfig), { defaultSort: "name" })]);
370
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
371
- case 1:
372
- agent = _a.sent();
373
- return [4 /*yield*/, agent.get("/admin/config").expect(200)];
374
- case 2:
375
- res = _a.sent();
376
- (0, bun_test_1.expect)(res.body.models[0].defaultSort).toBe("name");
377
- return [2 /*return*/];
378
- }
379
- });
380
- }); });
381
- (0, bun_test_1.it)("includes version-config custom screen", function () { return __awaiter(void 0, void 0, void 0, function () {
382
- var res;
383
- return __generator(this, function (_a) {
384
- switch (_a.label) {
385
- case 0: return [4 /*yield*/, adminAgent.get("/admin/config").expect(200)];
386
- case 1:
387
- res = _a.sent();
388
- (0, bun_test_1.expect)(res.body.customScreens).toEqual([
389
- { displayName: "Version Config", name: "version-config" },
390
- ]);
391
- return [2 /*return*/];
392
- }
393
- });
394
- }); });
395
- (0, bun_test_1.it)("normalizes home.slots so recentActivity is last in sidebar", function () { return __awaiter(void 0, void 0, void 0, function () {
396
- var agent, res;
397
- return __generator(this, function (_a) {
398
- switch (_a.label) {
399
- case 0:
400
- app = buildApp([foodModelConfig], {
401
- home: {
402
- slots: { sidebar: ["recentActivity", "versionConfig"] },
403
- title: "Ops",
404
- },
405
- });
406
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
407
- case 1:
408
- agent = _a.sent();
409
- return [4 /*yield*/, agent.get("/admin/config").expect(200)];
410
- case 2:
411
- res = _a.sent();
412
- (0, bun_test_1.expect)(res.body.home.title).toBe("Ops");
413
- (0, bun_test_1.expect)(res.body.home.slots.sidebar).toEqual(["versionConfig", "recentActivity"]);
414
- return [2 /*return*/];
415
- }
416
- });
417
- }); });
418
- });
419
- (0, bun_test_1.describe)("AdminApp model CRUD routes", function () {
420
- var app;
421
- var adminAgent;
422
- var notAdminAgent;
423
- (0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
424
- return __generator(this, function (_a) {
425
- switch (_a.label) {
426
- case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
427
- case 1:
428
- _a.sent();
429
- app = buildApp([__assign(__assign({}, foodModelConfig), { hiddenFields: ["hidden"] })]);
430
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
431
- case 2:
432
- adminAgent = _a.sent();
433
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "notAdmin")];
434
- case 3:
435
- notAdminAgent = _a.sent();
436
- return [2 /*return*/];
437
- }
438
- });
439
- }); });
440
- (0, bun_test_1.afterEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
441
- return __generator(this, function (_a) {
442
- switch (_a.label) {
443
- case 0: return [4 /*yield*/, tests_1.FoodModel.deleteMany({})];
444
- case 1:
445
- _a.sent();
446
- return [2 /*return*/];
447
- }
448
- });
449
- }); });
450
- (0, bun_test_1.it)("creates documents via POST and strips hidden fields from response", function () { return __awaiter(void 0, void 0, void 0, function () {
451
- var res;
452
- return __generator(this, function (_a) {
453
- switch (_a.label) {
454
- case 0: return [4 /*yield*/, adminAgent
455
- .post("/admin/foods")
456
- .send({ calories: 120, hidden: true, name: "Apple" })
457
- .expect(201)];
458
- case 1:
459
- res = _a.sent();
460
- (0, bun_test_1.expect)(res.body.data.name).toBe("Apple");
461
- (0, bun_test_1.expect)(res.body.data.calories).toBe(120);
462
- (0, bun_test_1.expect)(res.body.data.hidden).toBeUndefined();
463
- return [2 /*return*/];
464
- }
465
- });
466
- }); });
467
- (0, bun_test_1.it)("lists documents via GET and strips hidden fields from results", function () { return __awaiter(void 0, void 0, void 0, function () {
468
- var res, _a, _b, item;
469
- var e_1, _c;
470
- return __generator(this, function (_d) {
471
- switch (_d.label) {
472
- case 0: return [4 /*yield*/, tests_1.FoodModel.create({ calories: 120, hidden: true, name: "Apple" })];
473
- case 1:
474
- _d.sent();
475
- return [4 /*yield*/, tests_1.FoodModel.create({ calories: 95, hidden: false, name: "Banana" })];
476
- case 2:
477
- _d.sent();
478
- return [4 /*yield*/, adminAgent.get("/admin/foods").expect(200)];
479
- case 3:
480
- res = _d.sent();
481
- (0, bun_test_1.expect)(res.body.data).toHaveLength(2);
482
- try {
483
- for (_a = __values(res.body.data), _b = _a.next(); !_b.done; _b = _a.next()) {
484
- item = _b.value;
485
- (0, bun_test_1.expect)(item.hidden).toBeUndefined();
486
- }
487
- }
488
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
489
- finally {
490
- try {
491
- if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
492
- }
493
- finally { if (e_1) throw e_1.error; }
494
- }
495
- return [2 /*return*/];
496
- }
497
- });
498
- }); });
499
- (0, bun_test_1.it)("reads a document via GET /:id", function () { return __awaiter(void 0, void 0, void 0, function () {
500
- var food, res;
501
- return __generator(this, function (_a) {
502
- switch (_a.label) {
503
- case 0: return [4 /*yield*/, tests_1.FoodModel.create({ calories: 120, name: "Apple" })];
504
- case 1:
505
- food = _a.sent();
506
- return [4 /*yield*/, adminAgent.get("/admin/foods/".concat(food._id)).expect(200)];
507
- case 2:
508
- res = _a.sent();
509
- (0, bun_test_1.expect)(res.body.data.name).toBe("Apple");
510
- return [2 /*return*/];
511
- }
512
- });
513
- }); });
514
- (0, bun_test_1.it)("updates a document via PATCH /:id", function () { return __awaiter(void 0, void 0, void 0, function () {
515
- var food, res;
516
- return __generator(this, function (_a) {
517
- switch (_a.label) {
518
- case 0: return [4 /*yield*/, tests_1.FoodModel.create({ calories: 120, name: "Apple" })];
519
- case 1:
520
- food = _a.sent();
521
- return [4 /*yield*/, adminAgent
522
- .patch("/admin/foods/".concat(food._id))
523
- .send({ calories: 150 })
524
- .expect(200)];
525
- case 2:
526
- res = _a.sent();
527
- (0, bun_test_1.expect)(res.body.data.calories).toBe(150);
528
- return [2 /*return*/];
529
- }
530
- });
531
- }); });
532
- (0, bun_test_1.it)("deletes a document via DELETE /:id", function () { return __awaiter(void 0, void 0, void 0, function () {
533
- var food, _a;
534
- return __generator(this, function (_b) {
535
- switch (_b.label) {
536
- case 0: return [4 /*yield*/, tests_1.FoodModel.create({ calories: 120, name: "Apple" })];
537
- case 1:
538
- food = _b.sent();
539
- return [4 /*yield*/, adminAgent.delete("/admin/foods/".concat(food._id)).expect(204)];
540
- case 2:
541
- _b.sent();
542
- _a = bun_test_1.expect;
543
- return [4 /*yield*/, tests_1.FoodModel.findById(food._id)];
544
- case 3:
545
- _a.apply(void 0, [_b.sent()]).toBeNull();
546
- return [2 /*return*/];
547
- }
548
- });
549
- }); });
550
- (0, bun_test_1.it)("rejects non-admin users from model list", function () { return __awaiter(void 0, void 0, void 0, function () {
551
- var res;
552
- return __generator(this, function (_a) {
553
- switch (_a.label) {
554
- case 0: return [4 /*yield*/, notAdminAgent.get("/admin/foods")];
555
- case 1:
556
- res = _a.sent();
557
- // modelRouter with IsAdmin permission blocks non-admin users
558
- (0, bun_test_1.expect)([401, 403, 405]).toContain(res.status);
559
- return [2 /*return*/];
560
- }
561
- });
562
- }); });
563
- (0, bun_test_1.it)("supports models without hiddenFields (no responseHandler installed)", function () { return __awaiter(void 0, void 0, void 0, function () {
564
- var agent, res;
565
- return __generator(this, function (_a) {
566
- switch (_a.label) {
567
- case 0:
568
- app = buildApp([foodModelConfig]);
569
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
570
- case 1:
571
- agent = _a.sent();
572
- return [4 /*yield*/, tests_1.FoodModel.create({ calories: 120, hidden: true, name: "Apple" })];
573
- case 2:
574
- _a.sent();
575
- return [4 /*yield*/, agent.get("/admin/foods").expect(200)];
576
- case 3:
577
- res = _a.sent();
578
- (0, bun_test_1.expect)(res.body.data[0].hidden).toBe(true);
579
- return [2 /*return*/];
580
- }
581
- });
582
- }); });
583
- (0, bun_test_1.it)("recursively removes hidden fields from array item values", function () { return __awaiter(void 0, void 0, void 0, function () {
584
- var agent, res, _a, _b, doc;
585
- var e_2, _c;
586
- return __generator(this, function (_d) {
587
- switch (_d.label) {
588
- case 0: return [4 /*yield*/, tests_1.RequiredModel.deleteMany({})];
589
- case 1:
590
- _d.sent();
591
- app = buildApp([
592
- {
593
- displayName: "Required",
594
- hiddenFields: ["about"],
595
- listFields: ["name"],
596
- model: tests_1.RequiredModel,
597
- routePath: "/required",
598
- },
599
- ]);
600
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
601
- case 2:
602
- agent = _d.sent();
603
- return [4 /*yield*/, tests_1.RequiredModel.create({ about: "secret", name: "first" })];
604
- case 3:
605
- _d.sent();
606
- return [4 /*yield*/, tests_1.RequiredModel.create({ about: "secret2", name: "second" })];
607
- case 4:
608
- _d.sent();
609
- return [4 /*yield*/, agent.get("/admin/required").expect(200)];
610
- case 5:
611
- res = _d.sent();
612
- (0, bun_test_1.expect)(res.body.data).toHaveLength(2);
613
- try {
614
- for (_a = __values(res.body.data), _b = _a.next(); !_b.done; _b = _a.next()) {
615
- doc = _b.value;
616
- (0, bun_test_1.expect)(doc.about).toBeUndefined();
617
- (0, bun_test_1.expect)(doc.name).toBeDefined();
618
- }
619
- }
620
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
621
- finally {
622
- try {
623
- if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
624
- }
625
- finally { if (e_2) throw e_2.error; }
626
- }
627
- return [4 /*yield*/, tests_1.RequiredModel.deleteMany({})];
628
- case 6:
629
- _d.sent();
630
- return [2 /*return*/];
631
- }
632
- });
633
- }); });
634
- });
635
- (0, bun_test_1.describe)("AdminApp /admin/version-config", function () {
636
- var app;
637
- var adminAgent;
638
- var notAdminAgent;
639
- (0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
640
- return __generator(this, function (_a) {
641
- switch (_a.label) {
642
- case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
643
- case 1:
644
- _a.sent();
645
- return [4 /*yield*/, api_1.VersionConfig.deleteMany({})];
646
- case 2:
647
- _a.sent();
648
- app = buildApp([]);
649
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
650
- case 3:
651
- adminAgent = _a.sent();
652
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "notAdmin")];
653
- case 4:
654
- notAdminAgent = _a.sent();
655
- return [2 /*return*/];
656
- }
657
- });
658
- }); });
659
- (0, bun_test_1.afterEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
660
- return __generator(this, function (_a) {
661
- switch (_a.label) {
662
- case 0: return [4 /*yield*/, api_1.VersionConfig.deleteMany({})];
663
- case 1:
664
- _a.sent();
665
- return [2 /*return*/];
666
- }
667
- });
668
- }); });
669
- (0, bun_test_1.it)("returns default values when no config exists", function () { return __awaiter(void 0, void 0, void 0, function () {
670
- var res;
671
- return __generator(this, function (_a) {
672
- switch (_a.label) {
673
- case 0: return [4 /*yield*/, adminAgent.get("/admin/version-config").expect(200)];
674
- case 1:
675
- res = _a.sent();
676
- (0, bun_test_1.expect)(res.body.mobileRequiredVersion).toBe(0);
677
- (0, bun_test_1.expect)(res.body.webWarningVersion).toBe(0);
678
- (0, bun_test_1.expect)(res.body.requiredMessage).toInclude("update");
679
- return [2 /*return*/];
680
- }
681
- });
682
- }); });
683
- (0, bun_test_1.it)("returns the stored config when present", function () { return __awaiter(void 0, void 0, void 0, function () {
684
- var res;
685
- return __generator(this, function (_a) {
686
- switch (_a.label) {
687
- case 0: return [4 /*yield*/, api_1.VersionConfig.create({
688
- _singleton: "config",
689
- mobileRequiredVersion: 5,
690
- mobileWarningVersion: 3,
691
- updateUrl: "https://example.com/update",
692
- })];
693
- case 1:
694
- _a.sent();
695
- return [4 /*yield*/, adminAgent.get("/admin/version-config").expect(200)];
696
- case 2:
697
- res = _a.sent();
698
- (0, bun_test_1.expect)(res.body.mobileRequiredVersion).toBe(5);
699
- (0, bun_test_1.expect)(res.body.mobileWarningVersion).toBe(3);
700
- (0, bun_test_1.expect)(res.body.updateUrl).toBe("https://example.com/update");
701
- return [2 /*return*/];
702
- }
703
- });
704
- }); });
705
- (0, bun_test_1.it)("creates or updates the config via PUT", function () { return __awaiter(void 0, void 0, void 0, function () {
706
- var res, stored;
707
- return __generator(this, function (_a) {
708
- switch (_a.label) {
709
- case 0: return [4 /*yield*/, adminAgent
710
- .put("/admin/version-config")
711
- .send({
712
- mobileRequiredVersion: 10,
713
- mobileWarningVersion: 8,
714
- updateUrl: "https://example.com/upgrade",
715
- })
716
- .expect(200)];
717
- case 1:
718
- res = _a.sent();
719
- (0, bun_test_1.expect)(res.body.mobileRequiredVersion).toBe(10);
720
- (0, bun_test_1.expect)(res.body.updateUrl).toBe("https://example.com/upgrade");
721
- return [4 /*yield*/, api_1.VersionConfig.findOne({ _singleton: "config" })];
722
- case 2:
723
- stored = _a.sent();
724
- (0, bun_test_1.expect)(stored === null || stored === void 0 ? void 0 : stored.mobileRequiredVersion).toBe(10);
725
- return [2 /*return*/];
726
- }
727
- });
728
- }); });
729
- (0, bun_test_1.it)("unsets fields passed as null via PUT", function () { return __awaiter(void 0, void 0, void 0, function () {
730
- var res;
731
- return __generator(this, function (_a) {
732
- switch (_a.label) {
733
- case 0: return [4 /*yield*/, adminAgent
734
- .put("/admin/version-config")
735
- .send({ updateUrl: "https://example.com/upgrade" })
736
- .expect(200)];
737
- case 1:
738
- _a.sent();
739
- return [4 /*yield*/, adminAgent.put("/admin/version-config").send({ updateUrl: null }).expect(200)];
740
- case 2:
741
- res = _a.sent();
742
- (0, bun_test_1.expect)(res.body.updateUrl).toBeUndefined();
743
- return [2 /*return*/];
744
- }
745
- });
746
- }); });
747
- (0, bun_test_1.it)("ignores unknown fields via PUT", function () { return __awaiter(void 0, void 0, void 0, function () {
748
- var res;
749
- return __generator(this, function (_a) {
750
- switch (_a.label) {
751
- case 0: return [4 /*yield*/, adminAgent
752
- .put("/admin/version-config")
753
- .send({ bogusField: "nope", mobileRequiredVersion: 2 })
754
- .expect(200)];
755
- case 1:
756
- res = _a.sent();
757
- (0, bun_test_1.expect)(res.body.mobileRequiredVersion).toBe(2);
758
- (0, bun_test_1.expect)(res.body.bogusField).toBeUndefined();
759
- return [2 /*return*/];
760
- }
761
- });
762
- }); });
763
- (0, bun_test_1.it)("returns 403 for non-admins (GET)", function () { return __awaiter(void 0, void 0, void 0, function () {
764
- return __generator(this, function (_a) {
765
- switch (_a.label) {
766
- case 0: return [4 /*yield*/, notAdminAgent.get("/admin/version-config").expect(403)];
767
- case 1:
768
- _a.sent();
769
- return [2 /*return*/];
770
- }
771
- });
772
- }); });
773
- (0, bun_test_1.it)("returns 403 for non-admins (PUT)", function () { return __awaiter(void 0, void 0, void 0, function () {
774
- return __generator(this, function (_a) {
775
- switch (_a.label) {
776
- case 0: return [4 /*yield*/, notAdminAgent.put("/admin/version-config").send({}).expect(403)];
777
- case 1:
778
- _a.sent();
779
- return [2 /*return*/];
780
- }
781
- });
782
- }); });
783
- (0, bun_test_1.it)("returns 401 for unauthenticated users", function () { return __awaiter(void 0, void 0, void 0, function () {
784
- return __generator(this, function (_a) {
785
- switch (_a.label) {
786
- case 0: return [4 /*yield*/, (0, supertest_1.default)(app).get("/admin/version-config").expect(401)];
787
- case 1:
788
- _a.sent();
789
- return [2 /*return*/];
790
- }
791
- });
792
- }); });
793
- });
794
- (0, bun_test_1.describe)("AdminApp search route", function () {
795
- var app;
796
- var adminAgent;
797
- var notAdminAgent;
798
- (0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
799
- return __generator(this, function (_a) {
800
- switch (_a.label) {
801
- case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
802
- case 1:
803
- _a.sent();
804
- app = buildApp([foodModelConfig]);
805
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
806
- case 2:
807
- adminAgent = _a.sent();
808
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "notAdmin")];
809
- case 3:
810
- notAdminAgent = _a.sent();
811
- return [2 /*return*/];
812
- }
813
- });
814
- }); });
815
- (0, bun_test_1.afterEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
816
- return __generator(this, function (_a) {
817
- switch (_a.label) {
818
- case 0: return [4 /*yield*/, tests_1.FoodModel.deleteMany({})];
819
- case 1:
820
- _a.sent();
821
- return [2 /*return*/];
822
- }
823
- });
824
- }); });
825
- (0, bun_test_1.it)("returns empty results when q is empty", function () { return __awaiter(void 0, void 0, void 0, function () {
826
- var res;
827
- return __generator(this, function (_a) {
828
- switch (_a.label) {
829
- case 0: return [4 /*yield*/, adminAgent.get("/admin/foods/search").expect(200)];
830
- case 1:
831
- res = _a.sent();
832
- (0, bun_test_1.expect)(res.body.data).toEqual([]);
833
- return [2 /*return*/];
834
- }
835
- });
836
- }); });
837
- (0, bun_test_1.it)("returns results matching searchable string fields", function () { return __awaiter(void 0, void 0, void 0, function () {
838
- var res;
839
- return __generator(this, function (_a) {
840
- switch (_a.label) {
841
- case 0: return [4 /*yield*/, tests_1.FoodModel.create({ calories: 120, name: "Apple" })];
842
- case 1:
843
- _a.sent();
844
- return [4 /*yield*/, tests_1.FoodModel.create({ calories: 95, name: "Banana" })];
845
- case 2:
846
- _a.sent();
847
- return [4 /*yield*/, adminAgent.get("/admin/foods/search?q=App").expect(200)];
848
- case 3:
849
- res = _a.sent();
850
- (0, bun_test_1.expect)(res.body.data).toHaveLength(1);
851
- (0, bun_test_1.expect)(res.body.data[0].name).toBe("Apple");
852
- return [2 /*return*/];
853
- }
854
- });
855
- }); });
856
- (0, bun_test_1.it)("escapes regex special characters in search query", function () { return __awaiter(void 0, void 0, void 0, function () {
857
- var res;
858
- return __generator(this, function (_a) {
859
- switch (_a.label) {
860
- case 0: return [4 /*yield*/, tests_1.FoodModel.create({ calories: 100, name: "Rice.Bowl" })];
861
- case 1:
862
- _a.sent();
863
- return [4 /*yield*/, adminAgent.get("/admin/foods/search?q=Rice.Bowl").expect(200)];
864
- case 2:
865
- res = _a.sent();
866
- (0, bun_test_1.expect)(res.body.data).toHaveLength(1);
867
- (0, bun_test_1.expect)(res.body.data[0].name).toBe("Rice.Bowl");
868
- return [2 /*return*/];
869
- }
870
- });
871
- }); });
872
- (0, bun_test_1.it)("filters by explicit fields when provided", function () { return __awaiter(void 0, void 0, void 0, function () {
873
- var res;
874
- return __generator(this, function (_a) {
875
- switch (_a.label) {
876
- case 0: return [4 /*yield*/, tests_1.FoodModel.create({ calories: 100, name: "Apple" })];
877
- case 1:
878
- _a.sent();
879
- return [4 /*yield*/, adminAgent.get("/admin/foods/search?q=Apple&fields=calories").expect(200)];
880
- case 2:
881
- res = _a.sent();
882
- // calories isn't a searchable string field so the result should be empty
883
- (0, bun_test_1.expect)(res.body.data).toEqual([]);
884
- return [2 /*return*/];
885
- }
886
- });
887
- }); });
888
- (0, bun_test_1.it)("exercises ObjectId query branch when query looks like an ObjectId", function () { return __awaiter(void 0, void 0, void 0, function () {
889
- var user, res;
890
- return __generator(this, function (_a) {
891
- switch (_a.label) {
892
- case 0: return [4 /*yield*/, tests_1.UserModel.findOne({ email: "admin@example.com" })];
893
- case 1:
894
- user = _a.sent();
895
- return [4 /*yield*/, tests_1.FoodModel.create({ calories: 100, name: "Kale", ownerId: user === null || user === void 0 ? void 0 : user._id })];
896
- case 2:
897
- _a.sent();
898
- return [4 /*yield*/, adminAgent.get("/admin/foods/search?q=".concat(user === null || user === void 0 ? void 0 : user._id)).expect(200)];
899
- case 3:
900
- res = _a.sent();
901
- (0, bun_test_1.expect)(Array.isArray(res.body.data)).toBe(true);
902
- return [2 /*return*/];
903
- }
904
- });
905
- }); });
906
- (0, bun_test_1.it)("returns 403 for non-admins", function () { return __awaiter(void 0, void 0, void 0, function () {
907
- return __generator(this, function (_a) {
908
- switch (_a.label) {
909
- case 0: return [4 /*yield*/, notAdminAgent.get("/admin/foods/search?q=x").expect(403)];
910
- case 1:
911
- _a.sent();
912
- return [2 /*return*/];
913
- }
914
- });
915
- }); });
916
- (0, bun_test_1.it)("returns 401 for unauthenticated users", function () { return __awaiter(void 0, void 0, void 0, function () {
917
- return __generator(this, function (_a) {
918
- switch (_a.label) {
919
- case 0: return [4 /*yield*/, (0, supertest_1.default)(app).get("/admin/foods/search?q=x").expect(401)];
920
- case 1:
921
- _a.sent();
922
- return [2 /*return*/];
923
- }
924
- });
925
- }); });
926
- });
927
- (0, bun_test_1.describe)("AdminApp with scripts that use context", function () {
928
- var app;
929
- var adminAgent;
930
- (0, bun_test_1.afterEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
931
- var BackgroundTask;
932
- return __generator(this, function (_a) {
933
- switch (_a.label) {
934
- case 0: return [4 /*yield*/, Promise.resolve().then(function () { return __importStar(require("@terreno/api")); })];
935
- case 1:
936
- BackgroundTask = (_a.sent()).BackgroundTask;
937
- return [4 /*yield*/, BackgroundTask.deleteMany({})];
938
- case 2:
939
- _a.sent();
940
- return [2 /*return*/];
941
- }
942
- });
943
- }); });
944
- (0, bun_test_1.it)("reports progress, logs, and checks cancellation via ctx", function () { return __awaiter(void 0, void 0, void 0, function () {
945
- var BackgroundTask, appWithScript, admin, res, task;
946
- var _a;
947
- return __generator(this, function (_b) {
948
- switch (_b.label) {
949
- case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
950
- case 1:
951
- _b.sent();
952
- return [4 /*yield*/, Promise.resolve().then(function () { return __importStar(require("@terreno/api")); })];
953
- case 2:
954
- BackgroundTask = (_b.sent()).BackgroundTask;
955
- appWithScript = (0, tests_1.getBaseServer)();
956
- (0, api_1.setupAuth)(appWithScript, tests_1.UserModel);
957
- (0, api_1.addAuthRoutes)(appWithScript, tests_1.UserModel);
958
- admin = new adminApp_1.AdminApp({
959
- basePath: "/admin",
960
- models: [],
961
- scripts: [
962
- {
963
- description: "Exercises ctx",
964
- name: "ctx-script",
965
- runner: function (_wetRun, ctx) { return __awaiter(void 0, void 0, void 0, function () {
966
- return __generator(this, function (_a) {
967
- switch (_a.label) {
968
- case 0: return [4 /*yield*/, (ctx === null || ctx === void 0 ? void 0 : ctx.addLog("info", "hello"))];
969
- case 1:
970
- _a.sent();
971
- return [4 /*yield*/, (ctx === null || ctx === void 0 ? void 0 : ctx.updateProgress(50, "Halfway", "Processing"))];
972
- case 2:
973
- _a.sent();
974
- return [4 /*yield*/, (ctx === null || ctx === void 0 ? void 0 : ctx.checkCancellation())];
975
- case 3:
976
- _a.sent();
977
- return [2 /*return*/, { results: ["ok"], success: true }];
978
- }
979
- });
980
- }); },
981
- },
982
- ],
983
- });
984
- admin.register(appWithScript);
985
- appWithScript.use(api_1.apiUnauthorizedMiddleware);
986
- appWithScript.use(api_1.apiErrorMiddleware);
987
- app = appWithScript;
988
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
989
- case 3:
990
- adminAgent = _b.sent();
991
- return [4 /*yield*/, adminAgent.post("/admin/scripts/ctx-script/run").expect(201)];
992
- case 4:
993
- res = _b.sent();
994
- // Wait for script completion
995
- return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 150); })];
996
- case 5:
997
- // Wait for script completion
998
- _b.sent();
999
- return [4 /*yield*/, BackgroundTask.findById(res.body.taskId)];
1000
- case 6:
1001
- task = _b.sent();
1002
- (0, bun_test_1.expect)(task === null || task === void 0 ? void 0 : task.status).toBe("completed");
1003
- // addLog("info", "hello") should have persisted
1004
- (0, bun_test_1.expect)(task === null || task === void 0 ? void 0 : task.logs.some(function (l) { return l.message === "hello"; })).toBe(true);
1005
- (0, bun_test_1.expect)((_a = task === null || task === void 0 ? void 0 : task.progress) === null || _a === void 0 ? void 0 : _a.percentage).toBe(100);
1006
- return [2 /*return*/];
1007
- }
1008
- });
1009
- }); });
1010
- });
1011
- (0, bun_test_1.describe)("AdminApp admin UI v2 routes", function () {
1012
- var app;
1013
- var adminAgent;
1014
- var notAdminAgent;
1015
- (0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
1016
- return __generator(this, function (_a) {
1017
- switch (_a.label) {
1018
- case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
1019
- case 1:
1020
- _a.sent();
1021
- app = buildApp([__assign(__assign({}, foodModelConfig), { hiddenFields: ["hidden"] })]);
1022
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
1023
- case 2:
1024
- adminAgent = _a.sent();
1025
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "notAdmin")];
1026
- case 3:
1027
- notAdminAgent = _a.sent();
1028
- return [2 /*return*/];
1029
- }
1030
- });
1031
- }); });
1032
- (0, bun_test_1.afterEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
1033
- return __generator(this, function (_a) {
1034
- switch (_a.label) {
1035
- case 0: return [4 /*yield*/, tests_1.FoodModel.deleteMany({})];
1036
- case 1:
1037
- _a.sent();
1038
- return [4 /*yield*/, api_1.BackgroundTask.deleteMany({})];
1039
- case 2:
1040
- _a.sent();
1041
- return [2 /*return*/];
1042
- }
1043
- });
1044
- }); });
1045
- (0, bun_test_1.it)("rejects bulk-patch with an empty ids array", function () { return __awaiter(void 0, void 0, void 0, function () {
1046
- var res;
1047
- return __generator(this, function (_a) {
1048
- switch (_a.label) {
1049
- case 0: return [4 /*yield*/, adminAgent
1050
- .post("/admin/foods/bulk-patch")
1051
- .send({ ids: [], patch: { calories: 1 } })
1052
- .expect(400)];
1053
- case 1:
1054
- res = _a.sent();
1055
- (0, bun_test_1.expect)(res.body.title).toInclude("at least one");
1056
- return [2 /*return*/];
1057
- }
1058
- });
1059
- }); });
1060
- (0, bun_test_1.it)("bulk-patches allowlisted fields for many ids", function () { return __awaiter(void 0, void 0, void 0, function () {
1061
- var a, b, res, updated, updated_1, updated_1_1, row;
1062
- var e_3, _a;
1063
- return __generator(this, function (_b) {
1064
- switch (_b.label) {
1065
- case 0: return [4 /*yield*/, tests_1.FoodModel.create({ calories: 1, name: "A" })];
1066
- case 1:
1067
- a = _b.sent();
1068
- return [4 /*yield*/, tests_1.FoodModel.create({ calories: 2, name: "B" })];
1069
- case 2:
1070
- b = _b.sent();
1071
- return [4 /*yield*/, adminAgent
1072
- .post("/admin/foods/bulk-patch")
1073
- .send({
1074
- ids: [String(a._id), String(b._id)],
1075
- patch: { calories: 50 },
1076
- })
1077
- .expect(200)];
1078
- case 3:
1079
- res = _b.sent();
1080
- (0, bun_test_1.expect)(res.body.updated).toBe(2);
1081
- (0, bun_test_1.expect)(res.body.failures).toBeUndefined();
1082
- return [4 /*yield*/, tests_1.FoodModel.find({ _id: { $in: [a._id, b._id] } })
1083
- .lean()
1084
- .exec()];
1085
- case 4:
1086
- updated = _b.sent();
1087
- try {
1088
- for (updated_1 = __values(updated), updated_1_1 = updated_1.next(); !updated_1_1.done; updated_1_1 = updated_1.next()) {
1089
- row = updated_1_1.value;
1090
- (0, bun_test_1.expect)(row.calories).toBe(50);
1091
- }
1092
- }
1093
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
1094
- finally {
1095
- try {
1096
- if (updated_1_1 && !updated_1_1.done && (_a = updated_1.return)) _a.call(updated_1);
1097
- }
1098
- finally { if (e_3) throw e_3.error; }
1099
- }
1100
- return [2 /*return*/];
1101
- }
1102
- });
1103
- }); });
1104
- (0, bun_test_1.it)("rejects bulk-patch with more than 1000 ids", function () { return __awaiter(void 0, void 0, void 0, function () {
1105
- var ids, res;
1106
- return __generator(this, function (_a) {
1107
- switch (_a.label) {
1108
- case 0:
1109
- ids = Array.from({ length: 1001 }, function (_, index) { return index.toString(16).padStart(24, "0"); });
1110
- return [4 /*yield*/, adminAgent
1111
- .post("/admin/foods/bulk-patch")
1112
- .send({ ids: ids, patch: { calories: 1 } })
1113
- .expect(400)];
1114
- case 1:
1115
- res = _a.sent();
1116
- (0, bun_test_1.expect)(res.body.title).toInclude("1000");
1117
- return [2 /*return*/];
1118
- }
1119
- });
1120
- }); });
1121
- (0, bun_test_1.it)("rejects bulk-patch keys outside the allowlist", function () { return __awaiter(void 0, void 0, void 0, function () {
1122
- var a, res;
1123
- return __generator(this, function (_a) {
1124
- switch (_a.label) {
1125
- case 0: return [4 /*yield*/, tests_1.FoodModel.create({ calories: 1, name: "A" })];
1126
- case 1:
1127
- a = _a.sent();
1128
- return [4 /*yield*/, adminAgent
1129
- .post("/admin/foods/bulk-patch")
1130
- .send({
1131
- ids: [String(a._id)],
1132
- patch: { thisFieldIsNotAllowlisted: true },
1133
- })
1134
- .expect(400)];
1135
- case 2:
1136
- res = _a.sent();
1137
- (0, bun_test_1.expect)(res.body.title).toInclude("allowlisted");
1138
- return [2 /*return*/];
1139
- }
1140
- });
1141
- }); });
1142
- (0, bun_test_1.it)("enqueues a background task and returns taskId", function () { return __awaiter(void 0, void 0, void 0, function () {
1143
- var res, task;
1144
- return __generator(this, function (_a) {
1145
- switch (_a.label) {
1146
- case 0: return [4 /*yield*/, adminAgent
1147
- .post("/admin/background-tasks")
1148
- .send({ kind: "reindex-search", metadata: { scope: "foods" } })
1149
- .expect(201)];
1150
- case 1:
1151
- res = _a.sent();
1152
- (0, bun_test_1.expect)(typeof res.body.taskId).toBe("string");
1153
- return [4 /*yield*/, api_1.BackgroundTask.findById(res.body.taskId)];
1154
- case 2:
1155
- task = _a.sent();
1156
- (0, bun_test_1.expect)(task === null || task === void 0 ? void 0 : task.taskType).toBe("reindex-search");
1157
- (0, bun_test_1.expect)(task === null || task === void 0 ? void 0 : task.status).toBe("pending");
1158
- return [2 /*return*/];
1159
- }
1160
- });
1161
- }); });
1162
- (0, bun_test_1.it)("returns 403 for background-tasks when not admin", function () { return __awaiter(void 0, void 0, void 0, function () {
1163
- return __generator(this, function (_a) {
1164
- switch (_a.label) {
1165
- case 0: return [4 /*yield*/, notAdminAgent.post("/admin/background-tasks").send({ kind: "x" }).expect(403)];
1166
- case 1:
1167
- _a.sent();
1168
- return [2 /*return*/];
1169
- }
1170
- });
1171
- }); });
1172
- (0, bun_test_1.it)("strips readonly fields from PATCH updates", function () { return __awaiter(void 0, void 0, void 0, function () {
1173
- var agent, food, reRead;
1174
- return __generator(this, function (_a) {
1175
- switch (_a.label) {
1176
- case 0:
1177
- app = buildApp([
1178
- __assign(__assign({}, foodModelConfig), { readonlyFields: ["name"] }),
1179
- ]);
1180
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
1181
- case 1:
1182
- agent = _a.sent();
1183
- return [4 /*yield*/, tests_1.FoodModel.create({ calories: 10, name: "KeepMe" })];
1184
- case 2:
1185
- food = _a.sent();
1186
- return [4 /*yield*/, agent.patch("/admin/foods/".concat(food._id)).send({ calories: 99, name: "Changed" }).expect(200)];
1187
- case 3:
1188
- _a.sent();
1189
- return [4 /*yield*/, tests_1.FoodModel.findById(food._id).lean()];
1190
- case 4:
1191
- reRead = _a.sent();
1192
- (0, bun_test_1.expect)(reRead === null || reRead === void 0 ? void 0 : reRead.name).toBe("KeepMe");
1193
- (0, bun_test_1.expect)(reRead === null || reRead === void 0 ? void 0 : reRead.calories).toBe(99);
1194
- return [2 /*return*/];
1195
- }
1196
- });
1197
- }); });
1198
- (0, bun_test_1.it)("strips readonly fields from POST create", function () { return __awaiter(void 0, void 0, void 0, function () {
1199
- var agent, res, stored;
1200
- return __generator(this, function (_a) {
1201
- switch (_a.label) {
1202
- case 0:
1203
- app = buildApp([
1204
- __assign(__assign({}, foodModelConfig), { readonlyFields: ["name"] }),
1205
- ]);
1206
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
1207
- case 1:
1208
- agent = _a.sent();
1209
- return [4 /*yield*/, agent
1210
- .post("/admin/foods")
1211
- .send({ calories: 5, name: "IgnoredName" })
1212
- .expect(201)];
1213
- case 2:
1214
- res = _a.sent();
1215
- (0, bun_test_1.expect)(res.body.data.name).toBeUndefined();
1216
- (0, bun_test_1.expect)(res.body.data.calories).toBe(5);
1217
- return [4 /*yield*/, tests_1.FoodModel.findById(res.body.data._id).lean()];
1218
- case 3:
1219
- stored = _a.sent();
1220
- (0, bun_test_1.expect)(stored === null || stored === void 0 ? void 0 : stored.name).toBeUndefined();
1221
- (0, bun_test_1.expect)(stored === null || stored === void 0 ? void 0 : stored.calories).toBe(5);
1222
- return [2 /*return*/];
1223
- }
1224
- });
1225
- }); });
1226
- (0, bun_test_1.it)("disables DELETE when permissions.delete is false", function () { return __awaiter(void 0, void 0, void 0, function () {
1227
- var agent, food, res;
1228
- return __generator(this, function (_a) {
1229
- switch (_a.label) {
1230
- case 0:
1231
- app = buildApp([
1232
- __assign(__assign({}, foodModelConfig), { permissions: { delete: false } }),
1233
- ]);
1234
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
1235
- case 1:
1236
- agent = _a.sent();
1237
- return [4 /*yield*/, tests_1.FoodModel.create({ calories: 1, name: "Nope" })];
1238
- case 2:
1239
- food = _a.sent();
1240
- return [4 /*yield*/, agent.delete("/admin/foods/".concat(food._id))];
1241
- case 3:
1242
- res = _a.sent();
1243
- (0, bun_test_1.expect)([403, 405]).toContain(res.status);
1244
- return [2 /*return*/];
1245
- }
1246
- });
1247
- }); });
1248
- });
1249
- (0, bun_test_1.describe)("AdminApp onAdminAudit", function () {
1250
- var app;
1251
- var adminAgent;
1252
- var auditEvents = [];
1253
- (0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
1254
- return __generator(this, function (_a) {
1255
- switch (_a.label) {
1256
- case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
1257
- case 1:
1258
- _a.sent();
1259
- auditEvents.length = 0;
1260
- app = buildApp([__assign(__assign({}, foodModelConfig), { hiddenFields: ["hidden"] })], {
1261
- onAdminAudit: function (event) { return __awaiter(void 0, void 0, void 0, function () {
1262
- return __generator(this, function (_a) {
1263
- auditEvents.push(event);
1264
- return [2 /*return*/];
1265
- });
1266
- }); },
1267
- });
1268
- return [4 /*yield*/, (0, tests_1.authAsUser)(app, "admin")];
1269
- case 2:
1270
- adminAgent = _a.sent();
1271
- return [2 /*return*/];
1272
- }
1273
- });
1274
- }); });
1275
- (0, bun_test_1.afterEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
1276
- return __generator(this, function (_a) {
1277
- switch (_a.label) {
1278
- case 0: return [4 /*yield*/, tests_1.FoodModel.deleteMany({})];
1279
- case 1:
1280
- _a.sent();
1281
- return [2 /*return*/];
1282
- }
1283
- });
1284
- }); });
1285
- (0, bun_test_1.it)("invokes onAdminAudit with verb created after POST", function () { return __awaiter(void 0, void 0, void 0, function () {
1286
- return __generator(this, function (_a) {
1287
- switch (_a.label) {
1288
- case 0: return [4 /*yield*/, adminAgent.post("/admin/foods").send({ calories: 5, name: "Audited" }).expect(201)];
1289
- case 1:
1290
- _a.sent();
1291
- (0, bun_test_1.expect)(auditEvents.some(function (e) { return e.verb === "created" && e.modelName === "Food"; })).toBe(true);
1292
- return [2 /*return*/];
1293
- }
1294
- });
1295
- }); });
1296
- (0, bun_test_1.it)("invokes onAdminAudit with verb updated after PATCH", function () { return __awaiter(void 0, void 0, void 0, function () {
1297
- var food;
1298
- return __generator(this, function (_a) {
1299
- switch (_a.label) {
1300
- case 0: return [4 /*yield*/, tests_1.FoodModel.create({ calories: 10, name: "PatchMe" })];
1301
- case 1:
1302
- food = _a.sent();
1303
- return [4 /*yield*/, adminAgent.patch("/admin/foods/".concat(food._id)).send({ calories: 42 }).expect(200)];
1304
- case 2:
1305
- _a.sent();
1306
- (0, bun_test_1.expect)(auditEvents.some(function (e) { return e.verb === "updated" && e.modelName === "Food"; })).toBe(true);
1307
- return [2 /*return*/];
1308
- }
1309
- });
1310
- }); });
1311
- (0, bun_test_1.it)("invokes onAdminAudit with verb deleted after DELETE", function () { return __awaiter(void 0, void 0, void 0, function () {
1312
- var food;
1313
- return __generator(this, function (_a) {
1314
- switch (_a.label) {
1315
- case 0: return [4 /*yield*/, tests_1.FoodModel.create({ calories: 1, name: "DeleteMe" })];
1316
- case 1:
1317
- food = _a.sent();
1318
- return [4 /*yield*/, adminAgent.delete("/admin/foods/".concat(food._id)).expect(204)];
1319
- case 2:
1320
- _a.sent();
1321
- (0, bun_test_1.expect)(auditEvents.some(function (e) { return e.verb === "deleted" && e.modelName === "Food"; })).toBe(true);
1322
- return [2 /*return*/];
1323
- }
1324
- });
1325
- }); });
1326
- });
1327
- (0, bun_test_1.describe)("AdminApp onAdminAudit is best-effort", function () {
1328
- (0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
1329
- return __generator(this, function (_a) {
1330
- switch (_a.label) {
1331
- case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
1332
- case 1:
1333
- _a.sent();
1334
- return [2 /*return*/];
1335
- }
1336
- });
1337
- }); });
1338
- (0, bun_test_1.afterEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
1339
- return __generator(this, function (_a) {
1340
- switch (_a.label) {
1341
- case 0: return [4 /*yield*/, tests_1.FoodModel.deleteMany({})];
1342
- case 1:
1343
- _a.sent();
1344
- return [2 /*return*/];
1345
- }
1346
- });
1347
- }); });
1348
- var throwingAudit = {
1349
- onAdminAudit: function () { return __awaiter(void 0, void 0, void 0, function () {
1350
- return __generator(this, function (_a) {
1351
- throw new Error("audit sink failure");
1352
- });
1353
- }); },
1354
- };
1355
- (0, bun_test_1.it)("returns 201 on POST when onAdminAudit throws", function () { return __awaiter(void 0, void 0, void 0, function () {
1356
- var localApp, agent, res, stored;
1357
- return __generator(this, function (_a) {
1358
- switch (_a.label) {
1359
- case 0:
1360
- localApp = buildApp([foodModelConfig], throwingAudit);
1361
- return [4 /*yield*/, (0, tests_1.authAsUser)(localApp, "admin")];
1362
- case 1:
1363
- agent = _a.sent();
1364
- return [4 /*yield*/, agent
1365
- .post("/admin/foods")
1366
- .send({ calories: 5, name: "StillCreated" })
1367
- .expect(201)];
1368
- case 2:
1369
- res = _a.sent();
1370
- return [4 /*yield*/, tests_1.FoodModel.findById(res.body.data._id).lean()];
1371
- case 3:
1372
- stored = _a.sent();
1373
- (0, bun_test_1.expect)(stored === null || stored === void 0 ? void 0 : stored.name).toBe("StillCreated");
1374
- return [2 /*return*/];
1375
- }
1376
- });
1377
- }); });
1378
- (0, bun_test_1.it)("returns 200 on PATCH when onAdminAudit throws", function () { return __awaiter(void 0, void 0, void 0, function () {
1379
- var localApp, agent, food, res, stored;
1380
- return __generator(this, function (_a) {
1381
- switch (_a.label) {
1382
- case 0:
1383
- localApp = buildApp([foodModelConfig], throwingAudit);
1384
- return [4 /*yield*/, (0, tests_1.authAsUser)(localApp, "admin")];
1385
- case 1:
1386
- agent = _a.sent();
1387
- return [4 /*yield*/, tests_1.FoodModel.create({ calories: 10, name: "PatchMe" })];
1388
- case 2:
1389
- food = _a.sent();
1390
- return [4 /*yield*/, agent.patch("/admin/foods/".concat(food._id)).send({ calories: 99 }).expect(200)];
1391
- case 3:
1392
- res = _a.sent();
1393
- (0, bun_test_1.expect)(res.body.data.calories).toBe(99);
1394
- return [4 /*yield*/, tests_1.FoodModel.findById(food._id).lean()];
1395
- case 4:
1396
- stored = _a.sent();
1397
- (0, bun_test_1.expect)(stored === null || stored === void 0 ? void 0 : stored.calories).toBe(99);
1398
- return [2 /*return*/];
1399
- }
1400
- });
1401
- }); });
1402
- (0, bun_test_1.it)("returns 204 on DELETE when onAdminAudit throws", function () { return __awaiter(void 0, void 0, void 0, function () {
1403
- var localApp, agent, food, _a;
1404
- return __generator(this, function (_b) {
1405
- switch (_b.label) {
1406
- case 0:
1407
- localApp = buildApp([foodModelConfig], throwingAudit);
1408
- return [4 /*yield*/, (0, tests_1.authAsUser)(localApp, "admin")];
1409
- case 1:
1410
- agent = _b.sent();
1411
- return [4 /*yield*/, tests_1.FoodModel.create({ calories: 1, name: "DeleteMe" })];
1412
- case 2:
1413
- food = _b.sent();
1414
- return [4 /*yield*/, agent.delete("/admin/foods/".concat(food._id)).expect(204)];
1415
- case 3:
1416
- _b.sent();
1417
- _a = bun_test_1.expect;
1418
- return [4 /*yield*/, tests_1.FoodModel.findById(food._id)];
1419
- case 4:
1420
- _a.apply(void 0, [_b.sent()]).toBeNull();
1421
- return [2 /*return*/];
1422
- }
1423
- });
1424
- }); });
1425
- });
1426
- (0, bun_test_1.describe)("AdminApp per-model queryFilter", function () {
1427
- (0, bun_test_1.beforeEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
1428
- return __generator(this, function (_a) {
1429
- switch (_a.label) {
1430
- case 0: return [4 /*yield*/, (0, tests_1.setupDb)()];
1431
- case 1:
1432
- _a.sent();
1433
- return [2 /*return*/];
1434
- }
1435
- });
1436
- }); });
1437
- (0, bun_test_1.afterEach)(function () { return __awaiter(void 0, void 0, void 0, function () {
1438
- return __generator(this, function (_a) {
1439
- switch (_a.label) {
1440
- case 0: return [4 /*yield*/, tests_1.FoodModel.deleteMany({})];
1441
- case 1:
1442
- _a.sent();
1443
- return [4 /*yield*/, api_1.VersionConfig.deleteMany({})];
1444
- case 2:
1445
- _a.sent();
1446
- return [2 /*return*/];
1447
- }
1448
- });
1449
- }); });
1450
- (0, bun_test_1.it)("returns an empty list when queryFilter returns null", function () { return __awaiter(void 0, void 0, void 0, function () {
1451
- var localApp, agent, res;
1452
- return __generator(this, function (_a) {
1453
- switch (_a.label) {
1454
- case 0:
1455
- localApp = buildApp([
1456
- __assign(__assign({}, foodModelConfig), { queryFilter: function () { return null; } }),
1457
- ]);
1458
- return [4 /*yield*/, tests_1.FoodModel.create({ calories: 1, name: "HiddenRow" })];
1459
- case 1:
1460
- _a.sent();
1461
- return [4 /*yield*/, (0, tests_1.authAsUser)(localApp, "admin")];
1462
- case 2:
1463
- agent = _a.sent();
1464
- return [4 /*yield*/, agent.get("/admin/foods").expect(200)];
1465
- case 3:
1466
- res = _a.sent();
1467
- (0, bun_test_1.expect)(res.body.data).toEqual([]);
1468
- return [2 /*return*/];
1469
- }
1470
- });
1471
- }); });
1472
- (0, bun_test_1.it)("merges queryFilter constraints into list queries for that model only", function () { return __awaiter(void 0, void 0, void 0, function () {
1473
- var localApp, agent, res;
1474
- return __generator(this, function (_a) {
1475
- switch (_a.label) {
1476
- case 0:
1477
- localApp = buildApp([
1478
- __assign(__assign({}, foodModelConfig), { queryFilter: function (_user, query) { return (__assign(__assign({}, (query !== null && query !== void 0 ? query : {})), { name: "FilteredOnly" })); } }),
1479
- ]);
1480
- return [4 /*yield*/, tests_1.FoodModel.create({ calories: 1, name: "Other" })];
1481
- case 1:
1482
- _a.sent();
1483
- return [4 /*yield*/, tests_1.FoodModel.create({ calories: 2, name: "FilteredOnly" })];
1484
- case 2:
1485
- _a.sent();
1486
- return [4 /*yield*/, (0, tests_1.authAsUser)(localApp, "admin")];
1487
- case 3:
1488
- agent = _a.sent();
1489
- return [4 /*yield*/, agent.get("/admin/foods").expect(200)];
1490
- case 4:
1491
- res = _a.sent();
1492
- (0, bun_test_1.expect)(res.body.data).toHaveLength(1);
1493
- (0, bun_test_1.expect)(res.body.data[0].name).toBe("FilteredOnly");
1494
- return [2 /*return*/];
1495
- }
1496
- });
1497
- }); });
1498
- });