"""Tests for Gitea API client.""" import pytest import httpx from unittest.mock import AsyncMock, MagicMock from gitea_mcp.auth import AuthConfig from gitea_mcp.client import ( GiteaClient, GiteaClientError, GiteaAuthError, GiteaNotFoundError, GiteaServerError, ) @pytest.fixture def mock_config(monkeypatch): """Create mock authentication config.""" monkeypatch.setenv("GITEA_API_URL", "http://gitea.example.com/api/v1") monkeypatch.setenv("GITEA_API_TOKEN", "test_token_123") return AuthConfig() @pytest.mark.asyncio async def test_client_initialization(mock_config): """Test client initialization.""" client = GiteaClient(mock_config, timeout=10.0) assert client.config == mock_config assert client.timeout == 10.0 @pytest.mark.asyncio async def test_client_context_manager(mock_config): """Test client as async context manager.""" async with GiteaClient(mock_config) as client: assert client._client is not None assert isinstance(client._client, httpx.AsyncClient) @pytest.mark.asyncio async def test_client_without_context_manager_raises_error(mock_config): """Test that using client without context manager raises error.""" client = GiteaClient(mock_config) with pytest.raises(GiteaClientError, match="not initialized"): await client.get("/test") @pytest.mark.asyncio async def test_handle_error_401(mock_config): """Test handling 401 authentication error.""" response = MagicMock() response.status_code = 401 async with GiteaClient(mock_config) as client: with pytest.raises(GiteaAuthError, match="Authentication failed"): client._handle_error(response) @pytest.mark.asyncio async def test_handle_error_403(mock_config): """Test handling 403 forbidden error.""" response = MagicMock() response.status_code = 403 async with GiteaClient(mock_config) as client: with pytest.raises(GiteaAuthError, match="Access forbidden"): client._handle_error(response) @pytest.mark.asyncio async def test_handle_error_404(mock_config): """Test handling 404 not found error.""" response = MagicMock() response.status_code = 404 response.request = MagicMock() response.request.url = "http://gitea.example.com/api/v1/test" async with GiteaClient(mock_config) as client: with pytest.raises(GiteaNotFoundError, match="not found"): client._handle_error(response) @pytest.mark.asyncio async def test_handle_error_500(mock_config): """Test handling 500 server error.""" response = MagicMock() response.status_code = 500 response.text = "Internal Server Error" async with GiteaClient(mock_config) as client: with pytest.raises(GiteaServerError, match="server error"): client._handle_error(response)