Skip to content

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

chainable expect call

Matheus Nogueirab281a7c8c935
diff --git a/backend/e2e-test/routes/v1/secret-validation-rules.spec.ts b/backend/e2e-test/routes/v1/secret-validation-rules.spec.ts
index d04b626338..8149ffee97 100644
--- a/backend/e2e-test/routes/v1/secret-validation-rules.spec.ts
+++ b/backend/e2e-test/routes/v1/secret-validation-rules.spec.ts
@@ -1,9 +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 } from "e2e-test/testUtils/secrets";
-
-import { SecretType } from "@app/db/schemas";
+import { createSecretV2, updateSecretV2 } from "e2e-test/testUtils/secrets";
const PROD_ENV = "prod";
const STAGING_ENV = "staging";
@@ -110,25 +108,20 @@ describe("Secret validation rules", () => {
authToken
});
- const res = await testServer.inject({
- method: "POST",
- url: "/api/v3/secrets/raw/DUPLICATE",
- headers: { authorization: `Bearer ${authToken}` },
- body: {
- workspaceId: projectId,
- environment: PROD_ENV,
- type: SecretType.Shared,
- secretPath: "/",
- secretKey: "DUPLICATE",
- secretValue: "duplicated-value"
- }
- });
-
- expect(res.statusCode).toBe(400);
// The writer is a project admin here, so the message names where the value is already held.
- expect(res.json().message).toContain('Secret "DUPLICATE"');
- expect(res.json().message).toContain('value is already used by secret "ORIGINAL"');
- expect(res.json().message).toContain(`environment "${PROD_ENV}"`);
+ await createSecretV2({
+ workspaceId: projectId,
+ environmentSlug: PROD_ENV,
+ secretPath: "/",
+ key: "DUPLICATE",
+ value: "duplicated-value",
+ authToken
+ }).expect((res) => {
+ expect(res.statusCode).toBe(400);
+ expect(res.json().message).toContain('Secret "DUPLICATE"');
+ expect(res.json().message).toContain('value is already used by secret "ORIGINAL"');
+ expect(res.json().message).toContain(`environment "${PROD_ENV}"`);
+ });
});
test("rejects updating a secret to a value production already holds", async () => {
@@ -149,23 +142,19 @@ describe("Secret validation rules", () => {
authToken
});
- const res = await testServer.inject({
- method: "PATCH",
- url: "/api/v3/secrets/raw/UPDATE_TARGET",
- headers: { authorization: `Bearer ${authToken}` },
- body: {
- workspaceId: projectId,
- environment: PROD_ENV,
- type: SecretType.Shared,
- secretPath: "/",
- secretValue: "value-held-by-another-secret"
- }
+ await updateSecretV2({
+ workspaceId: projectId,
+ environmentSlug: PROD_ENV,
+ secretPath: "/",
+ key: "UPDATE_TARGET",
+ value: "value-held-by-another-secret",
+ authToken
+ }).expect((res) => {
+ expect(res.statusCode).toBe(400);
+ expect(res.json().message).toContain('Secret "UPDATE_TARGET"');
+ expect(res.json().message).toContain('value is already used by secret "UPDATE_HOLDER"');
+ expect(res.json().message).toContain(`environment "${PROD_ENV}"`);
});
-
- expect(res.statusCode).toBe(400);
- expect(res.json().message).toContain('Secret "UPDATE_TARGET"');
- expect(res.json().message).toContain('value is already used by secret "UPDATE_HOLDER"');
- expect(res.json().message).toContain(`environment "${PROD_ENV}"`);
});
test("accepts re-saving a secret with the value it already holds", async () => {
@@ -179,21 +168,17 @@ describe("Secret validation rules", () => {
});
// The secret being written is excluded from the lookup, so it does not collide with itself.
- const res = await testServer.inject({
- method: "PATCH",
- url: "/api/v3/secrets/raw/SELF_UPDATE",
- headers: { authorization: `Bearer ${authToken}` },
- body: {
- workspaceId: projectId,
- environment: PROD_ENV,
- type: SecretType.Shared,
- secretPath: "/",
- secretValue: "self-update-value",
- secretComment: "touched"
- }
+ const updated = await updateSecretV2({
+ workspaceId: projectId,
+ environmentSlug: PROD_ENV,
+ secretPath: "/",
+ key: "SELF_UPDATE",
+ value: "self-update-value",
+ comment: "touched",
+ authToken
});
- expect(res.statusCode).toBe(200);
+ expect(updated.secretValue).toBe("self-update-value");
});
});
});
diff --git a/backend/e2e-test/testUtils/request.ts b/backend/e2e-test/testUtils/request.ts
new file mode 100644
index 0000000000..a48eacbc36
--- /dev/null
+++ b/backend/e2e-test/testUtils/request.ts
@@ -0,0 +1,23 @@
+import { InjectOptions, LightMyRequestResponse } from "fastify";
+
+// The request is sent when the helper is called, not when the chain is awaited, so a call that is
+// neither awaited nor `.expect()`ed still reaches the server. A lazy chain would make that line a
+// silent no-op, and nothing in the eslint config catches a floating thenable.
+export type TRequestChain<T> = PromiseLike<T> & {
+ expect: (assert: (res: LightMyRequestResponse) => void) => Promise<LightMyRequestResponse>;
+ raw: () => Promise<LightMyRequestResponse>;
+};
+
+export const request = <T>(opts: InjectOptions, parse: (res: LightMyRequestResponse) => T): TRequestChain<T> => {
+ const sent = testServer.inject(opts);
+
+ return {
+ then: (onFulfilled, onRejected) => sent.then(parse).then(onFulfilled, onRejected),
+ expect: async (assert) => {
+ const res = await sent;
+ assert(res);
+ return res;
+ },
+ raw: () => sent
+ };
+};
diff --git a/backend/e2e-test/testUtils/secrets.ts b/backend/e2e-test/testUtils/secrets.ts
index 68e813442c..766a060625 100644
--- a/backend/e2e-test/testUtils/secrets.ts
+++ b/backend/e2e-test/testUtils/secrets.ts
@@ -1,5 +1,9 @@
+import { LightMyRequestResponse } from "fastify";
+
import { SecretType } from "@app/db/schemas";
+import { request } from "./request";
+
type TRawSecret = {
secretKey: string;
secretValue: string;
@@ -7,7 +11,14 @@ type TRawSecret = {
version: number;
};
-export const createSecretV2 = async (dto: {
+const parseSecret = (res: LightMyRequestResponse) => {
+ expect(res.statusCode).toBe(200);
+ const payload = res.json<{ secret: TRawSecret }>();
+ expect(payload).toHaveProperty("secret");
+ return payload.secret;
+};
+
+type TCreateSecretV2Dto = {
workspaceId: string;
environmentSlug: string;
secretPath: string;
@@ -16,118 +27,151 @@ export const createSecretV2 = async (dto: {
comment?: string;
authToken: string;
type?: SecretType;
-}) => {
- const createSecretReqBody = {
- workspaceId: dto.workspaceId,
- environment: dto.environmentSlug,
- type: dto.type || SecretType.Shared,
- secretPath: dto.secretPath,
- secretKey: dto.key,
- secretValue: dto.value,
- secretComment: dto.comment
- };
- const createSecRes = await testServer.inject({
- method: "POST",
- url: `/api/v3/secrets/raw/${dto.key}`,
- headers: {
- authorization: `Bearer ${dto.authToken}`
+};
+
+export const createSecretV2 = (dto: TCreateSecretV2Dto) =>
+ request(
+ {
+ method: "POST",
+ url: `/api/v3/secrets/raw/${dto.key}`,
+ headers: {
+ authorization: `Bearer ${dto.authToken}`
+ },
+ body: {
+ workspaceId: dto.workspaceId,
+ environment: dto.environmentSlug,
+ type: dto.type || SecretType.Shared,
+ secretPath: dto.secretPath,
+ secretKey: dto.key,
+ secretValue: dto.value,
+ secretComment: dto.comment
+ }
},
- body: createSecretReqBody
- });
- expect(createSecRes.statusCode).toBe(200);
- const createdSecretPayload = JSON.parse(createSecRes.payload);
- expect(createdSecretPayload).toHaveProperty("secret");
- return createdSecretPayload.secret as TRawSecret;
+ parseSecret
+ );
+
+type TUpdateSecretV2Dto = {
+ workspaceId: string;
+ environmentSlug: string;
+ secretPath: string;
+ key: string;
+ value?: string;
+ newKey?: string;
+ comment?: string;
+ authToken: string;
+ type?: SecretType;
};
-export const deleteSecretV2 = async (dto: {
+export const updateSecretV2 = (dto: TUpdateSecretV2Dto) =>
+ request(
+ {
+ method: "PATCH",
+ url: `/api/v3/secrets/raw/${dto.key}`,
+ headers: {
+ authorization: `Bearer ${dto.authToken}`
+ },
+ body: {
+ workspaceId: dto.workspaceId,
+ environment: dto.environmentSlug,
+ type: dto.type || SecretType.Shared,
+ secretPath: dto.secretPath,
+ secretValue: dto.value,
+ newSecretName: dto.newKey,
+ secretComment: dto.comment
+ }
+ },
+ parseSecret
+ );
+
+export const deleteSecretV2 = (dto: {
workspaceId: string;
environmentSlug: string;
secretPath: string;
key: string;
authToken: string;
-}) => {
- const deleteSecRes = await testServer.inject({
- method: "DELETE",
- url: `/api/v3/secrets/raw/${dto.key}`,
- headers: {
- authorization: `Bearer ${dto.authToken}`
+}) =>
+ request(
+ {
+ method: "DELETE",
+ url: `/api/v3/secrets/raw/${dto.key}`,
+ headers: {
+ authorization: `Bearer ${dto.authToken}`
+ },
+ body: {
+ workspaceId: dto.workspaceId,
+ environment: dto.environmentSlug,
+ secretPath: dto.secretPath
+ }
},
- body: {
- workspaceId: dto.workspaceId,
- environment: dto.environmentSlug,
- secretPath: dto.secretPath
- }
- });
- expect(deleteSecRes.statusCode).toBe(200);
- const updatedSecretPayload = JSON.parse(deleteSecRes.payload);
- expect(updatedSecretPayload).toHaveProperty("secret");
- return updatedSecretPayload.secret as TRawSecret;
-};
+ parseSecret
+ );
-export const getSecretByNameV2 = async (dto: {
+export const getSecretByNameV2 = (dto: {
workspaceId: string;
environmentSlug: string;
secretPath: string;
key: string;
authToken: string;
-}) => {
- const response = await testServer.inject({
- method: "GET",
- url: `/api/v3/secrets/raw/${dto.key}`,
- headers: {
- authorization: `Bearer ${dto.authToken}`
+}) =>
+ request(
+ {
+ method: "GET",
+ url: `/api/v3/secrets/raw/${dto.key}`,
+ headers: {
+ authorization: `Bearer ${dto.authToken}`
+ },
+ query: {
+ workspaceId: dto.workspaceId,
+ environment: dto.environmentSlug,
+ secretPath: dto.secretPath,
+ expandSecretReferences: "true",
+ include_imports: "true"
+ }
},
- query: {
- workspaceId: dto.workspaceId,
- environment: dto.environmentSlug,
- secretPath: dto.secretPath,
- expandSecretReferences: "true",
- include_imports: "true"
- }
- });
- expect(response.statusCode).toBe(200);
- const payload = JSON.parse(response.payload);
- expect(payload).toHaveProperty("secret");
- return payload.secret as TRawSecret;
+ parseSecret
+ );
+
+type TSecretsV2Payload = {
+ secrets: TRawSecret[];
+ imports: {
+ secretPath: string;
+ environment: string;
+ folderId: string;
+ secrets: TRawSecret[];
+ }[];
};
-export const getSecretsV2 = async (dto: {
+export const getSecretsV2 = (dto: {
workspaceId: string;
environmentSlug: string;
secretPath: string;
authToken: string;
recursive?: boolean;
-}) => {
- const getSecretsResponse = await testServer.inject({
- method: "GET",
- url: `/api/v3/secrets/raw`,
- headers: {
- authorization: `Bearer ${dto.authToken}`
+}) =>
+ request(
+ {
+ method: "GET",
+ url: `/api/v3/secrets/raw`,
+ headers: {
+ authorization: `Bearer ${dto.authToken}`
+ },
+ query: {
+ workspaceId: dto.workspaceId,
+ environment: dto.environmentSlug,
+ secretPath: dto.secretPath,
+ expandSecretReferences: "true",
+ include_imports: "true",
+ recursive: String(dto.recursive || false)
+ }
},
- query: {
- workspaceId: dto.workspaceId,
- environment: dto.environmentSlug,
- secretPath: dto.secretPath,
- expandSecretReferences: "true",
- include_imports: "true",
- recursive: String(dto.recursive || false)
+ (res) => {
+ expect(res.statusCode).toBe(200);
+ const payload = res.json<TSecretsV2Payload>();
+ expect(payload).toHaveProperty("secrets");
+ expect(payload).toHaveProperty("imports");
+ return payload;
}
- });
- expect(getSecretsResponse.statusCode).toBe(200);
- const getSecretsPayload = JSON.parse(getSecretsResponse.payload);
- expect(getSecretsPayload).toHaveProperty("secrets");
- expect(getSecretsPayload).toHaveProperty("imports");
- return getSecretsPayload as {
- secrets: TRawSecret[];
- imports: {
- secretPath: string;
- environment: string;
- folderId: string;
- secrets: TRawSecret[];
- }[];
- };
-};
+ );
// Replication and import propagation run asynchronously through BullMQ, so a
// secret shows up in the destination environment some time after the write.