Files
calendar/packages/features/ee/organizations/lib/OrganizationPermissionService.test.ts
T
837349e3d3 feat: Organization onboarding improvement - Payment before creation of org but onboarding doesn't require payment (#18990)
* create seed for org upgrade

* migrate about/new pages

* pages refactor + use persistant zustand store

* invited members view + confirm button

* add check to org slug

* check reserved subdomain

* remove quotes from subdomain env var

* intro to creation + billing

* create price

* fix types

* createWithPaymentIntent + permission check on teams

* open stripe link in popup

* intro to tests

* organization price and seat override tests

* move permissions to a new permission service

* update env and permission check

* intro to paid invoice trigger

* dont use subId

* wip

* Get e2e working with migration of teams and members

* fix ts errors

* Get flow working again

* Fix various issues and refactor

* Some more fixes

* Fix wrong page route link

* Platform onboarding fix and moving members of team to org rix

* Platform onboarding fix and moving members of team to org rix

* Fix tests, found a bug

* Get custom price flow working

* Get admin impersonation flow working for a non-existent user

* Fix unit test

* Admin onboarding handover

* fix admin onboarding tests

* fix ts error

* Get updateQuantity working

* More fixes

* fix test

* fix schema name

* Add tests

* Add missing file

* More tests

* Add more tests

* fix mt-2 moving down input into overflow

* fix handover layout removing HOC

* Update PR_TODO.md

* fix ts error due to merge conflict

---------

Co-authored-by: Hariom Balhara <hariombalhara@gmgmail.com>
Co-authored-by: Hariom <hariombalhara@gmail.com>
Co-authored-by: Omar López <zomars@me.com>
2025-03-13 11:17:43 +00:00

99 lines
3.2 KiB
TypeScript

import { describe, expect, it, vi, beforeEach } from "vitest";
import { prisma } from "@calcom/prisma";
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
import { OrganizationPermissionService } from "./OrganizationPermissionService";
vi.mock("@calcom/prisma", () => ({
prisma: {
organizationOnboarding: {
findUnique: vi.fn(),
},
membership: {
findMany: vi.fn(),
},
team: {
findFirst: vi.fn(),
},
},
}));
describe("OrganizationPermissionService", () => {
let service: OrganizationPermissionService;
const mockUser: TrpcSessionUser = {
id: 1,
email: "test@example.com",
role: "USER",
};
beforeEach(() => {
vi.clearAllMocks();
service = new OrganizationPermissionService(mockUser);
});
describe("hasPermissionToCreateForEmail", () => {
it("should allow users to create for their own email", async () => {
const result = await service.hasPermissionToCreateForEmail("test@example.com");
expect(result).toBe(true);
});
it("should not allow users to create for other emails", async () => {
const result = await service.hasPermissionToCreateForEmail("other@example.com");
expect(result).toBe(false);
});
it("should allow admins to create for any email", async () => {
const adminService = new OrganizationPermissionService({ ...mockUser, role: "ADMIN" });
const result = await adminService.hasPermissionToCreateForEmail("other@example.com");
expect(result).toBe(true);
});
});
describe("hasPermissionToMigrateTeams", () => {
it("should return true if user has required permissions for all teams", async () => {
vi.mocked(prisma.membership.findMany).mockResolvedValue([
{ userId: 1, teamId: 1, role: "OWNER" },
{ userId: 1, teamId: 2, role: "ADMIN" },
]);
const result = await service.hasPermissionToMigrateTeams([1, 2]);
expect(result).toBe(true);
});
it("should return false if user lacks permissions for any team", async () => {
vi.mocked(prisma.membership.findMany).mockResolvedValue([{ userId: 1, teamId: 1, role: "OWNER" }]);
const result = await service.hasPermissionToMigrateTeams([1, 2]);
expect(result).toBe(false);
});
it("should return true for empty team list", async () => {
const result = await service.hasPermissionToMigrateTeams([]);
expect(result).toBe(true);
});
});
describe("validatePermissions", () => {
it("should validate all permissions successfully", async () => {
vi.mocked(prisma.organizationOnboarding.findUnique).mockResolvedValue(null);
vi.mocked(prisma.membership.findMany).mockResolvedValue([{ userId: 1, teamId: 1, role: "OWNER" }]);
const result = await service.validatePermissions({
orgOwnerEmail: "test@example.com",
teams: [{ id: 1, isBeingMigrated: true }],
});
expect(result).toBe(true);
});
it("should throw error for unauthorized email", async () => {
await expect(
service.validatePermissions({
orgOwnerEmail: "other@example.com",
})
).rejects.toThrow("you_do_not_have_permission_to_create_an_organization_for_this_email");
});
});
});