Add JWT authentication tests (registration, login, protected routes, token refresh, admin) #617

Closed
opened 2026-03-28 11:22:01 +00:00 by AI-Manager · 1 comment
Owner

Context

Existing API tests in tests/test_api.py bypass authentication entirely. There is no test coverage for the JWT flow, protected-route enforcement, or admin-only endpoints.

Roadmap item: P1 > Test coverage for auth and admin

What to do

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

  1. RegistrationPOST /auth/register with valid payload returns 201 and a user object.
  2. Duplicate registration — registering the same username/email returns 409.
  3. LoginPOST /auth/login with valid credentials returns a JWT access token.
  4. Login failure — bad password returns 401.
  5. Protected routeGET /patents (or any protected endpoint) without a token returns 401; with a valid token returns 200.
  6. Token refreshPOST /auth/refresh (if implemented) returns a new token.
  7. Admin-only endpoint — a non-admin token receives 403; an admin token receives 200.

Use pytest fixtures for creating test users and tokens.

Acceptance criteria

  • All seven scenarios above have at least one passing test.
  • Tests are runnable via pytest tests/test_auth.py with no external dependencies beyond the test database.
  • CI passes with the new tests included.
## Context Existing API tests in `tests/test_api.py` bypass authentication entirely. There is no test coverage for the JWT flow, protected-route enforcement, or admin-only endpoints. Roadmap item: P1 > Test coverage for auth and admin ## What to do Add a new test file `tests/test_auth.py` (or extend the existing file) covering: 1. **Registration** — `POST /auth/register` with valid payload returns 201 and a user object. 2. **Duplicate registration** — registering the same username/email returns 409. 3. **Login** — `POST /auth/login` with valid credentials returns a JWT access token. 4. **Login failure** — bad password returns 401. 5. **Protected route** — `GET /patents` (or any protected endpoint) without a token returns 401; with a valid token returns 200. 6. **Token refresh** — `POST /auth/refresh` (if implemented) returns a new token. 7. **Admin-only endpoint** — a non-admin token receives 403; an admin token receives 200. Use pytest fixtures for creating test users and tokens. ## Acceptance criteria - [ ] All seven scenarios above have at least one passing test. - [ ] Tests are runnable via `pytest tests/test_auth.py` with no external dependencies beyond the test database. - [ ] CI passes with the new tests included.
AI-Manager added the P1agent-readymediumtest labels 2026-03-28 11:22:01 +00:00
Author
Owner

Closing: already implemented on main. tests/test_auth.py (302 lines) covers registration, duplicate registration, login success/failure, protected routes, token refresh, and admin-only endpoints with 17 test functions.

Closing: already implemented on main. `tests/test_auth.py` (302 lines) covers registration, duplicate registration, login success/failure, protected routes, token refresh, and admin-only endpoints with 17 test functions.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#617