Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
add tests
Dimitris Staratzis996ca8c6d202
diff --git a/backend/e2e-test/routes/v1/secret-validation-rules.spec.ts b/backend/e2e-test/routes/v1/secret-validation-rules.spec.tsindex af6feb7ff4..9a083ead89 100644--- a/backend/e2e-test/routes/v1/secret-validation-rules.spec.ts+++ b/backend/e2e-test/routes/v1/secret-validation-rules.spec.ts@@ -1,7 +1,7 @@import { createIsolatedOrgAndProject } from "e2e-test/testUtils/fixtures";import { createFolder } from "e2e-test/testUtils/folders";import { createStaticSecretsValidationRule } from "e2e-test/testUtils/secret-validation-rules";-import { createSecretV2, updateSecretV2 } from "e2e-test/testUtils/secrets";+import { createSecretV2, getSecretsV2, updateSecretV2 } from "e2e-test/testUtils/secrets";const PROD_ENV = "prod";const STAGING_ENV = "staging";@@ -181,4 +181,235 @@ describe("Secret validation rules", () => {expect(updated.secretValue).toBe("self-update-value");});});++ // A move writes its secrets at the destination, so they are held to the destination's rules like any+ // other write there, whether a single secret or a whole folder is moved.+ describe("Moving secrets and folders into a path with rules", () => {+ let projectId: string;+ let authToken: string;+ let cleanup: () => Promise<void>;++ const moveSecrets = (dto: {+ environment: string;+ sourceSecretPath: string;+ destinationSecretPath: string;+ secretIds: string[];+ }) =>+ testServer.inject({+ method: "POST",+ url: `/api/v4/secrets/move`,+ headers: {+ authorization: `Bearer ${authToken}`+ },+ body: {+ projectId,+ sourceEnvironment: dto.environment,+ sourceSecretPath: dto.sourceSecretPath,+ destinationEnvironment: dto.environment,+ destinationSecretPath: dto.destinationSecretPath,+ secretIds: dto.secretIds+ }+ });++ const moveFolder = (dto: { folderId: string; environment: string; destinationPath: string }) =>+ testServer.inject({+ method: "POST",+ url: `/api/v2/folders/move`,+ headers: {+ authorization: `Bearer ${authToken}`+ },+ body: {+ projectId,+ folderId: dto.folderId,+ destinationEnvironment: dto.environment,+ destinationPath: dto.destinationPath+ }+ });++ const secretKeysAt = async (secretPath: string, environment: string) => {+ const { secrets } = await getSecretsV2({+ workspaceId: projectId,+ environmentSlug: environment,+ secretPath,+ authToken+ });+ return secrets.map((secret) => secret.secretKey);+ };++ const folderNamesAt = async (secretPath: string, environment: string) => {+ const res = await testServer.inject({+ method: "GET",+ url: `/api/v2/folders`,+ headers: {+ authorization: `Bearer ${authToken}`+ },+ query: {+ projectId,+ environment,+ path: secretPath+ }+ });+ expect(res.statusCode).toBe(200);+ return (res.json().folders as { id: string; name: string }[]).map((folder) => folder.name);+ };++ beforeAll(async () => {+ ({ projectId, authToken, cleanup } = await createIsolatedOrgAndProject("secret-validation-rules-move-e2e"));++ // In production, "/strict" and everything under it requires values of at least 3 characters, while+ // "/lenient" has no rules.+ await createFolder({+ workspaceId: projectId,+ environmentSlug: PROD_ENV,+ secretPath: "/",+ name: "strict",+ authToken+ });+ await createFolder({+ workspaceId: projectId,+ environmentSlug: PROD_ENV,+ secretPath: "/",+ name: "lenient",+ authToken+ });+ await createStaticSecretsValidationRule({+ projectId,+ name: "min-length-under-strict",+ environment: PROD_ENV,+ secretPath: "/strict/**",+ valueConstraints: { minLength: 3 },+ authToken+ });++ // In staging, no two secrets anywhere may hold the same value.+ await createFolder({+ workspaceId: projectId,+ environmentSlug: STAGING_ENV,+ secretPath: "/",+ name: "from",+ authToken+ });+ await createFolder({+ workspaceId: projectId,+ environmentSlug: STAGING_ENV,+ secretPath: "/",+ name: "to",+ authToken+ });+ await createStaticSecretsValidationRule({+ projectId,+ name: "no-duplicate-values-in-staging",+ environment: STAGING_ENV,+ secretPath: "/**",+ valueConstraints: { uniqueWithinScope: true },+ authToken+ });+ });++ afterAll(async () => {+ await cleanup();+ });++ test("rejects moving a secret whose value breaks the destination's rule", async () => {+ const secret = await createSecretV2({+ workspaceId: projectId,+ environmentSlug: PROD_ENV,+ secretPath: "/lenient",+ key: "TOO_SHORT",+ value: "x",+ authToken+ });++ const res = await moveSecrets({+ environment: PROD_ENV,+ sourceSecretPath: "/lenient",+ destinationSecretPath: "/strict",+ secretIds: [secret.id]+ });+ expect(res.statusCode).toBe(400);+ expect(res.json().message).toContain('Secret "TOO_SHORT"');+ expect(res.json().message).toContain("value must be at least 3 characters");++ // the secret is untouched because the move was rejected before anything was written+ expect(await secretKeysAt("/lenient", PROD_ENV)).toContain("TOO_SHORT");+ expect(await secretKeysAt("/strict", PROD_ENV)).not.toContain("TOO_SHORT");+ }, 10_000);++ test("accepts moving a secret whose value meets the destination's rule", async () => {+ const secret = await createSecretV2({+ workspaceId: projectId,+ environmentSlug: PROD_ENV,+ secretPath: "/lenient",+ key: "LONG_ENOUGH",+ value: "long-enough-value",+ authToken+ });++ const res = await moveSecrets({+ environment: PROD_ENV,+ sourceSecretPath: "/lenient",+ destinationSecretPath: "/strict",+ secretIds: [secret.id]+ });+ expect(res.statusCode).toBe(200);++ expect(await secretKeysAt("/strict", PROD_ENV)).toContain("LONG_ENOUGH");+ expect(await secretKeysAt("/lenient", PROD_ENV)).not.toContain("LONG_ENOUGH");+ }, 10_000);++ test("rejects moving a folder holding a secret that breaks the destination's rule", async () => {+ const folder = await createFolder({+ workspaceId: projectId,+ environmentSlug: PROD_ENV,+ secretPath: "/lenient",+ name: "short-values",+ authToken+ });+ await createSecretV2({+ workspaceId: projectId,+ environmentSlug: PROD_ENV,+ secretPath: "/lenient/short-values",+ key: "NESTED_TOO_SHORT",+ value: "x",+ authToken+ });++ const res = await moveFolder({+ folderId: folder.id,+ environment: PROD_ENV,+ destinationPath: "/strict"+ });+ expect(res.statusCode).toBe(400);+ expect(res.json().message).toContain('Secret "NESTED_TOO_SHORT"');+ expect(res.json().message).toContain("value must be at least 3 characters");++ // the folder and its secret are untouched because the move was rejected before anything was written+ expect(await folderNamesAt("/lenient", PROD_ENV)).toContain("short-values");+ expect(await folderNamesAt("/strict", PROD_ENV)).not.toContain("short-values");+ expect(await secretKeysAt("/lenient/short-values", PROD_ENV)).toContain("NESTED_TOO_SHORT");+ }, 10_000);++ test("accepts moving a secret within a no-duplicates scope that also covers its source", async () => {+ const secret = await createSecretV2({+ workspaceId: projectId,+ environmentSlug: STAGING_ENV,+ secretPath: "/from",+ key: "MOVED_UNIQUE",+ value: "moved-unique-value",+ authToken+ });++ // The source copy still holds the value while the move runs, so it must not count as a duplicate.+ const res = await moveSecrets({+ environment: STAGING_ENV,+ sourceSecretPath: "/from",+ destinationSecretPath: "/to",+ secretIds: [secret.id]+ });+ expect(res.statusCode).toBe(200);++ expect(await secretKeysAt("/to", STAGING_ENV)).toContain("MOVED_UNIQUE");+ expect(await secretKeysAt("/from", STAGING_ENV)).not.toContain("MOVED_UNIQUE");+ }, 10_000);+ });});diff --git a/backend/e2e-test/testUtils/secrets.ts b/backend/e2e-test/testUtils/secrets.tsindex 766a060625..0b19edae12 100644--- a/backend/e2e-test/testUtils/secrets.ts+++ b/backend/e2e-test/testUtils/secrets.ts@@ -5,6 +5,7 @@ import { SecretType } from "@app/db/schemas";import { request } from "./request";type TRawSecret = {+ id: string;secretKey: string;secretValue: string;secretComment?: string;