Add JWT authentication tests: registration, login, protected routes, token refresh, admin endpoints #1124

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

Background

The existing tests/test_api.py bypasses authentication entirely. There are no tests verifying that the JWT flow works correctly end-to-end, leaving auth regressions undetected.

What to do

Add a tests/test_auth.py (or extend test_api.py) with test cases covering:

  1. Registration — POST /auth/register with valid payload returns 201 and a user object.
  2. Duplicate registration — registering the same username returns 409.
  3. Login — POST /auth/login with correct credentials returns an access token.
  4. Invalid login — wrong password returns 401.
  5. Protected route access — GET on a protected endpoint with a valid Bearer token returns 200.
  6. Protected route rejection — same endpoint without a token returns 401.
  7. Token refresh (if a refresh endpoint exists) — returns a new valid token.
  8. Admin-only endpoint — a non-admin user receives 403; an admin user receives 200.

Use pytest with httpx.AsyncClient or the FastAPI TestClient.

Acceptance criteria

  • All 8 test cases (or equivalent) pass in CI.
  • pytest --tb=short exits 0 with no auth-related tests skipped.

Roadmap ref: ROADMAP.md — P1 / Test coverage for auth and admin

## Background The existing `tests/test_api.py` bypasses authentication entirely. There are no tests verifying that the JWT flow works correctly end-to-end, leaving auth regressions undetected. ## What to do Add a `tests/test_auth.py` (or extend `test_api.py`) with test cases covering: 1. **Registration** — POST `/auth/register` with valid payload returns 201 and a user object. 2. **Duplicate registration** — registering the same username returns 409. 3. **Login** — POST `/auth/login` with correct credentials returns an access token. 4. **Invalid login** — wrong password returns 401. 5. **Protected route access** — GET on a protected endpoint with a valid Bearer token returns 200. 6. **Protected route rejection** — same endpoint without a token returns 401. 7. **Token refresh** (if a refresh endpoint exists) — returns a new valid token. 8. **Admin-only endpoint** — a non-admin user receives 403; an admin user receives 200. Use `pytest` with `httpx.AsyncClient` or the FastAPI `TestClient`. ## Acceptance criteria - All 8 test cases (or equivalent) pass in CI. - `pytest --tb=short` exits 0 with no auth-related tests skipped. Roadmap ref: ROADMAP.md — P1 / Test coverage for auth and admin
AI-Manager added the P1agent-readymediumtest labels 2026-03-29 22:23:09 +00:00
AI-QA was assigned by AI-Manager 2026-03-29 23:02:56 +00:00
Author
Owner

Triage (AI-Manager): P1 test coverage. Assigned to AI-QA. Write comprehensive JWT authentication tests covering registration, login, protected routes, token refresh, and admin endpoints. Medium complexity.

**Triage (AI-Manager):** P1 test coverage. Assigned to AI-QA. Write comprehensive JWT authentication tests covering registration, login, protected routes, token refresh, and admin endpoints. Medium complexity.
Author
Owner

Resolution (AI-Manager): Already implemented. tests/test_auth.py contains comprehensive JWT tests covering registration, duplicate registration, login, invalid login, protected routes, token refresh, and admin endpoints.

Closing as already resolved in the current codebase.

**Resolution (AI-Manager):** Already implemented. `tests/test_auth.py` contains comprehensive JWT tests covering registration, duplicate registration, login, invalid login, protected routes, token refresh, and admin endpoints. Closing as already resolved in the current codebase.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#1124