openadminjs 0.1.24 → 0.3.0-rc.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/README.md +117 -81
- package/dist/adapt-package-manager.d.ts +3 -0
- package/dist/adapt-package-manager.js +118 -0
- package/dist/create-project.d.ts +77 -2
- package/dist/create-project.js +685 -224
- package/dist/db-commands.d.ts +20 -0
- package/dist/db-commands.js +75 -0
- package/dist/detect-package-manager.d.ts +8 -0
- package/dist/detect-package-manager.js +37 -0
- package/dist/doctor.d.ts +26 -0
- package/dist/doctor.js +232 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +476 -166
- package/dist/render-schema.d.ts +3 -0
- package/dist/render-schema.js +33 -0
- package/dist/resource-slug.d.ts +1 -0
- package/dist/resource-slug.js +7 -0
- package/dist/runtime-mode.d.ts +16 -0
- package/dist/runtime-mode.js +138 -0
- package/dist/secrets.d.ts +24 -0
- package/dist/secrets.js +93 -0
- package/dist/security.d.ts +13 -0
- package/dist/security.js +168 -0
- package/dist/upgrade.d.ts +10 -0
- package/dist/upgrade.js +133 -0
- package/dist/validate-name.d.ts +33 -0
- package/dist/validate-name.js +206 -0
- package/package.json +39 -11
- package/template/.dockerignore +15 -0
- package/template/.env.production.example +5 -0
- package/template/Dockerfile +12 -0
- package/template/README.md +23 -3
- package/template/apps/admin/app/api/auth/login/route.ts +9 -9
- package/template/apps/admin/app/api/auth/logout/route.ts +11 -3
- package/template/apps/admin/app/api/auth/oidc/callback/route.ts +19 -0
- package/template/apps/admin/app/api/auth/oidc/start/route.ts +11 -0
- package/template/apps/admin/app/api/auth/oidc/status/route.ts +6 -0
- package/template/apps/admin/app/api/auth/refresh/route.ts +18 -11
- package/template/apps/admin/app/api/backend/[...path]/route.ts +48 -0
- package/template/apps/admin/app/api-tokens/page.tsx +39 -39
- package/template/apps/admin/app/assistant/page.tsx +11 -0
- package/template/apps/admin/app/dashboard/page.tsx +45 -462
- package/template/apps/admin/app/globals.css +52 -16
- package/template/apps/admin/app/layout.tsx +1 -1
- package/template/apps/admin/app/login/page.tsx +13 -145
- package/template/apps/admin/app/plugins/page.tsx +16 -402
- package/template/apps/admin/app/profile/page.tsx +5 -2
- package/template/apps/admin/app/settings/page.tsx +14 -265
- package/template/apps/admin/components/ai-assistant-widget.tsx +3 -6
- package/template/apps/admin/components/app-shell.tsx +25 -373
- package/template/apps/admin/components/auth-guard.tsx +8 -28
- package/template/apps/admin/components/logo.tsx +1 -30
- package/template/apps/admin/components/resource-form.tsx +3 -3
- package/template/apps/admin/components/resource-table.tsx +12 -8
- package/template/apps/admin/lib/api.ts +45 -107
- package/template/apps/admin/lib/resource-table-prefs.ts +1 -57
- package/template/apps/admin/lib/server-api.ts +7 -0
- package/template/apps/admin/lib/tokens.ts +9 -81
- package/template/apps/admin/middleware.ts +9 -50
- package/template/apps/admin/next.config.mjs +4 -0
- package/template/apps/admin/package.json +2 -1
- package/template/apps/admin/public/brand/favicon.svg +4 -1
- package/template/apps/admin/tailwind.config.ts +9 -8
- package/template/apps/api/package.json +13 -6
- package/template/apps/api/src/access/catalog.ts +4 -0
- package/template/apps/api/src/admin/admin-ai.controller.ts +15 -125
- package/template/apps/api/src/admin/admin-analytics.service.ts +16 -148
- package/template/apps/api/src/admin/admin-logs.controller.ts +4 -1
- package/template/apps/api/src/admin/admin-plugins.controller.ts +8 -8
- package/template/apps/api/src/admin/admin-plugins.service.ts +15 -259
- package/template/apps/api/src/admin/admin-resource.service.ts +105 -254
- package/template/apps/api/src/admin/admin.controller.ts +17 -4
- package/template/apps/api/src/admin/admin.module.ts +2 -2
- package/template/apps/api/src/admin/resource-policy.ts +1 -0
- package/template/apps/api/src/admin/resource-query.ts +1 -0
- package/template/apps/api/src/admin/safe-ai.service.ts +43 -0
- package/template/apps/api/src/admin/validate-resource-payload.ts +2 -0
- package/template/apps/api/src/auth/auth.controller.ts +4 -2
- package/template/apps/api/src/auth/auth.service.ts +68 -83
- package/template/apps/api/src/auth/sso-oidc.controller.ts +12 -74
- package/template/apps/api/src/auth/sso-oidc.service.ts +51 -268
- package/template/apps/api/src/common/api-exception.filter.ts +1 -0
- package/template/apps/api/src/common/auth.guard.ts +66 -39
- package/template/apps/api/src/common/json-field-codec.ts +138 -0
- package/template/apps/api/src/common/prisma.service.ts +12 -7
- package/template/apps/api/src/common/request-logging.interceptor.ts +8 -3
- package/template/apps/api/src/graphql/graphql.module.ts +14 -1
- package/template/apps/api/src/health/health.controller.ts +6 -0
- package/template/apps/api/src/main.ts +11 -2
- package/template/apps/api/src/plugins/plugin-loader.ts +5 -3
- package/template/apps/api/src/resources/commerce.resource.ts +3 -34
- package/template/apps/api/src/resources/operations.resource.ts +1 -0
- package/template/apps/api/src/store/payment/payment.controller.ts +15 -92
- package/template/apps/api/src/store/payment/payment.module.ts +2 -3
- package/template/apps/api/src/store/payment/payment.service.ts +121 -227
- package/template/apps/api/webpack.config.cjs +2 -1
- package/template/apps/web/next.config.mjs +4 -0
- package/template/apps/web/public/brand/favicon.svg +4 -1
- package/template/compose.yaml +56 -0
- package/template/examples/crm-admin/README.md +31 -0
- package/template/examples/crm-admin/package.json +17 -0
- package/template/examples/crm-admin/prisma/models.fragment.prisma +50 -0
- package/template/examples/crm-admin/src/index.ts +7 -0
- package/template/examples/crm-admin/src/resources/activities.resource.ts +29 -0
- package/template/examples/crm-admin/src/resources/companies.resource.ts +21 -0
- package/template/examples/crm-admin/src/resources/contacts.resource.ts +24 -0
- package/template/examples/crm-admin/src/resources/deals.resource.ts +30 -0
- package/template/examples/crm-admin/tsconfig.json +7 -0
- package/template/package.json +8 -1
- package/template/packages/admin-client/package.json +18 -0
- package/template/packages/admin-client/src/contracts.ts +29 -0
- package/template/packages/admin-client/src/index.ts +2 -0
- package/template/packages/admin-client/src/preferences.ts +57 -0
- package/template/packages/admin-client/tsconfig.json +12 -0
- package/template/packages/auth/package.json +1 -1
- package/template/packages/brand/site/assets/brand/favicon.svg +4 -1
- package/template/packages/brand/src/generate-assets.mjs +17 -186
- package/template/packages/core/package.json +1 -1
- package/template/packages/mail/package.json +1 -1
- package/template/packages/nest/package.json +22 -0
- package/template/packages/nest/src/api-exception.filter.ts +23 -0
- package/template/packages/nest/src/index.ts +3 -0
- package/template/packages/nest/src/resource-policy.ts +64 -0
- package/template/packages/nest/src/resource-query.ts +103 -0
- package/template/packages/nest/tsconfig.json +12 -0
- package/template/packages/notifications/package.json +1 -1
- package/template/packages/payments/package.json +1 -1
- package/template/packages/permissions/package.json +1 -1
- package/template/packages/permissions/src/index.ts +2 -2
- package/template/packages/plugin-sdk/package.json +1 -1
- package/template/packages/plugin-sdk/src/index.ts +2 -2
- package/template/packages/prisma/package.json +21 -0
- package/template/packages/prisma/schema/ai.prisma +36 -0
- package/template/packages/prisma/schema/audit.prisma +13 -0
- package/template/packages/prisma/schema/commerce.prisma +79 -0
- package/template/packages/prisma/schema/content.prisma +27 -0
- package/template/packages/prisma/schema/core.prisma +106 -0
- package/template/packages/prisma/schema/header.prisma +8 -0
- package/template/packages/prisma/schema/jobs.prisma +9 -0
- package/template/packages/prisma/schema/media.prisma +9 -0
- package/template/packages/prisma/schema/modules.json +47 -0
- package/template/packages/prisma/schema/notifications.prisma +9 -0
- package/template/packages/prisma/schema/settings.prisma +8 -0
- package/template/packages/prisma/schema/tokens.prisma +10 -0
- package/template/packages/prisma/src/index.d.ts +12 -0
- package/template/packages/prisma/src/index.mjs +138 -0
- package/template/packages/queue/package.json +1 -1
- package/template/packages/resource/package.json +1 -1
- package/template/packages/resource/src/index.ts +1 -0
- package/template/packages/storage/package.json +1 -1
- package/template/packages/storage/src/index.ts +37 -0
- package/template/prisma/migrations/20260713120000_add_user_oidc_key_and_category_order/migration.sql +11 -0
- package/template/prisma/migrations/20261126130000_add_sessions/migration.sql +26 -0
- package/template/prisma/migrations/20261126140000_api_token_hash/migration.sql +3 -0
- package/template/prisma/migrations/20261126150000_payment_idempotency/migration.sql +6 -0
- package/template/prisma/schema.prisma +82 -54
- package/template/prisma/seed.ts +14 -38
- package/template/runtime-versions.json +15 -0
- package/template/scripts/db-init.mjs +33 -0
- package/template/scripts/install-crm.mjs +50 -0
- package/template/scripts/migrate-existing.mjs +65 -0
- package/template/scripts/schema.mjs +48 -0
- package/template/tsconfig.base.json +23 -5
- package/LICENSE +0 -21
- package/dist/create-project.test.d.ts +0 -1
- package/dist/create-project.test.js +0 -99
- package/dist/index.test.d.ts +0 -1
- package/dist/index.test.js +0 -7
- package/dist/resource-slug.test.d.ts +0 -1
- package/dist/resource-slug.test.js +0 -9
- package/template/apps/admin/.storybook/main.ts +0 -12
- package/template/apps/admin/.storybook/preview.ts +0 -10
- package/template/apps/admin/app/login/page.test.tsx +0 -18
- package/template/apps/admin/components/auth-guard.test.tsx +0 -55
- package/template/apps/admin/components/logo.stories.tsx +0 -24
- package/template/apps/admin/lib/api.test.ts +0 -118
- package/template/apps/admin/lib/auth-flow.e2e.test.ts +0 -79
- package/template/apps/admin/vitest.config.ts +0 -7
- package/template/apps/api/src/access/derive-permissions.test.ts +0 -26
- package/template/apps/api/src/admin/admin-resource.service.test.ts +0 -118
- package/template/apps/api/src/admin/validate-resource-payload.test.ts +0 -44
- package/template/apps/api/src/app.module.test.ts +0 -12
- package/template/apps/api/src/auth/auth.service.test.ts +0 -78
- package/template/apps/api/src/auth/sso-oidc.service.test.ts +0 -84
- package/template/apps/api/src/common/auth.guard.test.ts +0 -62
- package/template/apps/api/src/common/casl-ability.factory.test.ts +0 -33
- package/template/apps/api/src/common/request-logging.interceptor.test.ts +0 -39
- package/template/apps/api/src/graphql/admin-resources.resolver.test.ts +0 -45
- package/template/apps/api/src/graphql/ensure-gql-paramtypes.test.ts +0 -44
- package/template/apps/api/src/graphql/graphql-schema.test.ts +0 -26
- package/template/apps/api/src/plugins/manifest.schema.test.ts +0 -36
- package/template/apps/api/src/plugins/plugin-runtime.test.ts +0 -24
- package/template/apps/api/src/queue/queue.service.test.ts +0 -33
- package/template/apps/api/src/resources/resource-hooks.registry.test.ts +0 -72
- package/template/apps/api/vitest.config.ts +0 -21
- package/template/apps/web/lib/seo.test.ts +0 -41
- package/template/apps/web/vitest.config.ts +0 -19
- package/template/packages/auth/src/index.test.ts +0 -10
- package/template/packages/brand/src/index.test.ts +0 -9
- package/template/packages/core/src/index.test.ts +0 -27
- package/template/packages/core/vitest.config.ts +0 -9
- package/template/packages/mail/src/index.test.ts +0 -23
- package/template/packages/notifications/src/index.test.ts +0 -9
- package/template/packages/permissions/src/blueprints.test.ts +0 -62
- package/template/packages/permissions/src/index.test.ts +0 -34
- package/template/packages/plugin-sdk/src/sdk.test.ts +0 -13
- package/template/packages/plugin-sdk/vitest.config.ts +0 -5
- package/template/packages/queue/src/index.test.ts +0 -12
- package/template/packages/resource/src/index.test.ts +0 -196
- package/template/packages/storage/src/index.test.ts +0 -10
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 OpenAdminJS contributors
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { existsSync, mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
-
import { join } from "node:path";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
|
-
import { describe, expect, it } from "vitest";
|
|
5
|
-
import { createProject, defaultTemplateDir } from "./create-project.js";
|
|
6
|
-
const BASE_OPTIONS = {
|
|
7
|
-
packageManager: "pnpm",
|
|
8
|
-
database: "postgresql",
|
|
9
|
-
superadminEmail: "admin@localhost.dev",
|
|
10
|
-
superadminPassword: "password1234",
|
|
11
|
-
databaseUrl: "postgresql://localhost:5432/my-app?schema=public",
|
|
12
|
-
redisUrl: "redis://localhost:6379",
|
|
13
|
-
jwtSecret: "test-jwt-secret",
|
|
14
|
-
jwtRefreshSecret: "test-jwt-refresh-secret",
|
|
15
|
-
adminOrigin: "http://localhost:3000",
|
|
16
|
-
apiPort: "4000",
|
|
17
|
-
git: false,
|
|
18
|
-
install: false
|
|
19
|
-
};
|
|
20
|
-
describe("create project", () => {
|
|
21
|
-
it("template .env.example contains placeholder keys for every secret", () => {
|
|
22
|
-
const envExample = readFileSync(join(defaultTemplateDir(), ".env.example"), "utf8");
|
|
23
|
-
for (const key of ["__DATABASE_URL__", "__REDIS_URL__", "__JWT_SECRET__", "__JWT_REFRESH_SECRET__", "__ADMIN_ORIGIN__", "__API_PORT__"]) {
|
|
24
|
-
expect(envExample, `missing placeholder ${key}`).toContain(key);
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
it("does not scaffold root .env.example or .env files", () => {
|
|
28
|
-
const cwd = mkdtempSync(join(tmpdir(), "openadminjs-cli-test-"));
|
|
29
|
-
try {
|
|
30
|
-
const result = createProject({ ...BASE_OPTIONS, projectName: "my-app", cwd, templateDir: defaultTemplateDir() });
|
|
31
|
-
expect(existsSync(join(result.targetDir, ".env.example"))).toBe(false);
|
|
32
|
-
expect(existsSync(join(result.targetDir, ".env"))).toBe(false);
|
|
33
|
-
expect(existsSync(join(result.targetDir, "tsconfig.base.json"))).toBe(true);
|
|
34
|
-
}
|
|
35
|
-
finally {
|
|
36
|
-
rmSync(cwd, { recursive: true, force: true });
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
it("scaffolds prisma.config.ts that loads apps/api/.env for Prisma CLI", () => {
|
|
40
|
-
const cwd = mkdtempSync(join(tmpdir(), "openadminjs-cli-test-"));
|
|
41
|
-
try {
|
|
42
|
-
const result = createProject({ ...BASE_OPTIONS, projectName: "my-app", cwd, templateDir: defaultTemplateDir() });
|
|
43
|
-
const prismaConfig = readFileSync(join(result.targetDir, "apps", "api", "prisma.config.ts"), "utf8");
|
|
44
|
-
expect(prismaConfig).toContain("dotenv");
|
|
45
|
-
expect(prismaConfig).toContain('.env")');
|
|
46
|
-
}
|
|
47
|
-
finally {
|
|
48
|
-
rmSync(cwd, { recursive: true, force: true });
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
it("writes apps/api/.env with real secrets and superadmin credentials", () => {
|
|
52
|
-
const cwd = mkdtempSync(join(tmpdir(), "openadminjs-cli-test-"));
|
|
53
|
-
try {
|
|
54
|
-
const result = createProject({ ...BASE_OPTIONS, projectName: "my-app", cwd, templateDir: defaultTemplateDir() });
|
|
55
|
-
const apiEnv = readFileSync(join(result.targetDir, "apps", "api", ".env"), "utf8");
|
|
56
|
-
expect(apiEnv).toContain("DATABASE_URL=postgresql://localhost:5432/my-app?schema=public");
|
|
57
|
-
expect(apiEnv).toContain("JWT_SECRET=test-jwt-secret");
|
|
58
|
-
expect(apiEnv).toContain("SUPERADMIN_EMAIL=admin@localhost.dev");
|
|
59
|
-
expect(apiEnv).toContain("SUPERADMIN_PASSWORD=password1234");
|
|
60
|
-
}
|
|
61
|
-
finally {
|
|
62
|
-
rmSync(cwd, { recursive: true, force: true });
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
it("fails fast when template package.json is missing", () => {
|
|
66
|
-
const cwd = mkdtempSync(join(tmpdir(), "openadminjs-cli-test-"));
|
|
67
|
-
const badTemplate = mkdtempSync(join(tmpdir(), "openadminjs-cli-template-"));
|
|
68
|
-
try {
|
|
69
|
-
writeFileSync(join(badTemplate, ".env.example"), "DATABASE_URL=__DATABASE_URL__\n");
|
|
70
|
-
mkdirSync(join(badTemplate, "apps"), { recursive: true });
|
|
71
|
-
expect(() => createProject({ ...BASE_OPTIONS, projectName: "broken-app", cwd, templateDir: badTemplate })).toThrow(/package\.json not found/);
|
|
72
|
-
}
|
|
73
|
-
finally {
|
|
74
|
-
rmSync(cwd, { recursive: true, force: true });
|
|
75
|
-
rmSync(badTemplate, { recursive: true, force: true });
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
it("copies template when installed inside node_modules (npx scenario)", () => {
|
|
79
|
-
// The old filter `source.includes("node_modules")` matched the entire absolute path when the
|
|
80
|
-
// package was installed via npx (~/.npm/_npx/.../node_modules/openadminjs/template/...) and
|
|
81
|
-
// blocked every file, so package.json never landed in the target.
|
|
82
|
-
const cwd = mkdtempSync(join(tmpdir(), "openadminjs-cli-test-"));
|
|
83
|
-
const fakeNpx = mkdtempSync(join(tmpdir(), "openadminjs-npx-"));
|
|
84
|
-
const templateDir = join(fakeNpx, "node_modules", "openadminjs", "template");
|
|
85
|
-
try {
|
|
86
|
-
mkdirSync(templateDir, { recursive: true });
|
|
87
|
-
writeFileSync(join(templateDir, "package.json"), JSON.stringify({ name: "__APP_NAME__", private: true }, null, 2) + "\n");
|
|
88
|
-
writeFileSync(join(templateDir, ".env.example"), "DATABASE_URL=__DATABASE_URL__\n");
|
|
89
|
-
const result = createProject({ ...BASE_OPTIONS, projectName: "npx-app", cwd, templateDir });
|
|
90
|
-
const packageJson = readFileSync(join(result.targetDir, "package.json"), "utf8");
|
|
91
|
-
expect(packageJson).toContain('"name": "npx-app"');
|
|
92
|
-
expect(existsSync(join(result.targetDir, "apps", "api", ".env"))).toBe(false);
|
|
93
|
-
}
|
|
94
|
-
finally {
|
|
95
|
-
rmSync(cwd, { recursive: true, force: true });
|
|
96
|
-
rmSync(fakeNpx, { recursive: true, force: true });
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
});
|
package/dist/index.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/index.test.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { modelNameToResourceSlug } from "./resource-slug.js";
|
|
3
|
-
describe("openadminjs cli", () => {
|
|
4
|
-
it("exposes resource slug helper used by generate resource", () => {
|
|
5
|
-
expect(modelNameToResourceSlug("OrderItem")).toBe("order-item");
|
|
6
|
-
});
|
|
7
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { modelNameToResourceSlug } from "./resource-slug.js";
|
|
3
|
-
describe("modelNameToResourceSlug", () => {
|
|
4
|
-
it("converts PascalCase to kebab-case", () => {
|
|
5
|
-
expect(modelNameToResourceSlug("Post")).toBe("post");
|
|
6
|
-
expect(modelNameToResourceSlug("OrderItem")).toBe("order-item");
|
|
7
|
-
expect(modelNameToResourceSlug("BlogCategory")).toBe("blog-category");
|
|
8
|
-
});
|
|
9
|
-
});
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { StorybookConfig } from "@storybook/nextjs";
|
|
2
|
-
|
|
3
|
-
const config: StorybookConfig = {
|
|
4
|
-
stories: ["../components/**/*.mdx", "../components/**/*.stories.@(js|jsx|mjs|ts|tsx)"],
|
|
5
|
-
addons: ["@storybook/addon-docs", "@storybook/addon-onboarding"],
|
|
6
|
-
framework: {
|
|
7
|
-
name: "@storybook/nextjs",
|
|
8
|
-
options: {}
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export default config;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { safeNextParam } from "./safe-next-param";
|
|
3
|
-
|
|
4
|
-
describe("login safeNextParam", () => {
|
|
5
|
-
it("defaults to dashboard for empty values", () => {
|
|
6
|
-
expect(safeNextParam(null)).toBe("/dashboard");
|
|
7
|
-
expect(safeNextParam("")).toBe("/dashboard");
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
it("rejects protocol-relative values", () => {
|
|
11
|
-
expect(safeNextParam("//evil.example")).toBe("/dashboard");
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
it("accepts internal paths", () => {
|
|
15
|
-
expect(safeNextParam("/dashboard")).toBe("/dashboard");
|
|
16
|
-
expect(safeNextParam("/resources/posts")).toBe("/resources/posts");
|
|
17
|
-
});
|
|
18
|
-
});
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
// @vitest-environment jsdom
|
|
2
|
-
import React from "react";
|
|
3
|
-
import { render, waitFor } from "@testing-library/react";
|
|
4
|
-
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
5
|
-
|
|
6
|
-
const replace = vi.fn();
|
|
7
|
-
const usePathname = vi.fn();
|
|
8
|
-
|
|
9
|
-
vi.mock("next/navigation", () => ({
|
|
10
|
-
usePathname: () => usePathname(),
|
|
11
|
-
useRouter: () => ({ replace })
|
|
12
|
-
}));
|
|
13
|
-
|
|
14
|
-
vi.mock("../lib/api", () => ({
|
|
15
|
-
hasUsableAccessToken: vi.fn()
|
|
16
|
-
}));
|
|
17
|
-
|
|
18
|
-
describe("AuthGuard", () => {
|
|
19
|
-
afterEach(() => {
|
|
20
|
-
vi.clearAllMocks();
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it("allows public login route without redirect", async () => {
|
|
24
|
-
const { hasUsableAccessToken } = await import("../lib/api");
|
|
25
|
-
vi.mocked(hasUsableAccessToken).mockReturnValue(false);
|
|
26
|
-
usePathname.mockReturnValue("/login");
|
|
27
|
-
const { AuthGuard } = await import("./auth-guard");
|
|
28
|
-
|
|
29
|
-
const { getByText } = render(
|
|
30
|
-
<AuthGuard>
|
|
31
|
-
<div>inside</div>
|
|
32
|
-
</AuthGuard>
|
|
33
|
-
);
|
|
34
|
-
|
|
35
|
-
expect(getByText("inside")).toBeTruthy();
|
|
36
|
-
expect(replace).not.toHaveBeenCalled();
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it("redirects to login when token is missing", async () => {
|
|
40
|
-
const { hasUsableAccessToken } = await import("../lib/api");
|
|
41
|
-
vi.mocked(hasUsableAccessToken).mockReturnValue(false);
|
|
42
|
-
usePathname.mockReturnValue("/dashboard");
|
|
43
|
-
const { AuthGuard } = await import("./auth-guard");
|
|
44
|
-
|
|
45
|
-
render(
|
|
46
|
-
<AuthGuard>
|
|
47
|
-
<div>inside</div>
|
|
48
|
-
</AuthGuard>
|
|
49
|
-
);
|
|
50
|
-
|
|
51
|
-
await waitFor(() => {
|
|
52
|
-
expect(replace).toHaveBeenCalledWith("/login?next=%2Fdashboard");
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
});
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import type { Meta, StoryObj } from "@storybook/nextjs";
|
|
2
|
-
import type { ComponentType } from "react";
|
|
3
|
-
import { ThemeProvider } from "next-themes";
|
|
4
|
-
import { Logo } from "./logo";
|
|
5
|
-
|
|
6
|
-
const meta = {
|
|
7
|
-
title: "UI/Logo",
|
|
8
|
-
component: Logo,
|
|
9
|
-
decorators: [
|
|
10
|
-
(Story: ComponentType) => (
|
|
11
|
-
<ThemeProvider attribute="class" defaultTheme="light" enableSystem={false}>
|
|
12
|
-
<div className="rounded-xl border border-slate-200 bg-white p-8">
|
|
13
|
-
<Story />
|
|
14
|
-
</div>
|
|
15
|
-
</ThemeProvider>
|
|
16
|
-
)
|
|
17
|
-
]
|
|
18
|
-
} satisfies Meta<typeof Logo>;
|
|
19
|
-
|
|
20
|
-
export default meta;
|
|
21
|
-
type Story = StoryObj<typeof Logo>;
|
|
22
|
-
|
|
23
|
-
export const Default: Story = {};
|
|
24
|
-
export const Compact: Story = { args: { compact: true } };
|
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
|
|
3
|
-
// Helper: encode a JWT-like payload for testing
|
|
4
|
-
function fakeJwt(payload: Record<string, unknown>): string {
|
|
5
|
-
const part = Buffer.from(JSON.stringify(payload)).toString("base64url");
|
|
6
|
-
return `header.${part}.sig`;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
describe("api client", () => {
|
|
10
|
-
afterEach(() => {
|
|
11
|
-
vi.unstubAllGlobals();
|
|
12
|
-
vi.restoreAllMocks();
|
|
13
|
-
vi.resetModules();
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
it("token returns null without document (SSR context)", async () => {
|
|
17
|
-
const { token } = await import("./tokens");
|
|
18
|
-
expect(token()).toBeNull();
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
it("token reads from document.cookie in browser", async () => {
|
|
22
|
-
const cookieValue = `oajs_at=${encodeURIComponent("my-access-token")}`;
|
|
23
|
-
vi.stubGlobal("document", { cookie: cookieValue });
|
|
24
|
-
const { token } = await import("./tokens");
|
|
25
|
-
expect(token()).toBe("my-access-token");
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it("api parses JSON on success", async () => {
|
|
29
|
-
vi.stubGlobal(
|
|
30
|
-
"fetch",
|
|
31
|
-
vi.fn().mockResolvedValue({
|
|
32
|
-
ok: true,
|
|
33
|
-
json: vi.fn().mockResolvedValue({ ok: true })
|
|
34
|
-
})
|
|
35
|
-
);
|
|
36
|
-
const { api } = await import("./api");
|
|
37
|
-
const body = await api<{ ok: boolean }>("/test");
|
|
38
|
-
expect(body.ok).toBe(true);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it("api throws with message on error response", async () => {
|
|
42
|
-
vi.stubGlobal(
|
|
43
|
-
"fetch",
|
|
44
|
-
vi.fn().mockResolvedValue({
|
|
45
|
-
ok: false,
|
|
46
|
-
statusText: "Bad",
|
|
47
|
-
json: vi.fn().mockResolvedValue({ message: "Nope" })
|
|
48
|
-
})
|
|
49
|
-
);
|
|
50
|
-
const { api } = await import("./api");
|
|
51
|
-
await expect(api("/x")).rejects.toThrow("Nope");
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it("isAccessTokenFresh returns false for malformed token", async () => {
|
|
55
|
-
const { isTokenFresh } = await import("./tokens");
|
|
56
|
-
expect(isTokenFresh("bad")).toBe(false);
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it("isAccessTokenFresh returns true for a far-future token", async () => {
|
|
60
|
-
vi.stubGlobal("atob", (v: string) => Buffer.from(v, "base64").toString("utf-8"));
|
|
61
|
-
const { isTokenFresh } = await import("./tokens");
|
|
62
|
-
const jwt = fakeJwt({ exp: 9_999_999_999 });
|
|
63
|
-
expect(isTokenFresh(jwt)).toBe(true);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it("api refreshes token on 401 via BFF /api/auth/refresh route", async () => {
|
|
67
|
-
const locationAssign = vi.fn();
|
|
68
|
-
const freshJwt = fakeJwt({ exp: 9_999_999_999 });
|
|
69
|
-
|
|
70
|
-
// Simulate document.cookie returning a fresh access token
|
|
71
|
-
let cookieStore = `oajs_at=${encodeURIComponent(freshJwt)}; oajs_rt=${encodeURIComponent("rt-token")}`;
|
|
72
|
-
const cookieDescriptor = { get: () => cookieStore, set: (v: string) => { cookieStore = v; } };
|
|
73
|
-
vi.stubGlobal("document", Object.defineProperty({}, "cookie", cookieDescriptor));
|
|
74
|
-
vi.stubGlobal("atob", (v: string) => Buffer.from(v, "base64").toString("utf-8"));
|
|
75
|
-
vi.stubGlobal("window", {
|
|
76
|
-
location: { pathname: "/dashboard", assign: locationAssign }
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
const fetchMock = vi
|
|
80
|
-
.fn()
|
|
81
|
-
// First call: original request → 401
|
|
82
|
-
.mockResolvedValueOnce({ ok: false, status: 401, statusText: "Unauthorized", json: vi.fn().mockResolvedValue({ message: "Unauthorized" }) })
|
|
83
|
-
// Second call: BFF /api/auth/refresh → ok
|
|
84
|
-
.mockResolvedValueOnce({ ok: true, status: 200, json: vi.fn().mockResolvedValue({ ok: true }) })
|
|
85
|
-
// Third call: retry original → 200
|
|
86
|
-
.mockResolvedValueOnce({ ok: true, status: 200, json: vi.fn().mockResolvedValue({ data: "result" }) });
|
|
87
|
-
vi.stubGlobal("fetch", fetchMock);
|
|
88
|
-
|
|
89
|
-
const { api } = await import("./api");
|
|
90
|
-
const response = await api<{ data: string }>("/admin/resources/overview");
|
|
91
|
-
expect(response.data).toBe("result");
|
|
92
|
-
expect(locationAssign).not.toHaveBeenCalled();
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it("api clears cookies and redirects to login when all refresh attempts fail", async () => {
|
|
96
|
-
const locationAssign = vi.fn();
|
|
97
|
-
const freshJwt = fakeJwt({ exp: 9_999_999_999 });
|
|
98
|
-
let cookieStore = `oajs_at=${encodeURIComponent(freshJwt)}; oajs_rt=${encodeURIComponent("rt")}`;
|
|
99
|
-
const cookieDescriptor = { get: () => cookieStore, set: (v: string) => { cookieStore = v; } };
|
|
100
|
-
vi.stubGlobal("document", Object.defineProperty({}, "cookie", cookieDescriptor));
|
|
101
|
-
vi.stubGlobal("atob", (v: string) => Buffer.from(v, "base64").toString("utf-8"));
|
|
102
|
-
vi.stubGlobal("window", { location: { pathname: "/dashboard", assign: locationAssign } });
|
|
103
|
-
|
|
104
|
-
const fetchMock = vi
|
|
105
|
-
.fn()
|
|
106
|
-
// Original request → 401
|
|
107
|
-
.mockResolvedValueOnce({ ok: false, status: 401, statusText: "Unauthorized", json: vi.fn().mockResolvedValue({ message: "Unauthorized" }) })
|
|
108
|
-
// BFF /api/auth/refresh → fails
|
|
109
|
-
.mockResolvedValueOnce({ ok: false, status: 401, json: vi.fn().mockResolvedValue({ message: "No refresh" }) })
|
|
110
|
-
// Direct NestJS /auth/refresh fallback → also fails
|
|
111
|
-
.mockResolvedValueOnce({ ok: false, status: 401, json: vi.fn().mockResolvedValue({ message: "No refresh direct" }) });
|
|
112
|
-
vi.stubGlobal("fetch", fetchMock);
|
|
113
|
-
|
|
114
|
-
const { api } = await import("./api");
|
|
115
|
-
await expect(api("/admin/resources/overview")).rejects.toThrow("Unauthorized");
|
|
116
|
-
expect(locationAssign).toHaveBeenCalledWith("/login");
|
|
117
|
-
});
|
|
118
|
-
});
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
// @vitest-environment jsdom
|
|
2
|
-
import { describe, expect, it, vi, afterEach } from "vitest";
|
|
3
|
-
|
|
4
|
-
function fakeJwt(payload: Record<string, unknown>): string {
|
|
5
|
-
const part = Buffer.from(JSON.stringify(payload)).toString("base64url");
|
|
6
|
-
return `header.${part}.sig`;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
describe("auth flow e2e-like (cookie-based tokens)", () => {
|
|
10
|
-
afterEach(() => {
|
|
11
|
-
vi.unstubAllGlobals();
|
|
12
|
-
vi.restoreAllMocks();
|
|
13
|
-
vi.resetModules();
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
it("expired token + failed refresh → clears cookies and redirects to login", async () => {
|
|
17
|
-
const locationAssign = vi.fn();
|
|
18
|
-
|
|
19
|
-
// Expired access token (exp in the past)
|
|
20
|
-
const expiredJwt = fakeJwt({ exp: 10 });
|
|
21
|
-
|
|
22
|
-
let cookieStore = `oajs_at=${encodeURIComponent(expiredJwt)}; oajs_rt=${encodeURIComponent("refresh-token")}`;
|
|
23
|
-
const cookieDescriptor = { get: () => cookieStore, set: (v: string) => { cookieStore = v; } };
|
|
24
|
-
vi.stubGlobal("document", Object.defineProperty({}, "cookie", cookieDescriptor));
|
|
25
|
-
vi.stubGlobal("atob", (v: string) => Buffer.from(v, "base64").toString("utf-8"));
|
|
26
|
-
vi.stubGlobal("window", { location: { pathname: "/dashboard", assign: locationAssign } });
|
|
27
|
-
|
|
28
|
-
vi.stubGlobal(
|
|
29
|
-
"fetch",
|
|
30
|
-
vi
|
|
31
|
-
.fn()
|
|
32
|
-
// Original request → 401 (no auth header since token is expired)
|
|
33
|
-
.mockResolvedValueOnce({
|
|
34
|
-
ok: false,
|
|
35
|
-
status: 401,
|
|
36
|
-
statusText: "Unauthorized",
|
|
37
|
-
json: vi.fn().mockResolvedValue({ message: "Unauthorized" })
|
|
38
|
-
})
|
|
39
|
-
// BFF /api/auth/refresh → fails
|
|
40
|
-
.mockResolvedValueOnce({
|
|
41
|
-
ok: false,
|
|
42
|
-
status: 401,
|
|
43
|
-
statusText: "Unauthorized",
|
|
44
|
-
json: vi.fn().mockResolvedValue({ message: "Bad refresh" })
|
|
45
|
-
})
|
|
46
|
-
// Direct NestJS /auth/refresh fallback → also fails
|
|
47
|
-
.mockResolvedValueOnce({
|
|
48
|
-
ok: false,
|
|
49
|
-
status: 401,
|
|
50
|
-
statusText: "Unauthorized",
|
|
51
|
-
json: vi.fn().mockResolvedValue({ message: "Bad refresh direct" })
|
|
52
|
-
})
|
|
53
|
-
);
|
|
54
|
-
|
|
55
|
-
const { api } = await import("./api");
|
|
56
|
-
await expect(api("/admin/resources/overview")).rejects.toThrow("Unauthorized");
|
|
57
|
-
expect(locationAssign).toHaveBeenCalledWith("/login");
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it("fresh token succeeds without redirect", async () => {
|
|
61
|
-
const freshJwt = fakeJwt({ exp: 9_999_999_999 });
|
|
62
|
-
const cookieStore = `oajs_at=${encodeURIComponent(freshJwt)}`;
|
|
63
|
-
vi.stubGlobal("document", { cookie: cookieStore });
|
|
64
|
-
vi.stubGlobal("atob", (v: string) => Buffer.from(v, "base64").toString("utf-8"));
|
|
65
|
-
|
|
66
|
-
vi.stubGlobal(
|
|
67
|
-
"fetch",
|
|
68
|
-
vi.fn().mockResolvedValue({
|
|
69
|
-
ok: true,
|
|
70
|
-
status: 200,
|
|
71
|
-
json: vi.fn().mockResolvedValue({ rows: [] })
|
|
72
|
-
})
|
|
73
|
-
);
|
|
74
|
-
|
|
75
|
-
const { api } = await import("./api");
|
|
76
|
-
const result = await api<{ rows: unknown[] }>("/admin/resources/posts");
|
|
77
|
-
expect(result.rows).toEqual([]);
|
|
78
|
-
});
|
|
79
|
-
});
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { defineResource } from "@openadminjs/core";
|
|
3
|
-
import { collectPermissionSlugsFromResources } from "./derive-permissions";
|
|
4
|
-
|
|
5
|
-
describe("collectPermissionSlugsFromResources", () => {
|
|
6
|
-
it("collects resource, field, action and listScope bypass slugs", () => {
|
|
7
|
-
const r = defineResource({
|
|
8
|
-
name: "widgets",
|
|
9
|
-
label: "Widgets",
|
|
10
|
-
model: "Widget",
|
|
11
|
-
permissions: { read: "widgets.read", custom: "widgets.custom" },
|
|
12
|
-
listScope: { type: "userOwns", field: "ownerId", bypassPermissions: ["widgets.read.all"] },
|
|
13
|
-
fields: {
|
|
14
|
-
id: { type: "id", list: true },
|
|
15
|
-
secret: { type: "text", list: false, permissions: { read: "widgets.secret.read" } }
|
|
16
|
-
},
|
|
17
|
-
actions: {
|
|
18
|
-
ping: { label: "Ping", permission: "widgets.ping" }
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
const set = collectPermissionSlugsFromResources([r]);
|
|
22
|
-
expect([...set].sort()).toEqual(
|
|
23
|
-
["widgets.custom", "widgets.ping", "widgets.read", "widgets.read.all", "widgets.secret.read"].sort()
|
|
24
|
-
);
|
|
25
|
-
});
|
|
26
|
-
});
|
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
import { ForbiddenException, NotFoundException } from "@nestjs/common";
|
|
2
|
-
import { describe, expect, it, vi } from "vitest";
|
|
3
|
-
|
|
4
|
-
vi.mock("../common/prisma.service.js", () => ({
|
|
5
|
-
PrismaService: class PrismaService {}
|
|
6
|
-
}));
|
|
7
|
-
|
|
8
|
-
import { AdminResourceService } from "./admin-resource.service";
|
|
9
|
-
import type { PrismaService } from "../common/prisma.service";
|
|
10
|
-
|
|
11
|
-
function prismaMock(overrides: Record<string, unknown> = {}): PrismaService {
|
|
12
|
-
const auditCreate = vi.fn().mockResolvedValue({});
|
|
13
|
-
const base = {
|
|
14
|
-
auditLog: { create: auditCreate },
|
|
15
|
-
$transaction: vi.fn().mockImplementation(async (fn: (tx: unknown) => Promise<unknown>) => {
|
|
16
|
-
const txProxy = new Proxy(
|
|
17
|
-
{ auditLog: { create: auditCreate }, ...overrides },
|
|
18
|
-
{ get: (target, key) => (target as Record<string | symbol, unknown>)[key] ?? (overrides as Record<string | symbol, unknown>)[key] }
|
|
19
|
-
);
|
|
20
|
-
return fn(txProxy);
|
|
21
|
-
}),
|
|
22
|
-
...overrides
|
|
23
|
-
};
|
|
24
|
-
return base as unknown as PrismaService;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
describe("AdminResourceService", () => {
|
|
28
|
-
it("listResources returns only resources the user may read", () => {
|
|
29
|
-
const service = new AdminResourceService(prismaMock());
|
|
30
|
-
const rows = service.listResources({ id: "1", permissions: ["posts.read"] });
|
|
31
|
-
expect(rows.some((r) => r.name === "posts")).toBe(true);
|
|
32
|
-
expect(rows.some((r) => r.name === "users")).toBe(false);
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it("listResources resolves localized labels when locale is passed", () => {
|
|
36
|
-
const service = new AdminResourceService(prismaMock());
|
|
37
|
-
const rows = service.listResources({ id: "1", permissions: ["posts.read"] }, "ru");
|
|
38
|
-
const posts = rows.find((r) => r.name === "posts");
|
|
39
|
-
expect(posts?.label).toBe("Записи");
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it("lists records with pagination metadata", async () => {
|
|
43
|
-
const findMany = vi.fn().mockResolvedValue([{ id: "a1", title: "Hello", status: "draft" }]);
|
|
44
|
-
const count = vi.fn().mockResolvedValue(1);
|
|
45
|
-
const prisma = prismaMock({
|
|
46
|
-
post: { findMany, count, findUnique: vi.fn(), create: vi.fn(), update: vi.fn(), delete: vi.fn() }
|
|
47
|
-
});
|
|
48
|
-
const service = new AdminResourceService(prisma);
|
|
49
|
-
const result = await service.list("posts", { page: "1", limit: "10" }, { id: "1", permissions: ["posts.read"] });
|
|
50
|
-
|
|
51
|
-
expect(result.meta).toEqual({ page: 1, limit: 10, total: 1, pages: 1 });
|
|
52
|
-
expect(result.data).toHaveLength(1);
|
|
53
|
-
expect(findMany).toHaveBeenCalled();
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
it("throws when resource is missing", async () => {
|
|
57
|
-
const service = new AdminResourceService(prismaMock());
|
|
58
|
-
await expect(service.list("unknown-resource", {}, { id: "1", permissions: ["*"] })).rejects.toBeInstanceOf(NotFoundException);
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
it("throws when user lacks read permission", async () => {
|
|
62
|
-
const prisma = prismaMock({
|
|
63
|
-
post: {
|
|
64
|
-
findMany: vi.fn(),
|
|
65
|
-
count: vi.fn(),
|
|
66
|
-
findUnique: vi.fn(),
|
|
67
|
-
create: vi.fn(),
|
|
68
|
-
update: vi.fn(),
|
|
69
|
-
delete: vi.fn()
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
const service = new AdminResourceService(prisma);
|
|
73
|
-
await expect(service.list("posts", {}, { id: "1", permissions: [] })).rejects.toBeInstanceOf(ForbiddenException);
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
it("creates post and writes audit log", async () => {
|
|
77
|
-
const create = vi.fn().mockResolvedValue({ id: "p1", title: "Hello", status: "draft" });
|
|
78
|
-
const prisma = prismaMock({
|
|
79
|
-
post: { findMany: vi.fn(), count: vi.fn(), findUnique: vi.fn(), create, update: vi.fn(), delete: vi.fn() }
|
|
80
|
-
});
|
|
81
|
-
const service = new AdminResourceService(prisma);
|
|
82
|
-
const result = await service.create("posts", { title: "Hello", status: "draft" }, { id: "u1", permissions: ["posts.create"] });
|
|
83
|
-
|
|
84
|
-
expect(create).toHaveBeenCalled();
|
|
85
|
-
expect((result as { id: string }).id).toBe("p1");
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it("updates post and enforces not found", async () => {
|
|
89
|
-
const findUnique = vi.fn().mockResolvedValue(null);
|
|
90
|
-
const prisma = prismaMock({
|
|
91
|
-
post: { findMany: vi.fn(), count: vi.fn(), findUnique, create: vi.fn(), update: vi.fn(), delete: vi.fn() }
|
|
92
|
-
});
|
|
93
|
-
const service = new AdminResourceService(prisma);
|
|
94
|
-
|
|
95
|
-
await expect(service.update("posts", "missing", { title: "x" }, { id: "u1", permissions: ["posts.update"] })).rejects.toBeInstanceOf(
|
|
96
|
-
NotFoundException
|
|
97
|
-
);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
it("deletes post when permission exists", async () => {
|
|
101
|
-
const deleteFn = vi.fn().mockResolvedValue({ id: "p1" });
|
|
102
|
-
const prisma = prismaMock({
|
|
103
|
-
post: {
|
|
104
|
-
findMany: vi.fn(),
|
|
105
|
-
count: vi.fn(),
|
|
106
|
-
findUnique: vi.fn().mockResolvedValue({ id: "p1", title: "Draft" }),
|
|
107
|
-
create: vi.fn(),
|
|
108
|
-
update: vi.fn(),
|
|
109
|
-
delete: deleteFn
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
const service = new AdminResourceService(prisma);
|
|
113
|
-
const result = await service.delete("posts", "p1", { id: "u1", permissions: ["posts.delete"] });
|
|
114
|
-
|
|
115
|
-
expect(deleteFn).toHaveBeenCalledWith({ where: { id: "p1" } });
|
|
116
|
-
expect(result).toEqual({ ok: true });
|
|
117
|
-
});
|
|
118
|
-
});
|