2026-02-03 12:30:13 +09:00
|
|
|
"""
|
|
|
|
|
E2E tests for authentication flow.
|
|
|
|
|
"""
|
2026-02-08 22:21:36 +09:00
|
|
|
import hashlib
|
|
|
|
|
|
2026-02-03 12:30:13 +09:00
|
|
|
import pytest
|
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
|
|
|
|
|
|
2026-02-08 22:21:36 +09:00
|
|
|
def _sha256(password: str) -> str:
|
|
|
|
|
"""SHA-256 hash to match client-side hashing."""
|
|
|
|
|
return hashlib.sha256(password.encode()).hexdigest()
|
|
|
|
|
|
|
|
|
|
|
2026-02-03 12:30:13 +09:00
|
|
|
def test_health_check(client: TestClient):
|
|
|
|
|
"""Test health check endpoint."""
|
|
|
|
|
response = client.get("/health")
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert response.json() == {"status": "healthy"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_login_success(client: TestClient, test_user):
|
|
|
|
|
"""Test successful login."""
|
|
|
|
|
response = client.post(
|
|
|
|
|
"/api/auth/login",
|
2026-02-08 22:21:36 +09:00
|
|
|
json={
|
2026-02-03 12:30:13 +09:00
|
|
|
"username": "testuser",
|
2026-02-08 22:21:36 +09:00
|
|
|
"password": _sha256("testpassword"),
|
2026-02-03 12:30:13 +09:00
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
data = response.json()
|
|
|
|
|
assert "access_token" in data
|
|
|
|
|
assert data["token_type"] == "bearer"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_login_wrong_password(client: TestClient, test_user):
|
|
|
|
|
"""Test login with wrong password."""
|
|
|
|
|
response = client.post(
|
|
|
|
|
"/api/auth/login",
|
2026-02-08 22:21:36 +09:00
|
|
|
json={
|
2026-02-03 12:30:13 +09:00
|
|
|
"username": "testuser",
|
2026-02-08 22:21:36 +09:00
|
|
|
"password": _sha256("wrongpassword"),
|
2026-02-03 12:30:13 +09:00
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
assert response.status_code == 401
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_login_nonexistent_user(client: TestClient):
|
|
|
|
|
"""Test login with nonexistent user."""
|
|
|
|
|
response = client.post(
|
|
|
|
|
"/api/auth/login",
|
2026-02-08 22:21:36 +09:00
|
|
|
json={
|
2026-02-03 12:30:13 +09:00
|
|
|
"username": "nonexistent",
|
2026-02-08 22:21:36 +09:00
|
|
|
"password": _sha256("password"),
|
2026-02-03 12:30:13 +09:00
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
assert response.status_code == 401
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_current_user(client: TestClient, auth_headers):
|
|
|
|
|
"""Test getting current user info."""
|
|
|
|
|
response = client.get("/api/auth/me", headers=auth_headers)
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
data = response.json()
|
|
|
|
|
assert data["username"] == "testuser"
|
|
|
|
|
assert data["email"] == "test@example.com"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_current_user_no_token(client: TestClient):
|
|
|
|
|
"""Test getting current user without token."""
|
|
|
|
|
response = client.get("/api/auth/me")
|
|
|
|
|
assert response.status_code == 401
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_current_user_invalid_token(client: TestClient):
|
|
|
|
|
"""Test getting current user with invalid token."""
|
|
|
|
|
response = client.get(
|
|
|
|
|
"/api/auth/me",
|
|
|
|
|
headers={"Authorization": "Bearer invalid_token"},
|
|
|
|
|
)
|
|
|
|
|
assert response.status_code == 401
|