316 lines
10 KiB
TypeScript
316 lines
10 KiB
TypeScript
import type {
|
|
AdminPermissionName,
|
|
AdminRoleKey,
|
|
} from "@hivegrid/db/admin-permissions";
|
|
import {
|
|
ADMIN_ROLE_DEFINITIONS,
|
|
permissionsForAdminRoles,
|
|
} from "@hivegrid/db/admin-permissions";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
import { AdminSidebar } from "~/application/sidebar/AdminSidebar";
|
|
import { getSidebarCommandActions } from "~/services/command-registry/commandRegistry";
|
|
import { filterCommandActions } from "~/services/search/commandSearch";
|
|
import {
|
|
ADMIN_AREA_ACCESS,
|
|
ADMIN_ROUTE_ACCESS,
|
|
} from "./admin-access-contract";
|
|
import type { AdminDashboardDataSources } from "./admin-dashboard-personalization.server";
|
|
import { loadPersonalizedAdminDashboard } from "./admin-dashboard-personalization.server";
|
|
|
|
const authMocks = vi.hoisted(() => ({
|
|
adminHasPermission: vi.fn(),
|
|
getUserInfo: vi.fn(),
|
|
getValidatedServerSession: vi.fn(),
|
|
runWithRlsContext: vi.fn(
|
|
async (
|
|
_context: unknown,
|
|
operation: () => Promise<unknown> | unknown
|
|
) => operation()
|
|
),
|
|
}));
|
|
|
|
vi.mock("@hivegrid/db/admin-rbac.server", () => ({
|
|
adminHasPermission: authMocks.adminHasPermission,
|
|
}));
|
|
|
|
vi.mock("@hivegrid/db/rls-context.server", () => ({
|
|
runWithRlsContext: authMocks.runWithRlsContext,
|
|
}));
|
|
|
|
vi.mock("~/utils/session.server", () => ({
|
|
getUserInfo: authMocks.getUserInfo,
|
|
getValidatedServerSession: authMocks.getValidatedServerSession,
|
|
}));
|
|
|
|
import { requireAdminIdentityPermission } from "./admin-identity.server";
|
|
|
|
const t = vi.fn((key: string) => key) as never;
|
|
|
|
const sources = {
|
|
salesOwn: vi.fn(async () => [
|
|
{ id: "sales_own_pipeline" as const, value: "10 €", href: "/sales/deals" },
|
|
{ id: "sales_today" as const, value: "2", href: "/sales/tasks/today" },
|
|
{ id: "sales_appointments" as const, value: "1", href: "/sales/bookings" },
|
|
{ id: "sales_followups" as const, value: "3", href: "/sales/tasks/today" },
|
|
]),
|
|
salesTeam: vi.fn(async () => [
|
|
{ id: "sales_team_pipeline" as const, value: "20 €", href: "/sales/deals" },
|
|
{ id: "sales_unassigned_leads" as const, value: "4", href: "/leads" },
|
|
{ id: "sales_team_activity" as const, value: "5", href: "/sales/tasks/today" },
|
|
]),
|
|
support: vi.fn(async () => [
|
|
{ id: "support_inbox" as const, value: "6", href: "/support" },
|
|
{ id: "support_customer_events" as const, value: "7", href: "/audit" },
|
|
]),
|
|
engineering: vi.fn(async () => [
|
|
{ id: "engineering_health" as const, value: "OK", href: "/system" },
|
|
{ id: "engineering_queues" as const, value: "0", href: "/system/jobs" },
|
|
{ id: "engineering_errors" as const, value: "0", href: "/events" },
|
|
{ id: "engineering_deploy" as const, value: "abc123", href: "/system/deploys" },
|
|
]),
|
|
executive: vi.fn(async () => [
|
|
{ id: "executive_forecast" as const, value: "1 €", href: "/sales/deals" },
|
|
{ id: "executive_revenue" as const, value: "2 €", href: "/sales/deals" },
|
|
{ id: "executive_mrr" as const, value: "3 €", href: "/dashboard" },
|
|
{ id: "executive_arr" as const, value: "36 €", href: "/dashboard" },
|
|
{ id: "executive_churn" as const, value: "0 %", href: "/dashboard" },
|
|
]),
|
|
} satisfies AdminDashboardDataSources;
|
|
|
|
const PERSONAS = [
|
|
{
|
|
role: "super_admin",
|
|
widgets: ["super_platform"],
|
|
allowedCommands: ["/dashboard", "/support", "/system", "/system/jobs", "/system/deploys"],
|
|
deniedCommands: [],
|
|
},
|
|
{
|
|
role: "sales",
|
|
widgets: [
|
|
"sales_own_pipeline",
|
|
"sales_today",
|
|
"sales_appointments",
|
|
"sales_followups",
|
|
],
|
|
allowedCommands: ["/dashboard", "/sales/deals"],
|
|
deniedCommands: ["/support", "/system", "/system/deploys"],
|
|
},
|
|
{
|
|
role: "sales_manager",
|
|
widgets: [
|
|
"sales_team_pipeline",
|
|
"sales_unassigned_leads",
|
|
"sales_team_activity",
|
|
],
|
|
allowedCommands: ["/dashboard", "/sales/deals"],
|
|
deniedCommands: ["/support", "/system", "/system/deploys"],
|
|
},
|
|
{
|
|
role: "support",
|
|
widgets: ["support_inbox", "support_customer_events"],
|
|
allowedCommands: ["/dashboard", "/support"],
|
|
deniedCommands: ["/system", "/system/deploys", "/sales/deals"],
|
|
},
|
|
{
|
|
role: "engineering",
|
|
widgets: [
|
|
"engineering_health",
|
|
"engineering_queues",
|
|
"engineering_errors",
|
|
"engineering_deploy",
|
|
],
|
|
allowedCommands: ["/dashboard", "/system", "/system/jobs", "/system/deploys"],
|
|
deniedCommands: ["/support", "/sales/deals"],
|
|
},
|
|
{
|
|
role: "executive",
|
|
widgets: [
|
|
"executive_forecast",
|
|
"executive_revenue",
|
|
"executive_mrr",
|
|
"executive_arr",
|
|
"executive_churn",
|
|
],
|
|
allowedCommands: ["/dashboard", "/sales/deals"],
|
|
deniedCommands: ["/support", "/system", "/system/deploys"],
|
|
},
|
|
{
|
|
role: "viewer",
|
|
widgets: ["viewer_reports"],
|
|
allowedCommands: ["/dashboard"],
|
|
deniedCommands: ["/support", "/system", "/system/deploys", "/sales/deals"],
|
|
},
|
|
] as const satisfies readonly {
|
|
role: AdminRoleKey;
|
|
widgets: readonly string[];
|
|
allowedCommands: readonly string[];
|
|
deniedCommands: readonly string[];
|
|
}[];
|
|
|
|
function permissionsFor(roleKeys: readonly AdminRoleKey[]) {
|
|
return [...permissionsForAdminRoles(roleKeys)];
|
|
}
|
|
|
|
function commandIdsFor(permissions: readonly AdminPermissionName[]) {
|
|
const actions = getSidebarCommandActions(
|
|
AdminSidebar({
|
|
t,
|
|
appConfiguration: {
|
|
app: { features: { tenantApiKeys: true } },
|
|
} as never,
|
|
}),
|
|
"admin"
|
|
);
|
|
return new Set(
|
|
filterCommandActions({
|
|
actions,
|
|
query: "",
|
|
layout: "admin",
|
|
appData: {
|
|
isSuperAdmin: false,
|
|
permissions,
|
|
user: { admin: true },
|
|
} as never,
|
|
rootData: { user: { admin: true } } as never,
|
|
}).map((action) => action.id)
|
|
);
|
|
}
|
|
|
|
describe("admin personas end to end", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
authMocks.getUserInfo.mockResolvedValue({ userId: "admin_persona" });
|
|
authMocks.getValidatedServerSession.mockResolvedValue({
|
|
adminUser: { id: "admin_persona" },
|
|
adminUserId: "admin_persona",
|
|
});
|
|
});
|
|
|
|
it.each(PERSONAS)(
|
|
"loads and exposes only the $role persona contract",
|
|
async ({ role, widgets, allowedCommands, deniedCommands }) => {
|
|
const permissions = permissionsFor([role]);
|
|
const vm = await loadPersonalizedAdminDashboard(
|
|
{
|
|
adminUserId: `admin_${role}`,
|
|
roleKeys: [role],
|
|
permissions,
|
|
},
|
|
sources
|
|
);
|
|
const commandIds = commandIdsFor(permissions);
|
|
|
|
expect(vm.widgets.map((widget) => widget.id)).toEqual(widgets);
|
|
expect(new Set(vm.widgets.map((widget) => widget.id)).size).toBe(
|
|
vm.widgets.length
|
|
);
|
|
for (const path of allowedCommands) {
|
|
expect(commandIds.has(path), `${role} should see ${path}`).toBe(true);
|
|
}
|
|
for (const path of deniedCommands) {
|
|
expect(commandIds.has(path), `${role} must not see ${path}`).toBe(false);
|
|
}
|
|
}
|
|
);
|
|
|
|
it.each(PERSONAS)(
|
|
"returns 404 for every denied $role direct-route permission",
|
|
async ({ role }) => {
|
|
const permissions = new Set(permissionsFor([role]));
|
|
authMocks.adminHasPermission.mockImplementation(
|
|
async (_adminUserId: string, permission: AdminPermissionName) =>
|
|
permissions.has(permission)
|
|
);
|
|
|
|
for (const route of ADMIN_ROUTE_ACCESS) {
|
|
const access = requireAdminIdentityPermission(
|
|
new Request(`https://admin.example.test${route.path}`),
|
|
route.permission
|
|
);
|
|
if (permissions.has(route.permission)) {
|
|
await expect(access, `${role} should open ${route.path}`).resolves.toMatchObject({
|
|
adminUserId: "admin_persona",
|
|
});
|
|
} else {
|
|
await expect(access, `${role} must 404 ${route.path}`).rejects.toMatchObject({
|
|
status: 404,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
);
|
|
|
|
it("unions combined roles across permissions, widgets and navigation", async () => {
|
|
const roleKeys = ["sales", "engineering"] as const;
|
|
const permissions = permissionsFor(roleKeys);
|
|
const vm = await loadPersonalizedAdminDashboard(
|
|
{
|
|
adminUserId: "admin_combined",
|
|
roleKeys,
|
|
permissions,
|
|
},
|
|
sources
|
|
);
|
|
const commandIds = commandIdsFor(permissions);
|
|
|
|
expect(vm.widgets.map((widget) => widget.id)).toEqual([
|
|
"sales_own_pipeline",
|
|
"sales_today",
|
|
"sales_appointments",
|
|
"sales_followups",
|
|
"engineering_health",
|
|
"engineering_queues",
|
|
"engineering_errors",
|
|
"engineering_deploy",
|
|
]);
|
|
expect(new Set(vm.widgets.map((widget) => widget.id)).size).toBe(
|
|
vm.widgets.length
|
|
);
|
|
for (const path of [
|
|
"/dashboard",
|
|
"/sales/deals",
|
|
"/system",
|
|
"/system/jobs",
|
|
"/system/deploys",
|
|
]) {
|
|
expect(commandIds.has(path), `combined role should see ${path}`).toBe(
|
|
true
|
|
);
|
|
}
|
|
expect(commandIds.has("/support")).toBe(false);
|
|
expect(sources.salesOwn).toHaveBeenCalledOnce();
|
|
expect(sources.engineering).toHaveBeenCalledOnce();
|
|
expect(sources.support).not.toHaveBeenCalled();
|
|
expect(sources.executive).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("keeps the own/all and read-only persona boundaries explicit", () => {
|
|
const sales = permissionsForAdminRoles(["sales"]);
|
|
const manager = permissionsForAdminRoles(["sales_manager"]);
|
|
const support = permissionsForAdminRoles(["support"]);
|
|
const engineering = permissionsForAdminRoles(["engineering"]);
|
|
const executive = permissionsForAdminRoles(["executive"]);
|
|
|
|
expect(sales.has("admin_sales.crm.read")).toBe(true);
|
|
expect(sales.has("admin_sales.crm.read_all")).toBe(false);
|
|
expect(manager.has("admin_sales.crm.read_all")).toBe(true);
|
|
expect(manager.has("admin_sales.communication.read_all")).toBe(true);
|
|
expect(support.has(ADMIN_AREA_ACCESS.deploys)).toBe(false);
|
|
expect(engineering.has(ADMIN_AREA_ACCESS.deploys)).toBe(true);
|
|
expect(executive.has("admin_sales.reporting.read_all")).toBe(true);
|
|
expect(executive.has("admin_sales.communication.read_all")).toBe(false);
|
|
expect(
|
|
[...executive].some((permission) => permission.includes(".write"))
|
|
).toBe(false);
|
|
});
|
|
|
|
it("covers every fixed role exactly once", () => {
|
|
const covered = PERSONAS.map((persona) => persona.role);
|
|
expect(new Set(covered)).toEqual(
|
|
new Set(ADMIN_ROLE_DEFINITIONS.map((role) => role.key))
|
|
);
|
|
expect(covered).toHaveLength(ADMIN_ROLE_DEFINITIONS.length);
|
|
});
|
|
});
|