172 lines
7.1 KiB
TypeScript
172 lines
7.1 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { LastSuperAdminError, adminHasPermission, disableAdminUser, replaceAdminSystemRoles, syncAdminRoleCatalog } from "./admin-rbac.server";
|
|
import { ADMIN_PERMISSIONS, ADMIN_ROLE_DEFINITIONS } from "./admin-permissions";
|
|
|
|
function transactionClient(overrides: Record<string, unknown> = {}) {
|
|
const transaction = {
|
|
adminPermission: {
|
|
upsert: vi.fn(async ({ where }: { where: { name: string } }) => ({ id: `permission:${where.name}`, name: where.name })),
|
|
},
|
|
adminRole: {
|
|
upsert: vi.fn(async ({ where }: { where: { systemKey: string } }) => ({ id: `role:${where.systemKey}` })),
|
|
findUnique: vi.fn<(args: { where: { systemKey: string } }) => Promise<{ id: string } | null>>(async () => null),
|
|
findMany: vi.fn(async ({ where }: { where: { systemKey: { in: string[] } } }) =>
|
|
where.systemKey.in.map((systemKey) => ({ id: `role:${systemKey}`, systemKey }))
|
|
),
|
|
},
|
|
adminRolePermission: {
|
|
createMany: vi.fn<
|
|
(args: {
|
|
data: Array<{ adminRoleId: string; adminPermissionId: string }>;
|
|
skipDuplicates: boolean;
|
|
}) => Promise<{ count: number }>
|
|
>(async () => ({ count: 1 })),
|
|
deleteMany: vi.fn(async () => ({ count: 0 })),
|
|
findFirst: vi.fn(),
|
|
findMany: vi.fn(),
|
|
},
|
|
adminUser: {
|
|
count: vi.fn(async () => 1),
|
|
findUnique: vi.fn(),
|
|
update: vi.fn(),
|
|
},
|
|
adminSession: {
|
|
updateMany: vi.fn(async () => ({ count: 1 })),
|
|
},
|
|
adminUserRole: {
|
|
createMany: vi.fn(async () => ({ count: 1 })),
|
|
deleteMany: vi.fn(async () => ({ count: 1 })),
|
|
},
|
|
...overrides,
|
|
};
|
|
return {
|
|
transaction,
|
|
client: {
|
|
...transaction,
|
|
$transaction: vi.fn(async (runner: (tx: typeof transaction) => Promise<unknown>) => runner(transaction)),
|
|
},
|
|
};
|
|
}
|
|
|
|
describe("admin RBAC", () => {
|
|
it("synchronizes the full typed role catalog idempotently", async () => {
|
|
const { client, transaction } = transactionClient();
|
|
await expect(syncAdminRoleCatalog(client as never)).resolves.toEqual({
|
|
permissionCount: ADMIN_PERMISSIONS.length,
|
|
roleCount: ADMIN_ROLE_DEFINITIONS.length,
|
|
});
|
|
expect(transaction.adminPermission.upsert).toHaveBeenCalledTimes(ADMIN_PERMISSIONS.length);
|
|
expect(transaction.adminRole.upsert).toHaveBeenCalledTimes(ADMIN_ROLE_DEFINITIONS.length);
|
|
expect(transaction.adminRolePermission.createMany).toHaveBeenCalledTimes(ADMIN_ROLE_DEFINITIONS.length);
|
|
});
|
|
|
|
it("preserves explicitly configured permission bundles on later catalog syncs", async () => {
|
|
const { client, transaction } = transactionClient();
|
|
transaction.adminRole.findUnique.mockResolvedValue({ id: "role:existing" });
|
|
await syncAdminRoleCatalog(client as never);
|
|
expect(transaction.adminRole.upsert).toHaveBeenCalledTimes(ADMIN_ROLE_DEFINITIONS.length);
|
|
expect(transaction.adminRolePermission.createMany).toHaveBeenCalledOnce();
|
|
expect(transaction.adminRolePermission.createMany).toHaveBeenCalledWith({
|
|
data: ADMIN_PERMISSIONS.map((permission) => ({
|
|
adminRoleId: "role:super_admin",
|
|
adminPermissionId: `permission:${permission}`,
|
|
})),
|
|
skipDuplicates: true,
|
|
});
|
|
});
|
|
|
|
it("backfills every new catalog permission into an existing super admin role", async () => {
|
|
const { client, transaction } = transactionClient();
|
|
transaction.adminRole.findUnique.mockImplementation(async ({ where }: { where: { systemKey: string } }) =>
|
|
where.systemKey === "super_admin" ? { id: "role:super_admin" } : { id: `role:${where.systemKey}` }
|
|
);
|
|
|
|
await syncAdminRoleCatalog(client as never);
|
|
|
|
const superAdminGrant = transaction.adminRolePermission.createMany.mock.calls[0]?.[0];
|
|
expect(transaction.adminRolePermission.createMany).toHaveBeenCalledOnce();
|
|
expect(superAdminGrant?.data).toHaveLength(ADMIN_PERMISSIONS.length);
|
|
expect(superAdminGrant?.data).toEqual(
|
|
expect.arrayContaining([
|
|
{ adminRoleId: "role:super_admin", adminPermissionId: "permission:admin_sales.crm.read_all" },
|
|
{ adminRoleId: "role:super_admin", adminPermissionId: "permission:admin_sales.crm.write_all" },
|
|
{ adminRoleId: "role:super_admin", adminPermissionId: "permission:admin_sales.communication.read_all" },
|
|
{ adminRoleId: "role:super_admin", adminPermissionId: "permission:admin_sales.reporting.read_all" },
|
|
])
|
|
);
|
|
});
|
|
|
|
it("checks productive permissions through dedicated admin role grants", async () => {
|
|
const { client, transaction } = transactionClient();
|
|
transaction.adminRolePermission.findFirst.mockResolvedValueOnce({ adminRoleId: "role:sales" });
|
|
await expect(adminHasPermission("admin-1", "admin_sales.crm.write", client as never)).resolves.toBe(true);
|
|
expect(transaction.adminRolePermission.findFirst).toHaveBeenCalledWith({
|
|
where: {
|
|
permission: { name: "admin_sales.crm.write" },
|
|
role: { active: true, users: { some: { adminUserId: "admin-1" } } },
|
|
},
|
|
select: { adminRoleId: true },
|
|
});
|
|
});
|
|
|
|
it("refuses to remove the final active super admin", async () => {
|
|
const { client, transaction } = transactionClient();
|
|
transaction.adminUser.findUnique.mockResolvedValue({
|
|
id: "admin-1",
|
|
status: "active",
|
|
roles: [{ role: { systemKey: "super_admin" } }],
|
|
});
|
|
transaction.adminUser.count.mockResolvedValue(0);
|
|
await expect(
|
|
replaceAdminSystemRoles({
|
|
adminUserId: "admin-1",
|
|
roleKeys: ["viewer"],
|
|
grantedByAdminUserId: "admin-1",
|
|
client: client as never,
|
|
})
|
|
).rejects.toBeInstanceOf(LastSuperAdminError);
|
|
expect(transaction.adminUserRole.deleteMany).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("revokes every active session when assigned roles change", async () => {
|
|
const { client, transaction } = transactionClient();
|
|
transaction.adminUser.findUnique.mockResolvedValue({
|
|
id: "admin-2",
|
|
status: "active",
|
|
roles: [{ role: { systemKey: "viewer" } }],
|
|
});
|
|
|
|
await expect(
|
|
replaceAdminSystemRoles({
|
|
adminUserId: "admin-2",
|
|
roleKeys: ["sales", "viewer"],
|
|
grantedByAdminUserId: "admin-1",
|
|
client: client as never,
|
|
})
|
|
).resolves.toBe(true);
|
|
|
|
expect(transaction.adminSession.updateMany).toHaveBeenCalledWith({
|
|
where: { adminUserId: "admin-2", revokedAt: null },
|
|
data: { revokedAt: expect.any(Date) },
|
|
});
|
|
});
|
|
|
|
it("disables a non-final admin and revokes every active admin session atomically", async () => {
|
|
const { client, transaction } = transactionClient();
|
|
transaction.adminUser.findUnique.mockResolvedValue({
|
|
id: "admin-2",
|
|
roles: [{ role: { systemKey: "sales" } }],
|
|
});
|
|
await expect(disableAdminUser({ adminUserId: "admin-2", reason: "Employee left", client: client as never })).resolves.toBe(true);
|
|
expect(transaction.adminUser.update).toHaveBeenCalledWith({
|
|
where: { id: "admin-2" },
|
|
data: { status: "disabled", disabledAt: expect.any(Date), disabledReason: "Employee left" },
|
|
});
|
|
expect(transaction.adminSession.updateMany).toHaveBeenCalledWith({
|
|
where: { adminUserId: "admin-2", revokedAt: null },
|
|
data: { revokedAt: expect.any(Date) },
|
|
});
|
|
});
|
|
});
|