""" Integration tests for LiteLLM health check operations. """ import pytest from unittest.mock import AsyncMock, patch class TestLiteLLMHealth: """Test LiteLLM health check endpoint.""" @pytest.mark.asyncio async def test_health_check_success(self, test_app, mock_config, admin_headers, monkeypatch): """Test successful health check.""" monkeypatch.setenv("LITELLM_URL", "http://mock-litellm:4000") monkeypatch.setenv("LITELLM_HEALTH_API_KEY", "") # Mock successful response mock_response = AsyncMock() mock_response.is_success = True mock_response.status_code = 200 with patch("httpx.AsyncClient") as mock_client: mock_client.return_value.__aenter__.return_value.get.return_value = mock_response response = test_app.get("/api/litellm/health", headers=admin_headers) assert response.status_code == 200 data = response.json() assert data["ok"] is True assert data["status"] == "up" assert "latency_ms" in data assert data["http_status"] == 200 @pytest.mark.asyncio async def test_health_check_auth_required(self, test_app, mock_config, admin_headers, monkeypatch): """Test health check when auth is required but not provided.""" monkeypatch.setenv("LITELLM_URL", "http://mock-litellm:4000") monkeypatch.setenv("LITELLM_HEALTH_API_KEY", "") # Mock 401 response without API key mock_response = AsyncMock() mock_response.is_success = False mock_response.status_code = 401 with patch("httpx.AsyncClient") as mock_client: mock_client.return_value.__aenter__.return_value.get.return_value = mock_response response = test_app.get("/api/litellm/health", headers=admin_headers) assert response.status_code == 200 data = response.json() assert data["ok"] is True assert data["status"] == "auth_required" assert data["http_status"] == 401 @pytest.mark.asyncio async def test_health_check_connection_error(self, test_app, mock_config, admin_headers, monkeypatch): """Test health check when connection fails.""" monkeypatch.setenv("LITELLM_URL", "http://mock-litellm:4000") monkeypatch.setenv("LITELLM_HEALTH_API_KEY", "") # Mock connection error with patch("httpx.AsyncClient") as mock_client: mock_client.return_value.__aenter__.return_value.get.side_effect = Exception("Connection refused") response = test_app.get("/api/litellm/health", headers=admin_headers) assert response.status_code == 200 data = response.json() assert data["ok"] is False assert data["status"] == "down" assert "Connection refused" in data["error"] def test_health_check_without_auth(self, test_app, mock_config): """Test health check without authentication.""" response = test_app.get("/api/litellm/health") assert response.status_code == 401