@terreno/admin-backend 0.10.0 → 0.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bunfig.toml +5 -0
- package/dist/adminApp.js +50 -11
- package/dist/adminApp.models.test.d.ts +1 -0
- package/dist/adminApp.models.test.js +969 -0
- package/dist/adminApp.test.js +9 -4
- package/dist/documentStorageApp.js +44 -27
- package/dist/documentStorageApp.test.d.ts +1 -0
- package/dist/documentStorageApp.test.js +874 -0
- package/package.json +4 -3
- package/src/adminApp.models.test.ts +479 -0
- package/src/adminApp.test.ts +15 -6
- package/src/adminApp.ts +101 -16
- package/src/documentStorageApp.test.ts +540 -0
- package/src/documentStorageApp.ts +55 -30
package/package.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
},
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"@google-cloud/storage": "^7.15.0",
|
|
8
|
-
"@terreno/api": "0.
|
|
8
|
+
"@terreno/api": "0.11.1",
|
|
9
9
|
"express": "^5.2.1",
|
|
10
10
|
"luxon": "^3.7.2",
|
|
11
11
|
"multer": "^1.4.5-lts.1"
|
|
@@ -49,8 +49,9 @@
|
|
|
49
49
|
"lint": "biome check ./src",
|
|
50
50
|
"lint:fix": "biome check --write ./src",
|
|
51
51
|
"test": "bun test",
|
|
52
|
-
"test:ci": "bun test"
|
|
52
|
+
"test:ci": "bun test",
|
|
53
|
+
"test:coverage": "bun run ../scripts/check-coverage.ts"
|
|
53
54
|
},
|
|
54
55
|
"types": "dist/index.d.ts",
|
|
55
|
-
"version": "0.
|
|
56
|
+
"version": "0.11.1"
|
|
56
57
|
}
|
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
import {afterEach, beforeEach, describe, expect, it} from "bun:test";
|
|
2
|
+
import {
|
|
3
|
+
addAuthRoutes,
|
|
4
|
+
apiErrorMiddleware,
|
|
5
|
+
apiUnauthorizedMiddleware,
|
|
6
|
+
setupAuth,
|
|
7
|
+
type UserModel as UserModelType,
|
|
8
|
+
VersionConfig,
|
|
9
|
+
} from "@terreno/api";
|
|
10
|
+
import {
|
|
11
|
+
authAsUser,
|
|
12
|
+
FoodModel,
|
|
13
|
+
getBaseServer,
|
|
14
|
+
RequiredModel,
|
|
15
|
+
setupDb,
|
|
16
|
+
UserModel,
|
|
17
|
+
} from "@terreno/api/src/tests";
|
|
18
|
+
import type express from "express";
|
|
19
|
+
import supertest from "supertest";
|
|
20
|
+
import type TestAgent from "supertest/lib/agent";
|
|
21
|
+
|
|
22
|
+
import type {AdminModelConfig} from "./adminApp";
|
|
23
|
+
import {AdminApp} from "./adminApp";
|
|
24
|
+
|
|
25
|
+
const buildApp = (models: AdminModelConfig[] = []): express.Application => {
|
|
26
|
+
const app = getBaseServer();
|
|
27
|
+
setupAuth(app, UserModel as unknown as UserModelType);
|
|
28
|
+
addAuthRoutes(app, UserModel as unknown as UserModelType);
|
|
29
|
+
|
|
30
|
+
const admin = new AdminApp({
|
|
31
|
+
basePath: "/admin",
|
|
32
|
+
models,
|
|
33
|
+
});
|
|
34
|
+
admin.register(app);
|
|
35
|
+
|
|
36
|
+
app.use(apiUnauthorizedMiddleware);
|
|
37
|
+
app.use(apiErrorMiddleware);
|
|
38
|
+
|
|
39
|
+
return app;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const foodModelConfig: AdminModelConfig = {
|
|
43
|
+
displayName: "Foods",
|
|
44
|
+
listFields: ["name", "calories"],
|
|
45
|
+
model: FoodModel,
|
|
46
|
+
routePath: "/foods",
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
describe("AdminApp /admin/config", () => {
|
|
50
|
+
let app: express.Application;
|
|
51
|
+
let adminAgent: TestAgent;
|
|
52
|
+
let notAdminAgent: TestAgent;
|
|
53
|
+
|
|
54
|
+
beforeEach(async () => {
|
|
55
|
+
await setupDb();
|
|
56
|
+
app = buildApp([
|
|
57
|
+
{
|
|
58
|
+
...foodModelConfig,
|
|
59
|
+
fieldOrder: ["name", "calories", "tags"],
|
|
60
|
+
fieldOverrides: {name: {widget: "markdown"}},
|
|
61
|
+
hiddenFields: ["hidden"],
|
|
62
|
+
},
|
|
63
|
+
]);
|
|
64
|
+
adminAgent = await authAsUser(app, "admin");
|
|
65
|
+
notAdminAgent = await authAsUser(app, "notAdmin");
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
afterEach(async () => {
|
|
69
|
+
await FoodModel.deleteMany({});
|
|
70
|
+
await VersionConfig.deleteMany({});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("returns metadata for configured models to admins", async () => {
|
|
74
|
+
const res = await adminAgent.get("/admin/config").expect(200);
|
|
75
|
+
|
|
76
|
+
expect(res.body.models).toHaveLength(1);
|
|
77
|
+
const [foodMeta] = res.body.models;
|
|
78
|
+
expect(foodMeta.name).toBe("Food");
|
|
79
|
+
expect(foodMeta.displayName).toBe("Foods");
|
|
80
|
+
expect(foodMeta.listFields).toEqual(["name", "calories"]);
|
|
81
|
+
expect(foodMeta.routePath).toBe("/admin/foods");
|
|
82
|
+
expect(foodMeta.defaultSort).toBe("-created");
|
|
83
|
+
expect(foodMeta.fieldOrder).toEqual(["name", "calories", "tags"]);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("applies fieldOverrides to generated config", async () => {
|
|
87
|
+
const res = await adminAgent.get("/admin/config").expect(200);
|
|
88
|
+
const [foodMeta] = res.body.models;
|
|
89
|
+
expect(foodMeta.fields.name.widget).toBe("markdown");
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("removes hidden fields from config and listFields", async () => {
|
|
93
|
+
app = buildApp([
|
|
94
|
+
{
|
|
95
|
+
...foodModelConfig,
|
|
96
|
+
hiddenFields: ["hidden"],
|
|
97
|
+
listFields: ["name", "hidden", "calories"],
|
|
98
|
+
},
|
|
99
|
+
]);
|
|
100
|
+
const agent = await authAsUser(app, "admin");
|
|
101
|
+
const res = await agent.get("/admin/config").expect(200);
|
|
102
|
+
const [foodMeta] = res.body.models;
|
|
103
|
+
expect(foodMeta.fields.hidden).toBeUndefined();
|
|
104
|
+
expect(foodMeta.listFields).toEqual(["name", "calories"]);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("extracts item sub-field metadata for array fields", async () => {
|
|
108
|
+
const res = await adminAgent.get("/admin/config").expect(200);
|
|
109
|
+
const [foodMeta] = res.body.models;
|
|
110
|
+
// likesIds is an array of subdocuments with likes + userId sub-fields
|
|
111
|
+
expect(foodMeta.fields.likesIds.type).toBe("array");
|
|
112
|
+
expect(foodMeta.fields.likesIds.items).toBeDefined();
|
|
113
|
+
expect(foodMeta.fields.likesIds.items.userId).toBeDefined();
|
|
114
|
+
expect(foodMeta.fields.likesIds.items.likes).toBeDefined();
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("extracts ref information for ObjectId fields", async () => {
|
|
118
|
+
const res = await adminAgent.get("/admin/config").expect(200);
|
|
119
|
+
const [foodMeta] = res.body.models;
|
|
120
|
+
expect(foodMeta.fields.ownerId.ref).toBe("User");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("extracts ref information for array of ObjectId references", async () => {
|
|
124
|
+
const res = await adminAgent.get("/admin/config").expect(200);
|
|
125
|
+
const [foodMeta] = res.body.models;
|
|
126
|
+
expect(foodMeta.fields.eatenBy.ref).toBe("User");
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("extracts itemType for primitive string arrays", async () => {
|
|
130
|
+
const res = await adminAgent.get("/admin/config").expect(200);
|
|
131
|
+
const [foodMeta] = res.body.models;
|
|
132
|
+
// tags is [String] — should expose itemType so the frontend renders a primitive list
|
|
133
|
+
expect(foodMeta.fields.tags.type).toBe("array");
|
|
134
|
+
expect(foodMeta.fields.tags.itemType).toBe("string");
|
|
135
|
+
expect(foodMeta.fields.tags.items).toBeUndefined();
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("extracts itemType and itemRef for arrays of ObjectId references", async () => {
|
|
139
|
+
const res = await adminAgent.get("/admin/config").expect(200);
|
|
140
|
+
const [foodMeta] = res.body.models;
|
|
141
|
+
// eatenBy is [{type: ObjectId, ref: "User"}] — should expose both itemType and itemRef
|
|
142
|
+
expect(foodMeta.fields.eatenBy.type).toBe("array");
|
|
143
|
+
expect(foodMeta.fields.eatenBy.itemType).toBe("objectid");
|
|
144
|
+
expect(foodMeta.fields.eatenBy.itemRef).toBe("User");
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("returns 403 for non-admin users", async () => {
|
|
148
|
+
const res = await notAdminAgent.get("/admin/config").expect(403);
|
|
149
|
+
expect(res.body.title).toInclude("Admin access required");
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("returns 401 for unauthenticated users", async () => {
|
|
153
|
+
await supertest(app).get("/admin/config").expect(401);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("defaults basePath to /admin when not provided", async () => {
|
|
157
|
+
const altApp = getBaseServer();
|
|
158
|
+
setupAuth(altApp, UserModel as unknown as UserModelType);
|
|
159
|
+
addAuthRoutes(altApp, UserModel as unknown as UserModelType);
|
|
160
|
+
new AdminApp({models: [foodModelConfig]}).register(altApp);
|
|
161
|
+
altApp.use(apiUnauthorizedMiddleware);
|
|
162
|
+
altApp.use(apiErrorMiddleware);
|
|
163
|
+
|
|
164
|
+
const agent = await authAsUser(altApp, "admin");
|
|
165
|
+
const res = await agent.get("/admin/config").expect(200);
|
|
166
|
+
expect(res.body.models[0].routePath).toBe("/admin/foods");
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("uses a custom defaultSort when provided", async () => {
|
|
170
|
+
app = buildApp([{...foodModelConfig, defaultSort: "name"}]);
|
|
171
|
+
const agent = await authAsUser(app, "admin");
|
|
172
|
+
const res = await agent.get("/admin/config").expect(200);
|
|
173
|
+
expect(res.body.models[0].defaultSort).toBe("name");
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("includes version-config custom screen", async () => {
|
|
177
|
+
const res = await adminAgent.get("/admin/config").expect(200);
|
|
178
|
+
expect(res.body.customScreens).toEqual([
|
|
179
|
+
{displayName: "Version Config", name: "version-config"},
|
|
180
|
+
]);
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
describe("AdminApp model CRUD routes", () => {
|
|
185
|
+
let app: express.Application;
|
|
186
|
+
let adminAgent: TestAgent;
|
|
187
|
+
let notAdminAgent: TestAgent;
|
|
188
|
+
|
|
189
|
+
beforeEach(async () => {
|
|
190
|
+
await setupDb();
|
|
191
|
+
app = buildApp([{...foodModelConfig, hiddenFields: ["hidden"]}]);
|
|
192
|
+
adminAgent = await authAsUser(app, "admin");
|
|
193
|
+
notAdminAgent = await authAsUser(app, "notAdmin");
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
afterEach(async () => {
|
|
197
|
+
await FoodModel.deleteMany({});
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("creates documents via POST and strips hidden fields from response", async () => {
|
|
201
|
+
const res = await adminAgent
|
|
202
|
+
.post("/admin/foods")
|
|
203
|
+
.send({calories: 120, hidden: true, name: "Apple"})
|
|
204
|
+
.expect(201);
|
|
205
|
+
|
|
206
|
+
expect(res.body.data.name).toBe("Apple");
|
|
207
|
+
expect(res.body.data.calories).toBe(120);
|
|
208
|
+
expect(res.body.data.hidden).toBeUndefined();
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it("lists documents via GET and strips hidden fields from results", async () => {
|
|
212
|
+
await FoodModel.create({calories: 120, hidden: true, name: "Apple"});
|
|
213
|
+
await FoodModel.create({calories: 95, hidden: false, name: "Banana"});
|
|
214
|
+
|
|
215
|
+
const res = await adminAgent.get("/admin/foods").expect(200);
|
|
216
|
+
expect(res.body.data).toHaveLength(2);
|
|
217
|
+
for (const item of res.body.data) {
|
|
218
|
+
expect(item.hidden).toBeUndefined();
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it("reads a document via GET /:id", async () => {
|
|
223
|
+
const food = await FoodModel.create({calories: 120, name: "Apple"});
|
|
224
|
+
const res = await adminAgent.get(`/admin/foods/${food._id}`).expect(200);
|
|
225
|
+
expect(res.body.data.name).toBe("Apple");
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("updates a document via PATCH /:id", async () => {
|
|
229
|
+
const food = await FoodModel.create({calories: 120, name: "Apple"});
|
|
230
|
+
const res = await adminAgent
|
|
231
|
+
.patch(`/admin/foods/${food._id}`)
|
|
232
|
+
.send({calories: 150})
|
|
233
|
+
.expect(200);
|
|
234
|
+
expect(res.body.data.calories).toBe(150);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it("deletes a document via DELETE /:id", async () => {
|
|
238
|
+
const food = await FoodModel.create({calories: 120, name: "Apple"});
|
|
239
|
+
await adminAgent.delete(`/admin/foods/${food._id}`).expect(204);
|
|
240
|
+
expect(await FoodModel.findById(food._id)).toBeNull();
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it("rejects non-admin users from model list", async () => {
|
|
244
|
+
const res = await notAdminAgent.get("/admin/foods");
|
|
245
|
+
// modelRouter with IsAdmin permission blocks non-admin users
|
|
246
|
+
expect([401, 403, 405]).toContain(res.status);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it("supports models without hiddenFields (no responseHandler installed)", async () => {
|
|
250
|
+
app = buildApp([foodModelConfig]);
|
|
251
|
+
const agent = await authAsUser(app, "admin");
|
|
252
|
+
await FoodModel.create({calories: 120, hidden: true, name: "Apple"});
|
|
253
|
+
const res = await agent.get("/admin/foods").expect(200);
|
|
254
|
+
expect(res.body.data[0].hidden).toBe(true);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it("recursively removes hidden fields from array item values", async () => {
|
|
258
|
+
await RequiredModel.deleteMany({});
|
|
259
|
+
app = buildApp([
|
|
260
|
+
{
|
|
261
|
+
displayName: "Required",
|
|
262
|
+
hiddenFields: ["about"],
|
|
263
|
+
listFields: ["name"],
|
|
264
|
+
model: RequiredModel,
|
|
265
|
+
routePath: "/required",
|
|
266
|
+
},
|
|
267
|
+
]);
|
|
268
|
+
const agent = await authAsUser(app, "admin");
|
|
269
|
+
await RequiredModel.create({about: "secret", name: "first"});
|
|
270
|
+
await RequiredModel.create({about: "secret2", name: "second"});
|
|
271
|
+
const res = await agent.get("/admin/required").expect(200);
|
|
272
|
+
expect(res.body.data).toHaveLength(2);
|
|
273
|
+
for (const doc of res.body.data) {
|
|
274
|
+
expect(doc.about).toBeUndefined();
|
|
275
|
+
expect(doc.name).toBeDefined();
|
|
276
|
+
}
|
|
277
|
+
await RequiredModel.deleteMany({});
|
|
278
|
+
});
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
describe("AdminApp /admin/version-config", () => {
|
|
282
|
+
let app: express.Application;
|
|
283
|
+
let adminAgent: TestAgent;
|
|
284
|
+
let notAdminAgent: TestAgent;
|
|
285
|
+
|
|
286
|
+
beforeEach(async () => {
|
|
287
|
+
await setupDb();
|
|
288
|
+
await VersionConfig.deleteMany({});
|
|
289
|
+
app = buildApp([]);
|
|
290
|
+
adminAgent = await authAsUser(app, "admin");
|
|
291
|
+
notAdminAgent = await authAsUser(app, "notAdmin");
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
afterEach(async () => {
|
|
295
|
+
await VersionConfig.deleteMany({});
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it("returns default values when no config exists", async () => {
|
|
299
|
+
const res = await adminAgent.get("/admin/version-config").expect(200);
|
|
300
|
+
expect(res.body.mobileRequiredVersion).toBe(0);
|
|
301
|
+
expect(res.body.webWarningVersion).toBe(0);
|
|
302
|
+
expect(res.body.requiredMessage).toInclude("update");
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it("returns the stored config when present", async () => {
|
|
306
|
+
await VersionConfig.create({
|
|
307
|
+
_singleton: "config",
|
|
308
|
+
mobileRequiredVersion: 5,
|
|
309
|
+
mobileWarningVersion: 3,
|
|
310
|
+
updateUrl: "https://example.com/update",
|
|
311
|
+
});
|
|
312
|
+
const res = await adminAgent.get("/admin/version-config").expect(200);
|
|
313
|
+
expect(res.body.mobileRequiredVersion).toBe(5);
|
|
314
|
+
expect(res.body.mobileWarningVersion).toBe(3);
|
|
315
|
+
expect(res.body.updateUrl).toBe("https://example.com/update");
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it("creates or updates the config via PUT", async () => {
|
|
319
|
+
const res = await adminAgent
|
|
320
|
+
.put("/admin/version-config")
|
|
321
|
+
.send({
|
|
322
|
+
mobileRequiredVersion: 10,
|
|
323
|
+
mobileWarningVersion: 8,
|
|
324
|
+
updateUrl: "https://example.com/upgrade",
|
|
325
|
+
})
|
|
326
|
+
.expect(200);
|
|
327
|
+
expect(res.body.mobileRequiredVersion).toBe(10);
|
|
328
|
+
expect(res.body.updateUrl).toBe("https://example.com/upgrade");
|
|
329
|
+
|
|
330
|
+
const stored = await VersionConfig.findOne({_singleton: "config"});
|
|
331
|
+
expect(stored?.mobileRequiredVersion).toBe(10);
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it("unsets fields passed as null via PUT", async () => {
|
|
335
|
+
await adminAgent
|
|
336
|
+
.put("/admin/version-config")
|
|
337
|
+
.send({updateUrl: "https://example.com/upgrade"})
|
|
338
|
+
.expect(200);
|
|
339
|
+
|
|
340
|
+
const res = await adminAgent.put("/admin/version-config").send({updateUrl: null}).expect(200);
|
|
341
|
+
|
|
342
|
+
expect(res.body.updateUrl).toBeUndefined();
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
it("ignores unknown fields via PUT", async () => {
|
|
346
|
+
const res = await adminAgent
|
|
347
|
+
.put("/admin/version-config")
|
|
348
|
+
.send({bogusField: "nope", mobileRequiredVersion: 2})
|
|
349
|
+
.expect(200);
|
|
350
|
+
expect(res.body.mobileRequiredVersion).toBe(2);
|
|
351
|
+
expect(res.body.bogusField).toBeUndefined();
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
it("returns 403 for non-admins (GET)", async () => {
|
|
355
|
+
await notAdminAgent.get("/admin/version-config").expect(403);
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
it("returns 403 for non-admins (PUT)", async () => {
|
|
359
|
+
await notAdminAgent.put("/admin/version-config").send({}).expect(403);
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
it("returns 401 for unauthenticated users", async () => {
|
|
363
|
+
await supertest(app).get("/admin/version-config").expect(401);
|
|
364
|
+
});
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
describe("AdminApp search route", () => {
|
|
368
|
+
let app: express.Application;
|
|
369
|
+
let adminAgent: TestAgent;
|
|
370
|
+
let notAdminAgent: TestAgent;
|
|
371
|
+
|
|
372
|
+
beforeEach(async () => {
|
|
373
|
+
await setupDb();
|
|
374
|
+
app = buildApp([foodModelConfig]);
|
|
375
|
+
adminAgent = await authAsUser(app, "admin");
|
|
376
|
+
notAdminAgent = await authAsUser(app, "notAdmin");
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
afterEach(async () => {
|
|
380
|
+
await FoodModel.deleteMany({});
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
it("returns empty results when q is empty", async () => {
|
|
384
|
+
const res = await adminAgent.get("/admin/foods/search").expect(200);
|
|
385
|
+
expect(res.body.data).toEqual([]);
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
it("returns results matching searchable string fields", async () => {
|
|
389
|
+
await FoodModel.create({calories: 120, name: "Apple"});
|
|
390
|
+
await FoodModel.create({calories: 95, name: "Banana"});
|
|
391
|
+
const res = await adminAgent.get("/admin/foods/search?q=App").expect(200);
|
|
392
|
+
expect(res.body.data).toHaveLength(1);
|
|
393
|
+
expect(res.body.data[0].name).toBe("Apple");
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
it("escapes regex special characters in search query", async () => {
|
|
397
|
+
await FoodModel.create({calories: 100, name: "Rice.Bowl"});
|
|
398
|
+
const res = await adminAgent.get("/admin/foods/search?q=Rice.Bowl").expect(200);
|
|
399
|
+
expect(res.body.data).toHaveLength(1);
|
|
400
|
+
expect(res.body.data[0].name).toBe("Rice.Bowl");
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
it("filters by explicit fields when provided", async () => {
|
|
404
|
+
await FoodModel.create({calories: 100, name: "Apple"});
|
|
405
|
+
const res = await adminAgent.get("/admin/foods/search?q=Apple&fields=calories").expect(200);
|
|
406
|
+
// calories isn't a searchable string field so the result should be empty
|
|
407
|
+
expect(res.body.data).toEqual([]);
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
it("exercises ObjectId query branch when query looks like an ObjectId", async () => {
|
|
411
|
+
const user = await UserModel.findOne({email: "admin@example.com"});
|
|
412
|
+
await FoodModel.create({calories: 100, name: "Kale", ownerId: user?._id});
|
|
413
|
+
// Whether any results come back depends on which fields the admin config
|
|
414
|
+
// identifies as ObjectId-typed; the important thing for coverage is that
|
|
415
|
+
// the ObjectId branch runs without throwing.
|
|
416
|
+
const res = await adminAgent.get(`/admin/foods/search?q=${user?._id}`).expect(200);
|
|
417
|
+
expect(Array.isArray(res.body.data)).toBe(true);
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
it("returns 403 for non-admins", async () => {
|
|
421
|
+
await notAdminAgent.get("/admin/foods/search?q=x").expect(403);
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
it("returns 401 for unauthenticated users", async () => {
|
|
425
|
+
await supertest(app).get("/admin/foods/search?q=x").expect(401);
|
|
426
|
+
});
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
describe("AdminApp with scripts that use context", () => {
|
|
430
|
+
let app: express.Application;
|
|
431
|
+
let adminAgent: TestAgent;
|
|
432
|
+
|
|
433
|
+
afterEach(async () => {
|
|
434
|
+
const {BackgroundTask} = await import("@terreno/api");
|
|
435
|
+
await BackgroundTask.deleteMany({});
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
it("reports progress, logs, and checks cancellation via ctx", async () => {
|
|
439
|
+
await setupDb();
|
|
440
|
+
const {BackgroundTask} = await import("@terreno/api");
|
|
441
|
+
|
|
442
|
+
const appWithScript = getBaseServer();
|
|
443
|
+
setupAuth(appWithScript, UserModel as unknown as UserModelType);
|
|
444
|
+
addAuthRoutes(appWithScript, UserModel as unknown as UserModelType);
|
|
445
|
+
|
|
446
|
+
const admin = new AdminApp({
|
|
447
|
+
basePath: "/admin",
|
|
448
|
+
models: [],
|
|
449
|
+
scripts: [
|
|
450
|
+
{
|
|
451
|
+
description: "Exercises ctx",
|
|
452
|
+
name: "ctx-script",
|
|
453
|
+
runner: async (_wetRun, ctx) => {
|
|
454
|
+
await ctx!.addLog("info", "hello");
|
|
455
|
+
await ctx!.updateProgress(50, "Halfway", "Processing");
|
|
456
|
+
await ctx!.checkCancellation();
|
|
457
|
+
return {results: ["ok"], success: true};
|
|
458
|
+
},
|
|
459
|
+
},
|
|
460
|
+
],
|
|
461
|
+
});
|
|
462
|
+
admin.register(appWithScript);
|
|
463
|
+
appWithScript.use(apiUnauthorizedMiddleware);
|
|
464
|
+
appWithScript.use(apiErrorMiddleware);
|
|
465
|
+
|
|
466
|
+
app = appWithScript;
|
|
467
|
+
adminAgent = await authAsUser(app, "admin");
|
|
468
|
+
const res = await adminAgent.post("/admin/scripts/ctx-script/run").expect(201);
|
|
469
|
+
|
|
470
|
+
// Wait for script completion
|
|
471
|
+
await new Promise((resolve) => setTimeout(resolve, 150));
|
|
472
|
+
|
|
473
|
+
const task = await BackgroundTask.findById(res.body.taskId);
|
|
474
|
+
expect(task?.status).toBe("completed");
|
|
475
|
+
// addLog("info", "hello") should have persisted
|
|
476
|
+
expect(task?.logs.some((l: {message: string}) => l.message === "hello")).toBe(true);
|
|
477
|
+
expect(task?.progress?.percentage).toBe(100);
|
|
478
|
+
});
|
|
479
|
+
});
|
package/src/adminApp.test.ts
CHANGED
|
@@ -4,7 +4,9 @@ import {
|
|
|
4
4
|
apiErrorMiddleware,
|
|
5
5
|
apiUnauthorizedMiddleware,
|
|
6
6
|
BackgroundTask,
|
|
7
|
+
type ScriptRunner,
|
|
7
8
|
setupAuth,
|
|
9
|
+
type UserModel as UserModelType,
|
|
8
10
|
} from "@terreno/api";
|
|
9
11
|
import {authAsUser, getBaseServer, setupDb, UserModel} from "@terreno/api/src/tests";
|
|
10
12
|
import type express from "express";
|
|
@@ -13,7 +15,11 @@ import type TestAgent from "supertest/lib/agent";
|
|
|
13
15
|
|
|
14
16
|
import {AdminApp} from "./adminApp";
|
|
15
17
|
|
|
16
|
-
const createTestScript = (overrides?: {
|
|
18
|
+
const createTestScript = (overrides?: {
|
|
19
|
+
runner?: ScriptRunner;
|
|
20
|
+
name?: string;
|
|
21
|
+
description?: string;
|
|
22
|
+
}) => ({
|
|
17
23
|
description: overrides?.description ?? "A test script",
|
|
18
24
|
name: overrides?.name ?? "test-script",
|
|
19
25
|
runner:
|
|
@@ -43,8 +49,8 @@ const createFailingScript = () => ({
|
|
|
43
49
|
|
|
44
50
|
const buildApp = (scripts = [createTestScript()]): express.Application => {
|
|
45
51
|
const app = getBaseServer();
|
|
46
|
-
setupAuth(app, UserModel as
|
|
47
|
-
addAuthRoutes(app, UserModel as
|
|
52
|
+
setupAuth(app, UserModel as unknown as UserModelType);
|
|
53
|
+
addAuthRoutes(app, UserModel as unknown as UserModelType);
|
|
48
54
|
|
|
49
55
|
const admin = new AdminApp({
|
|
50
56
|
basePath: "/admin",
|
|
@@ -241,7 +247,9 @@ describe("AdminApp script routes", () => {
|
|
|
241
247
|
const res = await adminAgent.delete(`/admin/scripts/tasks/${res1.body.taskId}`).expect(200);
|
|
242
248
|
|
|
243
249
|
const logs = res.body.task.logs;
|
|
244
|
-
const cancelLog = logs.find((l:
|
|
250
|
+
const cancelLog = logs.find((l: {message: string; level: string}) =>
|
|
251
|
+
l.message.includes("cancelled")
|
|
252
|
+
);
|
|
245
253
|
expect(cancelLog).toBeDefined();
|
|
246
254
|
expect(cancelLog.level).toBe("info");
|
|
247
255
|
});
|
|
@@ -292,7 +300,7 @@ describe("AdminApp script routes", () => {
|
|
|
292
300
|
});
|
|
293
301
|
|
|
294
302
|
it("includes scripts in config response", async () => {
|
|
295
|
-
const res = await
|
|
303
|
+
const res = await adminAgent.get("/admin/config").expect(200);
|
|
296
304
|
|
|
297
305
|
expect(res.body.scripts).toHaveLength(2);
|
|
298
306
|
expect(res.body.scripts[0].name).toBe("migrate-data");
|
|
@@ -303,7 +311,8 @@ describe("AdminApp script routes", () => {
|
|
|
303
311
|
|
|
304
312
|
it("returns empty scripts array when no scripts configured", async () => {
|
|
305
313
|
app = buildApp([]);
|
|
306
|
-
const
|
|
314
|
+
const freshAdmin = await authAsUser(app, "admin");
|
|
315
|
+
const res = await freshAdmin.get("/admin/config").expect(200);
|
|
307
316
|
|
|
308
317
|
expect(res.body.scripts).toHaveLength(0);
|
|
309
318
|
});
|