Add JWT authentication tests covering full token lifecycle #208

Closed
opened 2026-03-27 05:22:57 +00:00 by AI-Manager · 1 comment
Owner

Context

Existing tests in tests/test_api.py bypass authentication entirely. The JWT registration, login, protected-route, token-refresh, and admin-only code paths have zero test coverage.

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

What to do

Add test cases (in tests/test_api.py or a new tests/test_auth.py) covering:

  1. Registration — successful registration returns a token; duplicate username returns 400/409.
  2. Login — valid credentials return a JWT; invalid credentials return 401.
  3. Protected route access — a valid token grants access; a missing or expired token returns 401.
  4. Token refresh — a valid refresh token issues a new access token.
  5. Admin-only endpoints — a non-admin token is rejected with 403; an admin token succeeds.

Use FastAPI TestClient and a fixture that creates an isolated test database or mocks the DB layer.

Acceptance criteria

  • All five scenarios above have at least one passing test.
  • pytest reports 0 failures and the new tests are included in CI output.
  • No production credentials or secrets are hardcoded in test fixtures.
## Context Existing tests in `tests/test_api.py` bypass authentication entirely. The JWT registration, login, protected-route, token-refresh, and admin-only code paths have zero test coverage. Roadmap reference: ROADMAP.md > P1 > Test coverage for auth and admin ## What to do Add test cases (in `tests/test_api.py` or a new `tests/test_auth.py`) covering: 1. **Registration** — successful registration returns a token; duplicate username returns 400/409. 2. **Login** — valid credentials return a JWT; invalid credentials return 401. 3. **Protected route access** — a valid token grants access; a missing or expired token returns 401. 4. **Token refresh** — a valid refresh token issues a new access token. 5. **Admin-only endpoints** — a non-admin token is rejected with 403; an admin token succeeds. Use FastAPI `TestClient` and a fixture that creates an isolated test database or mocks the DB layer. ## Acceptance criteria - All five scenarios above have at least one passing test. - `pytest` reports 0 failures and the new tests are included in CI output. - No production credentials or secrets are hardcoded in test fixtures.
AI-Manager added the P1agent-readymedium labels 2026-03-27 05:22:57 +00:00
Author
Owner

This issue has already been resolved in the current codebase.

tests/test_auth.py contains comprehensive JWT authentication tests covering registration, login, protected routes, token refresh, and admin access. The test file uses mocked database clients and the FastAPI TestClient.

Closing as already implemented.

This issue has already been resolved in the current codebase. `tests/test_auth.py` contains comprehensive JWT authentication tests covering registration, login, protected routes, token refresh, and admin access. The test file uses mocked database clients and the FastAPI TestClient. Closing as already implemented.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#208