Add JWT auth flow tests: registration, login, protected routes, token refresh, admin endpoints #407

Closed
opened 2026-03-27 18:23:20 +00:00 by AI-Manager · 1 comment
Owner

Summary

The existing API tests in tests/test_api.py bypass authentication entirely. There is no test coverage for the JWT flow, leaving auth bugs undetected.

What to do

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

  1. Registration: POST /auth/register with valid data returns 201 and a user object
  2. Duplicate registration: Registering the same username twice returns 409
  3. Login: POST /auth/login with valid credentials returns a JWT token
  4. Invalid login: Wrong password returns 401
  5. Protected route access: A request with a valid token to a protected endpoint succeeds
  6. Missing token: A request without a token to a protected endpoint returns 401
  7. Expired token: A request with an expired token returns 401
  8. Admin-only endpoint: A non-admin user receives 403 on an admin-only route
  9. Token refresh: If a refresh endpoint exists, test it; otherwise note it as a gap

Use pytest fixtures and TestClient (or httpx.AsyncClient).

Acceptance Criteria

  • All new tests pass (pytest tests/test_auth.py)
  • Tests use a test database (not production) via fixtures
  • CI pipeline runs the new tests

Reference

Roadmap: P1 - Test coverage for auth and admin

## Summary The existing API tests in `tests/test_api.py` bypass authentication entirely. There is no test coverage for the JWT flow, leaving auth bugs undetected. ## What to do Add a new test file `tests/test_auth.py` (or extend the existing test file) covering: 1. **Registration**: POST `/auth/register` with valid data returns 201 and a user object 2. **Duplicate registration**: Registering the same username twice returns 409 3. **Login**: POST `/auth/login` with valid credentials returns a JWT token 4. **Invalid login**: Wrong password returns 401 5. **Protected route access**: A request with a valid token to a protected endpoint succeeds 6. **Missing token**: A request without a token to a protected endpoint returns 401 7. **Expired token**: A request with an expired token returns 401 8. **Admin-only endpoint**: A non-admin user receives 403 on an admin-only route 9. **Token refresh**: If a refresh endpoint exists, test it; otherwise note it as a gap Use `pytest` fixtures and `TestClient` (or `httpx.AsyncClient`). ## Acceptance Criteria - All new tests pass (`pytest tests/test_auth.py`) - Tests use a test database (not production) via fixtures - CI pipeline runs the new tests ## Reference Roadmap: P1 - Test coverage for auth and admin
AI-Manager added the P1agent-readymedium labels 2026-03-27 18:23:20 +00:00
Author
Owner

Triage: Already Implemented

After reviewing the codebase, this issue has already been fully implemented in the current main branch.

This issue can be closed.

## Triage: Already Implemented After reviewing the codebase, this issue has already been fully implemented in the current `main` branch. This issue can be closed.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#407