"""Integration tests for auth API endpoints. Uses FastAPI TestClient to test the full request-response cycle. Requires a running database or test DB configured via DATABASE_URL. """ from fastapi.testclient import TestClient from app.main import app client = TestClient(app) class TestAuthIntegration: def test_health(self): resp = client.get("/health") assert resp.status_code == 200 data = resp.json() assert data["status"] == "healthy" def test_register_validation(self): resp = client.post( "/api/v1/auth/register", json={"email": "bad", "password": "12", "display_name": ""}, ) assert resp.status_code == 422 def test_login_invalid_credentials(self): resp = client.post( "/api/v1/auth/login", json={"email": "nonexistent@test.com", "password": "wrongpass"}, ) assert resp.status_code == 401 assert resp.json()["detail"] == "Invalid credentials" def test_register_and_login_flow(self): import uuid suffix = uuid.uuid4().hex[:8] email = f"test{suffix}@example.com" reg_resp = client.post( "/api/v1/auth/register", json={ "email": email, "password": "StrongPass1", "display_name": "Test User", }, ) assert reg_resp.status_code == 201 tokens = reg_resp.json() assert "access_token" in tokens assert "refresh_token" in tokens login_resp = client.post( "/api/v1/auth/login", json={"email": email, "password": "StrongPass1"}, ) assert login_resp.status_code == 200 assert "access_token" in login_resp.json() def test_refresh_token(self): import uuid suffix = uuid.uuid4().hex[:8] email = f"refresh{suffix}@example.com" reg = client.post( "/api/v1/auth/register", json={"email": email, "password": "Pass1234", "display_name": "T"}, ) refresh_token = reg.json()["refresh_token"] refresh_resp = client.post( "/api/v1/auth/refresh", json={"refresh_token": refresh_token}, ) assert refresh_resp.status_code == 200 new_tokens = refresh_resp.json() assert new_tokens["refresh_token"] != refresh_token