68 lines
2.5 KiB
Python
68 lines
2.5 KiB
Python
# [DEF:backend.tests.test_dashboards_api:Module]
|
|
# @TIER: STANDARD
|
|
# @PURPOSE: Contract-driven tests for Dashboard Hub API
|
|
# @RELATION: TESTS -> backend.src.api.routes.dashboards
|
|
|
|
from fastapi.testclient import TestClient
|
|
from unittest.mock import MagicMock, patch
|
|
from src.app import app
|
|
from src.api.routes.dashboards import DashboardsResponse
|
|
|
|
client = TestClient(app)
|
|
|
|
# [DEF:test_get_dashboards_success:Function]
|
|
# @TEST: GET /api/dashboards returns 200 and valid schema
|
|
# @PRE: env_id exists
|
|
# @POST: Response matches DashboardsResponse schema
|
|
def test_get_dashboards_success():
|
|
with patch("src.api.routes.dashboards.get_config_manager") as mock_config, \
|
|
patch("src.api.routes.dashboards.get_resource_service") as mock_service, \
|
|
patch("src.api.routes.dashboards.has_permission") as mock_perm:
|
|
|
|
# Mock environment
|
|
mock_env = MagicMock()
|
|
mock_env.id = "prod"
|
|
mock_config.return_value.get_environments.return_value = [mock_env]
|
|
|
|
# Mock resource service response
|
|
mock_service.return_value.get_dashboards_with_status.return_value = [
|
|
{
|
|
"id": 1,
|
|
"title": "Sales Report",
|
|
"slug": "sales",
|
|
"git_status": {"branch": "main", "sync_status": "OK"},
|
|
"last_task": {"task_id": "task-1", "status": "SUCCESS"}
|
|
}
|
|
]
|
|
|
|
# Mock permission
|
|
mock_perm.return_value = lambda: True
|
|
|
|
response = client.get("/api/dashboards?env_id=prod")
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert "dashboards" in data
|
|
assert len(data["dashboards"]) == 1
|
|
assert data["dashboards"][0]["title"] == "Sales Report"
|
|
# Validate against Pydantic model
|
|
DashboardsResponse(**data)
|
|
|
|
# [DEF:test_get_dashboards_env_not_found:Function]
|
|
# @TEST: GET /api/dashboards returns 404 if env_id missing
|
|
# @PRE: env_id does not exist
|
|
# @POST: Returns 404 error
|
|
def test_get_dashboards_env_not_found():
|
|
with patch("src.api.routes.dashboards.get_config_manager") as mock_config, \
|
|
patch("src.api.routes.dashboards.has_permission") as mock_perm:
|
|
|
|
mock_config.return_value.get_environments.return_value = []
|
|
mock_perm.return_value = lambda: True
|
|
|
|
response = client.get("/api/dashboards?env_id=nonexistent")
|
|
|
|
assert response.status_code == 404
|
|
assert "Environment not found" in response.json()["detail"]
|
|
|
|
# [/DEF:backend.tests.test_dashboards_api:Module]
|