""" Integration tests for health check endpoint. Tests the /health endpoint with actual FastAPI TestClient. """ import tempfile from pathlib import Path import pytest from fastapi.testclient import TestClient class TestHealthEndpoint: """Integration tests for /health endpoint.""" @pytest.fixture def test_app(self, monkeypatch): """Create test FastAPI app with temporary database.""" # Set up test environment with tempfile.TemporaryDirectory() as tmpdir: db_path = Path(tmpdir) / "test.db" # Set required environment variables monkeypatch.setenv("GONDULF_SECRET_KEY", "a" * 32) monkeypatch.setenv("GONDULF_BASE_URL", "https://auth.example.com") monkeypatch.setenv("GONDULF_DATABASE_URL", f"sqlite:///{db_path}") monkeypatch.setenv("GONDULF_DEBUG", "true") # Import app AFTER setting env vars from gondulf.main import app yield app def test_health_check_success(self, test_app): """Test health check returns 200 when database is healthy.""" with TestClient(test_app) as client: response = client.get("/health") assert response.status_code == 200 data = response.json() assert data["status"] == "healthy" assert data["database"] == "connected" def test_health_check_response_format(self, test_app): """Test health check response has correct format.""" with TestClient(test_app) as client: response = client.get("/health") assert response.status_code == 200 data = response.json() assert "status" in data assert "database" in data def test_health_check_no_auth_required(self, test_app): """Test health check endpoint doesn't require authentication.""" with TestClient(test_app) as client: # Should work without any authentication headers response = client.get("/health") assert response.status_code == 200 def test_health_check_head_method(self, test_app): """Test health check endpoint supports HEAD requests.""" with TestClient(test_app) as client: response = client.head("/health") assert response.status_code == 200 # HEAD requests should not have a response body assert len(response.content) == 0 def test_root_endpoint(self, test_app): """Test root endpoint returns service information.""" client = TestClient(test_app) response = client.get("/") assert response.status_code == 200 data = response.json() assert "service" in data assert "version" in data assert "Gondulf" in data["service"] class TestHealthCheckUnhealthy: """Tests for unhealthy database scenarios.""" def test_health_check_unhealthy_bad_database(self, monkeypatch): """Test health check returns 503 when database inaccessible.""" # Set up with non-existent database path monkeypatch.setenv("GONDULF_SECRET_KEY", "a" * 32) monkeypatch.setenv("GONDULF_BASE_URL", "https://auth.example.com") monkeypatch.setenv( "GONDULF_DATABASE_URL", "sqlite:////nonexistent/path/db.db" ) monkeypatch.setenv("GONDULF_DEBUG", "true") # Import app AFTER setting env vars # This should fail during startup, so we need to handle it try: from gondulf.main import app client = TestClient(app, raise_server_exceptions=False) response = client.get("/health") # If startup succeeds but health check fails assert response.status_code == 503 data = response.json() assert data["status"] == "unhealthy" except Exception: # Startup failure is also acceptable for this test pass