Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/utils/services/rolesAndPermissionsService.seed-diagnostics.test.ts
T

192 lines
8.9 KiB
TypeScript

import { beforeEach, describe, expect, test, vi } from "vitest";
vi.mock("~/utils/db/permissions/roles.db.server", () => ({ createRole: vi.fn(), getAllRoles: vi.fn() }));
vi.mock("~/utils/db/permissions/userRoles.db.server", () => ({ createUserRoleAssignments: vi.fn() }));
vi.mock("~/utils/db/tenants.db.server", () => ({ adminGetAllTenants: vi.fn() }));
vi.mock("~/utils/db/users.db.server", () => ({ adminGetAllUsers: vi.fn() }));
vi.mock("~/utils/db.server", () => ({
db: {},
}));
vi.mock("@hivegrid/db/permissions-seed.server", async () => {
const actual = await vi.importActual<typeof import("@hivegrid/db/permissions-seed.server")>("@hivegrid/db/permissions-seed.server");
return {
...actual,
syncDefaultRolesAndPermissions: vi.fn(async () => ({
roleCount: actual.defaultAdminRoleSeeds.length + actual.defaultAppRoleSeeds.length,
permissionCount: actual.defaultPermissionSeeds.length,
createdPermissions: 0,
createdRolePermissions: 0,
})),
};
});
import { getAllRoles } from "~/utils/db/permissions/roles.db.server";
import { createUserRoleAssignments } from "~/utils/db/permissions/userRoles.db.server";
import { adminGetAllTenants } from "~/utils/db/tenants.db.server";
import { adminGetAllUsers } from "~/utils/db/users.db.server";
import { syncDefaultRolesAndPermissions } from "@hivegrid/db/permissions-seed.server";
import { defaultAdminRoles, defaultAppRoles, defaultPermissions, seedRolesAndPermissions, type RolesAndPermissionsSeedDiagnosticEvent } from "./rolesAndPermissionsService";
const getAllRolesMock = vi.mocked(getAllRoles);
const createUserRoleAssignmentsMock = vi.mocked(createUserRoleAssignments);
const adminGetAllTenantsMock = vi.mocked(adminGetAllTenants);
const adminGetAllUsersMock = vi.mocked(adminGetAllUsers);
const syncDefaultRolesAndPermissionsMock = vi.mocked(syncDefaultRolesAndPermissions);
type RoleRow = Awaited<ReturnType<typeof getAllRoles>>[number];
const roleBase = {
type: "admin",
createdAt: new Date("2026-01-01T00:00:00.000Z"),
updatedAt: new Date("2026-01-01T00:00:00.000Z"),
description: "Test role",
order: 0,
isDefault: true,
assignToNewUsers: false,
permissions: [],
};
const adminRoleRows = defaultAdminRoles.map((role, index) => ({ ...roleBase, id: `admin-role-${index}`, name: role.name, type: role.type }) as RoleRow);
const appRoleRows = defaultAppRoles.map((role, index) => ({ ...roleBase, id: `app-role-${index}`, name: role.name, type: role.type }) as RoleRow);
const allRoleRows = [...adminRoleRows, ...appRoleRows];
function serializedDiagnostics(events: RolesAndPermissionsSeedDiagnosticEvent[]) {
return JSON.stringify(events);
}
describe("seedRolesAndPermissions diagnostics", () => {
beforeEach(() => {
vi.clearAllMocks();
getAllRolesMock.mockImplementation(async (type?: string) => {
if (type === "admin") return adminRoleRows;
if (type === "app") return appRoleRows;
return allRoleRows;
});
adminGetAllUsersMock.mockResolvedValue({
items: [
{ id: "admin-user-id", email: "admin@email.com", admin: { userId: "admin-user-id" }, passwordHash: "hashed-password" },
{ id: "other-admin-id", email: "other@example.com", admin: { userId: "other-admin-id" }, token: "sk_live_secret" },
{ id: "app-user-id", email: "app@example.com", admin: null },
] as Awaited<ReturnType<typeof adminGetAllUsers>>["items"],
pagination: {} as Awaited<ReturnType<typeof adminGetAllUsers>>["pagination"],
});
adminGetAllTenantsMock.mockResolvedValue([
{
id: "tenant-1",
slug: "tenant-1",
users: [
{ userId: "admin-user-id" } as Awaited<ReturnType<typeof adminGetAllTenants>>[number]["users"][number],
{ userId: "app-user-id" } as Awaited<ReturnType<typeof adminGetAllTenants>>[number]["users"][number],
],
} as Awaited<ReturnType<typeof adminGetAllTenants>>[number],
]);
createUserRoleAssignmentsMock.mockResolvedValue({ count: adminRoleRows.length + 2 * appRoleRows.length } as never);
syncDefaultRolesAndPermissionsMock.mockResolvedValue({
roleCount: defaultAdminRoles.length + defaultAppRoles.length,
permissionCount: defaultPermissions.length,
createdPermissions: 0,
createdRolePermissions: 0,
} as Awaited<ReturnType<typeof syncDefaultRolesAndPermissions>>);
});
test("emits bounded start and success counts without credential fields or raw user records", async () => {
const diagnostics = vi.fn();
await seedRolesAndPermissions({ adminEmail: "admin@email.com", diagnostics });
expect(syncDefaultRolesAndPermissionsMock).toHaveBeenCalledOnce();
const events = diagnostics.mock.calls.map(([event]) => event as RolesAndPermissionsSeedDiagnosticEvent);
expect(events).toEqual([
{
phase: "start",
adminRoleCount: defaultAdminRoles.length,
appRoleCount: defaultAppRoles.length,
defaultPermissionCount: defaultPermissions.length,
dynamicEntityPermissionCount: 0,
adminUserAssignmentCount: 0,
tenantUserAssignmentCount: 0,
},
{
phase: "success",
adminRoleCount: adminRoleRows.length,
appRoleCount: appRoleRows.length,
defaultPermissionCount: defaultPermissions.length,
dynamicEntityPermissionCount: 0,
adminUserAssignmentCount: adminRoleRows.length,
tenantUserAssignmentCount: 2 * appRoleRows.length,
},
]);
expect(serializedDiagnostics(events)).not.toContain("password");
expect(serializedDiagnostics(events)).not.toContain("token");
expect(serializedDiagnostics(events)).not.toContain("hashed-password");
expect(serializedDiagnostics(events)).not.toContain("sk_live_secret");
expect(serializedDiagnostics(events)).not.toContain("admin-user-id");
expect(createUserRoleAssignmentsMock).toHaveBeenCalledWith(
expect.arrayContaining([
{ userId: "admin-user-id", roleId: adminRoleRows[0]?.id, tenantId: null },
{ userId: "admin-user-id", roleId: appRoleRows[0]?.id, tenantId: "tenant-1" },
{ userId: "app-user-id", roleId: appRoleRows[0]?.id, tenantId: "tenant-1" },
])
);
});
test("preserves the legacy adminEmail argument while assigning the seeded admin user", async () => {
await seedRolesAndPermissions("admin@email.com");
expect(createUserRoleAssignmentsMock).toHaveBeenCalledWith(
expect.arrayContaining([{ userId: "admin-user-id", roleId: adminRoleRows[0]?.id, tenantId: null }])
);
});
test("excludes managed tenants from boot-time app role assignments", async () => {
adminGetAllTenantsMock.mockResolvedValueOnce([
{
id: "tenant-1",
slug: "tenant-1",
users: [{ userId: "app-user-id" } as Awaited<ReturnType<typeof adminGetAllTenants>>[number]["users"][number]],
} as Awaited<ReturnType<typeof adminGetAllTenants>>[number],
{
id: "tenant-pro",
slug: "demo-pro-crm",
users: [{ userId: "managed-user-id" } as Awaited<ReturnType<typeof adminGetAllTenants>>[number]["users"][number]],
} as Awaited<ReturnType<typeof adminGetAllTenants>>[number],
]);
const diagnostics = vi.fn();
await seedRolesAndPermissions({
adminEmail: "admin@email.com",
diagnostics,
excludedTenantSlugs: ["demo-pro-crm"],
});
const assignments = createUserRoleAssignmentsMock.mock.calls[0]?.[0] ?? [];
expect(assignments).toEqual(expect.arrayContaining([{ userId: "app-user-id", roleId: appRoleRows[0]?.id, tenantId: "tenant-1" }]));
expect(assignments).not.toEqual(expect.arrayContaining([{ userId: "managed-user-id", roleId: appRoleRows[0]?.id, tenantId: "tenant-pro" }]));
expect(diagnostics).toHaveBeenLastCalledWith(expect.objectContaining({ phase: "success", tenantUserAssignmentCount: appRoleRows.length }));
});
test("emits failure diagnostics and rethrows the original seed error", async () => {
const diagnostics = vi.fn();
const originalError = new Error("seed failed without password token literals");
adminGetAllUsersMock.mockRejectedValueOnce(originalError);
await expect(seedRolesAndPermissions({ adminEmail: "admin@email.com", diagnostics })).rejects.toBe(originalError);
const events = diagnostics.mock.calls.map(([event]) => event as RolesAndPermissionsSeedDiagnosticEvent);
expect(events).toHaveLength(2);
expect(events[1]).toMatchObject({
phase: "failure",
adminRoleCount: adminRoleRows.length,
appRoleCount: appRoleRows.length,
defaultPermissionCount: defaultPermissions.length,
dynamicEntityPermissionCount: 0,
adminUserAssignmentCount: 0,
tenantUserAssignmentCount: 0,
error: originalError,
});
expect(serializedDiagnostics(events)).not.toContain("password");
expect(serializedDiagnostics(events)).not.toContain("token");
expect(serializedDiagnostics(events)).not.toContain("hashed-password");
expect(serializedDiagnostics(events)).not.toContain("sk_live_secret");
});
});