qonto-assistant/tests/test_flex_auth_client.py
tegwick b9349782f4 feat(audit): publish sequenced heartbeat and reconciliation evidence
Assistant: codex
Assistant-Model: gpt-6-astra
Assistant-Session: 01a06ec5-7e2b-7743-ac08-719e1b0f42e2
2026-09-05 01:39:48 +02:00

101 lines
3.1 KiB
Python

import json
import httpx
import pytest
from qonto_assistant.flex_auth_client import CheckRequest, FlexAuthCheckClient, new_request_id
def _request() -> CheckRequest:
return CheckRequest(
request_id=new_request_id(),
tenant="tenant:friendly:binky",
subject_id="agent-harness-binky",
subject_type="agent",
)
def _client(handler) -> FlexAuthCheckClient:
return FlexAuthCheckClient(
base_url="https://flex-auth.example.test",
timeout_seconds=1,
transport=httpx.MockTransport(handler),
)
def test_allow_effect_authorizes() -> None:
def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(
200,
json={"id": "d-1", "effect": "allow", "resource": {}, "subject": {}, "provenance": {}},
)
assert _client(handler).is_allowed(_request()) is True
@pytest.mark.parametrize("effect", ["deny", "redact", "audit_only", "not_applicable"])
def test_non_allow_effects_deny(effect: str) -> None:
def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(
200,
json={"id": "d-1", "effect": effect, "resource": {}, "subject": {}, "provenance": {}},
)
assert _client(handler).is_allowed(_request()) is False
def test_non_200_status_denies() -> None:
def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(500, json={"error": "internal"})
assert _client(handler).is_allowed(_request()) is False
def test_malformed_json_body_denies() -> None:
def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(200, content=b"not json")
assert _client(handler).is_allowed(_request()) is False
def test_non_object_json_body_denies() -> None:
def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(200, json=["not", "an", "object"])
assert _client(handler).is_allowed(_request()) is False
def test_connection_failure_denies() -> None:
def handler(request: httpx.Request) -> httpx.Response:
raise httpx.ConnectError("connection refused", request=request)
assert _client(handler).is_allowed(_request()) is False
def test_timeout_denies() -> None:
def handler(request: httpx.Request) -> httpx.Response:
raise httpx.TimeoutException("timed out", request=request)
assert _client(handler).is_allowed(_request()) is False
def test_request_body_matches_schema_shape() -> None:
seen: dict[str, object] = {}
def handler(request: httpx.Request) -> httpx.Response:
seen.update(json.loads(request.content))
return httpx.Response(
200,
json={"id": "d-1", "effect": "allow", "resource": {}, "subject": {}, "provenance": {}},
)
_client(handler).is_allowed(_request())
assert seen["tenant"] == "tenant:friendly:binky"
assert seen["action"] == "finance.qonto.read"
assert seen["subject"] == {"id": "agent-harness-binky", "type": "agent"}
assert seen["resource"] == {
"id": "finance-snapshot",
"type": "finance-snapshot",
"system": "qonto-assistant",
}