Skip to content

Feat/owner create case och attachemnts#234

Merged
annikaholmqvist94 merged 4 commits into
mainfrom
feat/owner-create-case
Apr 23, 2026
Merged

Feat/owner create case och attachemnts#234
annikaholmqvist94 merged 4 commits into
mainfrom
feat/owner-create-case

Conversation

@annikaholmqvist94

@annikaholmqvist94 annikaholmqvist94 commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

Öppnar upp AttachmentPolicy.canUpload så att VET alltid kan bifoga filer, oavsett kliniktillhörighet — t.ex. i remiss- och konsultflöden där remitterande
veterinär behöver komplettera ett ärende på en annan klinik. Samtidigt spärras VET på CLOSED-ärenden (samma regel som för OWNER) så att stängda journaler
förblir oförändrade.

Beteende

┌────────────────────┬──────────────┬───────────────┐
│ Roll │ Öppet ärende │ Stängt ärende │
├────────────────────┼──────────────┼───────────────┤
│ ADMIN │ ✅ │ ✅ │
├────────────────────┼──────────────┼───────────────┤
│ VET (samma klinik) │ ✅ │ ❌ 403 │ ──
├────────────────────┼──────────────┼───────────────┤
│ VET (annan klinik) │ ✅ (nytt) │ ❌ 403 │
├────────────────────┼──────────────┼───────────────┤
│ VET (ingen klinik) │ ✅ (nytt) │ ❌ 403 │
├────────────────────┼──────────────┼───────────────┤
│ OWNER (eget) │ ✅ │ ❌ 403 │
├────────────────────┼──────────────┼───────────────┤
│ OWNER (annans) │ ❌ 403 │ ❌ 403 │
└────────────────────┴──────────────┴───────────────┘

Ändringar

AttachmentPolicy.canUpload

  • VET: klinikkontrollen borttagen
  • VET: ny CLOSED-spärr ("Bilagor kan inte laddas upp på stängda ärenden")
  • OWNER, ADMIN: oförändrade
  • Kommentar ovan metoden motiverar remiss-/konsultflödet

