91 lines
3.9 KiB
TypeScript
91 lines
3.9 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 }));
|
|
const configMock = vi.hoisted(() => ({ nodeEnv: "test" }));
|
|
vi.mock("@hivegrid/config", () => ({ loadWebConfig: () => ({ config: { nodeEnv: configMock.nodeEnv } }) }));
|
|
|
|
import { SHK_ROLE_NAMES, SHK_ROLE_SEEDS, seedShkRolesForTenant, shouldSeedShkRoles } from "./shkRoles.server";
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
configMock.nodeEnv = "test";
|
|
});
|
|
|
|
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("shouldSeedShkRoles (production seeds admin-only)", () => {
|
|
it("skips the SHK seeds on production by default (David's D-P2 decision)", () => {
|
|
configMock.nodeEnv = "production";
|
|
expect(shouldSeedShkRoles({})).toBe(false);
|
|
});
|
|
|
|
it("seeds on staging (SENTRY_ENVIRONMENT wins over NODE_ENV=production) and local dev", () => {
|
|
// Staging containers run NODE_ENV=production — the deploy-scoped env var decides.
|
|
configMock.nodeEnv = "production";
|
|
expect(shouldSeedShkRoles({ SENTRY_ENVIRONMENT: "staging" })).toBe(true);
|
|
configMock.nodeEnv = "development";
|
|
expect(shouldSeedShkRoles({})).toBe(true);
|
|
});
|
|
|
|
it("lets SEED_SHK_ROLES override in both directions", () => {
|
|
configMock.nodeEnv = "production";
|
|
expect(shouldSeedShkRoles({ SEED_SHK_ROLES: "true" })).toBe(true);
|
|
configMock.nodeEnv = "test";
|
|
expect(shouldSeedShkRoles({ SEED_SHK_ROLES: "false" })).toBe(false);
|
|
});
|
|
|
|
it("makes seedShkRolesForTenant a no-op on production", async () => {
|
|
configMock.nodeEnv = "production";
|
|
const result = await seedShkRolesForTenant("t1");
|
|
expect(result).toEqual({ createdRoles: 0 });
|
|
expect(dbMock.permission.findMany).not.toHaveBeenCalled();
|
|
expect(dbMock.role.create).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
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();
|
|
});
|
|
});
|