"""Tests for 42crunch API client."""
import pytest
from unittest.mock import Mock, patch
from src.client import FortyTwoCrunchClient
from src.config import Config
class TestFortyTwoCrunchClient:
"""Test cases for FortyTwoCrunchClient."""
@pytest.fixture
def mock_config(self):
"""Create a mock configuration."""
config = Mock(spec=Config)
config.BASE_URL = "https://us.42crunch.cloud"
config.API_V1_BASE = "https://us.42crunch.cloud/api/v1"
config.API_V2_BASE = "https://us.42crunch.cloud/api/v2"
config.get_auth_headers.return_value = {
"X-MCP-AUTH": "test-token",
"Content-Type": "application/json",
}
return config
def test_list_collections(self, mock_config):
"""Test listing collections."""
with patch("src.client.httpx.Client") as mock_client_class:
mock_client = Mock()
mock_response = Mock()
mock_response.json.return_value = {"collections": []}
mock_response.raise_for_status = Mock()
mock_client.get.return_value = mock_response
mock_client_class.return_value = mock_client
client = FortyTwoCrunchClient(config=mock_config)
result = client.list_collections(page=1, per_page=10)
assert "collections" in result
mock_client.get.assert_called_once()
def test_get_collection_apis(self, mock_config):
"""Test getting APIs from a collection."""
with patch("src.client.httpx.Client") as mock_client_class:
mock_client = Mock()
mock_response = Mock()
mock_response.json.return_value = {"apis": []}
mock_response.raise_for_status = Mock()
mock_client.get.return_value = mock_response
mock_client_class.return_value = mock_client
client = FortyTwoCrunchClient(config=mock_config)
result = client.get_collection_apis(
collection_id="test-collection-id"
)
assert "apis" in result
mock_client.get.assert_called_once()
def test_get_api_details(self, mock_config):
"""Test getting API details."""
with patch("src.client.httpx.Client") as mock_client_class:
mock_client = Mock()
mock_response = Mock()
mock_response.json.return_value = {"api": {}}
mock_response.raise_for_status = Mock()
mock_client.get.return_value = mock_response
mock_client_class.return_value = mock_client
client = FortyTwoCrunchClient(config=mock_config)
result = client.get_api_details(api_id="test-api-id")
assert "api" in result
mock_client.get.assert_called_once()