Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/foundation/src/permissions.test.ts
T
2026-07-08 16:15:37 +02:00

84 lines
3.1 KiB
TypeScript

import { describe, expect, test, vi } from "vitest";
import { CatalogError } from "./errors.js";
import { requirePermission, withOwnerCheck, withPermission, withTenantScope } from "./permissions.js";
describe("requirePermission", () => {
test("returns the context unchanged when the permission is present in an array", () => {
const context = { permissions: ["resource.view", "resource.edit"] };
expect(requirePermission(context, "resource.edit")).toBe(context);
});
test("accepts a ReadonlySet of permissions", () => {
const context = { permissions: new Set(["resource.view"]) };
expect(requirePermission(context, "resource.view")).toBe(context);
});
test("throws a not-found catalog error when the permission is missing", () => {
try {
requirePermission({ permissions: ["resource.view"] }, "resource.delete");
throw new Error("expected throw");
} catch (error) {
expect(error).toBeInstanceOf(CatalogError);
expect((error as CatalogError).code).toBe("foundation.not_found");
expect((error as CatalogError).details).toEqual({ permission: "resource.delete" });
}
});
test("treats an absent permissions field as an empty permission set", () => {
expect(() => requirePermission({}, "resource.view")).toThrow(CatalogError);
});
});
describe("withPermission", () => {
test("invokes the handler with the original arguments when the permission is granted", () => {
const handler = vi.fn((a: number, b: number) => a + b);
const guarded = withPermission(
"calc.run",
() => ({ permissions: ["calc.run"] }),
handler,
);
expect(guarded(2, 3)).toBe(5);
expect(handler).toHaveBeenCalledWith(2, 3);
});
test("rejects before the handler runs when the permission is missing", () => {
const handler = vi.fn();
const guarded = withPermission("calc.run", () => ({ permissions: [] }), handler);
expect(() => guarded()).toThrow(CatalogError);
expect(handler).not.toHaveBeenCalled();
});
});
describe("withOwnerCheck", () => {
test("returns the value when the owner matches the acting user", () => {
const value = { ownerId: "user_1", payload: "secret" };
expect(withOwnerCheck({ userId: "user_1" }, value)).toBe(value);
});
test("hides ownership mismatches behind a not-found error", () => {
expect(() => withOwnerCheck({ userId: "user_1" }, { ownerId: "user_2" })).toThrow(/foundation.not_found/);
});
test("rejects when the value is null or undefined", () => {
expect(() => withOwnerCheck({ userId: "user_1" }, null)).toThrow(CatalogError);
expect(() => withOwnerCheck({ userId: "user_1" }, undefined)).toThrow(CatalogError);
});
test("rejects when the context has no acting user", () => {
expect(() => withOwnerCheck({}, { ownerId: "user_1" })).toThrow(CatalogError);
});
});
describe("withTenantScope", () => {
test("attaches the tenant id without mutating the source object", () => {
const value = { id: "row_1" };
const scoped = withTenantScope("tenant_9", value);
expect(scoped).toEqual({ id: "row_1", tenantId: "tenant_9" });
expect(value).not.toHaveProperty("tenantId");
});
});