ai-code-review-agent / tests /test_api_routes.py
Padmanav's picture
fix
42c1adb
Raw
History Blame Contribute Delete
2.67 kB
# tests/test_api_routes.py
from unittest.mock import AsyncMock, MagicMock, patch
from fastapi.testclient import TestClient
from app.api.limiter import limiter
from app.api.main import app
limiter.enabled = False
client = TestClient(app)
VALID_URL = "https://github.com/owner/repo"
def _mock_settings(api_key: str = "") -> MagicMock:
s = MagicMock()
s.api_key = api_key
return s
def test_analyze_rejects_missing_api_key():
with (
patch(
"app.api.dependencies.get_settings", return_value=_mock_settings("secret")
),
patch("app.api.routes.clone_repository", return_value="/tmp/fake"),
patch("app.api.routes.delete_repository"),
):
response = client.post("/api/v1/analyze", json={"github_url": VALID_URL})
assert response.status_code == 401
def test_analyze_rejects_wrong_api_key():
with (
patch(
"app.api.dependencies.get_settings", return_value=_mock_settings("secret")
),
patch("app.api.routes.clone_repository", return_value="/tmp/fake"),
patch("app.api.routes.delete_repository"),
):
response = client.post(
"/api/v1/analyze",
json={"github_url": VALID_URL},
headers={"X-API-Key": "wrong"},
)
assert response.status_code == 401
def test_analyze_rejects_invalid_github_url():
with patch("app.api.dependencies.get_settings", return_value=_mock_settings()):
response = client.post(
"/api/v1/analyze",
json={"github_url": "https://gitlab.com/owner/repo"},
)
assert response.status_code == 422
def test_quick_analyze_health_path():
from app.models.repository import RepositoryMetadata
mock_metadata = RepositoryMetadata(
url=VALID_URL, name="repo", local_path="/tmp/fake"
)
with (
patch("app.api.dependencies.get_settings", return_value=_mock_settings()),
patch("app.api.routes.clone_repository", return_value="/tmp/fake"),
patch("app.api.routes.delete_repository"),
patch(
"app.api.routes.repo_analysis_agent.run",
new=AsyncMock(return_value=mock_metadata),
),
):
response = client.post(
"/api/v1/quick-analyze",
json={"github_url": VALID_URL},
)
assert response.status_code == 200
def test_metrics_endpoint_returns_dict():
with patch("app.api.dependencies.get_settings", return_value=_mock_settings("")):
response = client.get("/api/v1/metrics")
assert response.status_code == 200
data = response.json()
assert "uptime_seconds" in data
assert "agents" in data