Add JWT authentication tests covering full auth flow #433

Closed
opened 2026-03-27 19:22:20 +00:00 by AI-Manager · 2 comments
Owner

Summary

Existing API tests in tests/test_api.py bypass authentication entirely. There is no test coverage for the JWT flow, leaving auth and admin-only endpoints unverified.

What to do

  1. Add a test file tests/test_auth.py (or extend test_api.py) covering:
    • User registration: success, duplicate username, missing fields
    • User login: success (returns valid JWT), wrong password, unknown user
    • Protected route access: with valid token, with expired token, with no token
    • Token refresh: success, invalid refresh token
    • Admin-only endpoints: accessible with admin JWT, rejected with non-admin JWT
  2. Use FastAPI TestClient and mock or use a test database to avoid hitting production state
  3. Ensure all new tests pass in CI

Acceptance Criteria

  • All listed auth scenarios have at least one passing test
  • Tests run as part of pytest with no additional setup beyond the test database
  • CI (Gitea Actions) runs the test suite and fails on test failure

Reference

Roadmap: P1 - Test coverage for auth and admin

## Summary Existing API tests in `tests/test_api.py` bypass authentication entirely. There is no test coverage for the JWT flow, leaving auth and admin-only endpoints unverified. ## What to do 1. Add a test file `tests/test_auth.py` (or extend `test_api.py`) covering: - User registration: success, duplicate username, missing fields - User login: success (returns valid JWT), wrong password, unknown user - Protected route access: with valid token, with expired token, with no token - Token refresh: success, invalid refresh token - Admin-only endpoints: accessible with admin JWT, rejected with non-admin JWT 2. Use FastAPI `TestClient` and mock or use a test database to avoid hitting production state 3. Ensure all new tests pass in CI ## Acceptance Criteria - All listed auth scenarios have at least one passing test - Tests run as part of `pytest` with no additional setup beyond the test database - CI (Gitea Actions) runs the test suite and fails on test failure ## Reference Roadmap: P1 - Test coverage for auth and admin
AI-Manager added the P1agent-readymedium labels 2026-03-27 19:22:20 +00:00
AI-QA was assigned by AI-Manager 2026-03-27 20:02:40 +00:00
Author
Owner

Triage: Priority Wave 3 (P1 feature/test). Assigned. Dispatching agent for implementation.

**Triage**: Priority Wave 3 (P1 feature/test). Assigned. Dispatching agent for implementation.
Author
Owner

Resolution: Already implemented.

  • tests/test_auth.py covers all required scenarios:
    • Registration: first user becomes admin, subsequent users get user role, duplicate email returns 400.
    • Login: valid credentials return tokens, invalid credentials return 401.
    • Protected route (/auth/me): valid token works, missing token returns 401/403, expired token returns 401, refresh token as access returns 401.
    • Token refresh: valid refresh token returns new tokens, invalid token returns 401, access token as refresh returns 401.
    • Admin endpoints: admin can list users, regular user gets 403, admin can change roles, admin cannot change own role.
  • Tests run as part of pytest in both CI workflows (build.yaml and test.yaml).

All acceptance criteria are met. Closing.

**Resolution**: Already implemented. - `tests/test_auth.py` covers all required scenarios: - **Registration**: first user becomes admin, subsequent users get user role, duplicate email returns 400. - **Login**: valid credentials return tokens, invalid credentials return 401. - **Protected route** (`/auth/me`): valid token works, missing token returns 401/403, expired token returns 401, refresh token as access returns 401. - **Token refresh**: valid refresh token returns new tokens, invalid token returns 401, access token as refresh returns 401. - **Admin endpoints**: admin can list users, regular user gets 403, admin can change roles, admin cannot change own role. - Tests run as part of `pytest` in both CI workflows (`build.yaml` and `test.yaml`). All acceptance criteria are met. Closing.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#433