mirror of
https://github.com/latinogino/dolibarr-mcp.git
synced 2026-04-22 18:02:40 +02:00
Fix API connection check signaling and add tests
This commit is contained in:
@@ -1389,6 +1389,7 @@ async def handle_call_tool(name: str, arguments: dict):
|
||||
async def test_api_connection(config: Config | None = None):
|
||||
"""Test API connection and yield client if successful."""
|
||||
created_config = False
|
||||
api_ok = False
|
||||
try:
|
||||
if config is None:
|
||||
config = Config()
|
||||
@@ -1399,14 +1400,14 @@ async def test_api_connection(config: Config | None = None):
|
||||
print("⚠️ Warning: DOLIBARR_URL not configured in .env file", file=sys.stderr)
|
||||
print("⚠️ Using placeholder URL - API calls will fail", file=sys.stderr)
|
||||
print("📝 Please configure your .env file with valid Dolibarr credentials", file=sys.stderr)
|
||||
yield True # Allow server to start anyway
|
||||
yield False # Configuration incomplete
|
||||
return
|
||||
|
||||
if not config.api_key or config.api_key == "your_dolibarr_api_key_here":
|
||||
print("⚠️ Warning: DOLIBARR_API_KEY not configured in .env file", file=sys.stderr)
|
||||
print("⚠️ API authentication will fail", file=sys.stderr)
|
||||
print("📝 Please configure your .env file with valid Dolibarr credentials", file=sys.stderr)
|
||||
yield True # Allow server to start anyway
|
||||
yield False # Configuration incomplete
|
||||
return
|
||||
|
||||
async with DolibarrClient(config) as client:
|
||||
@@ -1415,17 +1416,19 @@ async def test_api_connection(config: Config | None = None):
|
||||
if 'success' in result or 'dolibarr_version' in str(result):
|
||||
print("✅ Dolibarr API connection successful", file=sys.stderr)
|
||||
print("🎯 Full CRUD operations available for all Dolibarr modules", file=sys.stderr)
|
||||
yield True
|
||||
api_ok = True
|
||||
else:
|
||||
print(f"⚠️ API test returned unexpected result: {result}", file=sys.stderr)
|
||||
print("⚠️ Server will start but API calls may fail", file=sys.stderr)
|
||||
yield True # Allow server to start anyway
|
||||
api_ok = False
|
||||
except Exception as e:
|
||||
print(f"⚠️ API test error: {e}", file=sys.stderr)
|
||||
if config is None or created_config:
|
||||
print("💡 Check your .env file configuration", file=sys.stderr)
|
||||
print("⚠️ Server will start but API calls may fail", file=sys.stderr)
|
||||
yield True # Allow server to start anyway
|
||||
api_ok = False
|
||||
|
||||
yield api_ok
|
||||
|
||||
|
||||
async def _run_stdio_server(_config: Config) -> None:
|
||||
|
||||
71
tests/test_server_connection.py
Normal file
71
tests/test_server_connection.py
Normal file
@@ -0,0 +1,71 @@
|
||||
"""Tests for MCP server connection checks."""
|
||||
|
||||
import pytest
|
||||
|
||||
from dolibarr_mcp import dolibarr_mcp_server
|
||||
from dolibarr_mcp.config import Config
|
||||
from dolibarr_mcp.dolibarr_client import DolibarrAPIError
|
||||
|
||||
|
||||
class _DummyClient:
|
||||
"""Simple dummy client for test_api_connection success path."""
|
||||
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
||||
return False
|
||||
|
||||
async def get_status(self):
|
||||
return {"success": {"dolibarr_version": "1.0.0"}}
|
||||
|
||||
|
||||
class _ErrorClient:
|
||||
"""Dummy client that raises for test_api_connection error path."""
|
||||
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
||||
return False
|
||||
|
||||
async def get_status(self):
|
||||
raise DolibarrAPIError("boom")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_api_connection_success(monkeypatch):
|
||||
"""Yields True when the Dolibarr API status call succeeds."""
|
||||
monkeypatch.setattr(dolibarr_mcp_server, "DolibarrClient", lambda config: _DummyClient())
|
||||
config = Config(
|
||||
dolibarr_url="https://example.com/api/index.php",
|
||||
dolibarr_api_key="test_key",
|
||||
)
|
||||
|
||||
async with dolibarr_mcp_server.test_api_connection(config) as api_ok:
|
||||
assert api_ok is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_api_connection_missing_configuration():
|
||||
"""Yields False when the configuration is incomplete."""
|
||||
config = Config(
|
||||
dolibarr_url="https://your-dolibarr-instance.com/api/index.php",
|
||||
dolibarr_api_key="placeholder_api_key",
|
||||
)
|
||||
|
||||
async with dolibarr_mcp_server.test_api_connection(config) as api_ok:
|
||||
assert api_ok is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_api_connection_with_client_error(monkeypatch):
|
||||
"""Yields False when the Dolibarr client raises errors."""
|
||||
monkeypatch.setattr(dolibarr_mcp_server, "DolibarrClient", lambda config: _ErrorClient())
|
||||
config = Config(
|
||||
dolibarr_url="https://example.com/api/index.php",
|
||||
dolibarr_api_key="test_key",
|
||||
)
|
||||
|
||||
async with dolibarr_mcp_server.test_api_connection(config) as api_ok:
|
||||
assert api_ok is False
|
||||
Reference in New Issue
Block a user