Oförändrat

  • canDelete — VET kräver fortsatt samma klinik + egen uppladdning för att radera
  • SecurityConfig — POST /api/attachments/** faller redan igenom till authenticated()

Tester

AttachmentPolicyTest

  • canUpload_vetOtherClinic_shouldThrowForbidden → canUpload_vetOtherClinic_shouldNotThrow
  • canUpload_vetSameClinicOnClosedRecord_shouldNotThrow → canUpload_vetOnClosedRecord_shouldThrowForbidden
  • Ny: canUpload_vetWithoutClinic_shouldNotThrow
  • Ny: canUpload_adminOnClosedRecord_shouldNotThrow (tydliggör skillnaden mot VET)

Test plan

  • ./mvnw test — 500 gröna
  • VET på annan klinik laddar upp på öppet ärende → 201
  • VET på stängt ärende → 403
  • ADMIN på stängt ärende → 201
  • OWNER- och canDelete-flöden oförändrade

Summary by CodeRabbit

  • New Features

    • Veterinarians can now upload attachments to medical records across clinics when records are open.
    • Record owners can now update their own open medical records.
  • Bug Fixes

    • Enhanced enforcement of closed record restrictions to prevent unauthorized modifications to finalized medical records.

@coderabbitai

coderabbitai Bot commented Apr 23, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

Authorization logic is refactored to allow VET users to upload attachments across clinics when records are open, and OWNER users to update their own records. Security configuration URL matchers are narrowed to delegate OWNER updates to policy-level authorization checks rather than URL-level filters. Test coverage is expanded with a new comprehensive attachment policy test suite and updated record update tests.

Changes

Cohort / File(s) Summary
Authorization Policy Changes
src/main/java/org/example/vet1177/policy/AttachmentPolicy.java, src/main/java/org/example/vet1177/policy/MedicalRecordPolicy.java
AttachmentPolicy.canUpload shifts VET clinic-ownership restriction to record-status restriction (allows upload on open records across clinics). MedicalRecordPolicy.canUpdate now permits OWNER role to update own records by comparing user and record owner IDs rather than blocking all OWNER updates outright.
Security Configuration
src/main/java/org/example/vet1177/security/SecurityConfig.java
Removes catch-all PUT /api/medical-records/* authorization requirement for VET/ADMIN, replacing with narrower URL matchers for /close, /assign-vet, and /status endpoints. Delegates PUT /api/medical-records/{id} to policy-level checks via MedicalRecordPolicy.canUpdate.
Test Coverage
src/test/java/org/example/vet1177/policy/AttachmentPolicyTest.java, src/test/java/org/example/vet1177/policy/MedicalRecordPolicyTest.java
New AttachmentPolicyTest validates upload/delete permissions across roles, record status (OPEN/CLOSED), ownership, and clinic scope. Updated MedicalRecordPolicyTest asserts OWNER can update own open records without exception, while other-owned record updates trigger ForbiddenException before status checks.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related issues

Possibly related PRs

  • Fix/role based endpoint rules #216: Modifies SecurityConfig URL matchers for medical-record PUT endpoints and adjusts MedicalRecordPolicy owner-update behavior in parallel with this PR's narrowing of URL-level authorization.
  • feat add AttachmentPolicy #106: Introduces AttachmentPolicy class including the canUpload method whose role/status authorization logic is modified here.
  • 42 feat attachmentservicejava #112: Directly modifies AttachmentPolicy.canUpload method to add file size validation, overlapping with this PR's authorization rule changes.

Suggested labels

säkerhet, test

Suggested reviewers

  • johanbriger
  • lindaeskilsson
  • TatjanaTrajkovic

Poem

🐰 A hop through the code, where policies dance,
VET's clinic walls crumble at last!
OWNER now governs their own precious care,
While tests guard the door with detail and care. 🔐

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Title check ⚠️ Warning The title 'Feat/owner create case och attachemnts' is vague and misleading; it references 'owner create case' but the PR primarily changes VET upload permissions on medical records across clinics and OWNER record updates, not case creation. Change title to reflect main changes, e.g., 'Allow VET to upload attachments across clinics and enable OWNER record updates' or similar that accurately describes the authorization logic changes.
Docstring Coverage ⚠️ Warning Docstring coverage is 44.83% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/owner-create-case

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/test/java/org/example/vet1177/policy/AttachmentPolicyTest.java (1)

177-188: Nit: inconsistent exception handling — prefer throws Exception like the rest of the class.

Every other test in this file declares throws Exception and calls setPrivateField directly. Wrapping it here in a try/catch that rethrows as RuntimeException is just noise.

♻️ Suggested cleanup
-    `@Test`
-    void canDelete_vetSameClinicOthersUpload_shouldThrowForbidden() {
-        User otherVet = new User("Dr. Annan Klinik", "ak@vet.se", "hash", Role.VET, clinic);
-        try {
-            setPrivateField(otherVet, "id", UUID.randomUUID());
-        } catch (Exception e) {
-            throw new RuntimeException(e);
-        }
+    `@Test`
+    void canDelete_vetSameClinicOthersUpload_shouldThrowForbidden() throws Exception {
+        User otherVet = new User("Dr. Annan Klinik", "ak@vet.se", "hash", Role.VET, clinic);
+        setPrivateField(otherVet, "id", UUID.randomUUID());

         assertThatThrownBy(() -> policy.canDelete(otherVet, attachmentUploadedByVet))
                 .isInstanceOf(ForbiddenException.class);
     }
🤖 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/AttachmentPolicyTest.java` around
lines 177 - 188, The test
AttachmentPolicyTest::canDelete_vetSameClinicOthersUpload_shouldThrowForbidden
currently wraps setPrivateField in a try/catch and rethrows RuntimeException;
change the method signature to declare throws Exception (like other tests) and
call setPrivateField(otherVet, "id", UUID.randomUUID()) directly, removing the
try/catch, so the test compiles and follows the same exception-handling style
used across the class when preparing otherVet and attachmentUploadedByVet before
invoking policy.canDelete.
🤖 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/AttachmentPolicyTest.java`:
- Around line 177-188: The test
AttachmentPolicyTest::canDelete_vetSameClinicOthersUpload_shouldThrowForbidden
currently wraps setPrivateField in a try/catch and rethrows RuntimeException;
change the method signature to declare throws Exception (like other tests) and
call setPrivateField(otherVet, "id", UUID.randomUUID()) directly, removing the
try/catch, so the test compiles and follows the same exception-handling style
used across the class when preparing otherVet and attachmentUploadedByVet before
invoking policy.canDelete.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: dc5bb558-6f13-4c1c-b24f-c3fcb07e6fc0

📥 Commits

Reviewing files that changed from the base of the PR and between 0f8a5cf and 499daec.

📒 Files selected for processing (5)
  • src/main/java/org/example/vet1177/policy/AttachmentPolicy.java
  • src/main/java/org/example/vet1177/policy/MedicalRecordPolicy.java
  • src/main/java/org/example/vet1177/security/SecurityConfig.java
  • src/test/java/org/example/vet1177/policy/AttachmentPolicyTest.java
  • src/test/java/org/example/vet1177/policy/MedicalRecordPolicyTest.java

@johanbriger

Copy link
Copy Markdown
Contributor

Ser bra ut, samma logik som jag har tänkt kring detta.

@annikaholmqvist94
annikaholmqvist94 merged commit 4d12e0a into main Apr 23, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants