Files
calendar/apps/api/v1/test/lib/utils/retrieveScopedAccessibleUsers.integration-test.ts
T
Anik Dhabal BabuGitHubDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
e5abe93940 fix: Add beforeAll hooks to ensure organization settings are properly configured in integration tests (#24674)
The flaky test failures were caused by the tests depending on the database being properly seeded with the isAdminAPIEnabled flag set to true for the Acme organization. The tests would fail randomly when the database wasn't properly seeded or when the organization settings weren't configured correctly.

This fix adds beforeAll hooks to the failing integration tests to ensure that:
1. The Acme organization has isAdminAPIEnabled set to true
2. The Dunder Mifflin organization has isAdminAPIEnabled set to false

This ensures consistent test behavior regardless of the database state and prevents the flaky failures.

Fixes the following failing tests:
- isAdmin.integration-test.ts: Returns org-wide admin when user is set as such & admin API access is granted
- retrieveScopedAccessibleUsers.integration-test.ts: Returns members when admin user ID is supplied and members IDs are supplied
- retrieveScopedAccessibleUsers.integration-test.ts: Returns members when admin user ID is an admin of an org
- _get.integration-test.ts: Returns bookings for org users when accessed by org admin
- _patch.integration-test.ts: Allows PATCH when user is org-wide admin

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2025-10-27 05:32:05 -03:00

115 lines
3.8 KiB
TypeScript

import { describe, it, expect, beforeAll } from "vitest";
import prisma from "@calcom/prisma";
import {
getAccessibleUsers,
retrieveOrgScopedAccessibleUsers,
} from "../../../lib/utils/retrieveScopedAccessibleUsers";
describe("retrieveScopedAccessibleUsers tests", () => {
beforeAll(async () => {
const acmeOrg = await prisma.team.findFirst({
where: {
slug: "acme",
isOrganization: true,
},
});
if (acmeOrg) {
await prisma.organizationSettings.upsert({
where: {
organizationId: acmeOrg.id,
},
update: {
isAdminAPIEnabled: true,
},
create: {
organizationId: acmeOrg.id,
orgAutoAcceptEmail: "acme.com",
isAdminAPIEnabled: true,
},
});
}
});
describe("getAccessibleUsers", () => {
it("Does not return members when only admin user ID is supplied", async () => {
const adminUser = await prisma.user.findFirstOrThrow({ where: { email: "owner1-acme@example.com" } });
const accessibleUserIds = await getAccessibleUsers({
memberUserIds: [],
adminUserId: adminUser.id,
});
expect(accessibleUserIds.length).toBe(0);
});
it("Does not return members when admin user ID is not an admin of the user", async () => {
const adminUser = await prisma.user.findFirstOrThrow({ where: { email: "owner1-dunder@example.com" } });
const memberOneUser = await prisma.user.findFirstOrThrow({
where: { email: "member1-acme@example.com" },
});
const accessibleUserIds = await getAccessibleUsers({
memberUserIds: [memberOneUser.id],
adminUserId: adminUser.id,
});
expect(accessibleUserIds.length).toBe(0);
});
it("Returns members when admin user ID is supplied and members IDs are supplied", async () => {
const adminUser = await prisma.user.findFirstOrThrow({ where: { email: "owner1-acme@example.com" } });
const memberOneUser = await prisma.user.findFirstOrThrow({
where: { email: "member1-acme@example.com" },
});
const memberTwoUser = await prisma.user.findFirstOrThrow({
where: { email: "member2-acme@example.com" },
});
const accessibleUserIds = await getAccessibleUsers({
memberUserIds: [memberOneUser.id, memberTwoUser.id],
adminUserId: adminUser.id,
});
expect(accessibleUserIds.length).toBe(2);
expect(accessibleUserIds).toContain(memberOneUser.id);
expect(accessibleUserIds).toContain(memberTwoUser.id);
});
});
describe("retrieveOrgScopedAccessibleUsers", () => {
it("Does not return members when admin user ID is an admin of an org", async () => {
const memberOneUser = await prisma.user.findFirstOrThrow({
where: { email: "member1-acme@example.com" },
});
const accessibleUserIds = await retrieveOrgScopedAccessibleUsers({
adminId: memberOneUser.id,
});
expect(accessibleUserIds.length).toBe(0);
});
it("Returns members when admin user ID is an admin of an org", async () => {
const adminUser = await prisma.user.findFirstOrThrow({
where: { email: "owner1-acme@example.com" },
});
const accessibleUserIds = await retrieveOrgScopedAccessibleUsers({
adminId: adminUser.id,
});
const memberOneUser = await prisma.user.findFirstOrThrow({
where: { email: "member1-acme@example.com" },
});
const memberTwoUser = await prisma.user.findFirstOrThrow({
where: { email: "member2-acme@example.com" },
});
expect(accessibleUserIds.length).toBe(11);
expect(accessibleUserIds).toContain(memberOneUser.id);
expect(accessibleUserIds).toContain(memberTwoUser.id);
expect(accessibleUserIds).toContain(adminUser.id);
});
});
});