58 lines
2.5 KiB
TypeScript
58 lines
2.5 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const dbMock = vi.hoisted(() => ({
|
|
permission: { findMany: vi.fn() },
|
|
role: { findFirst: vi.fn(), create: vi.fn() },
|
|
rolePermission: { findFirst: vi.fn(), create: vi.fn() },
|
|
}));
|
|
vi.mock("~/utils/db.server", () => ({ db: dbMock, dbRead: dbMock }));
|
|
|
|
import { SHK_ROLE_NAMES, SHK_ROLE_SEEDS, seedShkRolesForTenant } from "./shkRoles.server";
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
describe("SHK role definitions", () => {
|
|
it("defines the six partner-named roles with Geschaeftsfuehrer as admin tier", () => {
|
|
expect(SHK_ROLE_NAMES).toEqual(["Geschäftsführer", "Projektleiter", "Verwaltung", "Vertriebler", "Kundensupporter", "Monteur"]);
|
|
expect(SHK_ROLE_SEEDS.find((role) => role.name === "Geschäftsführer")?.tier).toBe("admin");
|
|
});
|
|
|
|
it("gives every role at least dashboard access as the P2 baseline", () => {
|
|
for (const role of SHK_ROLE_SEEDS) {
|
|
expect(role.permissionKeys).toContain("app.dashboard.view");
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("seedShkRolesForTenant", () => {
|
|
it("creates the missing roles as tenant-scoped app roles and links resolvable permissions", async () => {
|
|
dbMock.permission.findMany.mockResolvedValue([{ id: "perm_dash", name: "app.dashboard.view" }]);
|
|
dbMock.role.findFirst.mockResolvedValue(null);
|
|
dbMock.role.create.mockImplementation(async ({ data }: { data: { name: string } }) => ({ id: `role_${data.name}` }));
|
|
dbMock.rolePermission.findFirst.mockResolvedValue(null);
|
|
dbMock.rolePermission.create.mockResolvedValue({});
|
|
|
|
const result = await seedShkRolesForTenant("t1");
|
|
|
|
expect(result.createdRoles).toBe(6);
|
|
expect(dbMock.role.create).toHaveBeenCalledTimes(6);
|
|
expect(dbMock.role.create).toHaveBeenCalledWith(
|
|
expect.objectContaining({ data: expect.objectContaining({ tenantId: "t1", name: "Geschäftsführer", type: "app", assignToNewUsers: false }) })
|
|
);
|
|
// Nur die aufloesbare Permission (dashboard) wird verknuepft.
|
|
expect(dbMock.rolePermission.create).toHaveBeenCalledWith(expect.objectContaining({ data: expect.objectContaining({ permissionId: "perm_dash" }) }));
|
|
});
|
|
|
|
it("is idempotent: existing roles are not recreated", async () => {
|
|
dbMock.permission.findMany.mockResolvedValue([]);
|
|
dbMock.role.findFirst.mockResolvedValue({ id: "existing" });
|
|
|
|
const result = await seedShkRolesForTenant("t1");
|
|
|
|
expect(result.createdRoles).toBe(0);
|
|
expect(dbMock.role.create).not.toHaveBeenCalled();
|
|
});
|
|
});
|