279 lines
14 KiB
Python
279 lines
14 KiB
Python
# #region Test.ReferenceSource [C:3] [TYPE Module] [SEMANTICS baseline,reference,snapshot,test]
|
|
# @TEST_INVARIANT BaselineEngine.ReferenceSource: resolved semantics are immutable and a missing or
|
|
# altered snapshot cannot acquire approval or pin authority.
|
|
from copy import deepcopy
|
|
from hashlib import sha256
|
|
import json
|
|
from types import SimpleNamespace
|
|
from unittest.mock import AsyncMock, Mock
|
|
|
|
import pytest
|
|
|
|
from src.services.dashboard_testing import reference_source as source_owner
|
|
from src.services.dashboard_testing.reference_source import build_reference_source, require_reference_source
|
|
|
|
|
|
def _source():
|
|
return build_reference_source(
|
|
url="https://superset.example/superset/dashboard/42/",
|
|
url_kind="dashboard",
|
|
source_url_ref="draft:run:" + "1" * 64,
|
|
environment_id="preprod", dashboard_id=42,
|
|
filters={"filters": [], "filters_hash": "sha256:4f53cda18c2baa0c0354bb5f9a3ecbe5ed12ab4d8e11ba873c2f11161202b945"},
|
|
query_model_fingerprint="sha256:" + "c" * 64,
|
|
)
|
|
|
|
|
|
def test_frozen_reference_has_expected_server_identity():
|
|
source = _source()
|
|
assert source["reference_digest"] == "4cb1188ff74d85e2c54d81d40457f97f2db193dd35bdc45b43f1b722d21a0c9a"
|
|
assert source["source_url_sha256"] == "e6919b071bb03b4f68ef6ce0586184e6b1a1ff30e0837067328ea8702cc4cf6d"
|
|
assert source["resolved_native_filters"] == []
|
|
assert require_reference_source(source, environment_id="preprod", dashboard_id=42) == source
|
|
with pytest.raises(ValueError, match="REFERENCE_SOURCE_MISMATCH"):
|
|
require_reference_source(source, agent_run_id="another-run")
|
|
|
|
|
|
@pytest.mark.parametrize("mutation", [
|
|
lambda value: value.pop("source_url_ref"),
|
|
lambda value: value.update(environment_id="other"),
|
|
lambda value: value.update(resolved_native_filters=[{"filter_id": "x"}]),
|
|
lambda value: value.update(query_model_fingerprint="sha256:" + "d" * 64),
|
|
])
|
|
def test_missing_or_changed_reference_is_rejected(mutation):
|
|
source = deepcopy(_source())
|
|
mutation(source)
|
|
with pytest.raises(ValueError, match="REFERENCE_SOURCE"):
|
|
require_reference_source(source, environment_id="preprod", dashboard_id=42)
|
|
|
|
|
|
# @TEST_INVARIANT [BaselineEngine.ReferenceSource] Current revision DTO requires exact reference provenance; file envelopes cannot supply it.
|
|
def test_current_revision_requires_frozen_reference():
|
|
from src.schemas.dashboard_testing.catalog_revision import CatalogEntryRevision
|
|
from tests.services.dashboard_testing.execution.pg_consumer_fixture import literal_wrapper
|
|
|
|
payload = literal_wrapper()
|
|
wrapper = CatalogEntryRevision.model_validate_json(json.dumps(payload))
|
|
assert wrapper.reference_source.dashboard_id == 2
|
|
del payload["reference_source"]
|
|
with pytest.raises(ValueError):
|
|
CatalogEntryRevision.model_validate_json(json.dumps(payload))
|
|
|
|
# #endregion Test.ReferenceSource
|
|
|
|
|
|
# #region Test.ReferenceSource.VerifiedUrl [C:3] [TYPE Function]
|
|
# @TEST_INVARIANT [BaselineEngine.ReferenceSource.LoadUrl] The exact bounded stored URL is returned only under its current run/hash/origin/dashboard identity.
|
|
def test_verified_url_preserves_exact_protected_query(monkeypatch):
|
|
from src.services.agent_runs import artifacts
|
|
|
|
url = "https://superset.example/superset/dashboard/42/?native_filters_key=protected-key"
|
|
data = url.encode()
|
|
storage = SimpleNamespace(retrieve_current=Mock(return_value=data))
|
|
monkeypatch.setattr(artifacts, "get_draft_storage", lambda: storage)
|
|
source = build_reference_source(
|
|
url=url, url_kind="native_filters_key", source_url_ref="draft:run:" + sha256(data).hexdigest(),
|
|
environment_id="preprod", dashboard_id=42,
|
|
filters={"filters": [], "filters_hash": "sha256:4f53cda18c2baa0c0354bb5f9a3ecbe5ed12ab4d8e11ba873c2f11161202b945"},
|
|
query_model_fingerprint="sha256:" + "c" * 64,
|
|
)
|
|
|
|
exact = source_owner.load_verified_reference_url(
|
|
source, environment_url="https://superset.example", environment_id="preprod",
|
|
dashboard_id=42, agent_run_id="run",
|
|
)
|
|
|
|
assert exact == url
|
|
storage.retrieve_current.assert_called_once_with(source["source_url_ref"], max_bytes=8192)
|
|
# #endregion Test.ReferenceSource.VerifiedUrl
|
|
|
|
|
|
# #region Test.ReferenceSource.VerifiedUrlRefusal [C:3] [TYPE Function]
|
|
# @TEST_INVARIANT [BaselineEngine.ReferenceSource.LoadUrl] Missing, altered, foreign-origin, and foreign-dashboard URLs refuse before navigation.
|
|
@pytest.mark.parametrize("case", ["missing", "hash", "origin", "dashboard"])
|
|
def test_verified_url_refuses_missing_or_foreign_source_before_navigation(monkeypatch, case):
|
|
from src.services.agent_runs import artifacts
|
|
|
|
url = "https://superset.example/superset/dashboard/42/"
|
|
if case == "origin":
|
|
url = "https://foreign.example/superset/dashboard/42/"
|
|
if case == "dashboard":
|
|
url = "https://superset.example/superset/dashboard/43/"
|
|
data = url.encode()
|
|
stored = None if case == "missing" else data
|
|
monkeypatch.setattr(artifacts, "get_draft_storage", lambda: SimpleNamespace(
|
|
retrieve_current=Mock(return_value=stored)))
|
|
source = build_reference_source(
|
|
url=url, url_kind="dashboard", source_url_ref="draft:run:" + "1" * 64,
|
|
environment_id="preprod", dashboard_id=42,
|
|
filters={"filters": [], "filters_hash": "sha256:4f53cda18c2baa0c0354bb5f9a3ecbe5ed12ab4d8e11ba873c2f11161202b945"},
|
|
query_model_fingerprint="sha256:" + "c" * 64,
|
|
)
|
|
if case == "hash":
|
|
source["source_url_sha256"] = "0" * 64
|
|
|
|
with pytest.raises(ValueError, match="REFERENCE_URL_(UNAVAILABLE|SCOPE_MISMATCH)"):
|
|
source_owner.load_verified_reference_url(
|
|
source, environment_url="https://superset.example", environment_id="preprod",
|
|
dashboard_id=42, agent_run_id="run",
|
|
)
|
|
# #endregion Test.ReferenceSource.VerifiedUrlRefusal
|
|
|
|
|
|
# #region Test.ReferenceSource.LoadPinnedUrl [C:3] [TYPE Function]
|
|
# @TEST_INVARIANT The run pin supplies the canonical draft owner and digest; the exact source URL is loaded only after both agree.
|
|
def test_pinned_url_uses_canonical_draft_owner_and_matching_content_digest(monkeypatch):
|
|
from src.services.agent_runs import artifacts
|
|
|
|
owner_id = "capture-owner-not-a-uuid"
|
|
url = "https://superset.example/superset/dashboard/42/?native_filters_key=protected"
|
|
url_sha256 = sha256(url.encode()).hexdigest()
|
|
source = build_reference_source(
|
|
url=url, url_kind="native_filters_key", source_url_ref=f"draft:{owner_id}:{url_sha256}",
|
|
environment_id="preprod", dashboard_id=42,
|
|
filters={"filters": [], "filters_hash": "sha256:4f53cda18c2baa0c0354bb5f9a3ecbe5ed12ab4d8e11ba873c2f11161202b945"},
|
|
query_model_fingerprint="sha256:" + "c" * 64,
|
|
)
|
|
retrieve = Mock(return_value=url.encode())
|
|
monkeypatch.setattr(artifacts, "get_draft_storage", lambda: SimpleNamespace(retrieve_current=retrieve))
|
|
|
|
assert source_owner.load_pinned_reference_url(
|
|
source, environment_url="https://superset.example", environment_id="preprod", dashboard_id=42,
|
|
) == url
|
|
retrieve.assert_called_once_with(source["source_url_ref"], max_bytes=8192)
|
|
|
|
|
|
@pytest.mark.parametrize("reference", [
|
|
"draft:owner:" + "a" * 63,
|
|
"draft:owner:" + "a" * 64,
|
|
"draft:owner:" + "a" * 64 + ":extra",
|
|
"draft::" + "a" * 64,
|
|
])
|
|
def test_pinned_url_rejects_noncanonical_owner_reference_before_storage_read(monkeypatch, reference):
|
|
from src.services.agent_runs import artifacts
|
|
|
|
url = "https://superset.example/superset/dashboard/42/"
|
|
data = url.encode()
|
|
source = build_reference_source(
|
|
url=url, url_kind="dashboard", source_url_ref="draft:owner:" + sha256(data).hexdigest(),
|
|
environment_id="preprod", dashboard_id=42,
|
|
filters={"filters": [], "filters_hash": "sha256:4f53cda18c2baa0c0354bb5f9a3ecbe5ed12ab4d8e11ba873c2f11161202b945"},
|
|
query_model_fingerprint="sha256:" + "c" * 64,
|
|
)
|
|
source["source_url_ref"] = reference
|
|
retrieve = Mock(return_value=data)
|
|
monkeypatch.setattr(artifacts, "get_draft_storage", lambda: SimpleNamespace(retrieve_current=retrieve))
|
|
|
|
with pytest.raises(ValueError, match="REFERENCE_SOURCE_INVALID"):
|
|
source_owner.load_pinned_reference_url(
|
|
source, environment_url="https://superset.example", environment_id="preprod", dashboard_id=42,
|
|
)
|
|
retrieve.assert_not_called()
|
|
# #endregion Test.ReferenceSource.LoadPinnedUrl
|
|
|
|
|
|
# #region Test.ReferenceSource.SamePageNavigation [C:3] [TYPE Function]
|
|
# @TEST_INVARIANT [BaselineEngine.VisualCaptureRuntime.ReferenceSource] The exact source URL is loaded before any page navigation; the unchanged session is not used as a source of defaults.
|
|
@pytest.mark.asyncio
|
|
async def test_visual_runtime_navigates_same_attested_page_to_exact_source(monkeypatch):
|
|
from src.services.agent_runs import artifacts
|
|
from src.services.dashboard_testing.visual_capture_runtime import load_reference_source
|
|
|
|
url = "https://superset.example/superset/dashboard/42/?native_filters_key=protected-key"
|
|
data = url.encode()
|
|
source = build_reference_source(
|
|
url=url, url_kind="native_filters_key", source_url_ref="draft:run:" + sha256(data).hexdigest(),
|
|
environment_id="preprod", dashboard_id=42,
|
|
filters={"filters": [], "filters_hash": "sha256:4f53cda18c2baa0c0354bb5f9a3ecbe5ed12ab4d8e11ba873c2f11161202b945"},
|
|
query_model_fingerprint="sha256:" + "c" * 64,
|
|
)
|
|
monkeypatch.setattr(artifacts, "get_draft_storage", lambda: SimpleNamespace(
|
|
retrieve_current=Mock(return_value=data)))
|
|
|
|
class Page:
|
|
url = "https://superset.example/superset/dashboard/42/?standalone=true"
|
|
|
|
async def goto(self, target, **kwargs):
|
|
self.got = (target, kwargs)
|
|
self.url = target
|
|
|
|
page = Page()
|
|
context = object()
|
|
session = SimpleNamespace(page=page, context=context)
|
|
service = SimpleNamespace(env=SimpleNamespace(url="https://superset.example"))
|
|
run = SimpleNamespace(id="run", context_snapshot={"reference_source": source})
|
|
binding = SimpleNamespace(binding_id="binding", environment_id="preprod", dashboard_id=42,
|
|
reference_digest=source["reference_digest"])
|
|
refuse = Mock(side_effect=AssertionError("unexpected refusal"))
|
|
|
|
exact = load_reference_source(service, run, binding, refuse)
|
|
|
|
assert exact == url
|
|
assert not hasattr(page, "got")
|
|
assert session.page is page and session.context is context
|
|
refuse.assert_not_called()
|
|
# #endregion Test.ReferenceSource.SamePageNavigation
|
|
|
|
|
|
# #region Test.ReferenceSource.NavigationRefusal [C:3] [TYPE Function]
|
|
# @TEST_INVARIANT [BaselineEngine.VisualCaptureRuntime.ReferenceSource] Missing, altered, foreign-origin, or foreign-dashboard source refuses before navigation/capability/query.
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("case", ["missing", "hash", "origin", "dashboard"])
|
|
async def test_visual_runtime_refuses_invalid_source_before_native_io(monkeypatch, case):
|
|
from src.services.agent_runs import artifacts
|
|
from src.services.dashboard_testing.visual_capture_runtime import load_reference_source
|
|
|
|
url = "https://superset.example/superset/dashboard/42/"
|
|
if case == "origin":
|
|
url = "https://foreign.example/superset/dashboard/42/"
|
|
if case == "dashboard":
|
|
url = "https://superset.example/superset/dashboard/43/"
|
|
data = url.encode()
|
|
stored = None if case == "missing" else data
|
|
monkeypatch.setattr(artifacts, "get_draft_storage", lambda: SimpleNamespace(
|
|
retrieve_current=Mock(return_value=stored)))
|
|
source = build_reference_source(
|
|
url=url, url_kind="dashboard", source_url_ref="draft:run:" + "1" * 64,
|
|
environment_id="preprod", dashboard_id=42,
|
|
filters={"filters": [], "filters_hash": "sha256:4f53cda18c2baa0c0354bb5f9a3ecbe5ed12ab4d8e11ba873c2f11161202b945"},
|
|
query_model_fingerprint="sha256:" + "c" * 64,
|
|
)
|
|
if case == "hash":
|
|
source["source_url_sha256"] = "0" * 64
|
|
page = SimpleNamespace(url="https://superset.example/superset/dashboard/42/", goto=AsyncMock())
|
|
session = SimpleNamespace(page=page, context=object())
|
|
service = SimpleNamespace(env=SimpleNamespace(url="https://superset.example"))
|
|
run = SimpleNamespace(id="run", context_snapshot={"reference_source": source})
|
|
binding = SimpleNamespace(binding_id="binding", environment_id="preprod", dashboard_id=42,
|
|
reference_digest=source["reference_digest"])
|
|
|
|
def refuse(code, _run_id):
|
|
raise ValueError("VISUAL_CAPTURE_REFERENCE_SOURCE_UNAVAILABLE")
|
|
|
|
with pytest.raises(ValueError, match="VISUAL_CAPTURE_REFERENCE_SOURCE_UNAVAILABLE"):
|
|
load_reference_source(service, run, binding, refuse)
|
|
page.goto.assert_not_called()
|
|
# #endregion Test.ReferenceSource.NavigationRefusal
|
|
|
|
|
|
# #region Test.ReferenceSource.BindingRefusal [C:2] [TYPE Function]
|
|
# @TEST_INVARIANT [BaselineEngine.VisualCaptureRuntime.ReferenceSource] Source digest divergence refuses before storage read, navigation, capability, or query.
|
|
@pytest.mark.asyncio
|
|
async def test_visual_runtime_refuses_source_digest_divergence_before_navigation():
|
|
from src.services.dashboard_testing.visual_capture_runtime import load_reference_source
|
|
|
|
page = SimpleNamespace(url="https://superset.example/superset/dashboard/42/", goto=AsyncMock())
|
|
session = SimpleNamespace(page=page, context=object())
|
|
service = SimpleNamespace(env=SimpleNamespace(url="https://superset.example"))
|
|
run = SimpleNamespace(id="run", context_snapshot={"reference_source": {"reference_digest": "a" * 64}})
|
|
binding = SimpleNamespace(binding_id="binding", environment_id="preprod", dashboard_id=42,
|
|
reference_digest="b" * 64)
|
|
|
|
def refuse(code, _run_id):
|
|
raise ValueError(code)
|
|
|
|
with pytest.raises(ValueError, match="visual_capture_reference_source_unavailable"):
|
|
load_reference_source(service, run, binding, refuse)
|
|
page.goto.assert_not_called()
|
|
# #endregion Test.ReferenceSource.BindingRefusal
|