openadminjs 0.2.0 → 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 +106 -151
- package/dist/adapt-package-manager.js +4 -1
- package/dist/create-project.d.ts +46 -0
- package/dist/create-project.js +417 -150
- package/dist/doctor.d.ts +13 -0
- package/dist/doctor.js +35 -6
- package/dist/index.d.ts +1 -0
- package/dist/index.js +257 -39
- package/dist/render-schema.d.ts +0 -17
- package/dist/render-schema.js +25 -29
- 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/upgrade.d.ts +10 -0
- package/dist/upgrade.js +133 -0
- package/package.json +14 -11
- package/template/.dockerignore +15 -0
- package/template/.env.production.example +5 -0
- package/template/Dockerfile +12 -0
- package/template/README.md +4 -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 +10 -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 +12 -13
- package/template/apps/api/src/common/prisma.service.ts +9 -17
- package/template/apps/api/src/common/request-logging.interceptor.ts +8 -3
- package/template/apps/api/src/graphql/graphql.module.ts +5 -4
- 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 +6 -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/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/adapt-package-manager.test.d.ts +0 -1
- package/dist/adapt-package-manager.test.js +0 -76
- package/dist/cli.e2e.test.d.ts +0 -1
- package/dist/cli.e2e.test.js +0 -87
- package/dist/create-project.test.d.ts +0 -1
- package/dist/create-project.test.js +0 -275
- package/dist/database-providers.integration.test.d.ts +0 -1
- package/dist/database-providers.integration.test.js +0 -64
- package/dist/db-commands.test.d.ts +0 -1
- package/dist/db-commands.test.js +0 -27
- package/dist/doctor.test.d.ts +0 -1
- package/dist/doctor.test.js +0 -102
- package/dist/index.test.d.ts +0 -1
- package/dist/index.test.js +0 -11
- package/dist/render-schema.test.d.ts +0 -1
- package/dist/render-schema.test.js +0 -57
- package/dist/resource-slug.test.d.ts +0 -1
- package/dist/resource-slug.test.js +0 -9
- package/dist/secrets.test.d.ts +0 -1
- package/dist/secrets.test.js +0 -36
- package/dist/security.test.d.ts +0 -1
- package/dist/security.test.js +0 -54
- package/dist/validate-name.test.d.ts +0 -1
- package/dist/validate-name.test.js +0 -63
- 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/json-field-codec.test.ts +0 -76
- package/template/apps/api/src/common/prisma.service.test.ts +0 -19
- 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-apollo-config.test.ts +0 -26
- 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/dist/security.test.js
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
-
import { join } from "node:path";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
|
-
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
5
|
-
import { runSecurityChecks } from "./security.js";
|
|
6
|
-
import { generateSecret } from "./secrets.js";
|
|
7
|
-
let dir;
|
|
8
|
-
function sev(findings, id) {
|
|
9
|
-
return findings.find((f) => f.id === id)?.severity;
|
|
10
|
-
}
|
|
11
|
-
function writeEnv(root, content) {
|
|
12
|
-
mkdirSync(join(root, "apps", "api"), { recursive: true });
|
|
13
|
-
writeFileSync(join(root, "apps", "api", ".env"), content);
|
|
14
|
-
}
|
|
15
|
-
beforeEach(() => {
|
|
16
|
-
dir = mkdtempSync(join(tmpdir(), "oaj-sec-"));
|
|
17
|
-
});
|
|
18
|
-
afterEach(() => {
|
|
19
|
-
rmSync(dir, { recursive: true, force: true });
|
|
20
|
-
});
|
|
21
|
-
describe("runSecurityChecks", () => {
|
|
22
|
-
it("flags weak/identical JWT secrets and weak admin password as critical", () => {
|
|
23
|
-
writeEnv(dir, "JWT_SECRET=secret\nJWT_REFRESH_SECRET=secret\nSUPERADMIN_PASSWORD=admin\n");
|
|
24
|
-
writeFileSync(join(dir, ".gitignore"), ".env\n");
|
|
25
|
-
const { findings, ok } = runSecurityChecks(dir, { skipNetwork: true });
|
|
26
|
-
expect(sev(findings, "jwt-secret")).toBe("critical");
|
|
27
|
-
expect(sev(findings, "jwt-identical")).toBe("critical");
|
|
28
|
-
expect(sev(findings, "admin-password")).toBe("critical");
|
|
29
|
-
expect(ok).toBe(false);
|
|
30
|
-
});
|
|
31
|
-
it("flags missing .env exclusion in .gitignore as critical", () => {
|
|
32
|
-
writeEnv(dir, `JWT_SECRET=${generateSecret()}\nJWT_REFRESH_SECRET=${generateSecret()}\n`);
|
|
33
|
-
writeFileSync(join(dir, ".gitignore"), "node_modules\n");
|
|
34
|
-
const { findings, ok } = runSecurityChecks(dir, { skipNetwork: true });
|
|
35
|
-
expect(sev(findings, "gitignore-env")).toBe("critical");
|
|
36
|
-
expect(ok).toBe(false);
|
|
37
|
-
});
|
|
38
|
-
it("passes for a hardened project", () => {
|
|
39
|
-
writeEnv(dir, `JWT_SECRET=${generateSecret()}\nJWT_REFRESH_SECRET=${generateSecret()}\nSUPERADMIN_PASSWORD=a-strong-passphrase\n`);
|
|
40
|
-
writeFileSync(join(dir, ".gitignore"), ".env\n.env.*\n!.env.example\n");
|
|
41
|
-
const { findings, ok } = runSecurityChecks(dir, { skipNetwork: true });
|
|
42
|
-
expect(ok).toBe(true);
|
|
43
|
-
expect(sev(findings, "jwt-ok")).toBe("info");
|
|
44
|
-
});
|
|
45
|
-
it("detects CORS '*' with credentials", () => {
|
|
46
|
-
writeEnv(dir, `JWT_SECRET=${generateSecret()}\nJWT_REFRESH_SECRET=${generateSecret()}\n`);
|
|
47
|
-
writeFileSync(join(dir, ".gitignore"), ".env\n");
|
|
48
|
-
mkdirSync(join(dir, "apps", "api", "src"), { recursive: true });
|
|
49
|
-
writeFileSync(join(dir, "apps", "api", "src", "main.ts"), "app.enableCors({ origin: '*', credentials: true });\n");
|
|
50
|
-
const { findings, ok } = runSecurityChecks(dir, { skipNetwork: true });
|
|
51
|
-
expect(sev(findings, "cors-credentials")).toBe("critical");
|
|
52
|
-
expect(ok).toBe(false);
|
|
53
|
-
});
|
|
54
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { assertInsideDir, InvalidNameError, toCamelCase, toKebabCase, toPascalCase, validateName, validatePluginId } from "./validate-name.js";
|
|
3
|
-
describe("case conversion", () => {
|
|
4
|
-
it("converts to PascalCase / camelCase / kebab-case", () => {
|
|
5
|
-
expect(toPascalCase("order item")).toBe("OrderItem");
|
|
6
|
-
expect(toPascalCase("order-item")).toBe("OrderItem");
|
|
7
|
-
expect(toPascalCase("OrderItem")).toBe("OrderItem");
|
|
8
|
-
expect(toCamelCase("order-item")).toBe("orderItem");
|
|
9
|
-
expect(toKebabCase("OrderItem")).toBe("order-item");
|
|
10
|
-
expect(toKebabCase("Order Item")).toBe("order-item");
|
|
11
|
-
});
|
|
12
|
-
});
|
|
13
|
-
describe("validateName - rejects unsafe input", () => {
|
|
14
|
-
const bad = [
|
|
15
|
-
"../admin",
|
|
16
|
-
"../../outside",
|
|
17
|
-
"foo/bar",
|
|
18
|
-
"foo\\bar",
|
|
19
|
-
'"; process.exit()',
|
|
20
|
-
"class",
|
|
21
|
-
"CON",
|
|
22
|
-
".",
|
|
23
|
-
"..",
|
|
24
|
-
"",
|
|
25
|
-
" ",
|
|
26
|
-
"/etc/passwd",
|
|
27
|
-
"foo\0bar",
|
|
28
|
-
"1name",
|
|
29
|
-
"-name"
|
|
30
|
-
];
|
|
31
|
-
for (const input of bad) {
|
|
32
|
-
it(`rejects ${JSON.stringify(input)}`, () => {
|
|
33
|
-
expect(() => validateName(input, "resource")).toThrow(InvalidNameError);
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
describe("validateName - accepts valid input", () => {
|
|
38
|
-
for (const input of ["Post", "order-item", "order_item", "BlogPost", "user2"]) {
|
|
39
|
-
it(`accepts ${JSON.stringify(input)}`, () => {
|
|
40
|
-
expect(validateName(input, "resource")).toBe(input.trim());
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
describe("validatePluginId", () => {
|
|
45
|
-
it("accepts reverse-DNS style ids", () => {
|
|
46
|
-
expect(validatePluginId("com.example.my-plugin")).toBe("com.example.my-plugin");
|
|
47
|
-
expect(validatePluginId("seo")).toBe("seo");
|
|
48
|
-
});
|
|
49
|
-
it("rejects traversal and injection attempts", () => {
|
|
50
|
-
for (const bad of ["../evil", "a/../b", "foo/bar", "a..b", "$(x)", "a;b", ".hidden", "1abc"]) {
|
|
51
|
-
expect(() => validatePluginId(bad), bad).toThrow(InvalidNameError);
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
describe("assertInsideDir", () => {
|
|
56
|
-
it("allows paths inside the base directory", () => {
|
|
57
|
-
expect(() => assertInsideDir("/tmp/proj", "apps/api/x.ts")).not.toThrow();
|
|
58
|
-
});
|
|
59
|
-
it("rejects traversal outside the base directory", () => {
|
|
60
|
-
expect(() => assertInsideDir("/tmp/proj", "../../etc/passwd")).toThrow(InvalidNameError);
|
|
61
|
-
expect(() => assertInsideDir("/tmp/proj", "/etc/passwd")).toThrow(InvalidNameError);
|
|
62
|
-
});
|
|
63
|
-
});
|
|
@@ -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
|
-
});
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { UnprocessableEntityException } from "@nestjs/common";
|
|
2
|
-
import { describe, expect, it } from "vitest";
|
|
3
|
-
import { defineResource } from "@openadminjs/core";
|
|
4
|
-
import { parseWritePayload } from "./validate-resource-payload";
|
|
5
|
-
|
|
6
|
-
describe("parseWritePayload", () => {
|
|
7
|
-
const widget = defineResource({
|
|
8
|
-
name: "widgets",
|
|
9
|
-
label: "Widgets",
|
|
10
|
-
model: "Widget",
|
|
11
|
-
permissions: { read: "w.read", create: "w.create", update: "w.update" },
|
|
12
|
-
fields: {
|
|
13
|
-
id: { type: "id", list: true },
|
|
14
|
-
title: { type: "text", required: true, list: true },
|
|
15
|
-
count: { type: "number", list: true },
|
|
16
|
-
mode: { type: "select", options: ["a", "b"], list: true },
|
|
17
|
-
published: { type: "boolean", required: true, list: true }
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
it("accepts valid create payload", () => {
|
|
22
|
-
const out = parseWritePayload(widget, "create", { title: "Hi", count: "3", mode: "a", published: true });
|
|
23
|
-
expect(out.title).toBe("Hi");
|
|
24
|
-
expect(out.count).toBe(3);
|
|
25
|
-
expect(out.mode).toBe("a");
|
|
26
|
-
expect(out.published).toBe(true);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it("rejects missing required field on create", () => {
|
|
30
|
-
expect(() => parseWritePayload(widget, "create", { count: 1 })).toThrow(UnprocessableEntityException);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it("rejects missing required boolean on create (does not default to false)", () => {
|
|
34
|
-
expect(() => parseWritePayload(widget, "create", { title: "x", count: 0, mode: "a" })).toThrow(
|
|
35
|
-
UnprocessableEntityException
|
|
36
|
-
);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it("rejects unknown keys (strict)", () => {
|
|
40
|
-
expect(() =>
|
|
41
|
-
parseWritePayload(widget, "create", { title: "x", mode: "a", published: false, extra: 1 })
|
|
42
|
-
).toThrow(UnprocessableEntityException);
|
|
43
|
-
});
|
|
44
|
-
});
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { throttleIpTracker } from "./app.module";
|
|
3
|
-
|
|
4
|
-
describe("app throttling", () => {
|
|
5
|
-
it("handles GraphQL requests without an HTTP request object", () => {
|
|
6
|
-
expect(throttleIpTracker(undefined)).toBe("unknown");
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
it("uses the request IP for HTTP requests", () => {
|
|
10
|
-
expect(throttleIpTracker({ ip: "127.0.0.1" })).toBe("127.0.0.1");
|
|
11
|
-
});
|
|
12
|
-
});
|