Test: add tests for ActivityLogPolicy to check access for admin, vet …#157
Conversation
📝 WalkthroughWalkthroughA new JUnit 5 test class Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 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.
🧹 Nitpick comments (1)
src/test/java/org/example/vet1177/policy/ActivityLogPolicyTest.java (1)
111-132: Add tests for the remaining defensive branches incanView.The suite still misses explicit cases for null role (
user.getRole() == null), vet null-clinic branches, and owner null-owner/null-owner-id branches. Covering these would fully lock down the guard logic.Proposed additional test cases
+ `@Test` + void canView_shouldThrow_whenUserRoleIsNull() { + User noRoleUser = mock(User.class); + when(noRoleUser.getRole()).thenReturn(null); + + assertThrows(ForbiddenException.class, () -> policy.canView(noRoleUser, log)); + } + + `@Test` + void canView_shouldThrow_whenVetClinicIsNull() { + User vetNoClinic = mock(User.class); + when(vetNoClinic.getRole()).thenReturn(Role.VET); + when(vetNoClinic.getClinic()).thenReturn(null); + + assertThrows(ForbiddenException.class, () -> policy.canView(vetNoClinic, log)); + } + + `@Test` + void canView_shouldThrow_whenRecordOwnerIsNull() { + when(record.getOwner()).thenReturn(null); + + assertThrows(ForbiddenException.class, () -> policy.canView(owner, log)); + }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/test/java/org/example/vet1177/policy/ActivityLogPolicyTest.java` around lines 111 - 132, Add unit tests in ActivityLogPolicyTest to cover the remaining defensive branches of policy.canView: write one test where the user's role is null (mock user.getRole() to return null) and assertThrows(ForbiddenException.class, () -> policy.canView(user, log)); write one test where the medical record's vet has a null clinic (mock log.getMedicalRecord().getVet() to return a vet whose getClinic() returns null) and assertThrows for policy.canView(vetUser, log); and write two tests for owner-null scenarios: one where record.getOwner() returns null and one where record.getOwner().getId() returns null, both asserting that policy.canView(admin, log) throws ForbiddenException; use the existing mocks/variables (policy, log, admin, etc.) and Mockito when(...) chains to set these return values.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/test/java/org/example/vet1177/policy/ActivityLogPolicyTest.java`:
- Around line 111-132: Add unit tests in ActivityLogPolicyTest to cover the
remaining defensive branches of policy.canView: write one test where the user's
role is null (mock user.getRole() to return null) and
assertThrows(ForbiddenException.class, () -> policy.canView(user, log)); write
one test where the medical record's vet has a null clinic (mock
log.getMedicalRecord().getVet() to return a vet whose getClinic() returns null)
and assertThrows for policy.canView(vetUser, log); and write two tests for
owner-null scenarios: one where record.getOwner() returns null and one where
record.getOwner().getId() returns null, both asserting that
policy.canView(admin, log) throws ForbiddenException; use the existing
mocks/variables (policy, log, admin, etc.) and Mockito when(...) chains to set
these return values.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: c55f5329-2e69-4df6-ae80-cd0f66de8a37
📒 Files selected for processing (1)
src/test/java/org/example/vet1177/policy/ActivityLogPolicyTest.java
annikaholmqvist94
left a comment
There was a problem hiding this comment.
Bra jobb då har vi med alla cases:)
…and owner
Summary by CodeRabbit