Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

pam account schema unit test

x032205db47ef2c2591
diff --git a/backend/src/ee/services/pam-account/pam-account-schemas.test.ts b/backend/src/ee/services/pam-account/pam-account-schemas.test.ts
new file mode 100644
index 0000000000..97e4175d0d
--- /dev/null
+++ b/backend/src/ee/services/pam-account/pam-account-schemas.test.ts
@@ -0,0 +1,94 @@
+import { describe, expect, test } from "vitest";
+
+import { PamAccountType } from "../pam/pam-enums";
+import {
+ buildPamAccountTypeMetadata,
+ PamAccountTypeMetadataSchema,
+ PamFieldDescriptorSchema
+} from "./pam-account-schemas";
+
+// These assertions exercise the Zod-introspection path (buildPamAccountTypeMetadata reads schema internals to derive field descriptors)
+describe("buildPamAccountTypeMetadata", () => {
+ const metadata = buildPamAccountTypeMetadata();
+ const byType = new Map(metadata.map((m) => [m.type, m]));
+
+ const fieldByKey = <T extends { key: string }>(fields: T[], key: string) => fields.find((f) => f.key === key);
+
+ test("emits valid, non-empty field descriptors for every configured account type", () => {
+ expect(metadata.length).toBeGreaterThan(0);
+
+ metadata.forEach((meta) => {
+ expect(PamAccountTypeMetadataSchema.safeParse(meta).success).toBe(true);
+
+ expect(meta.connectionFields.length + meta.credentialFields.length).toBeGreaterThan(0);
+ [...meta.connectionFields, ...meta.credentialFields].forEach((field) => {
+ expect(PamFieldDescriptorSchema.safeParse(field).success).toBe(true);
+ });
+ });
+ });
+
+ test("derives Postgres connection and credential fields from the schema", () => {
+ const postgres = byType.get(PamAccountType.Postgres);
+ expect(postgres).toBeDefined();
+ expect(postgres?.name).toBe("PostgreSQL");
+
+ expect(postgres?.connectionFields.map((f) => f.key)).toEqual([
+ "host",
+ "port",
+ "database",
+ "sslEnabled",
+ "sslRejectUnauthorized",
+ "sslCertificate"
+ ]);
+ expect(fieldByKey(postgres!.connectionFields, "host")).toMatchObject({
+ widget: "text",
+ required: true
+ });
+ expect(fieldByKey(postgres!.connectionFields, "port")).toMatchObject({
+ widget: "number",
+ required: true
+ });
+ expect(fieldByKey(postgres!.connectionFields, "sslEnabled")).toMatchObject({ widget: "boolean" });
+
+ expect(fieldByKey(postgres!.connectionFields, "sslCertificate")).toMatchObject({
+ widget: "textarea",
+ required: false
+ });
+
+ expect(fieldByKey(postgres!.credentialFields, "username")).toMatchObject({
+ widget: "text",
+ required: true,
+ secret: false
+ });
+ expect(fieldByKey(postgres!.credentialFields, "password")).toMatchObject({
+ widget: "password",
+ secret: true,
+ required: false
+ });
+ });
+
+ test("flattens the SSH discriminated union into a select plus conditional variant fields", () => {
+ const ssh = byType.get(PamAccountType.SSH);
+ expect(ssh).toBeDefined();
+ expect(ssh?.connectionFields.map((f) => f.key)).toEqual(["host", "port"]);
+
+ const authMethod = fieldByKey(ssh!.credentialFields, "authMethod");
+ expect(authMethod).toMatchObject({ widget: "select", required: true });
+ expect(authMethod?.options?.map((o) => o.value)).toEqual(["password", "public-key", "certificate"]);
+
+ const username = fieldByKey(ssh!.credentialFields, "username");
+ expect(username).toMatchObject({ widget: "text", required: true });
+ expect(username?.showWhen).toBeUndefined();
+
+ expect(fieldByKey(ssh!.credentialFields, "password")).toMatchObject({
+ widget: "password",
+ secret: true,
+ showWhen: { field: "authMethod", equals: "password" }
+ });
+ expect(fieldByKey(ssh!.credentialFields, "privateKey")).toMatchObject({
+ widget: "textarea",
+ secret: true,
+ showWhen: { field: "authMethod", equals: "public-key" }
+ });
+ });
+});