"""The store of what agents delivered, and the signed links to it (ADR-0004).""" from __future__ import annotations import pytest from orchestration.links import Signer from orchestration.store import Store FILES = { "guide.md": "# Guide", "graph.json": '{"elements": []}', "terraform/main.tf": 'resource "x" "y" {}', } def kept(store: Store, execution_id: str = "a1", **overrides: object) -> dict: values: dict = { "tenant": "t1", "case": "acme", "execution_id": execution_id, "work": "architecture", "status": "completed", "request": "A landing zone", "files": FILES, "guide": "guide.md", "project_id": "p1", } values.update(overrides) return store.keep(**values) def test_an_execution_is_kept_with_its_record(tmp_path): store = Store(tmp_path) record = kept(store) assert record["work"] == "architecture" assert record["status"] == "completed" assert record["customer_case"] == "acme" assert record["project_id"] == "p1" assert record["files"] == sorted(FILES) assert store.record("t1", "acme", "a1") == record def test_a_file_comes_back_with_its_media_type(tmp_path): store = Store(tmp_path) kept(store) assert store.file("t1", "acme", "a1", "guide.md") == ( b"# Guide", "text/markdown; charset=utf-8", ) assert store.file("t1", "acme", "a1", "graph.json")[1] == "application/json" assert ( store.file("t1", "acme", "a1", "terraform/main.tf")[0] == FILES["terraform/main.tf"].encode() ) assert store.file("t1", "acme", "a1", "missing.md") is None def test_what_is_kept_is_never_changed(tmp_path): store = Store(tmp_path) kept(store) with pytest.raises(ValueError, match="a1"): kept(store, files={"guide.md": "# Another"}) @pytest.mark.parametrize( "name", ["../escape.md", "/etc/passwd", "a/../../b", "a\\b", ""] ) def test_a_file_name_outside_the_execution_is_refused(tmp_path, name): store = Store(tmp_path) with pytest.raises(ValueError): kept(store, files={name: "x"}) assert store.file("t1", "acme", "a1", name) is None @pytest.mark.parametrize("slug", ["..", "a/b", "", "a b"]) def test_a_tenant_or_case_that_is_not_a_slug_is_refused(tmp_path, slug): with pytest.raises(ValueError): kept(Store(tmp_path), case=slug) def test_latest_is_the_newest_completed_execution_of_the_work(tmp_path): store = Store(tmp_path) kept(store, "a1") kept(store, "a2") kept(store, "a3", status="completed_with_warnings") kept(store, "o1", work="operations") assert store.latest("t1", "acme", "architecture")["execution_id"] == "a2" assert store.latest("t1", "acme", "nothing") is None assert [r["execution_id"] for r in store.case("t1", "acme")] == [ "o1", "a3", "a2", "a1", ] def test_a_signed_path_is_accepted_and_a_changed_one_is_not(): signer = Signer("secret", ttl_seconds=60, now=lambda: 1000) query = signer.sign("/tenants/t1/cases/acme") assert signer.verify("/tenants/t1/cases/acme", **query) assert not signer.verify("/tenants/t1/cases/other", **query) assert not signer.verify("/tenants/t1/cases/acme", exp=query["exp"], sig="0" * 64) assert not Signer("other", ttl_seconds=60, now=lambda: 1000).verify( "/tenants/t1/cases/acme", **query ) def test_a_link_expires_unless_its_lifetime_is_unlimited(): clock = {"now": 1000} expiring = Signer("secret", ttl_seconds=60, now=lambda: clock["now"]) forever = Signer("secret", ttl_seconds=0, now=lambda: clock["now"]) short, lasting = expiring.sign("/x"), forever.sign("/x") clock["now"] = 1061 assert not expiring.verify("/x", **short) assert forever.verify("/x", **lasting)