e5abe93940
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>
142 lines
3.9 KiB
TypeScript
142 lines
3.9 KiB
TypeScript
import type { Request, Response } from "express";
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
import { createMocks } from "node-mocks-http";
|
|
import { describe, it, expect, beforeAll } from "vitest";
|
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
import { isAdminGuard } from "../../../lib/utils/isAdmin";
|
|
import { ScopeOfAdmin } from "../../../lib/utils/scopeOfAdmin";
|
|
|
|
type CustomNextApiRequest = NextApiRequest & Request;
|
|
type CustomNextApiResponse = NextApiResponse & Response;
|
|
|
|
describe("isAdmin guard", () => {
|
|
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,
|
|
},
|
|
});
|
|
}
|
|
|
|
const dunderOrg = await prisma.team.findFirst({
|
|
where: {
|
|
slug: "dunder-mifflin",
|
|
isOrganization: true,
|
|
},
|
|
});
|
|
|
|
if (dunderOrg) {
|
|
await prisma.organizationSettings.upsert({
|
|
where: {
|
|
organizationId: dunderOrg.id,
|
|
},
|
|
update: {
|
|
isAdminAPIEnabled: false,
|
|
},
|
|
create: {
|
|
organizationId: dunderOrg.id,
|
|
orgAutoAcceptEmail: "dunder-mifflin.com",
|
|
isAdminAPIEnabled: false,
|
|
},
|
|
});
|
|
}
|
|
});
|
|
it("Returns false when user does not exist in the system", async () => {
|
|
const { req } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({
|
|
method: "POST",
|
|
body: {},
|
|
});
|
|
|
|
req.userId = 0;
|
|
req.user = undefined;
|
|
|
|
const { isAdmin, scope } = await isAdminGuard(req);
|
|
|
|
expect(isAdmin).toBe(false);
|
|
expect(scope).toBe(null);
|
|
});
|
|
|
|
it("Returns false when org user is a member", async () => {
|
|
const { req } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({
|
|
method: "POST",
|
|
body: {},
|
|
});
|
|
|
|
const memberUser = await prisma.user.findFirstOrThrow({ where: { email: "member2-acme@example.com" } });
|
|
|
|
req.userId = memberUser.id;
|
|
req.user = memberUser;
|
|
|
|
const { isAdmin, scope } = await isAdminGuard(req);
|
|
|
|
expect(isAdmin).toBe(false);
|
|
expect(scope).toBe(null);
|
|
});
|
|
|
|
it("Returns system-wide admin when user is marked as such", async () => {
|
|
const { req } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({
|
|
method: "POST",
|
|
body: {},
|
|
});
|
|
|
|
const adminUser = await prisma.user.findFirstOrThrow({ where: { email: "admin@example.com" } });
|
|
|
|
req.userId = adminUser.id;
|
|
req.user = adminUser;
|
|
|
|
const { isAdmin, scope } = await isAdminGuard(req);
|
|
|
|
expect(isAdmin).toBe(true);
|
|
expect(scope).toBe(ScopeOfAdmin.SystemWide);
|
|
});
|
|
|
|
it("Returns org-wide admin when user is set as such & admin API access is granted", async () => {
|
|
const { req } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({
|
|
method: "POST",
|
|
body: {},
|
|
});
|
|
|
|
const adminUser = await prisma.user.findFirstOrThrow({ where: { email: "owner1-acme@example.com" } });
|
|
|
|
req.userId = adminUser.id;
|
|
req.user = adminUser;
|
|
|
|
const { isAdmin, scope } = await isAdminGuard(req);
|
|
expect(isAdmin).toBe(true);
|
|
expect(scope).toBe(ScopeOfAdmin.OrgOwnerOrAdmin);
|
|
});
|
|
|
|
it("Returns no admin when user is set as org admin but admin API access is revoked", async () => {
|
|
const { req } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({
|
|
method: "POST",
|
|
body: {},
|
|
});
|
|
|
|
const adminUser = await prisma.user.findFirstOrThrow({ where: { email: "owner1-dunder@example.com" } });
|
|
|
|
req.userId = adminUser.id;
|
|
req.user = adminUser;
|
|
|
|
const { isAdmin } = await isAdminGuard(req);
|
|
expect(isAdmin).toBe(false);
|
|
});
|
|
});
|