Repository navigation
feat(projects): Add permission checks to delete project - #120
Conversation
WalkthroughReplaces the not-found/permission error path in the project deletion action with a user-facing message and changes the error code from Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/actions/projects/deleteProject/action.ts (1)
26-29: Validate impact of switching from NOT_FOUND to UNAUTHORIZED and consider de‑duplicating the messageThe new message and use of
ErrorCodes.UNAUTHORIZEDin the!existingProjectpath look consistent with the intent to avoid leaking whether the project exists. The only two follow‑ups I'd suggest:
- Confirm no callers or tests rely on this branch returning
ErrorCodes.NOT_FOUND(e.g., to distinguish stale links vs. permission issues). If such logic exists, it will now see anUNAUTHORIZEDinstead.- Very minor DRY improvement: avoid repeating the same literal string for both the toast and the
error:- if (!existingProject) { - toast.error('You are not authorized to delete this project or it does not exist.'); - return error('You are not authorized to delete this project or it does not exist.', ErrorCodes.UNAUTHORIZED); - } + if (!existingProject) { + const message = 'You are not authorized to delete this project or it does not exist.'; + toast.error(message); + return error(message, ErrorCodes.UNAUTHORIZED); + }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/actions/projects/deleteProject/action.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/actions/projects/deleteProject/action.ts (2)
src/components/ui/sonner.tsx (1)
toast(26-26)src/lib/result.ts (1)
error(38-44)
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (5)
src/actions/projects/deleteProject/action.test.ts (5)
1-26: Mocks and wiring align with the action’s dependenciesThe module mocks for
prisma,getSession, anddeleteProjectmatch howdeleteProjectActionuses them, so the tests are nicely isolated. One minor nit: sincedeleteProjectis mocked and you never assert onprisma.project.delete, that mock field is currently unused — you can drop it now or keep it if you plan to assert against it later.
32-38: Session mocking is consistent; consider asserting it’s actually usedThe shared
beforeEachcorrectly ensures every invocation ofdeleteProjectActionsees the samemockUserIdand a validsavefunction. As an optional tightening, you could addexpect(getSession).toHaveBeenCalled()in one test to guarantee the action still flows through the auth/session layer and doesn’t regress to using a hard‑coded user.
40-60: Unauthorized-path test gives good coverage; could be slightly less brittleThis test cleanly verifies:
- the UNAUTHORIZED result (message +
ErrorCodes.UNAUTHORIZED), and- that the Prisma
findFirstfilter includes bothidanduserId, whiledeleteProjectis not invoked.To make it a bit more resilient to future changes (e.g., adding extra selected fields), you could switch the Prisma assertion to
expect.objectContaining:expect(prisma.project.findFirst).toHaveBeenCalledWith( expect.objectContaining({ where: { id: mockProjectId, userId: mockUserId }, }), );This still guarantees the permission filter while tolerating additional options in the query.
62-82: Success-path test validates both authorization and delegation to business logicThe success test correctly:
- stubs
findFirstto return a project,- stubs
deleteProjectto succeed,- asserts
result.success === trueand thatresult.datais the value returned fromdeleteProject, and- verifies the Prisma filter again includes both
idanduserId, and thatdeleteProjectis called with the parsed input.As with the previous test, consider using
expect.objectContainingfor the Prisma call to reduce brittleness if the query evolves while still locking in the permission check.
84-105: Delete-failure test nicely pins the BAD_REQUEST behaviorThis test cleanly covers the branch where
deleteProjectreturns a failedResult, ensuring:
- the action surfaces the same error message,
- uses
ErrorCodes.BAD_REQUEST, and- still enforces the
(id, userId)filter and delegates todeleteProjectwith the correct payload.If you want even fuller coverage in the future, you could add a separate test where Prisma (or
deleteProject) throws to exercise thecatchpath anderrorFromException, but that’s optional for this PR.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/actions/projects/deleteProject/action.test.ts(1 hunks)src/actions/projects/deleteProject/action.ts(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- src/actions/projects/deleteProject/action.ts
🧰 Additional context used
🧬 Code graph analysis (1)
src/actions/projects/deleteProject/action.test.ts (4)
src/lib/session.ts (1)
getSession(33-36)src/lib/prisma.ts (1)
prisma(14-18)src/actions/projects/deleteProject/action.ts (1)
deleteProjectAction(10-45)src/actions/projects/deleteProject/logic.ts (1)
deleteProject(7-13)
|
the added permission checks for project deletion are a solid move for security. approved to merge! 🚀 |
This PR introduces robust permission checks to the
deleteProjectaction, ensuring only authorized users can remove projects.Changes
permissionsutility withinsrc/actions/projects/deleteProject/action.tsto validate user access before project deletion.Result.errwith a clear unauthorized error if the requesting user lacks the necessary permissions.Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.