- Authoritative candidate capture with server-issued artifacts and raw-byte
immutability hashing (source_response_hash server-owned)
- Closed-period lifecycle: request-hash bound approvals, persisted closure
immutability violations, byte-for-byte catalog stability on reclosure
- Verification runs: persisted VerificationRun model + FK migration,
publish gate (block_publish), scheduled observability runs (02:00 UTC)
- FR-013 baseline inheritance: prior_release_id migration, plan_inheritance/
execute_inheritance classification and re-extraction, API endpoints
- Visual executor bound to release-deployment environment; caller mismatch
rejected; visual SSIM/reconciliation modules
- Query execution decomposed: envelope/model/executor split, no direct SQL
- AgentRun approvals extracted to submodule; evidence adapter; _utils
- Dashboard testing service decomposed into 30+ modules (all <400 LOC)
- Five Feature-037 agent tools with permission guards (tools_037.py)
- API readiness endpoint; Alembic env/migrations; test fixture repos
- Specs 036/037 contracts, openapi.yaml, schema.json, tasks/traceability
updated; semantic index rebuilt with 0 parse warnings
- Fix ADR-0003 parser ambiguity: remove [DEF🆔ADR] prose example
- Add axiom-mcp-agent-feedback.md: agent findings for MCP rework plan
- Tests: 298 service + 1464 API + 45 agent passing; ruff clean
164 lines
6.2 KiB
Python
164 lines
6.2 KiB
Python
# #region Test.Api.Health [C:3] [TYPE Module] [SEMANTICS test,health,api]
|
|
# @BRIEF Unit tests for health API routes — summary, delete report.
|
|
# @RELATION BINDS_TO -> [Api.Health.HealthRouter]
|
|
# @TEST_EDGE: feature_disabled -> 404
|
|
# @TEST_EDGE: report_not_found -> 404
|
|
# @TEST_EDGE: environment_filter -> 200
|
|
|
|
import os
|
|
|
|
os.environ.setdefault("DATABASE_URL", "sqlite:///:memory:")
|
|
os.environ.setdefault("AUTH_DATABASE_URL", "sqlite:///:memory:")
|
|
os.environ.setdefault("SECRET_KEY", "test-secret-key-for-tests")
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
import pytest
|
|
from fastapi import FastAPI
|
|
from fastapi.testclient import TestClient
|
|
|
|
_src = str(Path(__file__).resolve().parent.parent.parent / "src")
|
|
if _src not in sys.path:
|
|
sys.path.insert(0, _src)
|
|
|
|
|
|
def _make_client(overrides: dict | None = None) -> TestClient:
|
|
from src.api.routes.health import router
|
|
from src.core.database import get_db
|
|
from src.dependencies import get_config_manager, get_current_user, get_task_manager, has_permission
|
|
from src.schemas.auth import User, RoleSchema
|
|
|
|
app = FastAPI()
|
|
app.include_router(router)
|
|
|
|
mock_user = User(
|
|
id="admin-1", username="admin", email="admin@x.com",
|
|
auth_source="LOCAL",
|
|
created_at=__import__("datetime").datetime.now(),
|
|
roles=[RoleSchema(id="r1", name="Admin", description="", is_admin=True, permissions=[])],
|
|
)
|
|
|
|
defaults = {
|
|
get_db: lambda: MagicMock(),
|
|
get_config_manager: lambda: MagicMock(),
|
|
get_task_manager: lambda: MagicMock(),
|
|
get_current_user: lambda: mock_user,
|
|
}
|
|
if overrides:
|
|
defaults.update(overrides)
|
|
for dep, mock_fn in defaults.items():
|
|
app.dependency_overrides[dep] = mock_fn
|
|
return TestClient(app)
|
|
|
|
|
|
class TestGetHealthSummary:
|
|
"""GET /api/health/summary"""
|
|
|
|
def test_summary_success(self):
|
|
"""Happy path: returns health summary."""
|
|
mock_config = MagicMock()
|
|
mock_config.get_config.return_value.settings.features.health_monitor = True
|
|
|
|
mock_service = AsyncMock()
|
|
mock_service.get_health_summary.return_value = {
|
|
"items": [],
|
|
"pass_count": 0,
|
|
"warn_count": 0,
|
|
"fail_count": 0,
|
|
"unknown_count": 0,
|
|
}
|
|
|
|
with patch("src.api.routes.health.HealthService", return_value=mock_service):
|
|
from src.dependencies import get_config_manager
|
|
client = _make_client({get_config_manager: lambda: mock_config})
|
|
resp = client.get("/api/health/summary")
|
|
assert resp.status_code == 200
|
|
|
|
def test_summary_with_environment_filter(self):
|
|
"""Environment filter passed to service."""
|
|
mock_config = MagicMock()
|
|
mock_config.get_config.return_value.settings.features.health_monitor = True
|
|
|
|
mock_service = AsyncMock()
|
|
mock_service.get_health_summary.return_value = {
|
|
"items": [],
|
|
"pass_count": 0,
|
|
"warn_count": 0,
|
|
"fail_count": 0,
|
|
"unknown_count": 0,
|
|
}
|
|
|
|
with patch("src.api.routes.health.HealthService", return_value=mock_service):
|
|
from src.dependencies import get_config_manager
|
|
client = _make_client({get_config_manager: lambda: mock_config})
|
|
resp = client.get("/api/health/summary?environment_id=prod")
|
|
assert resp.status_code == 200
|
|
mock_service.get_health_summary.assert_called_with(environment_id="prod")
|
|
|
|
def test_summary_feature_disabled(self):
|
|
"""Health monitor disabled returns 404."""
|
|
mock_config = MagicMock()
|
|
mock_config.get_config.return_value.settings.features.health_monitor = False
|
|
|
|
from src.dependencies import get_config_manager
|
|
client = _make_client({get_config_manager: lambda: mock_config})
|
|
resp = client.get("/api/health/summary")
|
|
assert resp.status_code == 404
|
|
assert "Health monitor feature is disabled" in resp.text
|
|
|
|
|
|
class TestDeleteHealthReport:
|
|
"""DELETE /api/health/summary/{record_id}"""
|
|
|
|
def test_delete_report_success(self):
|
|
"""Happy path: report deleted returns 204."""
|
|
mock_config = MagicMock()
|
|
mock_config.get_config.return_value.settings.features.health_monitor = True
|
|
|
|
mock_service = MagicMock()
|
|
mock_service.delete_validation_report.return_value = True
|
|
|
|
with patch("src.api.routes.health.HealthService", return_value=mock_service):
|
|
from src.dependencies import get_config_manager, get_task_manager
|
|
client = _make_client({
|
|
get_config_manager: lambda: mock_config,
|
|
get_task_manager: lambda: MagicMock(),
|
|
})
|
|
resp = client.delete("/api/health/summary/rec-1")
|
|
assert resp.status_code == 204
|
|
mock_service.delete_validation_report.assert_called_once()
|
|
|
|
def test_delete_report_not_found(self):
|
|
"""Non-existent report returns 404."""
|
|
mock_config = MagicMock()
|
|
mock_config.get_config.return_value.settings.features.health_monitor = True
|
|
|
|
mock_service = MagicMock()
|
|
mock_service.delete_validation_report.return_value = False
|
|
|
|
with patch("src.api.routes.health.HealthService", return_value=mock_service):
|
|
from src.dependencies import get_config_manager, get_task_manager
|
|
client = _make_client({
|
|
get_config_manager: lambda: mock_config,
|
|
get_task_manager: lambda: MagicMock(),
|
|
})
|
|
resp = client.delete("/api/health/summary/rec-missing")
|
|
assert resp.status_code == 404
|
|
assert "Health report not found" in resp.text
|
|
|
|
def test_delete_report_feature_disabled(self):
|
|
"""Health monitor disabled returns 404."""
|
|
mock_config = MagicMock()
|
|
mock_config.get_config.return_value.settings.features.health_monitor = False
|
|
|
|
from src.dependencies import get_config_manager, get_task_manager
|
|
client = _make_client({
|
|
get_config_manager: lambda: mock_config,
|
|
get_task_manager: lambda: MagicMock(),
|
|
})
|
|
resp = client.delete("/api/health/summary/rec-1")
|
|
assert resp.status_code == 404
|
|
# #endregion Test.Api.Health
|