|
| 1 | +# Week 4 Checkout Summary — Backend |
| 2 | + |
| 3 | +## Overview (Week 4 — late October) |
| 4 | +1. ### Standardized NOT_FOUND semantics |
| 5 | + - Introduced `ResourceNotFoundException` in the common layer and mapped it in `GlobalExceptionHandler` to HTTP 404 with error code `NOT_FOUND`. |
| 6 | + - Services throw the exception; controllers remain thin and rely on the centralized handler. |
| 7 | + - Added tests: service unit test for missing user and WebMvc test asserting the 404 envelope. |
| 8 | + |
| 9 | +2. ### BAD_REQUEST mapping coverage |
| 10 | + - Added a focused WebMvc test to assert `IllegalArgumentException` is translated to HTTP 400 with `{ "error": "BAD_REQUEST" }`. |
| 11 | + - Keeps error envelope consistent and prevents controller-specific ad hoc responses. |
| 12 | + |
| 13 | +3. ### Endpoint hardening for future admin/public profiles |
| 14 | + - Added a gated user read endpoint `GET /users/{id}` returning `PublicUserDto` (no email or sensitive fields). |
| 15 | + - Endpoint is disabled by default; enable with `sameboat.endpoints.user-read=true`. |
| 16 | + - Access control: only the authenticated user (self) or `ADMIN` may fetch. Tests are present but disabled until the feature is enabled for real environments. |
| 17 | + |
| 18 | +4. ### OpenAPI & Docs |
| 19 | + - Updated `openapi/sameboat.yaml` to include `/api/version` and a reusable `ErrorResponse` schema enumerating current error codes (`UNAUTHENTICATED`, `BAD_CREDENTIALS`, `SESSION_EXPIRED`, `VALIDATION_ERROR`, `BAD_REQUEST`, `RATE_LIMITED`, `NOT_FOUND`, `INTERNAL_ERROR`). |
| 20 | + - `docs/Architecture.md`: added a Week 4 Backend Summary with the above items. |
| 21 | + - README: added Week 4 highlights, Windows `mvn` quoting note, and included `NOT_FOUND` in error catalog table. |
| 22 | + |
| 23 | +5. ### Tests & Quality Gates |
| 24 | + - All tests pass locally (migration Testcontainers profile skipped without Docker). |
| 25 | + - JaCoCo instruction coverage: ~84% total (gate ≥ 70% remains green). |
| 26 | + |
| 27 | +## Key Decisions & Rationale |
| 28 | +- Centralize 404 behavior via a domain exception to reduce controller boilerplate and ensure consistent envelopes. |
| 29 | +- Keep cross-user reads gated and least-privilege by default; only self or `ADMIN` can fetch when feature is enabled. |
| 30 | +- Public profile DTO excludes email to reduce PII exposure; future public profile endpoint can reuse the same DTO or an even slimmer one. |
| 31 | + |
| 32 | +## What Went Well |
| 33 | +- Layering preserved (Controller → Service → Repository). Controllers remain thin and secure. |
| 34 | +- Focused tests captured envelope behavior for both NOT_FOUND and BAD_REQUEST without bloating integration suites. |
| 35 | +- OpenAPI is back in sync, making frontend consumption clearer. |
| 36 | + |
| 37 | +## What I Struggled With |
| 38 | +- Security slices in WebMvc tests required explicit authentication setup; solved with `@WithMockUser` or session cookie when appropriate. |
| 39 | +- Property-gated endpoint meant careful test enable/disable to keep the suite green by default. |
| 40 | + |
| 41 | +## Suggested Next Steps (Backend) |
| 42 | +- When bringing up the admin console: |
| 43 | + - Enable `sameboat.endpoints.user-read=true` in admin/staging environments. |
| 44 | + - Add role-based method security (`@EnableMethodSecurity`) and migrate inline checks to `@PreAuthorize`. |
| 45 | + - Add integration tests for `403` (non-self, non-admin) and `200` for admin fetching other users. |
| 46 | +- Consider adding a dedicated public profile route (e.g., `/profiles/{id}`) with no auth but constrained DTO. |
| 47 | +- Continue ratcheting coverage toward 75% by adding small unit tests (e.g., more `UserService` edge cases, filter behaviors). |
| 48 | + |
| 49 | +## Verification (Local) |
| 50 | +- Run fast tests (Windows cmd): |
| 51 | +```cmd |
| 52 | +mvn "-Dskip.migration.test=false" test |
| 53 | +``` |
| 54 | +- Full verify (tests + coverage gate): |
| 55 | +```cmd |
| 56 | +mvn "-Dskip.migration.test=false" verify |
| 57 | +``` |
| 58 | +- Optional migration schema test (requires Docker/Testcontainers): |
| 59 | +```cmd |
| 60 | +mvn -Pwith-migration-test test |
| 61 | +``` |
| 62 | + |
| 63 | +## Checklist (Backend Focus) |
| 64 | +- [x] ResourceNotFoundException + 404 mapping in GlobalExceptionHandler |
| 65 | +- [x] Service method throws on missing resource and unit test |
| 66 | +- [x] WebMvc test for NOT_FOUND envelope |
| 67 | +- [x] Focused BAD_REQUEST mapping test (`IllegalArgumentException`) |
| 68 | +- [x] OpenAPI sync with `/api/version` and error schema |
| 69 | +- [x] Architecture.md Week 4 summary |
| 70 | +- [x] README updated (Week 4 highlights, Windows mvn note, error catalog includes NOT_FOUND) |
| 71 | +- [x] Coverage gate ≥ 70% (current ~84%) |
| 72 | +- [x] Gated `GET /users/{id}` returning `PublicUserDto`; access: self or ADMIN; disabled by default |
| 73 | + |
0 commit comments