fix(session): resolve GET session 404 issue and add comprehensive lifecycle tests
MR Description:
📝 Description / Overview
This MR resolves the issue where the GET /api/v1/sessions/{session_id} endpoint returned a 404 Session not found error immediately after a successful session creation via POST /api/v1/sessions/start.
It also fixes identical transaction handling bugs in session updates, preventing manual progress updates (PATCH), session resumes, and session completions from failing to persist.
🔍 Root Cause & Fix Details
-
Root Cause: In
app/crud/crud_session.py, bothcreate_sessionandupdate_sessionrelied onawait db.flush(). This flushed queries into SQLite/Postgres inside the active transaction boundary, but never committed them. Because FastAPI starts a newAsyncSessiondatabase connection per request viaget_db, any subsequentGETrequest ran on a separate transaction which could not see the uncommitted data. -
Resolution: Replaced
await db.flush()withawait db.commit()followed byawait db.refresh(session). This ensures session data is permanently persisted to the database and reloads all server-generated values (such asid,started_at, andlast_active_at) before serializing the API response.
🛠 ️ Changes Introduced
Backend Logic
-
[MODIFY]
app/crud/crud_session.py:- Updated
create_sessionto correctly commit and refresh the session object. - Updated
update_sessionto commit and refresh the session object on patch updates.
- Updated
Test Suite
-
[NEW]
tests/test_sessions.py: Added 10 automated integration tests covering the complete session lifecycle:-
test_start_session_creates_and_returns_session: Confirms session creation with accurate metadata. -
test_get_session_returns_created_session: Verifies the GET endpoint retrieves the session (regression test). -
test_get_session_not_found_for_random_uuid: Verifies 404 response on fake IDs. -
test_get_session_forbidden_for_another_user: Validates cross-user security access (403). -
test_update_session_progress: Confirms manual step/percentage updates viaPATCH. -
test_updated_session_persists_on_get: Assures patch updates persist on subsequent fetches. -
test_resume_session_returns_session/test_resume_completed_session_fails: Assures resume routing and constraints work. -
test_complete_session_without_answers_fails: Ensures validation checks for unanswered required questions. -
test_multiple_sessions_per_user: Validates that a user can create/manage multiple independent sessions.
-
✅ Verification & Test Results
Automated Integration Tests
All integration tests pass successfully in the local virtual environment:
backend/venv/bin/pytest tests/test_sessions.py tests/test_auth_phase1.py tests/test_seeds.py -v
Results:
tests/test_sessions.py::test_start_session_creates_and_returns_session PASSED
tests/test_sessions.py::test_get_session_returns_created_session PASSED
tests/test_sessions.py::test_get_session_not_found_for_random_uuid PASSED
tests/test_sessions.py::test_get_session_forbidden_for_another_user PASSED
tests/test_sessions.py::test_update_session_progress PASSED
tests/test_sessions.py::test_updated_session_persists_on_get PASSED
tests/test_sessions.py::test_resume_session_returns_session PASSED
tests/test_sessions.py::test_resume_completed_session_fails PASSED
tests/test_sessions.py::test_complete_session_without_answers_fails PASSED
tests/test_sessions.py::test_multiple_sessions_per_user PASSED
======================= 16 passed, 3 warnings in 14.22s ========================





