Discover dashboard filters, datasets and metrics from a parsed reference URL; capture and review baseline candidates with source provenance. Improve scenario DAG and run result views, add isolated browser coverage, and align contracts and ADRs.
150 lines
7.6 KiB
Python
150 lines
7.6 KiB
Python
# #region Test.Api.ReferenceDiscovery [C:4] [TYPE Module] [SEMANTICS baseline,reference,discovery,api,test]
|
|
# @defgroup Tests Scenario-bound guarded REST discovery.
|
|
# @RELATION VERIFIES -> [Api.DashboardTesting.ReferenceDiscovery.Post]
|
|
from __future__ import annotations
|
|
|
|
import inspect
|
|
from types import SimpleNamespace
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
from fastapi import HTTPException
|
|
from pydantic import ValidationError
|
|
import pytest
|
|
|
|
from src.api.routes.dashboard_testing.reference_discovery import (
|
|
ReferenceDiscoveryRequest, reference_discovery, router,
|
|
)
|
|
from src.models.auth import Role, User
|
|
|
|
_URL = "/api/dashboard-testing/reference-discovery"
|
|
|
|
|
|
def _db(environment_ids=None):
|
|
return SimpleNamespace(get=lambda model, key: SimpleNamespace(
|
|
dashboard_id=42, environment_ids=["preprod"] if environment_ids is None else environment_ids,
|
|
) if key == "scenario-1" else None)
|
|
|
|
|
|
def _body(url="https://superset.example/dashboard/42/", **kwargs):
|
|
return ReferenceDiscoveryRequest(scenario_id="scenario-1", url=url, **kwargs)
|
|
|
|
|
|
def _environment(env_id="preprod", url="https://superset.example"):
|
|
return SimpleNamespace(id=env_id, url=url)
|
|
|
|
|
|
# #region Test.Api.ReferenceDiscovery.Binding [C:4] [TYPE Function]
|
|
# @TEST_INVARIANT The scenario dashboard and matching configured environment reach discovery; client body cannot override either.
|
|
@pytest.mark.asyncio
|
|
async def test_scenario_dashboard_and_origin_are_server_bound():
|
|
assert _URL in [route.path for route in router.routes]
|
|
manager = SimpleNamespace(get_environments=lambda: [_environment()])
|
|
preview = {"environment_id": "preprod", "dashboard_id": 42, "coordinates": []}
|
|
with (
|
|
patch("src.api.routes.dashboard_testing.reference_discovery.get_config_manager", return_value=manager),
|
|
patch("src.api.routes.dashboard_testing.reference_discovery.get_superset_client", new=AsyncMock(return_value="configured-client")),
|
|
patch("src.api.routes.dashboard_testing.reference_discovery.discover_reference_dashboard", new=AsyncMock(return_value=preview)) as discover,
|
|
):
|
|
result = await reference_discovery(_body(max_queries=2), _reader=object(), _executor=object(), db=_db())
|
|
assert result == preview
|
|
discover.assert_awaited_once_with(
|
|
"configured-client", "preprod", 42,
|
|
"https://superset.example/dashboard/42/", max_queries=2,
|
|
)
|
|
with pytest.raises(ValidationError):
|
|
ReferenceDiscoveryRequest(scenario_id="scenario-1", url="https://superset.example/dashboard/42/", dashboard_id=999)
|
|
# #endregion Test.Api.ReferenceDiscovery.Binding
|
|
|
|
|
|
# #region Test.Api.ReferenceDiscovery.PublicEvidenceDigest [C:3] [TYPE Function]
|
|
# @TEST_INVARIANT Public preview evidence digests use the `sha256:<64 lowercase hex>` shape required by capture.
|
|
@pytest.mark.asyncio
|
|
async def test_public_preview_normalizes_bare_source_hashes():
|
|
raw_digest = "a" * 64
|
|
already_prefixed = "sha256:" + "b" * 64
|
|
preview = {"coordinates": [
|
|
{"evidence_digest": raw_digest},
|
|
{"evidence_digest": already_prefixed},
|
|
{"evidence_digest": None},
|
|
]}
|
|
manager = SimpleNamespace(get_environments=lambda: [_environment()])
|
|
with (
|
|
patch("src.api.routes.dashboard_testing.reference_discovery.get_config_manager", return_value=manager),
|
|
patch("src.api.routes.dashboard_testing.reference_discovery.get_superset_client", new=AsyncMock(return_value="client")),
|
|
patch("src.api.routes.dashboard_testing.reference_discovery.discover_reference_dashboard", new=AsyncMock(return_value=preview)),
|
|
):
|
|
result = await reference_discovery(_body(), _reader=object(), _executor=object(), db=_db())
|
|
assert [item["evidence_digest"] for item in result["coordinates"]] == [
|
|
"sha256:" + raw_digest, already_prefixed, None,
|
|
]
|
|
# #endregion Test.Api.ReferenceDiscovery.PublicEvidenceDigest
|
|
|
|
|
|
# #region Test.Api.ReferenceDiscovery.InvalidOrigin [C:4] [TYPE Function]
|
|
# @TEST_INVARIANT Unknown or ambiguous origins reject before any SupersetClient construction.
|
|
@pytest.mark.asyncio
|
|
async def test_origin_rejection_and_conflict():
|
|
cases = [
|
|
([_environment()], "https://evil.example/dashboard/42/", 422, "UNKNOWN_REFERENCE_ORIGIN"),
|
|
([_environment("one"), _environment("two")], "https://superset.example/dashboard/42/", 409, "AMBIGUOUS_REFERENCE_ORIGIN"),
|
|
]
|
|
for environments, url, status_code, code in cases:
|
|
manager = SimpleNamespace(get_environments=lambda: environments)
|
|
with (
|
|
patch("src.api.routes.dashboard_testing.reference_discovery.get_config_manager", return_value=manager),
|
|
patch("src.api.routes.dashboard_testing.reference_discovery.get_superset_client", new=AsyncMock()) as client,
|
|
):
|
|
with pytest.raises(HTTPException) as error:
|
|
await reference_discovery(_body(url), _reader=object(), _executor=object(), db=_db())
|
|
assert error.value.status_code == status_code
|
|
assert error.value.detail["code"] == code
|
|
client.assert_not_awaited()
|
|
# #endregion Test.Api.ReferenceDiscovery.InvalidOrigin
|
|
|
|
|
|
# #region Test.Api.ReferenceDiscovery.DashboardMismatch [C:4] [TYPE Function]
|
|
# @TEST_INVARIANT A different dashboard yields typed 422 with no partial preview.
|
|
@pytest.mark.asyncio
|
|
async def test_dashboard_mismatch_is_typed_422():
|
|
manager = SimpleNamespace(get_environments=lambda: [_environment()])
|
|
with (
|
|
patch("src.api.routes.dashboard_testing.reference_discovery.get_config_manager", return_value=manager),
|
|
patch("src.api.routes.dashboard_testing.reference_discovery.get_superset_client", new=AsyncMock(return_value="client")),
|
|
patch("src.api.routes.dashboard_testing.reference_discovery.discover_reference_dashboard", new=AsyncMock(side_effect=ValueError("Reference URL dashboard differs from authoritative model"))),
|
|
):
|
|
with pytest.raises(HTTPException) as error:
|
|
await reference_discovery(_body("https://superset.example/dashboard/43/"), _reader=object(), _executor=object(), db=_db())
|
|
assert error.value.status_code == 422
|
|
assert error.value.detail["code"] == "DASHBOARD_MISMATCH"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_unbound_scenario_rejects_reference_environment_before_superset_io():
|
|
manager = SimpleNamespace(get_environments=lambda: [_environment()])
|
|
with (
|
|
patch("src.api.routes.dashboard_testing.reference_discovery.get_config_manager", return_value=manager),
|
|
patch("src.api.routes.dashboard_testing.reference_discovery.get_superset_client", new=AsyncMock()) as client,
|
|
):
|
|
with pytest.raises(HTTPException) as error:
|
|
await reference_discovery(_body(), _reader=object(), _executor=object(), db=_db([]))
|
|
assert error.value.detail["code"] == "ENVIRONMENT_MISMATCH"
|
|
client.assert_not_awaited()
|
|
# #endregion Test.Api.ReferenceDiscovery.DashboardMismatch
|
|
|
|
|
|
# #region Test.Api.ReferenceDiscovery.Rbac [C:4] [TYPE Function]
|
|
# @TEST_INVARIANT Both READ and EXECUTE dependencies reject an unprivileged principal.
|
|
def test_read_and_execute_permissions_are_required():
|
|
parameters = inspect.signature(reference_discovery).parameters
|
|
viewer_role = Role(id="reference-viewer", name="Viewer", is_admin=False)
|
|
viewer = User(id="reference-viewer", username="viewer", email="viewer@test.com")
|
|
viewer.roles = [viewer_role]
|
|
for name, action in (("_reader", "READ"), ("_executor", "EXECUTE")):
|
|
checker = parameters[name].default.dependency
|
|
with pytest.raises(HTTPException) as error:
|
|
checker(viewer)
|
|
assert error.value.status_code == 403
|
|
assert action in error.value.detail
|
|
# #endregion Test.Api.ReferenceDiscovery.Rbac
|
|
# #endregion Test.Api.ReferenceDiscovery
|