@terreno/admin-backend 57.3.0 → 57.6.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 (51) hide show
  1. package/README.md +3 -3
  2. package/bunfig.toml +9 -1
  3. package/dist/adminApp.d.ts +20 -2
  4. package/dist/adminApp.js +1261 -1873
  5. package/dist/adminConfigIdentity.d.ts +1 -0
  6. package/dist/adminConfigIdentity.js +27 -50
  7. package/dist/adminMigrations.d.ts +10 -0
  8. package/dist/adminMigrations.js +120 -0
  9. package/dist/adminScriptJob.d.ts +32 -0
  10. package/dist/adminScriptJob.js +202 -0
  11. package/dist/adminTextSearch.d.ts +1 -0
  12. package/dist/adminTextSearch.js +22 -78
  13. package/dist/adminUiV2.d.ts +2 -0
  14. package/dist/adminUiV2.js +49 -146
  15. package/dist/aggregateAdmin.js +104 -227
  16. package/dist/documentStorageApp.js +184 -325
  17. package/dist/enrichAdminListFilters.d.ts +7 -0
  18. package/dist/enrichAdminListFilters.js +26 -0
  19. package/dist/filterParser.js +230 -183
  20. package/dist/index.d.ts +2 -0
  21. package/dist/index.js +4 -1
  22. package/dist/legacy.js +21 -23
  23. package/dist/resolvedAdminModel.js +23 -78
  24. package/dist/routePath.js +3 -3
  25. package/dist/scriptCli.js +161 -307
  26. package/dist/tests/bunSetup.js +1 -1
  27. package/package.json +10 -10
  28. package/src/adminApp.migrations.test.ts +232 -0
  29. package/src/adminApp.models.test.ts +731 -1
  30. package/src/adminApp.syncMeta.test.ts +237 -0
  31. package/src/adminApp.test.ts +92 -0
  32. package/src/adminApp.ts +373 -130
  33. package/src/adminConfigIdentity.ts +1 -0
  34. package/src/adminHttpGoldens.test.ts +337 -0
  35. package/src/adminMigrations.ts +157 -0
  36. package/src/adminScriptJob.test.ts +297 -0
  37. package/src/adminScriptJob.ts +271 -0
  38. package/src/adminSyncMutationAuthMismatch.test.ts +650 -0
  39. package/src/adminTextSearch.test.ts +28 -0
  40. package/src/adminTextSearch.ts +1 -0
  41. package/src/adminUiV2.ts +2 -0
  42. package/src/aggregateAdmin.test.ts +125 -0
  43. package/src/aggregateAdmin.ts +2 -0
  44. package/src/enrichAdminListFilters.test.ts +44 -0
  45. package/src/enrichAdminListFilters.ts +29 -0
  46. package/src/filterParser.test.ts +95 -0
  47. package/src/filterParser.ts +156 -10
  48. package/src/index.ts +2 -0
  49. package/src/legacy.ts +5 -1
  50. package/src/resolvedAdminModel.ts +2 -1
  51. package/tsconfig.json +6 -21
