113 lines
6.0 KiB
TypeScript
113 lines
6.0 KiB
TypeScript
// @vitest-environment node
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
CUSTOMER_PORTAL_UAT_COMMERCIAL_CONFIRMATION,
|
|
CUSTOMER_PORTAL_UAT_TENANT_SLUG,
|
|
parseCustomerPortalUatCommand,
|
|
safeCustomerPortalUatError,
|
|
} from "./contracts";
|
|
|
|
const baseEnvironment = {
|
|
NODE_ENV: "production",
|
|
SENTRY_ENVIRONMENT: "staging",
|
|
RLS_RUNTIME_MODE: "required",
|
|
DATABASE_RUNTIME_ROLE: "hivegrid_app",
|
|
DATABASE_URL: "postgresql://hivegrid_app:runtime-secret@db.staging.internal:6432/hivegrid_staging",
|
|
CUSTOMER_PORTAL_UAT_EXPECTED_DATABASE_HOST: "db.staging.internal",
|
|
CUSTOMER_PORTAL_UAT_EXPECTED_DATABASE_PORT: "6432",
|
|
CUSTOMER_PORTAL_UAT_EXPECTED_DATABASE: "hivegrid_staging",
|
|
CUSTOMER_PORTAL_UAT_OPERATOR_USER_ID: "operator-user-id",
|
|
CUSTOMER_PORTAL_UAT_EXPECTED_TENANT_ID: "tenant-demo-pro",
|
|
CUSTOMER_PORTAL_UAT_EXPECTED_STARTER_TENANT_ID: "tenant-demo-starter",
|
|
CUSTOMER_PORTAL_UAT_EXPECTED_PRO_TENANT_ID: "tenant-demo-pro",
|
|
CUSTOMER_PORTAL_UAT_EXPECTED_ENTERPRISE_TENANT_ID: "tenant-demo-enterprise",
|
|
CUSTOMER_PORTAL_UAT_PASSWORD: "Q7!portal-UAT-strong-random-2026",
|
|
LAGO_API_URL: "http://127.0.0.1:3108",
|
|
LAGO_API_KEY: "lago-staging-key",
|
|
AUDIT_CHAIN_SECRET: "staging-audit-chain-secret-at-least-32-bytes",
|
|
} as const;
|
|
|
|
describe("customer-portal UAT command contract", () => {
|
|
it("defaults to a read-only plan and keeps catalog and fixture inputs separate", () => {
|
|
const catalog = parseCustomerPortalUatCommand("catalog", [], baseEnvironment);
|
|
const fixture = parseCustomerPortalUatCommand("fixture", ["--verify"], baseEnvironment);
|
|
|
|
expect(catalog).toMatchObject({ target: "catalog", mode: "plan", lagoApiUrl: "http://127.0.0.1:3108" });
|
|
expect(fixture).toMatchObject({ target: "fixture", mode: "verify", expectedTenantId: "tenant-demo-pro" });
|
|
expect("password" in catalog).toBe(false);
|
|
expect("lagoApiKey" in fixture).toBe(false);
|
|
});
|
|
|
|
it("binds the commercial fixture to three distinct explicit tenant ids", () => {
|
|
const fixture = parseCustomerPortalUatCommand("commercial-fixture", ["--verify"], baseEnvironment);
|
|
expect(fixture).toMatchObject({
|
|
target: "commercial-fixture",
|
|
mode: "verify",
|
|
expectedStarterTenantId: "tenant-demo-starter",
|
|
expectedProTenantId: "tenant-demo-pro",
|
|
expectedEnterpriseTenantId: "tenant-demo-enterprise",
|
|
});
|
|
expect(() =>
|
|
parseCustomerPortalUatCommand("commercial-fixture", [], {
|
|
...baseEnvironment,
|
|
CUSTOMER_PORTAL_UAT_EXPECTED_STARTER_TENANT_ID: "same-tenant",
|
|
CUSTOMER_PORTAL_UAT_EXPECTED_PRO_TENANT_ID: "same-tenant",
|
|
CUSTOMER_PORTAL_UAT_EXPECTED_ENTERPRISE_TENANT_ID: "tenant-demo-enterprise",
|
|
})
|
|
).toThrow("must be distinct");
|
|
});
|
|
|
|
it("requires an exact tenant confirmation for every mutation", () => {
|
|
expect(() => parseCustomerPortalUatCommand("catalog", ["--apply"], baseEnvironment)).toThrow(`--confirm=${CUSTOMER_PORTAL_UAT_TENANT_SLUG}`);
|
|
expect(() => parseCustomerPortalUatCommand("catalog", ["--apply", `--confirm=${CUSTOMER_PORTAL_UAT_TENANT_SLUG}`], baseEnvironment)).not.toThrow();
|
|
expect(() => parseCustomerPortalUatCommand("fixture", ["--plan", `--confirm=${CUSTOMER_PORTAL_UAT_TENANT_SLUG}`], baseEnvironment)).toThrow(
|
|
"only with --apply"
|
|
);
|
|
expect(() => parseCustomerPortalUatCommand("commercial-fixture", ["--apply", `--confirm=${CUSTOMER_PORTAL_UAT_TENANT_SLUG}`], baseEnvironment)).toThrow(
|
|
`--confirm=${CUSTOMER_PORTAL_UAT_COMMERCIAL_CONFIRMATION}`
|
|
);
|
|
expect(() =>
|
|
parseCustomerPortalUatCommand("commercial-fixture", ["--apply", `--confirm=${CUSTOMER_PORTAL_UAT_COMMERCIAL_CONFIRMATION}`], baseEnvironment)
|
|
).not.toThrow();
|
|
});
|
|
|
|
it.each([
|
|
["non-production Node runtime", { NODE_ENV: "development" }],
|
|
["production environment", { SENTRY_ENVIRONMENT: "production" }],
|
|
["RLS disabled", { RLS_RUNTIME_MODE: "off" }],
|
|
["wrong runtime role", { DATABASE_RUNTIME_ROLE: "postgres" }],
|
|
["wrong URL role", { DATABASE_URL: "postgresql://postgres:secret@db.staging.internal:6432/hivegrid_staging" }],
|
|
["URL host override", { DATABASE_URL: "postgresql://hivegrid_app:secret@db.staging.internal:6432/hivegrid_staging?host=production-db" }],
|
|
["URL user override", { DATABASE_URL: "postgresql://hivegrid_app:secret@db.staging.internal:6432/hivegrid_staging?user=postgres" }],
|
|
[
|
|
"URL options override",
|
|
{ DATABASE_URL: "postgresql://hivegrid_app:secret@db.staging.internal:6432/hivegrid_staging?options=-c%20hivegrid.environment%3Dstaging" },
|
|
],
|
|
["URL service override", { DATABASE_URL: "postgresql://hivegrid_app:secret@db.staging.internal:6432/hivegrid_staging?service=production" }],
|
|
["libpq PGOPTIONS override", { PGOPTIONS: "-c hivegrid.environment=staging" }],
|
|
["libpq PGHOST override", { PGHOST: "production-db" }],
|
|
["libpq PGSERVICE override", { PGSERVICE: "production" }],
|
|
["wrong database binding", { CUSTOMER_PORTAL_UAT_EXPECTED_DATABASE: "other" }],
|
|
["migration credential present", { DIRECT_URL: "postgresql://migrator:secret@db/hivegrid" }],
|
|
["migrator credential present", { MIGRATOR_PASSWORD: "do-not-use" }],
|
|
["runtime seed flag present", { SEED_ON_BOOT: "true" }],
|
|
["non-staging Lago endpoint", { LAGO_API_URL: "https://billing.example.test" }],
|
|
["credential-bearing Lago URL", { LAGO_API_URL: "http://user:pass@127.0.0.1:3108" }],
|
|
["missing audit secret", { AUDIT_CHAIN_SECRET: "" }],
|
|
["short audit secret", { AUDIT_CHAIN_SECRET: "too-short" }],
|
|
])("fails closed for %s", (_label, override) => {
|
|
expect(() => parseCustomerPortalUatCommand("catalog", [], { ...baseEnvironment, ...override })).toThrow();
|
|
});
|
|
|
|
it("rejects weak fixture passwords without disclosing the supplied value", () => {
|
|
expect(() => parseCustomerPortalUatCommand("fixture", [], { ...baseEnvironment, CUSTOMER_PORTAL_UAT_PASSWORD: "password" })).toThrow(
|
|
"strong random secret"
|
|
);
|
|
});
|
|
|
|
it("does not expose unexpected error details", () => {
|
|
expect(safeCustomerPortalUatError(new Error("postgresql://user:secret@host/database"))).not.toContain("secret");
|
|
});
|
|
});
|