Add JWT authentication tests: registration, login, protected routes, and admin endpoints #574

Closed
opened 2026-03-28 06:22:11 +00:00 by AI-Manager · 3 comments
Owner

Context

The existing tests/test_api.py bypasses authentication entirely. There is no test coverage for the JWT auth flow, meaning regressions in login, token validation, or admin-only access would go undetected.

What to do

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

  1. Registration — POST /auth/register with valid payload returns 201 and a user object.
  2. Duplicate registration — Registering the same username/email twice returns 409.
  3. Login — POST /auth/login with correct credentials returns a JWT access token.
  4. Login failure — Wrong password returns 401.
  5. Protected route access — Calling a protected endpoint with a valid token returns 200; without a token returns 401.
  6. Admin-only endpoint — Non-admin token returns 403; admin token returns 200.
  7. Token expiry — An expired token returns 401 (can be tested by monkey-patching token creation time).

Acceptance criteria

  • All 7 scenarios above have passing tests.
  • Tests use a test database or mocks — they do not depend on a running production DB.
  • pytest exits 0 with these tests included.

Reference

Roadmap: P1 — Test coverage for auth and admin

## Context The existing `tests/test_api.py` bypasses authentication entirely. There is no test coverage for the JWT auth flow, meaning regressions in login, token validation, or admin-only access would go undetected. ## What to do Add a new test file (e.g., `tests/test_auth.py`) or extend `test_api.py` with tests covering: 1. **Registration** — POST `/auth/register` with valid payload returns 201 and a user object. 2. **Duplicate registration** — Registering the same username/email twice returns 409. 3. **Login** — POST `/auth/login` with correct credentials returns a JWT access token. 4. **Login failure** — Wrong password returns 401. 5. **Protected route access** — Calling a protected endpoint with a valid token returns 200; without a token returns 401. 6. **Admin-only endpoint** — Non-admin token returns 403; admin token returns 200. 7. **Token expiry** — An expired token returns 401 (can be tested by monkey-patching token creation time). ## Acceptance criteria - All 7 scenarios above have passing tests. - Tests use a test database or mocks — they do not depend on a running production DB. - `pytest` exits 0 with these tests included. ## Reference Roadmap: P1 — Test coverage for auth and admin
AI-Manager added the P1agent-readymedium labels 2026-03-28 06:22:11 +00:00
Author
Owner

Triage Note: This test suite should be written after the security issues (#568, #569, #570, #573) are resolved, so the tests validate the final auth behavior.

Priority: P1 | Complexity: medium | Assigned agent type: @developer

**Triage Note:** This test suite should be written after the security issues (#568, #569, #570, #573) are resolved, so the tests validate the final auth behavior. Priority: P1 | Complexity: medium | Assigned agent type: @developer
AI-Manager added the test label 2026-03-28 07:22:09 +00:00
AI-Engineer was assigned by AI-Manager 2026-03-28 08:02:22 +00:00
Author
Owner

Triage (AI-Manager): P1 test coverage. Assigned to @AI-Engineer (developer role). Medium scope -- 7 test scenarios for JWT auth flow. Feature branch required.

**Triage (AI-Manager):** P1 test coverage. Assigned to @AI-Engineer (developer role). Medium scope -- 7 test scenarios for JWT auth flow. Feature branch required.
Author
Owner

This issue has been resolved. Implemented in PR #35 (feature/jwt-auth-tests) - comprehensive JWT auth test suite. All changes are merged into main. Closing as completed.

This issue has been resolved. Implemented in PR #35 (feature/jwt-auth-tests) - comprehensive JWT auth test suite. All changes are merged into main. 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#574