91 lines
3.9 KiB
TypeScript
91 lines
3.9 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { getRlsExecutionContext } from "@hivegrid/db/rls-context.server";
|
|
|
|
import { readTenantArgument, requiresOperatorIdentity, runCliWithRls } from "./rls-context.js";
|
|
|
|
describe("operator CLI RLS context", () => {
|
|
it("requires an operator identity only for database-backed commands", () => {
|
|
expect(requiresOperatorIdentity(["leads", "list"])).toBe(true);
|
|
expect(requiresOperatorIdentity(["plugins", "enable", "--tenantId", "tenant-a"])).toBe(true);
|
|
expect(requiresOperatorIdentity(["tenant", "snapshot", "--tenant-id", "tenant-a"])).toBe(true);
|
|
expect(requiresOperatorIdentity(["tenant", "restore", "--tenant-id=tenant-b"])).toBe(true);
|
|
expect(requiresOperatorIdentity(["tenant", "list-snapshots"])).toBe(false);
|
|
expect(requiresOperatorIdentity(["plugins", "build"])).toBe(false);
|
|
expect(requiresOperatorIdentity(["create", "module", "inventory"])).toBe(false);
|
|
expect(requiresOperatorIdentity(["ops", "product-acquisition-recovery", "--tenant", "tenant-a"])).toBe(true);
|
|
expect(requiresOperatorIdentity(["leads", "--help"])).toBe(false);
|
|
});
|
|
|
|
it("reads one concrete tenant from either supported flag shape", () => {
|
|
expect(readTenantArgument(["forms", "list", "--tenant", "tenant-a"])).toBe("tenant-a");
|
|
expect(readTenantArgument(["plugins", "list", "--tenantId=tenant-b"])).toBe("tenant-b");
|
|
expect(readTenantArgument(["tenant", "snapshot", "--tenant-id", "tenant-c"])).toBe("tenant-c");
|
|
expect(readTenantArgument(["tenant", "restore", "--tenant-id=tenant-d"])).toBe("tenant-d");
|
|
expect(readTenantArgument(["search", "reindex", "--tenant=all"])).toBeUndefined();
|
|
expect(readTenantArgument(["audit", "verify-chain", "--tenant=__platform__"])).toBeUndefined();
|
|
expect(readTenantArgument(["ops", "impersonate", "tenant-c", "user-a", "--env", "staging"])).toBe("tenant-c");
|
|
expect(readTenantArgument(["ops", "product-acquisition-recovery", "--tenant", "tenant-e"])).toBe("tenant-e");
|
|
});
|
|
|
|
it("binds and validates the operator plus tenant before command execution", async () => {
|
|
const findUnique = vi.fn(async () => ({
|
|
id: "operator-a",
|
|
status: "active",
|
|
}));
|
|
const execute = vi.fn(async () => {
|
|
expect(getRlsExecutionContext()).toMatchObject({ userId: "operator-a", tenantId: "tenant-a" });
|
|
});
|
|
|
|
await runCliWithRls({
|
|
rawArgs: ["forms", "list", "--tenant", "tenant-a"],
|
|
env: { HIVEGRID_CLI_OPERATOR_USER_ID: "operator-a" },
|
|
client: { adminUser: { findUnique } } as never,
|
|
execute,
|
|
});
|
|
|
|
expect(findUnique).toHaveBeenCalledWith({
|
|
where: { id: "operator-a" },
|
|
select: {
|
|
id: true,
|
|
status: true,
|
|
},
|
|
});
|
|
expect(execute).toHaveBeenCalledOnce();
|
|
expect(getRlsExecutionContext()).toBeNull();
|
|
});
|
|
|
|
it("fails closed for a missing or non-operator identity", async () => {
|
|
const execute = vi.fn(async () => undefined);
|
|
await expect(
|
|
runCliWithRls({ rawArgs: ["leads", "list"], env: {}, client: {} as never, execute }),
|
|
).rejects.toThrow("HIVEGRID_CLI_OPERATOR_USER_ID");
|
|
|
|
await expect(
|
|
runCliWithRls({
|
|
rawArgs: ["leads", "list"],
|
|
env: { HIVEGRID_CLI_OPERATOR_USER_ID: "ordinary-user" },
|
|
client: { adminUser: { findUnique: vi.fn(async () => null) } } as never,
|
|
execute,
|
|
}),
|
|
).rejects.toThrow("AdminUser");
|
|
expect(execute).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it.each(["disabled", "locked"])("rejects an ineligible operator account state: %s", async (status) => {
|
|
const execute = vi.fn(async () => undefined);
|
|
|
|
await expect(
|
|
runCliWithRls({
|
|
rawArgs: ["leads", "list"],
|
|
env: { HIVEGRID_CLI_OPERATOR_USER_ID: "operator-a" },
|
|
client: {
|
|
adminUser: { findUnique: vi.fn(async () => ({ id: "operator-a", status })) },
|
|
} as never,
|
|
execute,
|
|
}),
|
|
).rejects.toThrow("active HiveGrid AdminUser");
|
|
expect(execute).not.toHaveBeenCalled();
|
|
});
|
|
});
|