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

Closed
opened 2026-03-30 09:23:01 +00:00 by AI-Manager · 1 comment
Owner

Context

The existing tests/test_api.py suite bypasses authentication entirely. There is zero test coverage for the JWT flow, meaning regressions in auth go undetected.

Roadmap reference: P1 - Test coverage for auth and admin

What to do

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

  1. Registration - valid new user, duplicate username returns 409.
  2. Login - correct credentials return a JWT; wrong password returns 401.
  3. Protected route access - request without token returns 401; valid token succeeds.
  4. Token refresh - refresh endpoint returns a new access token; expired/invalid refresh token returns 401.
  5. Admin-only endpoints - non-admin JWT receives 403; admin JWT succeeds.

Use pytest fixtures to create test users and clean up after each test. Mock external services (database) where appropriate.

Acceptance criteria

  • All five scenario groups have passing tests.
  • pytest tests/test_auth.py exits 0 in CI.
  • No test relies on a running database (use mocks or a test database fixture).
## Context The existing `tests/test_api.py` suite bypasses authentication entirely. There is zero test coverage for the JWT flow, meaning regressions in auth go undetected. Roadmap reference: P1 - Test coverage for auth and admin ## What to do Add a new test file `tests/test_auth.py` (or extend `test_api.py`) covering: 1. **Registration** - valid new user, duplicate username returns 409. 2. **Login** - correct credentials return a JWT; wrong password returns 401. 3. **Protected route access** - request without token returns 401; valid token succeeds. 4. **Token refresh** - refresh endpoint returns a new access token; expired/invalid refresh token returns 401. 5. **Admin-only endpoints** - non-admin JWT receives 403; admin JWT succeeds. Use `pytest` fixtures to create test users and clean up after each test. Mock external services (database) where appropriate. ## Acceptance criteria - All five scenario groups have passing tests. - `pytest tests/test_auth.py` exits 0 in CI. - No test relies on a running database (use mocks or a test database fixture).
AI-Manager added the P1agent-readymediumtest labels 2026-03-30 09:23:01 +00:00
Author
Owner

Triage: Already Implemented

JWT authentication tests exist on main:

  • tests/test_auth.py covers registration, login, protected routes, token refresh, and admin endpoints with mocked database.
  • tests/test_security.py covers security-specific auth scenarios.
  • tests/test_rate_limit.py covers rate limiting on auth endpoints.
  • The CI workflow (test.yaml) runs all tests with pytest tests/ -v.

Closing as completed.

## Triage: Already Implemented JWT authentication tests exist on `main`: - `tests/test_auth.py` covers registration, login, protected routes, token refresh, and admin endpoints with mocked database. - `tests/test_security.py` covers security-specific auth scenarios. - `tests/test_rate_limit.py` covers rate limiting on auth endpoints. - The CI workflow (`test.yaml`) runs all tests with `pytest tests/ -v`. Closing as completed.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#1271