"""Disk-state round-trip invariants.
(1) Anon namespace `api_config.json` MUST omit `owner_id` (byte-equal with
pre-auth output — the backward-compat lockfile relied on by every
upgrade scenario).
(2) Auth-required namespace `api_config.json` MUST include `owner_id` for
every entry registered with a caller.
(3) Revoking a key removes its hash from the in-memory by-hash index so a
subsequent authenticate() lookup is fast-fail (defense in depth).
"""
from __future__ import annotations
import json
def test_anon_namespace_api_config_has_no_owner_id(
auth_initialized_app, anon_dataset, agent_card
):
"""Register on an anon namespace from auth-initialized server.
Persisted api_config.json must NOT contain owner_id."""
client, _ = auth_initialized_app
card = agent_card("anon-roundtrip")
r = client.post(
f"/api/datasets/{anon_dataset}/services/a2a",
json={"agent_card": card, "dataset": anon_dataset, "persistent": True},
)
assert r.status_code == 200, r.text
from a2x_registry.common.paths import database_dir
api_file = database_dir() / anon_dataset / "api_config.json"
payload = json.loads(api_file.read_text(encoding="utf-8"))
for svc in payload.get("services", []):
assert "owner_id" not in svc, (
f"anon namespace api_config.json must not contain owner_id; "
f"found in entry {svc.get('service_id')!r}"
)
def test_auth_required_namespace_api_config_records_owner(
auth_initialized_app, auth_dataset, provider_headers, agent_card
):
"""Register on an auth-required namespace; api_config.json MUST persist
owner_id (the provider's principal_id)."""
client, _ = auth_initialized_app
card = agent_card("auth-roundtrip")
r = client.post(
f"/api/datasets/{auth_dataset}/services/a2a",
json={"agent_card": card, "dataset": auth_dataset, "persistent": True},
headers=provider_headers,
)
assert r.status_code == 200, r.text
sid = r.json()["service_id"]
me = client.get("/api/auth/whoami", headers=provider_headers).json()
expected_owner = me["principal_id"]
from a2x_registry.common.paths import database_dir
api_file = database_dir() / auth_dataset / "api_config.json"
payload = json.loads(api_file.read_text(encoding="utf-8"))
entries = {s["service_id"]: s for s in payload.get("services", [])}
assert sid in entries, f"sid {sid!r} not in api_config.json"
assert entries[sid]["owner_id"] == expected_owner
def test_revoked_key_removed_from_index(auth_initialized_app, provider_headers, admin_headers):
"""After revoke, the in-memory by-hash index drops the hash — so
authenticate() misses fast even before the JSON re-read on next restart."""
client, _ = auth_initialized_app
new = client.post("/api/auth/keys", json={"name": "tombstone"}, headers=provider_headers).json()
new_h = {"Authorization": f"Bearer {new['token']}"}
assert client.get("/api/auth/whoami", headers=new_h).status_code == 200
client.delete(f"/api/auth/keys/{new['key_id']}", headers=admin_headers)
from a2x_registry.auth.deps import get_auth_store
from a2x_registry.auth.tokens import hash_token
store = get_auth_store()
h = hash_token(new["token"])
assert h not in store._keys_by_hash, (
"revoked key's hash must be removed from the by-hash index"
)
assert client.get("/api/auth/whoami", headers=new_h).status_code == 401
def test_principals_and_keys_files_survive_restart(
auth_initialized_app, admin_headers, auth_dataset
):
"""A fresh AuthStore.load_or_none() built from the same data_dir
must round-trip every principal + key exactly."""
client, _ = auth_initialized_app
p = client.post(
"/api/auth/principals",
json={"handle": "restart_target", "role": "user", "namespaces": [auth_dataset]},
headers=admin_headers,
).json()
target_token = p["token"]
target_headers = {"Authorization": f"Bearer {target_token}"}
from a2x_registry.auth.deps import get_auth_store
from a2x_registry.auth.store import AuthStore
data_dir = get_auth_store().data_dir
fresh = AuthStore.load_or_none(data_dir=data_dir)
assert fresh is not None
p2 = fresh.get_principal(p["principal_id"])
assert p2 is not None
assert p2.handle == "restart_target"
ctx = fresh.authenticate(target_token)
assert ctx.principal_id == p["principal_id"]