Skip to content

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

use test lifecicle to delete resources

Thiago Araujo da Silvac5b4cfa18a51
diff --git a/backend/e2e-test/routes/v1/access-approval-policy.spec.ts b/backend/e2e-test/routes/v1/access-approval-policy.spec.ts
index 8e442fe67a..81487e191c 100644
--- a/backend/e2e-test/routes/v1/access-approval-policy.spec.ts
+++ b/backend/e2e-test/routes/v1/access-approval-policy.spec.ts
@@ -8,6 +8,27 @@ import { ApproverType } from "@app/ee/services/access-approval-policy/access-app
const getDb = () => (globalThis as unknown as { testDb: Knex }).testDb;
+// Policies are created on the *shared* seeded project and environment, so any
+// that outlive this file change how later specs behave. Nothing in the suite
+// exercises access requests today, which is the only reason leftovers here have
+// been harmless — the sibling secret-approval leak silently broke secret writes
+// at "/" for every later spec. Track what we create and remove it after each
+// test rather than relying on that staying true.
+const createdPolicyIds: string[] = [];
+
+const deletePolicy = async (policyId: string) => {
+ const res = await testServer.inject({
+ method: "DELETE",
+ url: `/api/v1/access-approvals/policies/${policyId}`,
+ headers: {
+ authorization: `Bearer ${jwtAuthToken}`
+ }
+ });
+ if (res.statusCode !== 200 && res.statusCode !== 404) {
+ throw new Error(`cleanup: unexpected ${res.statusCode} deleting policy ${policyId} — ${res.payload}`);
+ }
+};
+
const createPolicy = async (dto: {
name: string;
secretPath: string;
@@ -31,11 +52,15 @@ const createPolicy = async (dto: {
});
expect(res.statusCode).toBe(200);
- return res.json().approval;
+ const { approval } = res.json();
+ createdPolicyIds.push(approval.id);
+ return approval;
};
-const createPolicyWithGroupApprover = (dto: { name: string; groupId: string; secretPath: string }) =>
- testServer.inject({
+// Returns the raw response because callers assert on rejections too, so only
+// register a policy when one was actually created.
+const createPolicyWithGroupApprover = async (dto: { name: string; groupId: string; secretPath: string }) => {
+ const res = await testServer.inject({
method: "POST",
url: `/api/v1/access-approvals/policies`,
headers: {
@@ -51,6 +76,12 @@ const createPolicyWithGroupApprover = (dto: { name: string; groupId: string; sec
}
});
+ if (res.statusCode === 200) {
+ createdPolicyIds.push(res.json().approval.id);
+ }
+ return res;
+};
+
const seedGroup = async (db: Knex, dto: { slug: string; addToProject: boolean }) => {
const [group] = await db(TableName.Groups)
.insert({
@@ -95,6 +126,13 @@ const cleanupGroup = async (db: Knex, groupId: string) => {
};
describe("Access approval policy router", async () => {
+ // The update test hard-deletes its own policy rows in a finally, so its id is
+ // already gone by the time this runs — deletePolicy tolerates the 404.
+ afterEach(async () => {
+ const ids = createdPolicyIds.splice(0);
+ await Promise.all(ids.map(deletePolicy));
+ });
+
test("Create policy", async () => {
const policy = await createPolicy({
secretPath: "/",
diff --git a/backend/e2e-test/routes/v1/secret-approval-policy.spec.ts b/backend/e2e-test/routes/v1/secret-approval-policy.spec.ts
index e71925acf7..5deaf1b491 100644
--- a/backend/e2e-test/routes/v1/secret-approval-policy.spec.ts
+++ b/backend/e2e-test/routes/v1/secret-approval-policy.spec.ts
@@ -8,6 +8,26 @@ import { ApproverType } from "@app/ee/services/access-approval-policy/access-app
const getDb = () => (globalThis as unknown as { testDb: Knex }).testDb;
+// Policies are created on the *shared* seeded project and environment, so any
+// that outlive this file change how every later spec behaves: a policy at "/"
+// makes secret writes there return an approval request instead of a secret,
+// which breaks specs that have nothing to do with approvals. Track what we
+// create and remove it after each test.
+const createdPolicyIds: string[] = [];
+
+const deletePolicy = async (sapId: string) => {
+ const res = await testServer.inject({
+ method: "DELETE",
+ url: `/api/v1/secret-approvals/${sapId}`,
+ headers: {
+ authorization: `Bearer ${jwtAuthToken}`
+ }
+ });
+ if (res.statusCode !== 200 && res.statusCode !== 404) {
+ throw new Error(`cleanup: unexpected ${res.statusCode} deleting policy ${sapId} — ${res.payload}`);
+ }
+};
+
const createPolicy = async (dto: {
name: string;
secretPath: string;
@@ -31,11 +51,15 @@ const createPolicy = async (dto: {
});
expect(res.statusCode).toBe(200);
- return res.json().approval;
+ const { approval } = res.json();
+ createdPolicyIds.push(approval.id);
+ return approval;
};
-const createPolicyWithGroupApprover = (dto: { name: string; groupId: string; secretPath: string }) =>
- testServer.inject({
+// Returns the raw response because callers assert on rejections too, so only
+// register a policy when one was actually created.
+const createPolicyWithGroupApprover = async (dto: { name: string; groupId: string; secretPath: string }) => {
+ const res = await testServer.inject({
method: "POST",
url: `/api/v1/secret-approvals`,
headers: {
@@ -51,6 +75,12 @@ const createPolicyWithGroupApprover = (dto: { name: string; groupId: string; sec
}
});
+ if (res.statusCode === 200) {
+ createdPolicyIds.push(res.json().approval.id);
+ }
+ return res;
+};
+
const seedGroup = async (db: Knex, dto: { slug: string; addToProject: boolean }) => {
const [group] = await db(TableName.Groups)
.insert({
@@ -95,6 +125,11 @@ const cleanupGroup = async (db: Knex, groupId: string) => {
};
describe("Secret approval policy router", async () => {
+ afterEach(async () => {
+ const ids = createdPolicyIds.splice(0);
+ await Promise.all(ids.map(deletePolicy));
+ });
+
test("Create policy", async () => {
const policy = await createPolicy({
secretPath: "/",
diff --git a/backend/e2e-test/routes/v2/secret-folder.spec.ts b/backend/e2e-test/routes/v2/secret-folder.spec.ts
index eabfce0988..033d184378 100644
--- a/backend/e2e-test/routes/v2/secret-folder.spec.ts
+++ b/backend/e2e-test/routes/v2/secret-folder.spec.ts
@@ -407,6 +407,16 @@ describe("Secret Folder Move Router", async () => {
secretPath: "/policy-src/protected"
});
+ // Registered rather than left at the end of the body so a failing assertion
+ // can't leak the policy — a stray approval policy silently changes how later
+ // specs' writes behave. The policy has to go before the folders it governs,
+ // or $checkFolderPolicy blocks the source tree delete.
+ onTestFinished(async () => {
+ await deleteSecretApprovalPolicy(policy.id);
+ await deleteFolderInEnv({ path: "/", id: srcRoot.id, environment: sourceEnv, forceDelete: true });
+ await deleteFolderInEnv({ path: "/", id: destParent.id, environment: sourceEnv, forceDelete: true });
+ });
+
const res = await moveFolder({
folderId: srcRoot.id,
destinationEnvironment: sourceEnv,
@@ -423,11 +433,6 @@ describe("Secret Folder Move Router", async () => {
// nothing was copied to the destination
expect(await getFolders({ path: "/policy-dest", environment: sourceEnv })).toHaveLength(0);
-
- // cleanup: removing the policy first so $checkFolderPolicy no longer blocks the source tree delete
- await deleteSecretApprovalPolicy(policy.id);
- await deleteFolderInEnv({ path: "/", id: srcRoot.id, environment: sourceEnv, forceDelete: true });
- await deleteFolderInEnv({ path: "/", id: destParent.id, environment: sourceEnv, forceDelete: true });
}, 10_000);
test("Blocks moving a folder when the destination path is governed by an approval policy", async () => {
@@ -443,6 +448,12 @@ describe("Secret Folder Move Router", async () => {
secretPath: "/policy-dest-guard/move-into-policy"
});
+ onTestFinished(async () => {
+ await deleteSecretApprovalPolicy(policy.id);
+ await deleteFolderInEnv({ path: "/", id: srcRoot.id, environment: sourceEnv, forceDelete: true });
+ await deleteFolderInEnv({ path: "/", id: destParent.id, environment: sourceEnv, forceDelete: true });
+ });
+
const res = await moveFolder({
folderId: srcRoot.id,
destinationEnvironment: sourceEnv,
@@ -458,9 +469,5 @@ describe("Secret Folder Move Router", async () => {
// nothing was copied to the destination
expect(await getFolders({ path: "/policy-dest-guard", environment: sourceEnv })).toHaveLength(0);
-
- await deleteSecretApprovalPolicy(policy.id);
- await deleteFolderInEnv({ path: "/", id: srcRoot.id, environment: sourceEnv, forceDelete: true });
- await deleteFolderInEnv({ path: "/", id: destParent.id, environment: sourceEnv, forceDelete: true });
}, 10_000);
});