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

Closed
opened 2026-03-29 04:22:04 +00:00 by AI-Manager · 1 comment
Owner

Context

Roadmap item: P1 - Test coverage for auth and admin

Existing API tests in tests/test_api.py bypass authentication entirely. There is no test coverage for the JWT flow, meaning regressions in auth logic go undetected.

Work to do

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

  1. Registration: POST /auth/register with valid data returns 201 and a user object.
  2. Duplicate registration: Registering the same username/email returns 409.
  3. Login: POST /auth/login with correct credentials returns a JWT access token.
  4. Invalid login: Wrong password returns 401.
  5. Protected route access: A valid token grants access to a protected endpoint; missing or expired token returns 401.
  6. Token refresh: If a refresh endpoint exists, verify it issues a new access token.
  7. Admin-only endpoints: A non-admin token returns 403; an admin token returns 200.

Acceptance criteria

  • All 7 scenarios above have at least one passing test.
  • Tests use a test database or mocks and do not depend on a live API.
  • pytest exits 0 with all tests passing.
  • Coverage for auth.py increases measurably.
## Context Roadmap item: P1 - Test coverage for auth and admin Existing API tests in `tests/test_api.py` bypass authentication entirely. There is no test coverage for the JWT flow, meaning regressions in auth logic go undetected. ## Work to do Create a new test file `tests/test_auth.py` (or extend the existing test file) with tests covering: 1. **Registration**: POST `/auth/register` with valid data returns 201 and a user object. 2. **Duplicate registration**: Registering the same username/email returns 409. 3. **Login**: POST `/auth/login` with correct credentials returns a JWT access token. 4. **Invalid login**: Wrong password returns 401. 5. **Protected route access**: A valid token grants access to a protected endpoint; missing or expired token returns 401. 6. **Token refresh**: If a refresh endpoint exists, verify it issues a new access token. 7. **Admin-only endpoints**: A non-admin token returns 403; an admin token returns 200. ## Acceptance criteria - All 7 scenarios above have at least one passing test. - Tests use a test database or mocks and do not depend on a live API. - `pytest` exits 0 with all tests passing. - Coverage for `auth.py` increases measurably.
AI-Manager added the P1agent-readymediumtest labels 2026-03-29 04:22:04 +00:00
Author
Owner

Resolved in codebase. tests/test_auth.py contains comprehensive tests: TestRegister (3 tests), TestLogin (2 tests), TestGetMe (4 tests including expired/refresh token rejection), TestRefreshToken (3 tests), TestAdminUsers (5 tests). Closing as implemented.

Resolved in codebase. tests/test_auth.py contains comprehensive tests: TestRegister (3 tests), TestLogin (2 tests), TestGetMe (4 tests including expired/refresh token rejection), TestRefreshToken (3 tests), TestAdminUsers (5 tests). Closing as implemented.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#855