|
| 1 | +""" |
| 2 | +ESP-001 downstream non-call test. |
| 3 | +
|
| 4 | +This is a synthetic, in-memory pytest check. |
| 5 | +It proves the mock email adapter is not called when authority_token is missing. |
| 6 | +It does not prove live SMTP/API non-execution or enterprise readiness. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import importlib.util |
| 12 | +from pathlib import Path |
| 13 | +from typing import Any, Dict |
| 14 | + |
| 15 | + |
| 16 | +ROOT = Path(__file__).resolve().parents[1] |
| 17 | +ADAPTER_PATH = ROOT / "adapters" / "mock_email_adapter.py" |
| 18 | + |
| 19 | +spec = importlib.util.spec_from_file_location("mock_email_adapter", ADAPTER_PATH) |
| 20 | +mock_email_adapter = importlib.util.module_from_spec(spec) |
| 21 | +assert spec.loader is not None |
| 22 | +spec.loader.exec_module(mock_email_adapter) |
| 23 | +MockEmailAdapter = mock_email_adapter.MockEmailAdapter |
| 24 | + |
| 25 | + |
| 26 | +PAYLOAD_HASH = "sha256:a3f8c2d1e7b904561ac38fd2e190bba47c2d3f8a1e056b7c9d4e2f1a8b3c7d90" |
| 27 | + |
| 28 | + |
| 29 | +def evaluate_gate(request: Dict[str, Any]) -> Dict[str, Any]: |
| 30 | + required_fields = [ |
| 31 | + "actor", |
| 32 | + "action_type", |
| 33 | + "recipient_scope", |
| 34 | + "payload_hash", |
| 35 | + "authority_token", |
| 36 | + "expiry", |
| 37 | + "nonce", |
| 38 | + ] |
| 39 | + |
| 40 | + for field in required_fields: |
| 41 | + if not request.get(field): |
| 42 | + return { |
| 43 | + "decision": "DENY", |
| 44 | + "missing_field": field, |
| 45 | + "reason": f"{field} absent", |
| 46 | + } |
| 47 | + |
| 48 | + return {"decision": "ALLOW", "missing_field": None, "reason": "all required fields present"} |
| 49 | + |
| 50 | + |
| 51 | +def dispatch_if_allowed(request: Dict[str, Any], adapter: MockEmailAdapter) -> Dict[str, Any]: |
| 52 | + verdict = evaluate_gate(request) |
| 53 | + |
| 54 | + if verdict["decision"] != "ALLOW": |
| 55 | + return { |
| 56 | + "decision": verdict["decision"], |
| 57 | + "missing_field": verdict["missing_field"], |
| 58 | + "downstream_send": False, |
| 59 | + "send_call_count": adapter.send_call_count, |
| 60 | + "receipt_written": True, |
| 61 | + } |
| 62 | + |
| 63 | + adapter.send( |
| 64 | + recipient=request["recipient_scope"], |
| 65 | + payload_hash=request["payload_hash"], |
| 66 | + actor=request["actor"], |
| 67 | + ) |
| 68 | + return { |
| 69 | + "decision": "ALLOW", |
| 70 | + "missing_field": None, |
| 71 | + "downstream_send": True, |
| 72 | + "send_call_count": adapter.send_call_count, |
| 73 | + "receipt_written": False, |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | +def test_missing_authority_token_never_calls_mock_email_adapter() -> None: |
| 78 | + adapter = MockEmailAdapter() |
| 79 | + request = { |
| 80 | + "actor": "agent://morpheus-draft-bot-v1", |
| 81 | + "action_type": "SEND_EXTERNAL_EMAIL", |
| 82 | + "recipient_scope": "external:partner-domain.com", |
| 83 | + "payload_hash": PAYLOAD_HASH, |
| 84 | + "authority_token": None, |
| 85 | + "expiry": "2026-05-12T10:05:00Z", |
| 86 | + "nonce": "nonce-esp-001", |
| 87 | + } |
| 88 | + |
| 89 | + result = dispatch_if_allowed(request, adapter) |
| 90 | + |
| 91 | + assert result["decision"] == "DENY" |
| 92 | + assert result["missing_field"] == "authority_token" |
| 93 | + assert result["downstream_send"] is False |
| 94 | + assert result["send_call_count"] == 0 |
| 95 | + assert adapter.sent_messages == [] |
| 96 | + assert result["receipt_written"] is True |
| 97 | + |
| 98 | + |
| 99 | +def test_valid_authority_calls_mock_email_adapter_once() -> None: |
| 100 | + adapter = MockEmailAdapter() |
| 101 | + request = { |
| 102 | + "actor": "agent://morpheus-draft-bot-v1", |
| 103 | + "action_type": "SEND_EXTERNAL_EMAIL", |
| 104 | + "recipient_scope": "external:partner-domain.com", |
| 105 | + "payload_hash": PAYLOAD_HASH, |
| 106 | + "authority_token": "signed:test-authority-token", |
| 107 | + "expiry": "2026-05-12T10:05:00Z", |
| 108 | + "nonce": "nonce-esp-001-valid", |
| 109 | + } |
| 110 | + |
| 111 | + result = dispatch_if_allowed(request, adapter) |
| 112 | + |
| 113 | + assert result["decision"] == "ALLOW" |
| 114 | + assert result["downstream_send"] is True |
| 115 | + assert result["send_call_count"] == 1 |
| 116 | + assert len(adapter.sent_messages) == 1 |
0 commit comments