@terreno/admin-backend 58.0.0-beta.1 → 58.0.0-beta.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.
- package/CHANGELOG.md +46 -0
- package/package.json +11 -4
- package/.claude/CLAUDE.local.md +0 -223
- package/.cursor/rules/00-root.mdc +0 -357
- package/.github/copilot-instructions.md +0 -352
- package/AGENTS.md +0 -358
- package/biome.jsonc +0 -21
- package/bunfig.toml +0 -16
- package/e2e/smoke.ts +0 -246
- package/tsconfig.json +0 -18
- package/tsconfig.typecheck.json +0 -18
package/biome.jsonc
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "./node_modules/@biomejs/biome/configuration_schema.json",
|
|
3
|
-
"extends": ["../biome.jsonc"],
|
|
4
|
-
"files": {
|
|
5
|
-
"includes": [
|
|
6
|
-
"package.json",
|
|
7
|
-
"src/**/*.ts",
|
|
8
|
-
"src/**/*.tsx",
|
|
9
|
-
"e2e/**/*.ts",
|
|
10
|
-
"!!**/node_modules",
|
|
11
|
-
"!!node_modules",
|
|
12
|
-
"!!**/dist",
|
|
13
|
-
"!!dist",
|
|
14
|
-
"!!**/build",
|
|
15
|
-
"!!build",
|
|
16
|
-
"!!**/coverage",
|
|
17
|
-
"!!coverage"
|
|
18
|
-
]
|
|
19
|
-
},
|
|
20
|
-
"root": true
|
|
21
|
-
}
|
package/bunfig.toml
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
[test]
|
|
2
|
-
preload = ["./src/tests/bunSetup.ts"]
|
|
3
|
-
root = "./src"
|
|
4
|
-
coveragePathIgnorePatterns = [
|
|
5
|
-
"**/*.test.ts",
|
|
6
|
-
"**/tests/**",
|
|
7
|
-
# Sibling workspace packages are compiled into `dist/` for this package's CI run;
|
|
8
|
-
# they carry their own coverage gates.
|
|
9
|
-
"../**",
|
|
10
|
-
"dist/**",
|
|
11
|
-
"**/node_modules/**",
|
|
12
|
-
]
|
|
13
|
-
# Enforced by scripts/check-coverage.ts; Bun parses this key but does not
|
|
14
|
-
# fail the run when coverage is below threshold. See
|
|
15
|
-
# https://github.com/oven-sh/bun/issues/7367 and https://github.com/oven-sh/bun/pull/27933.
|
|
16
|
-
coverageThreshold = { line = 95, function = 95 }
|
package/e2e/smoke.ts
DELETED
|
@@ -1,246 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Real-process smoke for @terreno/admin-backend. Run `bun e2e/smoke.ts` after `bun run compile`.
|
|
3
|
-
*
|
|
4
|
-
* The smoke re-runs this file with `serve` as a child process: a minimal TerrenoApp from the
|
|
5
|
-
* compiled `@terreno/api` with one `modelRouter` model that declares `admin:`, and the compiled
|
|
6
|
-
* `AdminApp`, on a free port, against `smokeMongoUri()`. The child seeds one admin user, because
|
|
7
|
-
* signup never grants admin. The parent then checks over real HTTP that the admin reads the admin
|
|
8
|
-
* config and lists the model's documents, and that a signed-up non-admin is denied both. Exits
|
|
9
|
-
* non-zero on the first failed check.
|
|
10
|
-
*/
|
|
11
|
-
import {
|
|
12
|
-
freePort,
|
|
13
|
-
type SpawnedProcess,
|
|
14
|
-
smokeMongoUri,
|
|
15
|
-
spawnUntilReady,
|
|
16
|
-
stopSmokeMongo,
|
|
17
|
-
} from "@terreno/test";
|
|
18
|
-
import type express from "express";
|
|
19
|
-
import {DateTime} from "luxon";
|
|
20
|
-
import type {Schema} from "mongoose";
|
|
21
|
-
|
|
22
|
-
const SERVE_ARG = "serve";
|
|
23
|
-
const PASSWORD = "admin-backend-smoke-password-1";
|
|
24
|
-
const WIDGET_DISPLAY_NAME = "Smoke widgets";
|
|
25
|
-
/** AdminApp mounts a model at `basePath` + its modelRouter path; the app also serves `/widgets`. */
|
|
26
|
-
const ADMIN_WIDGETS_PATH = "/admin/widgets";
|
|
27
|
-
|
|
28
|
-
interface AdminConfigModel {
|
|
29
|
-
displayName: string;
|
|
30
|
-
name: string;
|
|
31
|
-
routePath: string;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
interface SmokeWidget {
|
|
35
|
-
_id: string;
|
|
36
|
-
name: string;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Child: the compiled API with auth, one admin-enabled model, the compiled AdminApp, and a health
|
|
41
|
-
* plugin. Both packages are imported here, not at the top, so the parent process never loads
|
|
42
|
-
* them.
|
|
43
|
-
*/
|
|
44
|
-
const serve = async (): Promise<void> => {
|
|
45
|
-
const {createdUpdatedPlugin, logger, modelRouter, Permissions, TerrenoApp} = await import(
|
|
46
|
-
"@terreno/api"
|
|
47
|
-
);
|
|
48
|
-
const {AdminApp} = await import("@terreno/admin-backend");
|
|
49
|
-
const {default: mongoose} = await import("mongoose");
|
|
50
|
-
const {default: passportLocalMongoose} = await import("passport-local-mongoose");
|
|
51
|
-
|
|
52
|
-
const userSchema = new mongoose.Schema({
|
|
53
|
-
admin: {default: false, description: "Whether the user is an admin", type: Boolean},
|
|
54
|
-
email: {description: "Sign-in email", type: String},
|
|
55
|
-
name: {description: "Display name", type: String},
|
|
56
|
-
});
|
|
57
|
-
userSchema.plugin(passportLocalMongoose as unknown as (schema: Schema, options: object) => void, {
|
|
58
|
-
usernameCaseInsensitive: true,
|
|
59
|
-
usernameField: "email",
|
|
60
|
-
});
|
|
61
|
-
userSchema.plugin(createdUpdatedPlugin);
|
|
62
|
-
const User = mongoose.model("User", userSchema);
|
|
63
|
-
|
|
64
|
-
const widgetSchema = new mongoose.Schema({
|
|
65
|
-
name: {description: "Widget name", required: true, type: String},
|
|
66
|
-
});
|
|
67
|
-
widgetSchema.plugin(createdUpdatedPlugin);
|
|
68
|
-
const Widget = mongoose.model("Widget", widgetSchema);
|
|
69
|
-
|
|
70
|
-
const widgetRouter = modelRouter("/widgets", Widget, {
|
|
71
|
-
admin: {displayName: WIDGET_DISPLAY_NAME, listFields: ["name", "created"]},
|
|
72
|
-
permissions: {
|
|
73
|
-
create: [Permissions.IsAdmin],
|
|
74
|
-
delete: [Permissions.IsAdmin],
|
|
75
|
-
list: [Permissions.IsAdmin],
|
|
76
|
-
read: [Permissions.IsAdmin],
|
|
77
|
-
update: [Permissions.IsAdmin],
|
|
78
|
-
},
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
const healthPlugin = {
|
|
82
|
-
register: (app: express.Application): void => {
|
|
83
|
-
app.get("/health", (_req, res) => {
|
|
84
|
-
res.json({healthy: mongoose.connection.readyState === 1});
|
|
85
|
-
});
|
|
86
|
-
},
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
await mongoose.connect(process.env.MONGO_URI as string);
|
|
90
|
-
await (
|
|
91
|
-
User as unknown as {register: (user: object, password: string) => Promise<unknown>}
|
|
92
|
-
).register(
|
|
93
|
-
{admin: true, email: process.env.SMOKE_ADMIN_EMAIL, name: "Smoke admin"},
|
|
94
|
-
process.env.SMOKE_ADMIN_PASSWORD as string
|
|
95
|
-
);
|
|
96
|
-
await Widget.create({name: process.env.SMOKE_WIDGET_NAME});
|
|
97
|
-
|
|
98
|
-
const app = new TerrenoApp({
|
|
99
|
-
loggingOptions: {logRequests: false},
|
|
100
|
-
userModel: User as unknown as ConstructorParameters<typeof TerrenoApp>[0]["userModel"],
|
|
101
|
-
})
|
|
102
|
-
.register(widgetRouter)
|
|
103
|
-
.register(new AdminApp({}))
|
|
104
|
-
.register(healthPlugin);
|
|
105
|
-
app.start();
|
|
106
|
-
await app.whenReady();
|
|
107
|
-
logger.info(`[adminBackendSmoke] serving on port ${process.env.PORT}`);
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
const expectStatus = async (res: Response, status: number, step: string): Promise<unknown> => {
|
|
111
|
-
const body = await res.text();
|
|
112
|
-
if (res.status !== status) {
|
|
113
|
-
throw new Error(`${step}: expected ${status}, got ${res.status}: ${body}`);
|
|
114
|
-
}
|
|
115
|
-
return body ? JSON.parse(body) : undefined;
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
const bearer = (token: string): Record<string, string> => ({
|
|
119
|
-
Authorization: `Bearer ${token}`,
|
|
120
|
-
"Content-Type": "application/json",
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
const signIn = async (
|
|
124
|
-
baseUrl: string,
|
|
125
|
-
route: "login" | "signup",
|
|
126
|
-
email: string
|
|
127
|
-
): Promise<string> => {
|
|
128
|
-
const res = (await expectStatus(
|
|
129
|
-
await fetch(`${baseUrl}/auth/${route}`, {
|
|
130
|
-
body: JSON.stringify({email, password: PASSWORD}),
|
|
131
|
-
headers: {"Content-Type": "application/json"},
|
|
132
|
-
method: "POST",
|
|
133
|
-
}),
|
|
134
|
-
200,
|
|
135
|
-
`POST /auth/${route}`
|
|
136
|
-
)) as {data: {token: string}};
|
|
137
|
-
return res.data.token;
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
/** Parent: boots the child and walks the admin read path and the non-admin denial over HTTP. */
|
|
141
|
-
const run = async (): Promise<void> => {
|
|
142
|
-
const startedAt = DateTime.now();
|
|
143
|
-
const runId = startedAt.toMillis();
|
|
144
|
-
const adminEmail = `admin-backend-smoke-admin-${runId}@example.com`;
|
|
145
|
-
const widgetName = `Smoke widget ${runId}`;
|
|
146
|
-
const port = await freePort();
|
|
147
|
-
const baseUrl = `http://127.0.0.1:${port}`;
|
|
148
|
-
let server: SpawnedProcess | undefined;
|
|
149
|
-
|
|
150
|
-
try {
|
|
151
|
-
server = await spawnUntilReady({
|
|
152
|
-
cmd: [process.execPath, __filename, SERVE_ARG],
|
|
153
|
-
env: {
|
|
154
|
-
MONGO_URI: await smokeMongoUri({databaseName: "adminBackendSmoke"}),
|
|
155
|
-
PORT: String(port),
|
|
156
|
-
REFRESH_TOKEN_SECRET: "admin-backend-smoke-refresh-secret",
|
|
157
|
-
SESSION_SECRET: "admin-backend-smoke-session-secret",
|
|
158
|
-
SMOKE_ADMIN_EMAIL: adminEmail,
|
|
159
|
-
SMOKE_ADMIN_PASSWORD: PASSWORD,
|
|
160
|
-
SMOKE_WIDGET_NAME: widgetName,
|
|
161
|
-
TOKEN_ISSUER: "admin-backend-smoke",
|
|
162
|
-
TOKEN_SECRET: "admin-backend-smoke-token-secret",
|
|
163
|
-
},
|
|
164
|
-
timeoutMs: 30000,
|
|
165
|
-
url: `${baseUrl}/health`,
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
const adminToken = await signIn(baseUrl, "login", adminEmail);
|
|
169
|
-
console.info("✅ POST /auth/login signs in the seeded admin");
|
|
170
|
-
|
|
171
|
-
const config = (await expectStatus(
|
|
172
|
-
await fetch(`${baseUrl}/admin/config`, {headers: bearer(adminToken)}),
|
|
173
|
-
200,
|
|
174
|
-
"GET /admin/config as admin"
|
|
175
|
-
)) as {models: AdminConfigModel[]};
|
|
176
|
-
const widgetModel = config.models.find((model) => model.displayName === WIDGET_DISPLAY_NAME);
|
|
177
|
-
if (!widgetModel) {
|
|
178
|
-
throw new Error(
|
|
179
|
-
`GET /admin/config: the "${WIDGET_DISPLAY_NAME}" model is missing: ${JSON.stringify(config.models)}`
|
|
180
|
-
);
|
|
181
|
-
}
|
|
182
|
-
if (widgetModel.routePath !== ADMIN_WIDGETS_PATH) {
|
|
183
|
-
throw new Error(
|
|
184
|
-
`GET /admin/config: expected routePath ${ADMIN_WIDGETS_PATH}, got ${widgetModel.routePath}`
|
|
185
|
-
);
|
|
186
|
-
}
|
|
187
|
-
console.info(`✅ GET /admin/config lists the ${widgetModel.name} model`);
|
|
188
|
-
|
|
189
|
-
const list = (await expectStatus(
|
|
190
|
-
await fetch(`${baseUrl}${ADMIN_WIDGETS_PATH}`, {headers: bearer(adminToken)}),
|
|
191
|
-
200,
|
|
192
|
-
`GET ${ADMIN_WIDGETS_PATH} as admin`
|
|
193
|
-
)) as {data: SmokeWidget[]};
|
|
194
|
-
if (!list.data.some((widget) => widget.name === widgetName)) {
|
|
195
|
-
throw new Error(
|
|
196
|
-
`GET ${ADMIN_WIDGETS_PATH}: the seeded widget is missing: ${JSON.stringify(list.data)}`
|
|
197
|
-
);
|
|
198
|
-
}
|
|
199
|
-
console.info(`✅ GET ${ADMIN_WIDGETS_PATH} lists the seeded widget`);
|
|
200
|
-
|
|
201
|
-
const userToken = await signIn(
|
|
202
|
-
baseUrl,
|
|
203
|
-
"signup",
|
|
204
|
-
`admin-backend-smoke-user-${runId}@example.com`
|
|
205
|
-
);
|
|
206
|
-
console.info("✅ POST /auth/signup signs in a non-admin user");
|
|
207
|
-
|
|
208
|
-
await expectStatus(
|
|
209
|
-
await fetch(`${baseUrl}/admin/config`, {headers: bearer(userToken)}),
|
|
210
|
-
403,
|
|
211
|
-
"GET /admin/config as non-admin"
|
|
212
|
-
);
|
|
213
|
-
console.info("✅ GET /admin/config returns 403 for the non-admin");
|
|
214
|
-
|
|
215
|
-
// modelRouter answers a permission denial on a CRUD route with 405, not 403.
|
|
216
|
-
await expectStatus(
|
|
217
|
-
await fetch(`${baseUrl}${ADMIN_WIDGETS_PATH}`, {headers: bearer(userToken)}),
|
|
218
|
-
405,
|
|
219
|
-
`GET ${ADMIN_WIDGETS_PATH} as non-admin`
|
|
220
|
-
);
|
|
221
|
-
console.info(`✅ GET ${ADMIN_WIDGETS_PATH} returns 405 for the non-admin`);
|
|
222
|
-
} catch (error: unknown) {
|
|
223
|
-
if (server) {
|
|
224
|
-
console.error(`--- server output ---\n${server.output()}`);
|
|
225
|
-
}
|
|
226
|
-
throw error;
|
|
227
|
-
} finally {
|
|
228
|
-
await server?.stop();
|
|
229
|
-
await stopSmokeMongo();
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
console.info(`admin-backend smoke passed in ${DateTime.now().diff(startedAt).toMillis()} ms`);
|
|
233
|
-
};
|
|
234
|
-
|
|
235
|
-
const main = process.argv[2] === SERVE_ARG ? serve : run;
|
|
236
|
-
main().then(
|
|
237
|
-
() => {
|
|
238
|
-
if (main === run) {
|
|
239
|
-
process.exit(0);
|
|
240
|
-
}
|
|
241
|
-
},
|
|
242
|
-
(error: unknown) => {
|
|
243
|
-
console.error(`❌ ${error instanceof Error ? error.message : String(error)}`);
|
|
244
|
-
process.exit(1);
|
|
245
|
-
}
|
|
246
|
-
);
|
package/tsconfig.json
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"declaration": true,
|
|
4
|
-
"isolatedModules": true,
|
|
5
|
-
"lib": ["es2023"],
|
|
6
|
-
"module": "nodenext",
|
|
7
|
-
"noImplicitAny": false,
|
|
8
|
-
"noImplicitReturns": true,
|
|
9
|
-
"outDir": "./dist",
|
|
10
|
-
"rootDir": "./src",
|
|
11
|
-
"skipLibCheck": true,
|
|
12
|
-
"strict": true,
|
|
13
|
-
"target": "es2023",
|
|
14
|
-
"types": ["node"]
|
|
15
|
-
},
|
|
16
|
-
"exclude": ["node_modules", "**/*.test.ts"],
|
|
17
|
-
"include": ["src"]
|
|
18
|
-
}
|
package/tsconfig.typecheck.json
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "./tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"noEmit": true,
|
|
5
|
-
"composite": false,
|
|
6
|
-
"incremental": false,
|
|
7
|
-
"declaration": false,
|
|
8
|
-
"declarationMap": false,
|
|
9
|
-
"types": [
|
|
10
|
-
"node",
|
|
11
|
-
"bun"
|
|
12
|
-
]
|
|
13
|
-
},
|
|
14
|
-
"exclude": [
|
|
15
|
-
"node_modules",
|
|
16
|
-
"dist"
|
|
17
|
-
]
|
|
18
|
-
}
|