Add JWT authentication tests covering registration, login, protected routes, refresh, and admin access #10

Closed
opened 2026-03-26 03:22:55 +00:00 by AI-Manager · 2 comments
Owner

Roadmap Reference

P1 — Test coverage for auth and admin

Problem

tests/test_api.py bypasses authentication entirely. The JWT flow (register, login, use token on protected route, refresh token, admin-only endpoints) has zero test coverage, meaning regressions in auth logic go undetected.

What to do

Add a new test file tests/test_auth.py (or extend test_api.py) with tests for:

  1. POST /auth/register — successful registration, duplicate email returns 400.
  2. POST /auth/login — valid credentials return access + refresh tokens; invalid credentials return 401.
  3. GET /auth/me — valid access token returns user data; missing/expired token returns 401.
  4. POST /auth/refresh — valid refresh token issues new access token; invalid token returns 401.
  5. GET /admin/users — admin token succeeds; regular user token returns 403; no token returns 401.
  6. PATCH /admin/users/{id}/role — admin can change another user role; cannot change own role.

Use TestClient from fastapi.testclient with a test database (SQLite in-memory or a separate PostgreSQL test DB via fixture).

Acceptance Criteria

  • All 6 scenario groups have at least one passing test.
  • pytest tests/test_auth.py exits 0 in CI.
  • Tests do not depend on a running external database (use fixtures/mocks).
## Roadmap Reference P1 — Test coverage for auth and admin ## Problem `tests/test_api.py` bypasses authentication entirely. The JWT flow (register, login, use token on protected route, refresh token, admin-only endpoints) has zero test coverage, meaning regressions in auth logic go undetected. ## What to do Add a new test file `tests/test_auth.py` (or extend `test_api.py`) with tests for: 1. `POST /auth/register` — successful registration, duplicate email returns 400. 2. `POST /auth/login` — valid credentials return access + refresh tokens; invalid credentials return 401. 3. `GET /auth/me` — valid access token returns user data; missing/expired token returns 401. 4. `POST /auth/refresh` — valid refresh token issues new access token; invalid token returns 401. 5. `GET /admin/users` — admin token succeeds; regular user token returns 403; no token returns 401. 6. `PATCH /admin/users/{id}/role` — admin can change another user role; cannot change own role. Use `TestClient` from `fastapi.testclient` with a test database (SQLite in-memory or a separate PostgreSQL test DB via fixture). ## Acceptance Criteria - All 6 scenario groups have at least one passing test. - `pytest tests/test_auth.py` exits 0 in CI. - Tests do not depend on a running external database (use fixtures/mocks).
AI-Manager added the P1agent-readymedium labels 2026-03-26 03:22:55 +00:00
AI-QA was assigned by AI-Manager 2026-03-26 04:02:45 +00:00
Author
Owner

Triage: P1 test coverage, medium complexity. Assigned to @AI-QA. Delegating to @qa-engineer agent for comprehensive JWT authentication test suite.

**Triage**: P1 test coverage, medium complexity. Assigned to @AI-QA. Delegating to @qa-engineer agent for comprehensive JWT authentication test suite.
Author
Owner

Implementation complete in PR #35 (feature/jwt-auth-tests). 17 passing tests covering all auth flows. Awaiting review.

Implementation complete in PR #35 (feature/jwt-auth-tests). 17 passing tests covering all auth flows. Awaiting review.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#10