Skip to content

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

refactor test

Thiago Araujo da Silva22dc31322b48
diff --git a/backend/e2e-test/routes/v1/org-user-access.spec.ts b/backend/e2e-test/routes/v1/org-user-access.spec.ts
index 82edd4c7dd..18c7250d0a 100644
--- a/backend/e2e-test/routes/v1/org-user-access.spec.ts
+++ b/backend/e2e-test/routes/v1/org-user-access.spec.ts
@@ -1,7 +1,8 @@
-import { AccessScope, OrgMembershipRole, TableName } from "@app/db/schemas";
+import { OrgMembershipRole } from "@app/db/schemas";
import { alphaNumericNanoId } from "@app/lib/nanoid";
import { createIsolatedOrgAndProject } from "../../testUtils/fixtures";
+import { addUsersToGroup, createOrgGroup } from "../../testUtils/groups";
import { addUserMembership, createUser, deleteUsers } from "../../testUtils/users";
type TUserAccessRow = {
@@ -22,32 +23,6 @@ describe("Organization user access listing", () => {
let adminGroupId: string;
let memberGroupId: string;
- // Group rows go straight into the database: the API only adds a user to a group once they are an org
- // member, and the case under test is a user whose only way into the org is the group.
- const createOrgGroup = async (
- name: string,
- role: OrgMembershipRole,
- temporaryRole?: { role: OrgMembershipRole; endsAt: Date }
- ) => {
- const [group] = await testDb(TableName.Groups).insert({ orgId, name, slug: name }).returning("*");
- const [membership] = await testDb(TableName.Membership)
- .insert({ scope: AccessScope.Organization, scopeOrgId: orgId, actorGroupId: group.id, isActive: true })
- .returning("*");
- await testDb(TableName.MembershipRole).insert({ membershipId: membership.id, role });
- if (temporaryRole) {
- await testDb(TableName.MembershipRole).insert({
- membershipId: membership.id,
- role: temporaryRole.role,
- isTemporary: true,
- temporaryMode: "relative",
- temporaryRange: "1h",
- temporaryAccessStartTime: new Date(temporaryRole.endsAt.getTime() - HOUR_MS),
- temporaryAccessEndTime: temporaryRole.endsAt
- });
- }
- return group.id as string;
- };
-
const listUserAccess = async () => {
const res = await testServer.inject({
method: "GET",
@@ -69,24 +44,25 @@ describe("Organization user access listing", () => {
await addUserMembership({ userId: directUser.userId, orgId, role: OrgMembershipRole.Member });
await addUserMembership({ userId: directAndGroupUser.userId, orgId, role: OrgMembershipRole.Member });
- adminGroupId = await createOrgGroup(`engineering-${suffix}`, OrgMembershipRole.Admin, {
- role: OrgMembershipRole.Member,
- endsAt: new Date(Date.now() - HOUR_MS)
- });
- memberGroupId = await createOrgGroup(`qa-team-${suffix}`, OrgMembershipRole.Member, {
+ adminGroupId = await createOrgGroup({
+ orgId,
+ name: `engineering-${suffix}`,
role: OrgMembershipRole.Admin,
- endsAt: new Date(Date.now() + HOUR_MS)
+ temporaryRole: { role: OrgMembershipRole.Member, endsAt: new Date(Date.now() - HOUR_MS) }
+ });
+ memberGroupId = await createOrgGroup({
+ orgId,
+ name: `qa-team-${suffix}`,
+ role: OrgMembershipRole.Member,
+ temporaryRole: { role: OrgMembershipRole.Admin, endsAt: new Date(Date.now() + HOUR_MS) }
});
- await testDb(TableName.UserGroupMembership).insert([
- { userId: directAndGroupUser.userId, groupId: adminGroupId },
- { userId: groupOnlyUser.userId, groupId: adminGroupId },
- { userId: groupOnlyUser.userId, groupId: memberGroupId }
- ]);
+ await addUsersToGroup(adminGroupId, [directAndGroupUser.userId, groupOnlyUser.userId]);
+ await addUsersToGroup(memberGroupId, [groupOnlyUser.userId]);
});
+ // Deleting the org cascades the groups and their membership rows.
afterAll(async () => {
- await testDb(TableName.UserGroupMembership).whereIn("groupId", [adminGroupId, memberGroupId]).del();
await deleteUsers([directUser.userId, directAndGroupUser.userId, groupOnlyUser.userId]);
await cleanup();
});
diff --git a/backend/e2e-test/testUtils/groups.ts b/backend/e2e-test/testUtils/groups.ts
new file mode 100644
index 0000000000..3dcdbe91e2
--- /dev/null
+++ b/backend/e2e-test/testUtils/groups.ts
@@ -0,0 +1,52 @@
+import { AccessScope, OrgMembershipRole, TableName } from "@app/db/schemas";
+
+const HOUR_MS = 60 * 60 * 1000;
+
+// Writes the rows directly: the group API only adds a user once they already hold an org membership, and
+// the specs that need a group are usually testing a user whose only way into the org is the group.
+export const createOrgGroup = async (dto: {
+ orgId: string;
+ name: string;
+ role: OrgMembershipRole;
+ temporaryRole?: { role: OrgMembershipRole; endsAt: Date };
+}) => {
+ const [group] = await testDb(TableName.Groups)
+ .insert({ orgId: dto.orgId, name: dto.name, slug: dto.name })
+ .returning("id");
+ const groupId = (group as { id: string }).id;
+
+ const [membership] = await testDb(TableName.Membership)
+ .insert({ scope: AccessScope.Organization, scopeOrgId: dto.orgId, actorGroupId: groupId, isActive: true })
+ .returning("id");
+ const membershipId = (membership as { id: string }).id;
+
+ await testDb(TableName.MembershipRole).insert({ membershipId, role: dto.role });
+ if (dto.temporaryRole) {
+ await testDb(TableName.MembershipRole).insert({
+ membershipId,
+ role: dto.temporaryRole.role,
+ isTemporary: true,
+ temporaryMode: "relative",
+ temporaryRange: "1h",
+ temporaryAccessStartTime: new Date(dto.temporaryRole.endsAt.getTime() - HOUR_MS),
+ temporaryAccessEndTime: dto.temporaryRole.endsAt
+ });
+ }
+
+ return groupId;
+};
+
+export const addUsersToGroup = async (groupId: string, userIds: string[]) => {
+ if (userIds.length === 0) return;
+ await testDb(TableName.UserGroupMembership).insert(userIds.map((userId) => ({ userId, groupId })));
+};
+
+// The org delete cascades the group rows; this is for specs that reuse a shared org.
+export const deleteGroups = async (groupIds: string[]) => {
+ if (groupIds.length === 0) return;
+ await testDb(TableName.UserGroupMembership).whereIn("groupId", groupIds).delete();
+ const membershipIds = await testDb(TableName.Membership).whereIn("actorGroupId", groupIds).pluck("id");
+ await testDb(TableName.MembershipRole).whereIn("membershipId", membershipIds).delete();
+ await testDb(TableName.Membership).whereIn("id", membershipIds).delete();
+ await testDb(TableName.Groups).whereIn("id", groupIds).delete();
+};
diff --git a/backend/src/lib/api-docs/constants.ts b/backend/src/lib/api-docs/constants.ts
index e737af736f..2251be9ffb 100644
--- a/backend/src/lib/api-docs/constants.ts
+++ b/backend/src/lib/api-docs/constants.ts
@@ -877,6 +877,19 @@ export const ORGANIZATIONS = {
LIST_USER_MEMBERSHIPS: {
organizationId: "The ID of the organization to get memberships from."
},
+ LIST_USER_ACCESS: {
+ organizationId: "The ID of the organization to list user access for.",
+ user: "The user who can reach the organization.",
+ membership:
+ "The user's direct organization membership, or null when they only reach the organization through groups.",
+ groups: "The groups holding an organization membership that the user belongs to.",
+ groupId: "The ID of the group.",
+ groupName: "The name of the group.",
+ groupSlug: "The slug of the group.",
+ groupRoles: "The group's active organization roles. Expired temporary roles are left out.",
+ groupRole: "The role slug, or 'custom' when it is a custom role.",
+ groupRoleId: "The ID of the custom role, or null for a built-in role."
+ },
GET_USER_MEMBERSHIP: {
organizationId: "The ID of the organization to get the membership for.",
membershipId: "The ID of the membership to get."
diff --git a/backend/src/server/routes/v1/organization-router.ts b/backend/src/server/routes/v1/organization-router.ts
index 3d2322c808..355680b16a 100644
--- a/backend/src/server/routes/v1/organization-router.ts
+++ b/backend/src/server/routes/v1/organization-router.ts
@@ -463,7 +463,7 @@ export const registerOrgRouter = async (server: FastifyZodProvider) => {
description:
"List every user with access to the organization, through a direct membership, through groups, or both",
params: z.object({
- organizationId: z.string().trim().uuid()
+ organizationId: z.string().trim().uuid().describe(ORGANIZATIONS.LIST_USER_ACCESS.organizationId)
}),
response: {
200: z.object({
@@ -475,7 +475,9 @@ export const registerOrgRouter = async (server: FastifyZodProvider) => {
firstName: true,
lastName: true,
id: true
- }).merge(z.object({ publicKey: z.string().nullable().optional(), superAdmin: z.boolean().nullish() })),
+ })
+ .merge(z.object({ publicKey: z.string().nullable().optional(), superAdmin: z.boolean().nullish() }))
+ .describe(ORGANIZATIONS.LIST_USER_ACCESS.user),
membership: OrgMembershipsSchema.pick({
id: true,
role: true,
@@ -485,15 +487,24 @@ export const registerOrgRouter = async (server: FastifyZodProvider) => {
isActive: true,
lastLoginAuthMethod: true,
lastLoginTime: true
- }).nullable(),
+ })
+ .nullable()
+ .describe(ORGANIZATIONS.LIST_USER_ACCESS.membership),
groups: z
.object({
- id: z.string().uuid(),
- name: z.string(),
- slug: z.string(),
- roles: z.object({ role: z.string(), roleId: z.string().uuid().nullable() }).array()
+ id: z.string().uuid().describe(ORGANIZATIONS.LIST_USER_ACCESS.groupId),
+ name: z.string().describe(ORGANIZATIONS.LIST_USER_ACCESS.groupName),
+ slug: z.string().describe(ORGANIZATIONS.LIST_USER_ACCESS.groupSlug),
+ roles: z
+ .object({
+ role: z.string().describe(ORGANIZATIONS.LIST_USER_ACCESS.groupRole),
+ roleId: z.string().uuid().nullable().describe(ORGANIZATIONS.LIST_USER_ACCESS.groupRoleId)
+ })
+ .array()
+ .describe(ORGANIZATIONS.LIST_USER_ACCESS.groupRoles)
})
.array()
+ .describe(ORGANIZATIONS.LIST_USER_ACCESS.groups)
})
.array()
})