// @vitest-environment node import { describe, expect, it } from "vitest"; import { computeAuditChainHeadMac, type AuditChainHeadRecord } from "@hivegrid/db/audit-chain-checkpoint.server"; import { assertCustomerPortalAuditAnchor, assertCustomerPortalDatabaseSession, assertCustomerPortalOperatorIdentity } from "./database"; const stagingSession = { sessionUser: "hivegrid_app", currentUser: "hivegrid_app", databaseName: "hivegrid_staging", databaseEnvironmentMarked: true, roleIsSuperuser: false, roleBypassesRls: false, roleCanCreateRole: false, roleCanCreateDatabase: false, } as const; describe("customer-portal operator database guard", () => { it("accepts only the restricted runtime role on the explicitly bound staging database", () => { expect(() => assertCustomerPortalDatabaseSession(stagingSession, "hivegrid_staging")).not.toThrow(); }); it("fails closed when the independent staging marker is missing or different", () => { expect(() => assertCustomerPortalDatabaseSession({ ...stagingSession, databaseEnvironmentMarked: false }, "hivegrid_staging")).toThrow( "database is not independently marked as staging" ); }); it("rejects a privileged role or a different database even with the staging marker", () => { expect(() => assertCustomerPortalDatabaseSession({ ...stagingSession, sessionUser: "postgres" }, "hivegrid_staging")).toThrow( "database session is not the restricted hivegrid_app role" ); expect(() => assertCustomerPortalDatabaseSession(stagingSession, "hivegrid_production")).toThrow( "connected database does not match the explicit staging binding" ); }); it.each([ ["superuser", { roleIsSuperuser: true }], ["BYPASSRLS", { roleBypassesRls: true }], ["CREATEROLE", { roleCanCreateRole: true }], ["CREATEDB", { roleCanCreateDatabase: true }], ])("rejects a hivegrid_app role with %s privilege", (_label, privilege) => { expect(() => assertCustomerPortalDatabaseSession({ ...stagingSession, ...privilege }, "hivegrid_staging")).toThrow( "database session is not the restricted hivegrid_app role" ); }); it("authenticates the existing platform audit anchor and rejects a different long secret", () => { const anchorWithoutMac: Omit = { chainKey: "platform", tenantId: null, eventId: "event-1", eventHash: "a".repeat(64), eventCreatedAt: new Date("2026-07-17T00:00:00.000Z"), }; const canonicalSecret = "canonical-staging-audit-secret-at-least-32-bytes"; const anchor = { ...anchorWithoutMac, headMac: computeAuditChainHeadMac(anchorWithoutMac, canonicalSecret) }; expect(() => assertCustomerPortalAuditAnchor(anchor, canonicalSecret)).not.toThrow(); expect(() => assertCustomerPortalAuditAnchor(anchor, "wrong-but-still-long-audit-secret-at-least-32-bytes")).toThrow( "audit-chain anchor authentication failed" ); expect(() => assertCustomerPortalAuditAnchor(null, canonicalSecret)).toThrow("audit-chain anchor authentication failed"); }); it("accepts only the explicitly bound active platform AdminUser", () => { expect(() => assertCustomerPortalOperatorIdentity({ id: "admin-1", status: "active" }, "admin-1")).not.toThrow(); expect(() => assertCustomerPortalOperatorIdentity({ id: "admin-2", status: "active" }, "admin-1")).toThrow( "operator identity is not an active AdminUser" ); expect(() => assertCustomerPortalOperatorIdentity({ id: "admin-1", status: "disabled" }, "admin-1")).toThrow( "operator identity is not an active AdminUser" ); expect(() => assertCustomerPortalOperatorIdentity(null, "admin-1")).toThrow("operator identity is not an active AdminUser"); }); });