test: 7 agents — plugins ~158 tests, extractor 98-100%, dashboard routes 99-100%, git services 94-100%, services 94-100%, dataset_review 100%. Fix test_api_key_routes.py sys.modules pollution

This commit is contained in:
2026-06-15 18:30:05 +03:00
parent 3de67c258a
commit 51d90f58c1
40 changed files with 9629 additions and 4 deletions

View File

@@ -464,4 +464,84 @@ class TestGetDashboards:
# Only dash-1 has a slug
assert data["total"] == 1
assert data["dashboards"][0]["slug"] == "my-dash"
def test_get_dashboards_profile_filter_empty_aliases(self, base_mocks):
"""Profile filter with empty actor aliases falls back to bound_username (line 252)."""
mock_rs = base_mocks["resource_service"]
mock_rs.get_dashboards_page_with_status.side_effect = Exception("No page support")
mock_rs.get_dashboards_with_status.return_value = [
{"id": 1, "title": "My Dash", "slug": "my-dash", "owners": [{"username": "testuser"}], "modified_by": None},
{"id": 2, "title": "Other Dash", "slug": "other", "owners": [{"username": "other"}], "modified_by": None},
]
mocks = dict(base_mocks)
profile_svc = MagicMock()
profile_svc.get_dashboard_filter_binding.return_value = {
"superset_username": "testuser",
"superset_username_normalized": "testuser",
"show_only_my_dashboards": True,
"show_only_slug_dashboards": False,
}
profile_svc.matches_dashboard_actor.side_effect = lambda bound_username, owners, modified_by: (
any("testuser" in str(o) for o in (owners or []))
)
mocks["_profile_service"] = profile_svc
with patch("src.api.routes.dashboards._listing_routes.ProfileService", return_value=profile_svc) as MockPS:
MockPS.__module__ = "unittest.mock"
async def _empty_aliases(*args, **kwargs):
return []
with patch("src.api.routes.dashboards._listing_routes._resolve_profile_actor_aliases", side_effect=_empty_aliases):
client = _make_client(mocks)
resp = client.get("/api/dashboards?env_id=env-1&page_context=dashboards_main&apply_profile_default=true")
assert resp.status_code == 200
data = resp.json()
assert data["total"] == 1
def test_get_dashboards_changed_on_no_match(self, base_mocks):
"""Changed-on filter value does not match dashboard (line 337 return False)."""
mock_rs = base_mocks["resource_service"]
mock_rs.get_dashboards_page_with_status.side_effect = Exception("No page support")
mock_rs.get_dashboards_with_status.return_value = [
{"id": 1, "title": "Main", "slug": "main", "owners": [], "last_modified": "2024-01-01T12:00:00"},
]
client = _make_client(base_mocks)
resp = client.get("/api/dashboards?env_id=env-1&filter_changed_on=2099-12-31")
assert resp.status_code == 200
data = resp.json()
assert data["total"] == 0
def test_get_dashboards_http_exception_re_raised(self, base_mocks):
"""HTTPException from full-scan path is re-raised (line 382 except HTTPException)."""
from fastapi import HTTPException as FastAPIHTTPException
mock_rs = base_mocks["resource_service"]
async def _raise_conflict(*a, **kw):
raise FastAPIHTTPException(status_code=409, detail="Conflict from service")
mock_rs.get_dashboards_with_status.side_effect = _raise_conflict
client = _make_client(base_mocks)
resp = client.get("/api/dashboards?env_id=env-1&filter_git_status=ok")
assert resp.status_code == 409
def test_get_dashboards_slug_fallback_no_git(self, base_mocks):
"""Slug-only filter via fallback when page fetch fails and needs_full_scan is false."""
mock_rs = base_mocks["resource_service"]
mock_rs.get_dashboards_page_with_status.side_effect = Exception("No page support")
mock_rs.get_dashboards_with_status.return_value = [
{"id": 1, "title": "Has Slug", "slug": "has-slug", "owners": []},
{"id": 2, "title": "No Slug", "slug": "", "owners": []},
]
mocks = dict(base_mocks)
with patch("src.api.routes.dashboards._listing_routes.ProfileService") as MockPS:
profile_svc = MagicMock()
profile_svc.get_dashboard_filter_binding.return_value = {
"superset_username": "",
"superset_username_normalized": "",
"show_only_my_dashboards": False,
"show_only_slug_dashboards": False,
}
MockPS.return_value = profile_svc
MockPS.__module__ = "unittest.mock"
client = _make_client(mocks)
resp = client.get("/api/dashboards?env_id=env-1")
assert resp.status_code == 200
data = resp.json()
assert data["total"] == 2
# #endregion Test.Api.DashboardListingRoutes