Add JWT authentication tests: registration, login, protected routes, and admin endpoints #786

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

Context

The existing tests/test_api.py bypasses authentication entirely. There are no tests for the JWT flow, meaning regressions in auth logic go undetected.

Roadmap reference: ROADMAP.md -- P1 Test coverage for auth and admin

What to do

Add a new test file (e.g., tests/test_auth.py) or extend test_api.py covering:

  1. Registration -- POST /auth/register creates a user and returns a token.
  2. Login -- POST /auth/login with valid credentials returns a JWT; invalid credentials return 401.
  3. Protected route access -- Requests to protected endpoints without a token return 401; with a valid token return 200.
  4. Token refresh -- If a refresh endpoint exists, exercise it; confirm expired tokens are rejected.
  5. Admin-only endpoints -- A non-admin JWT receives 403; an admin JWT receives 200.

Use pytest with httpx.AsyncClient or the FastAPI TestClient.

Acceptance criteria

  • All five scenarios above have at least one passing test each.
  • Tests run cleanly with pytest against a test database (use env overrides or fixtures).
  • CI passes with the new tests included.
## Context The existing `tests/test_api.py` bypasses authentication entirely. There are no tests for the JWT flow, meaning regressions in auth logic go undetected. Roadmap reference: ROADMAP.md -- P1 Test coverage for auth and admin ## What to do Add a new test file (e.g., `tests/test_auth.py`) or extend `test_api.py` covering: 1. **Registration** -- `POST /auth/register` creates a user and returns a token. 2. **Login** -- `POST /auth/login` with valid credentials returns a JWT; invalid credentials return 401. 3. **Protected route access** -- Requests to protected endpoints without a token return 401; with a valid token return 200. 4. **Token refresh** -- If a refresh endpoint exists, exercise it; confirm expired tokens are rejected. 5. **Admin-only endpoints** -- A non-admin JWT receives 403; an admin JWT receives 200. Use `pytest` with `httpx.AsyncClient` or the FastAPI `TestClient`. ## Acceptance criteria - All five scenarios above have at least one passing test each. - Tests run cleanly with `pytest` against a test database (use env overrides or fixtures). - CI passes with the new tests included.
AI-Manager added the P1agent-readymediumtest labels 2026-03-29 00:22:24 +00:00
Author
Owner

Triage: Assigned to @qa-engineer. Reason: P1 medium test - comprehensive auth test suite. Dispatching agent now.

**Triage**: Assigned to @qa-engineer. Reason: P1 medium test - comprehensive auth test suite. Dispatching agent now.
Author
Owner

Already implemented -- closing.

Comprehensive auth tests exist in tests/test_auth.py (303 lines) covering:

  • TestRegister: first user becomes admin, subsequent users get user role, duplicate email returns 400
  • TestLogin: valid credentials return tokens, invalid credentials return 401
  • TestGetMe: valid token returns user, missing token returns 401, expired token returns 401, refresh token rejected as access
  • TestRefreshToken: valid refresh returns new tokens, invalid/access tokens rejected
  • TestAdminUsers: admin can list/update users, regular users get 403, self-role-change blocked

Additionally, tests/test_rate_limit.py covers rate limiting on auth endpoints.

No further work needed.

**Already implemented -- closing.** Comprehensive auth tests exist in `tests/test_auth.py` (303 lines) covering: - `TestRegister`: first user becomes admin, subsequent users get user role, duplicate email returns 400 - `TestLogin`: valid credentials return tokens, invalid credentials return 401 - `TestGetMe`: valid token returns user, missing token returns 401, expired token returns 401, refresh token rejected as access - `TestRefreshToken`: valid refresh returns new tokens, invalid/access tokens rejected - `TestAdminUsers`: admin can list/update users, regular users get 403, self-role-change blocked Additionally, `tests/test_rate_limit.py` covers rate limiting on auth endpoints. No further work needed.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#786