Skip to content

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

test(e2e): restore the helpers the kept approval guard cases use

varonix3b9a8d21c38c
diff --git a/backend/e2e-test/routes/v1/secret-approval-global-guards.spec.ts b/backend/e2e-test/routes/v1/secret-approval-global-guards.spec.ts
index bca968ab5b..d92da99e5a 100644
--- a/backend/e2e-test/routes/v1/secret-approval-global-guards.spec.ts
+++ b/backend/e2e-test/routes/v1/secret-approval-global-guards.spec.ts
@@ -4,15 +4,75 @@ import { createFolder, deleteFolder } from "e2e-test/testUtils/folders";
import { seedLegacySecretApprovalPolicy } from "e2e-test/testUtils/secret-approval-policies";
import { Knex } from "knex";
-import { TableName } from "@app/db/schemas";
+import { AccessScope, OrgMembershipRole, ProjectMembershipRole, TableName } from "@app/db/schemas";
import { seedData1 } from "@app/db/seed-data";
+import { ApproverType } from "@app/ee/services/access-approval-policy/access-approval-policy-types";
+import { ApprovalPolicyType } from "@app/services/approval-policy/approval-policy-enums";
const getDb = () => (globalThis as unknown as { testDb: Knex }).testDb;
const projectId = seedData1.projectV3.id;
+const orgId = seedData1.organization.id;
const envSlug = seedData1.environment.slug;
const authHeaders = () => ({ authorization: `Bearer ${jwtAuthToken}` });
+const insertMembership = async (
+ membership: { scope: AccessScope; scopeProjectId?: string; actorUserId?: string; actorGroupId?: string },
+ role: string
+) => {
+ const [row] = await getDb()(TableName.Membership)
+ .insert({ scopeOrgId: orgId, ...membership })
+ .returning("id");
+ const membershipId = (row as { id: string }).id;
+ await getDb()(TableName.MembershipRole).insert({ membershipId, role });
+ return membershipId;
+};
+
+const createProjectUser = async (label: string) => {
+ const username = `${label}-${crypto.randomUUID()}@localhost.local`;
+ const [user] = await getDb()(TableName.Users)
+ .insert({ username, email: username, firstName: label, isAccepted: true, isGhost: false })
+ .returning("id");
+ const userId = (user as { id: string }).id;
+ await insertMembership({ scope: AccessScope.Organization, actorUserId: userId }, OrgMembershipRole.Member);
+ await insertMembership(
+ { scope: AccessScope.Project, scopeProjectId: projectId, actorUserId: userId },
+ ProjectMembershipRole.Member
+ );
+ return { userId, username };
+};
+
+const createGlobalPolicy = async (name: string, secretPath: string, approver: { type: ApproverType; id: string }) => {
+ const res = await testServer.inject({
+ method: "POST",
+ url: "/api/v2/secret-approvals",
+ headers: authHeaders(),
+ body: { projectId, environment: envSlug, secretPath, approvers: [approver], approvals: 1, name }
+ });
+ expect(res.statusCode).toBe(200);
+ const policyId = (res.json() as { approval: { id: string } }).approval.id;
+ expect(await getDb()(TableName.ApprovalPolicies).where({ id: policyId }).first()).toBeDefined();
+ return policyId;
+};
+
+const replaceGlobalPolicyApprover = async (policyId: string, approver: { type: ApproverType; id: string }) => {
+ const res = await testServer.inject({
+ method: "PATCH",
+ url: `/api/v2/secret-approvals/${policyId}`,
+ headers: authHeaders(),
+ body: { approvers: [approver], approvals: 1 }
+ });
+ expect(res.statusCode).toBe(200);
+};
+
+const removeProjectUser = (username: string) =>
+ testServer.inject({
+ method: "DELETE",
+ url: `/api/v1/projects/${projectId}/memberships`,
+ headers: authHeaders(),
+ body: { usernames: [username] }
+ });
+
const deleteFolderRequest = (folderId: string, secretPath: string) =>
testServer.inject({
method: "DELETE",
@@ -57,13 +117,23 @@ const openRequestMessage = (folderPath: string) =>
`You cannot delete the selected folder because it has open change requests at folder path "${folderPath}". Merge or close the change requests and try again.`;
describe("Global approval system guards outside the approval routes", () => {
+ const userIds: string[] = [];
+ const groupIds: string[] = [];
+ const policyIds: string[] = [];
+ const requestIds: string[] = [];
const legacyPolicyIds: string[] = [];
const legacyRequestIds: string[] = [];
afterAll(async () => {
const db = getDb();
+ await db(TableName.ApprovalRequests).whereIn("id", requestIds).del();
+ await db(TableName.ApprovalPolicies).whereIn("id", policyIds).del();
await db(TableName.SecretApprovalRequest).whereIn("id", legacyRequestIds).del();
await db(TableName.SecretApprovalPolicy).whereIn("id", legacyPolicyIds).del();
+ await db(TableName.Membership).whereIn("actorUserId", userIds).del();
+ await db(TableName.Membership).whereIn("actorGroupId", groupIds).del();
+ await db(TableName.Groups).whereIn("id", groupIds).del();
+ await db(TableName.Users).whereIn("id", userIds).del();
});
test("a user who approves a migrated policy's legacy twin is removable once the global policy drops them", async () => {