@@ -0,0 +1,232 @@
1
+ import {afterEach, beforeEach, describe, expect, it} from "bun:test";
2
+ import {mkdtemp, rm, writeFile} from "node:fs/promises";
3
+ import {tmpdir} from "node:os";
4
+ import {join} from "node:path";
5
+ import {
6
+ type AnyTerrenoAccess,
7
+ addAuthRoutes,
8
+ apiErrorMiddleware,
9
+ apiUnauthorizedMiddleware,
10
+ BackgroundTask,
11
+ createAccess,
12
+ MIGRATION_LOCK_ID,
13
+ MIGRATIONS_COLLECTION,
14
+ setupAuth,
15
+ terrenoStatements,
16
+ type UserModel as UserModelType,
17
+ } from "@terreno/api";
18
+ import {authAsUser, getBaseServer, setupDb, UserModel} from "@terreno/api/testing";
19
+ import type express from "express";
20
+ import mongoose from "mongoose";
21
+ import type TestAgent from "supertest/lib/agent";
22
+
23
+ import {AdminApp} from "./adminApp";
24
+
25
+ const fixturesDir = join(import.meta.dir, "../../api/src/migrations/fixtures/valid");
26
+
27
+ const buildApp = ({
28
+ accessControl,
29
+ migrationsDir,
30
+ }: {
31
+ accessControl?: AnyTerrenoAccess;
32
+ migrationsDir?: string;
33
+ } = {}): express.Application => {
34
+ const app = getBaseServer();
35
+ setupAuth(app, UserModel as unknown as UserModelType);
36
+ addAuthRoutes(app, UserModel as unknown as UserModelType);
37
+ const admin = new AdminApp({
38
+ accessControl,
39
+ basePath: "/admin",
40
+ migrations: migrationsDir ? {dir: migrationsDir} : undefined,
41
+ models: [],
42
+ });
43
+ admin.register(app);
44
+ app.use(apiUnauthorizedMiddleware);
45
+ app.use(apiErrorMiddleware);
46
+ return app;
47
+ };
48
+
49
+ const waitForTask = (): Promise<void> => {
50
+ return new Promise((resolve) => setTimeout(resolve, 500));
51
+ };
52
+
53
+ const appliedIds = async (): Promise<string[]> => {
54
+ const docs = await mongoose.connection
55
+ .collection(MIGRATIONS_COLLECTION)
56
+ .find({_id: {$ne: MIGRATION_LOCK_ID}})
57
+ .project({id: 1})
58
+ .toArray();
59
+ return docs.map((doc) => String(doc.id)).sort();
60
+ };
61
+
62
+ describe("AdminApp migrations routes", () => {
63
+ let app: express.Application;
64
+ let adminAgent: TestAgent;
65
+ let notAdminAgent: TestAgent;
66
+
67
+ afterEach(async () => {
68
+ await waitForTask();
69
+ await BackgroundTask.deleteMany({});
70
+ });
71
+
72
+ describe("when migrations are not configured", () => {
73
+ beforeEach(async () => {
74
+ await setupDb();
75
+ await mongoose.connection.collection(MIGRATIONS_COLLECTION).deleteMany({});
76
+ app = buildApp();
77
+ adminAgent = await authAsUser(app, "admin");
78
+ });
79
+
80
+ it("returns 404 for GET /admin/migrations/status", async () => {
81
+ await adminAgent.get("/admin/migrations/status").expect(404);
82
+ });
83
+
84
+ it("returns 404 for POST /admin/migrations/run", async () => {
85
+ await adminAgent.post("/admin/migrations/run").expect(404);
86
+ });
87
+
88
+ it("omits enabled migrations from config", async () => {
89
+ const res = await adminAgent.get("/admin/config").expect(200);
90
+ expect(res.body.migrations?.enabled).toBe(false);
91
+ });
92
+ });
93
+
94
+ describe("when migrations are configured", () => {
95
+ beforeEach(async () => {
96
+ await setupDb();
97
+ await mongoose.connection.collection(MIGRATIONS_COLLECTION).deleteMany({});
98
+ app = buildApp({migrationsDir: fixturesDir});
99
+ adminAgent = await authAsUser(app, "admin");
100
+ notAdminAgent = await authAsUser(app, "notAdmin");
101
+ });
102
+
103
+ it("returns 404 when the configured directory is missing", async () => {
104
+ const missingApp = buildApp({
105
+ migrationsDir: join(tmpdir(), "terreno-admin-migrations-missing"),
106
+ });
107
+ const agent = await authAsUser(missingApp, "admin");
108
+ const res = await agent.get("/admin/migrations/status").expect(404);
109
+ expect(res.body.title).toBe("Migration directory not found");
110
+ });
111
+
112
+ it("returns pending status for admins", async () => {
113
+ const res = await adminAgent.get("/admin/migrations/status").expect(200);
114
+ expect(res.body.data.pending.map((row: {id: string}) => row.id)).toEqual([
115
+ "20260910120000-alpha",
116
+ "20260910120001-beta",
117
+ ]);
118
+ });
119
+
120
+ it("disables CRUD on the migrations mount", async () => {
121
+ await adminAgent.get("/admin/migrations").expect(405);
122
+ });
123
+
124
+ it("forbids non-admins", async () => {
125
+ await notAdminAgent.get("/admin/migrations/status").expect(405);
126
+ await notAdminAgent.post("/admin/migrations/run").expect(405);
127
+ });
128
+
129
+ it("requires admin:runScripts to start a run when accessControl is configured", async () => {
130
+ const accessControl = createAccess({
131
+ connection: mongoose.connection,
132
+ resolvePermissions: async () => ({admin: ["access"]}),
133
+ statements: terrenoStatements,
134
+ });
135
+ const rbacApp = buildApp({accessControl, migrationsDir: fixturesDir});
136
+ const accessOnly = await authAsUser(rbacApp, "admin");
137
+
138
+ await accessOnly.get("/admin/migrations/status").expect(200);
139
+ await accessOnly.post("/admin/migrations/run").expect(405);
140
+ await accessOnly.post("/admin/migrations/run?wetRun=true").expect(405);
141
+ expect(await appliedIds()).toEqual([]);
142
+ });
143
+
144
+ it("allows admin:runScripts to start a dry-run", async () => {
145
+ const accessControl = createAccess({
146
+ connection: mongoose.connection,
147
+ resolvePermissions: async () => ({
148
+ admin: ["access", "runScripts", "viewBackgroundTasks"],
149
+ }),
150
+ statements: terrenoStatements,
151
+ });
152
+ const rbacApp = buildApp({accessControl, migrationsDir: fixturesDir});
153
+ const runner = await authAsUser(rbacApp, "admin");
154
+
155
+ const res = await runner.post("/admin/migrations/run").expect(201);
156
+ expect(res.body.data.taskId).toBeDefined();
157
+ await waitForTask();
158
+ expect(await appliedIds()).toEqual([]);
159
+ });
160
+
161
+ it("sets migrations.enabled on config", async () => {
162
+ const res = await adminAgent.get("/admin/config").expect(200);
163
+ expect(res.body.migrations.enabled).toBe(true);
164
+ });
165
+
166
+ it("dry-run does not persist applied ids", async () => {
167
+ const res = await adminAgent.post("/admin/migrations/run").expect(201);
168
+ expect(res.body.data.taskId).toBeDefined();
169
+ await waitForTask();
170
+ expect(await appliedIds()).toEqual([]);
171
+ const task = await BackgroundTask.findById(res.body.data.taskId);
172
+ expect(task?.isDryRun).toBe(true);
173
+ expect(task?.taskType).toBe("migrations:up");
174
+ const polled = await adminAgent
175
+ .get(`/admin/scripts/tasks/${res.body.data.taskId}`)
176
+ .expect(200);
177
+ expect(polled.body.task.status).toBe("completed");
178
+ });
179
+
180
+ it("applies pending on wet run", async () => {
181
+ const res = await adminAgent.post("/admin/migrations/run?wetRun=true").expect(201);
182
+ await waitForTask();
183
+ expect(await appliedIds()).toEqual(["20260910120000-alpha", "20260910120001-beta"]);
184
+ const task = await BackgroundTask.findById(res.body.data.taskId);
185
+ expect(task?.isDryRun).toBe(false);
186
+ });
187
+
188
+ it("returns 403 for production wet apply without ALLOW_MIGRATIONS", async () => {
189
+ const previousNodeEnv = process.env.NODE_ENV;
190
+ process.env.NODE_ENV = "production";
191
+ Reflect.deleteProperty(process.env, "ALLOW_MIGRATIONS");
192
+ try {
193
+ await adminAgent.post("/admin/migrations/run?wetRun=true").expect(403);
194
+ expect(await appliedIds()).toEqual([]);
195
+ } finally {
196
+ process.env.NODE_ENV = previousNodeEnv;
197
+ }
198
+ });
199
+ });
200
+
201
+ describe("when a migration up throws", () => {
202
+ let dir: string;
203
+
204
+ beforeEach(async () => {
205
+ await setupDb();
206
+ await mongoose.connection.collection(MIGRATIONS_COLLECTION).deleteMany({});
207
+ dir = await mkdtemp(join(tmpdir(), "admin-mig-fail-"));
208
+ await writeFile(
209
+ join(dir, "20260910120000-boom.ts"),
210
+ `export const id = "20260910120000-boom";
211
+ export const up = async () => {
212
+ throw new Error("boom");
213
+ };
214
+ `
215
+ );
216
+ app = buildApp({migrationsDir: dir});
217
+ adminAgent = await authAsUser(app, "admin");
218
+ });
219
+
220
+ afterEach(async () => {
221
+ await rm(dir, {force: true, recursive: true});
222
+ });
223
+
224
+ it("marks the background task failed", async () => {
225
+ const res = await adminAgent.post("/admin/migrations/run").expect(201);
226
+ await waitForTask();
227
+ const task = await BackgroundTask.findById(res.body.data.taskId);
228
+ expect(task?.status).toBe("failed");
229
+ expect(task?.error).toContain("boom");
230
+ });
231
+ });
232
+ });