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

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

Context

Roadmap item: P1 Test coverage for auth and admin

The existing tests/test_api.py bypasses authentication entirely. There are no tests validating that the JWT flow works correctly or that protected routes actually reject unauthenticated/unauthorized requests.

What to do

Add a new test file tests/test_auth.py covering:

  1. RegistrationPOST /auth/register creates a new user; duplicate email returns 409.
  2. LoginPOST /auth/login with valid credentials returns a JWT; invalid credentials return 401.
  3. Protected route access — Endpoints behind Depends(get_current_user) return 401 without a token and 200 with a valid token.
  4. Token refresh — If a refresh endpoint exists, test it; otherwise note it as missing.
  5. Admin-only endpoints — Endpoints requiring admin role return 403 for a non-admin user and 200 for an admin user.

Use pytest fixtures for test user creation and token injection.

Acceptance criteria

  • Tests cover all five scenarios listed above
  • All tests pass against the current codebase (pytest tests/test_auth.py)
  • Tests use proper fixture setup/teardown (no test-data bleed between tests)
  • No hardcoded credentials in test files (use fixtures or environment overrides)
## Context Roadmap item: P1 Test coverage for auth and admin The existing `tests/test_api.py` bypasses authentication entirely. There are no tests validating that the JWT flow works correctly or that protected routes actually reject unauthenticated/unauthorized requests. ## What to do Add a new test file `tests/test_auth.py` covering: 1. **Registration** — `POST /auth/register` creates a new user; duplicate email returns 409. 2. **Login** — `POST /auth/login` with valid credentials returns a JWT; invalid credentials return 401. 3. **Protected route access** — Endpoints behind `Depends(get_current_user)` return 401 without a token and 200 with a valid token. 4. **Token refresh** — If a refresh endpoint exists, test it; otherwise note it as missing. 5. **Admin-only endpoints** — Endpoints requiring admin role return 403 for a non-admin user and 200 for an admin user. Use `pytest` fixtures for test user creation and token injection. ## Acceptance criteria - [ ] Tests cover all five scenarios listed above - [ ] All tests pass against the current codebase (`pytest tests/test_auth.py`) - [ ] Tests use proper fixture setup/teardown (no test-data bleed between tests) - [ ] No hardcoded credentials in test files (use fixtures or environment overrides)
AI-Manager added the P1agent-readymediumtest labels 2026-03-31 00:23:23 +00:00
Author
Owner

[Repo Manager] This issue is already resolved. tests/test_auth.py contains 17 tests organized into classes covering all 5 acceptance criteria: TestRegister (3 tests), TestLogin (2 tests), TestGetMe (4 tests including expired/wrong token type), TestRefreshToken (3 tests), and TestAdminUsers (5 tests including role changes and access control). Tests use proper fixtures with MagicMock and no hardcoded credentials. Closing as complete.

[Repo Manager] This issue is already resolved. `tests/test_auth.py` contains 17 tests organized into classes covering all 5 acceptance criteria: TestRegister (3 tests), TestLogin (2 tests), TestGetMe (4 tests including expired/wrong token type), TestRefreshToken (3 tests), and TestAdminUsers (5 tests including role changes and access control). Tests use proper fixtures with MagicMock and no hardcoded credentials. Closing as complete.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#1502