-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
fix: Invalid 403 forbidden for event-types GET #14528
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
917a8ef
fix: Invalid 403 forbidden for event-types GET
keithwillcode 3aee79b
Merge branch 'main' into fix/api-event-types-permission-check
keithwillcode fb18250
chore: Add unit tests event types api (#14536)
keithwillcode f5952f5
Merge branch 'main' into fix/api-event-types-permission-check
zomars c98a56d
Added userId to explicitly be different from the userId on the request
keithwillcode File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
18 changes: 0 additions & 18 deletions
18
apps/api/v1/pages/api/event-types/[id]/_auth-middleware.ts
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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,143 @@ | ||
import prismaMock from "../../../../../../../tests/libs/__mocks__/prismaMock"; | ||
|
||
import type { Request, Response } from "express"; | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
import { createMocks } from "node-mocks-http"; | ||
import { describe, expect, test } from "vitest"; | ||
|
||
import { buildEventType } from "@calcom/lib/test/builder"; | ||
import { MembershipRole } from "@calcom/prisma/enums"; | ||
|
||
import handler from "../../../../pages/api/event-types/[id]/_get"; | ||
|
||
type CustomNextApiRequest = NextApiRequest & Request; | ||
type CustomNextApiResponse = NextApiResponse & Response; | ||
|
||
describe("GET /api/event-types/[id]", () => { | ||
describe("Errors", () => { | ||
test("Returns 403 if user not admin/team member/event owner", async () => { | ||
const { req, res } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({ | ||
method: "GET", | ||
body: {}, | ||
query: { | ||
id: 123456, | ||
}, | ||
}); | ||
|
||
prismaMock.eventType.findUnique.mockResolvedValue( | ||
buildEventType({ | ||
id: 123456, | ||
userId: 444444, | ||
}) | ||
); | ||
|
||
req.userId = 333333; | ||
await handler(req, res); | ||
|
||
expect(res.statusCode).toBe(403); | ||
}); | ||
}); | ||
|
||
describe("Success", async () => { | ||
test("Returns event type if user is admin", async () => { | ||
const eventTypeId = 123456; | ||
const { req, res } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({ | ||
method: "GET", | ||
body: {}, | ||
query: { | ||
id: eventTypeId, | ||
}, | ||
}); | ||
|
||
prismaMock.eventType.findUnique.mockResolvedValue( | ||
buildEventType({ | ||
id: eventTypeId, | ||
}) | ||
); | ||
|
||
req.isAdmin = true; | ||
req.userId = 333333; | ||
await handler(req, res); | ||
|
||
expect(res.statusCode).toBe(200); | ||
expect(JSON.parse(res._getData()).event_type.id).toEqual(eventTypeId); | ||
}); | ||
|
||
test("Returns event type if user is in team associated with event type", async () => { | ||
const eventTypeId = 123456; | ||
const teamId = 9999; | ||
const userId = 333333; | ||
const { req, res } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({ | ||
method: "GET", | ||
body: {}, | ||
query: { | ||
id: eventTypeId, | ||
}, | ||
}); | ||
|
||
prismaMock.eventType.findUnique.mockResolvedValue( | ||
buildEventType({ | ||
id: eventTypeId, | ||
teamId, | ||
}) | ||
); | ||
|
||
prismaMock.team.findFirst.mockResolvedValue({ | ||
id: teamId, | ||
members: [ | ||
{ | ||
userId, | ||
}, | ||
], | ||
}); | ||
|
||
req.isAdmin = false; | ||
req.userId = userId; | ||
await handler(req, res); | ||
|
||
expect(res.statusCode).toBe(200); | ||
expect(JSON.parse(res._getData()).event_type.id).toEqual(eventTypeId); | ||
expect(prismaMock.team.findFirst).toHaveBeenCalledWith({ | ||
where: { | ||
id: teamId, | ||
members: { | ||
some: { | ||
userId: req.userId, | ||
role: { | ||
in: [MembershipRole.OWNER, MembershipRole.ADMIN, MembershipRole.MEMBER], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
test("Returns event type if user is the event type owner", async () => { | ||
const eventTypeId = 123456; | ||
const userId = 333333; | ||
const { req, res } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({ | ||
method: "GET", | ||
body: {}, | ||
query: { | ||
id: eventTypeId, | ||
}, | ||
}); | ||
|
||
prismaMock.eventType.findUnique.mockResolvedValue( | ||
buildEventType({ | ||
id: eventTypeId, | ||
userId, | ||
scheduleId: 1111, | ||
}) | ||
); | ||
|
||
req.isAdmin = false; | ||
req.userId = userId; | ||
await handler(req, res); | ||
|
||
expect(res.statusCode).toBe(200); | ||
expect(JSON.parse(res._getData()).event_type.id).toEqual(eventTypeId); | ||
expect(prismaMock.team.findFirst).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
This file contains 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
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.
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.
401 was the wrong HTTP status code to use here because the user is authenticated at this point given the API key passed in but they are forbidden from performing this action.