-
Notifications
You must be signed in to change notification settings - Fork 11.1k
fix: Remove hosts - verify event type belongs to event type #25321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9943a5c
Add check that event type belongs to team
joeauyeung e232826
Add `findAcceptedMembershipsByUserIdsInTeam` to `MembershipRepository`
joeauyeung f781039
Validate that passed `userIds` belong to a team
joeauyeung 090396c
Add tests
joeauyeung 16f3ebd
Typo fix
joeauyeung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
346 changes: 346 additions & 0 deletions
346
packages/trpc/server/routers/viewer/teams/removeHostsFromEventTypes.handler.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,346 @@ | ||
| /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
| import { describe, it, beforeEach, vi, expect } from "vitest"; | ||
|
|
||
| import { MembershipRepository } from "@calcom/features/membership/repositories/MembershipRepository"; | ||
| import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service"; | ||
| import prisma from "@calcom/prisma"; | ||
|
|
||
| import type { TrpcSessionUser } from "../../../types"; | ||
| import removeHostsFromEventTypesHandler from "./removeHostsFromEventTypes.handler"; | ||
|
|
||
| vi.mock("@calcom/prisma", () => ({ | ||
| default: { | ||
| host: { | ||
| deleteMany: vi.fn(), | ||
| }, | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock("@calcom/features/pbac/services/permission-check.service", () => ({ | ||
| PermissionCheckService: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@calcom/features/membership/repositories/MembershipRepository", () => ({ | ||
| MembershipRepository: { | ||
| findAcceptedMembershipsByUserIdsInTeam: vi.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| describe("removeHostsFromEventTypesHandler", () => { | ||
| const mockUser = { | ||
| id: 1, | ||
| name: "Test User", | ||
| } as NonNullable<TrpcSessionUser>; | ||
|
|
||
| const mockInput = { | ||
| userIds: [101, 102], | ||
| eventTypeIds: [201, 202], | ||
| teamId: 300, | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("throws UNAUTHORIZED if user does not have eventType.update permission", async () => { | ||
| const mockCheckPermission = vi.fn().mockResolvedValue(false); | ||
| (PermissionCheckService as any).mockImplementation(() => ({ | ||
| checkPermission: mockCheckPermission, | ||
| })); | ||
|
|
||
| await expect( | ||
| removeHostsFromEventTypesHandler({ | ||
| ctx: { user: mockUser }, | ||
| input: mockInput, | ||
| }) | ||
| ).rejects.toMatchObject({ | ||
| code: "UNAUTHORIZED", | ||
| }); | ||
|
|
||
| expect(mockCheckPermission).toHaveBeenCalledWith({ | ||
| userId: mockUser.id, | ||
| teamId: mockInput.teamId, | ||
| permission: "eventType.update", | ||
| fallbackRoles: ["OWNER", "ADMIN"], | ||
| }); | ||
|
|
||
| expect(MembershipRepository.findAcceptedMembershipsByUserIdsInTeam).not.toHaveBeenCalled(); | ||
| expect(prisma.host.deleteMany).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("deletes hosts when user has permission and all users are team members", async () => { | ||
| const mockCheckPermission = vi.fn().mockResolvedValue(true); | ||
| (PermissionCheckService as any).mockImplementation(() => ({ | ||
| checkPermission: mockCheckPermission, | ||
| })); | ||
|
|
||
| // Mock that all userIds are valid team members | ||
| (MembershipRepository.findAcceptedMembershipsByUserIdsInTeam as any).mockResolvedValue([ | ||
| { userId: 101 }, | ||
| { userId: 102 }, | ||
| ]); | ||
|
|
||
| const mockDeleteResult = { count: 3 }; | ||
| (prisma.host.deleteMany as any).mockResolvedValue(mockDeleteResult); | ||
|
|
||
| const result = await removeHostsFromEventTypesHandler({ | ||
| ctx: { user: mockUser }, | ||
| input: mockInput, | ||
| }); | ||
|
|
||
| expect(result).toEqual(mockDeleteResult); | ||
|
|
||
| expect(mockCheckPermission).toHaveBeenCalledWith({ | ||
| userId: mockUser.id, | ||
| teamId: mockInput.teamId, | ||
| permission: "eventType.update", | ||
| fallbackRoles: ["OWNER", "ADMIN"], | ||
| }); | ||
|
|
||
| expect(MembershipRepository.findAcceptedMembershipsByUserIdsInTeam).toHaveBeenCalledWith({ | ||
| userIds: mockInput.userIds, | ||
| teamId: mockInput.teamId, | ||
| }); | ||
|
|
||
| expect(prisma.host.deleteMany).toHaveBeenCalledWith({ | ||
| where: { | ||
| eventTypeId: { | ||
| in: mockInput.eventTypeIds, | ||
| }, | ||
| eventType: { | ||
| teamId: mockInput.teamId, | ||
| }, | ||
| userId: { | ||
| in: mockInput.userIds, | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it("only removes hosts for userIds that are team members (filters out non-members)", async () => { | ||
| const mockCheckPermission = vi.fn().mockResolvedValue(true); | ||
| (PermissionCheckService as any).mockImplementation(() => ({ | ||
| checkPermission: mockCheckPermission, | ||
| })); | ||
|
|
||
| // Mock that only userId 101 is a team member, 102 is not | ||
| (MembershipRepository.findAcceptedMembershipsByUserIdsInTeam as any).mockResolvedValue([ | ||
| { userId: 101 }, | ||
| ]); | ||
|
|
||
| const mockDeleteResult = { count: 1 }; | ||
| (prisma.host.deleteMany as any).mockResolvedValue(mockDeleteResult); | ||
|
|
||
| const result = await removeHostsFromEventTypesHandler({ | ||
| ctx: { user: mockUser }, | ||
| input: mockInput, | ||
| }); | ||
|
|
||
| expect(result).toEqual(mockDeleteResult); | ||
|
|
||
| expect(MembershipRepository.findAcceptedMembershipsByUserIdsInTeam).toHaveBeenCalledWith({ | ||
| userIds: mockInput.userIds, | ||
| teamId: mockInput.teamId, | ||
| }); | ||
|
|
||
| // Should only delete for userId 101, not 102 | ||
| expect(prisma.host.deleteMany).toHaveBeenCalledWith({ | ||
| where: { | ||
| eventTypeId: { | ||
| in: mockInput.eventTypeIds, | ||
| }, | ||
| eventType: { | ||
| teamId: mockInput.teamId, | ||
| }, | ||
| userId: { | ||
| in: [101], // Only 101, not 102 | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it("handles empty userIds array", async () => { | ||
| const mockCheckPermission = vi.fn().mockResolvedValue(true); | ||
| (PermissionCheckService as any).mockImplementation(() => ({ | ||
| checkPermission: mockCheckPermission, | ||
| })); | ||
|
|
||
| // Empty array means no memberships to validate | ||
| (MembershipRepository.findAcceptedMembershipsByUserIdsInTeam as any).mockResolvedValue([]); | ||
|
|
||
| const mockDeleteResult = { count: 0 }; | ||
| (prisma.host.deleteMany as any).mockResolvedValue(mockDeleteResult); | ||
|
|
||
| const emptyUsersInput = { | ||
| ...mockInput, | ||
| userIds: [], | ||
| }; | ||
|
|
||
| const result = await removeHostsFromEventTypesHandler({ | ||
| ctx: { user: mockUser }, | ||
| input: emptyUsersInput, | ||
| }); | ||
|
|
||
| expect(result).toEqual(mockDeleteResult); | ||
|
|
||
| expect(prisma.host.deleteMany).toHaveBeenCalledWith({ | ||
| where: { | ||
| eventTypeId: { | ||
| in: mockInput.eventTypeIds, | ||
| }, | ||
| eventType: { | ||
| teamId: mockInput.teamId, | ||
| }, | ||
| userId: { | ||
| in: [], | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it("handles empty eventTypeIds array", async () => { | ||
| const mockCheckPermission = vi.fn().mockResolvedValue(true); | ||
| (PermissionCheckService as any).mockImplementation(() => ({ | ||
| checkPermission: mockCheckPermission, | ||
| })); | ||
|
|
||
| // Mock that all userIds are valid team members | ||
| (MembershipRepository.findAcceptedMembershipsByUserIdsInTeam as any).mockResolvedValue([ | ||
| { userId: 101 }, | ||
| { userId: 102 }, | ||
| ]); | ||
|
|
||
| const mockDeleteResult = { count: 0 }; | ||
| (prisma.host.deleteMany as any).mockResolvedValue(mockDeleteResult); | ||
|
|
||
| const emptyEventTypesInput = { | ||
| ...mockInput, | ||
| eventTypeIds: [], | ||
| }; | ||
|
|
||
| const result = await removeHostsFromEventTypesHandler({ | ||
| ctx: { user: mockUser }, | ||
| input: emptyEventTypesInput, | ||
| }); | ||
|
|
||
| expect(result).toEqual(mockDeleteResult); | ||
|
|
||
| expect(prisma.host.deleteMany).toHaveBeenCalledWith({ | ||
| where: { | ||
| eventTypeId: { | ||
| in: [], | ||
| }, | ||
| eventType: { | ||
| teamId: mockInput.teamId, | ||
| }, | ||
| userId: { | ||
| in: mockInput.userIds, | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it("returns count of 0 when no hosts match the criteria", async () => { | ||
| const mockCheckPermission = vi.fn().mockResolvedValue(true); | ||
| (PermissionCheckService as any).mockImplementation(() => ({ | ||
| checkPermission: mockCheckPermission, | ||
| })); | ||
|
|
||
| // Mock that all userIds are valid team members | ||
| (MembershipRepository.findAcceptedMembershipsByUserIdsInTeam as any).mockResolvedValue([ | ||
| { userId: 101 }, | ||
| { userId: 102 }, | ||
| ]); | ||
|
|
||
| const mockDeleteResult = { count: 0 }; | ||
| (prisma.host.deleteMany as any).mockResolvedValue(mockDeleteResult); | ||
|
|
||
| const result = await removeHostsFromEventTypesHandler({ | ||
| ctx: { user: mockUser }, | ||
| input: mockInput, | ||
| }); | ||
|
|
||
| expect(result.count).toBe(0); | ||
| }); | ||
|
|
||
| it("returns count of 0 when userId is a team member but not a host on the specified event types", async () => { | ||
| const mockCheckPermission = vi.fn().mockResolvedValue(true); | ||
| (PermissionCheckService as any).mockImplementation(() => ({ | ||
| checkPermission: mockCheckPermission, | ||
| })); | ||
|
|
||
| // User 999 is a valid team member | ||
| (MembershipRepository.findAcceptedMembershipsByUserIdsInTeam as any).mockResolvedValue([ | ||
| { userId: 999 }, | ||
| ]); | ||
|
|
||
| // But they're not a host on any of the event types | ||
| const mockDeleteResult = { count: 0 }; | ||
| (prisma.host.deleteMany as any).mockResolvedValue(mockDeleteResult); | ||
|
|
||
| const nonHostInput = { | ||
| ...mockInput, | ||
| userIds: [999], | ||
| }; | ||
|
|
||
| const result = await removeHostsFromEventTypesHandler({ | ||
| ctx: { user: mockUser }, | ||
| input: nonHostInput, | ||
| }); | ||
|
|
||
| expect(result.count).toBe(0); | ||
|
|
||
| expect(prisma.host.deleteMany).toHaveBeenCalledWith({ | ||
| where: { | ||
| eventTypeId: { | ||
| in: mockInput.eventTypeIds, | ||
| }, | ||
| eventType: { | ||
| teamId: mockInput.teamId, | ||
| }, | ||
| userId: { | ||
| in: [999], | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it("returns count of 0 when event types do not belong to the specified team", async () => { | ||
| const mockCheckPermission = vi.fn().mockResolvedValue(true); | ||
| (PermissionCheckService as any).mockImplementation(() => ({ | ||
| checkPermission: mockCheckPermission, | ||
| })); | ||
|
|
||
| // Mock that all userIds are valid team members | ||
| (MembershipRepository.findAcceptedMembershipsByUserIdsInTeam as any).mockResolvedValue([ | ||
| { userId: 101 }, | ||
| { userId: 102 }, | ||
| ]); | ||
|
|
||
| // Event types belong to a different team, so no hosts should be deleted | ||
| const mockDeleteResult = { count: 0 }; | ||
| (prisma.host.deleteMany as any).mockResolvedValue(mockDeleteResult); | ||
|
|
||
| const result = await removeHostsFromEventTypesHandler({ | ||
| ctx: { user: mockUser }, | ||
| input: mockInput, | ||
| }); | ||
|
|
||
| expect(result.count).toBe(0); | ||
|
|
||
| // Verify the query includes the teamId check | ||
| expect(prisma.host.deleteMany).toHaveBeenCalledWith({ | ||
| where: { | ||
| eventTypeId: { | ||
| in: mockInput.eventTypeIds, | ||
| }, | ||
| eventType: { | ||
| teamId: mockInput.teamId, | ||
| }, | ||
| userId: { | ||
| in: mockInput.userIds, | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Limit this query to the fields actually consumed (e.g., userId) instead of returning full membership rows; otherwise Prisma fetches unnecessary, potentially sensitive columns.
Prompt for AI agents