Files

68 lines
4.4 KiB
Python
Raw Permalink Normal View History

2026-10-06 14:47:01 -06:00
import tempfile
import unittest
from datetime import UTC, datetime
from pathlib import Path
from scheduling_core.ledger import Ledger
from scheduling_core.policy import PolicyConfig, evaluate_intent
from scheduling_core.postmark import BoundMessage, PostmarkAdapter
from scripts.verify_postmark_readonly import build_readonly_request
class CapturingClient:
def __init__(self): self.calls = []
def send(self, **kwargs): self.calls.append(kwargs); return "postmark-id"
class PostmarkExecutionTests(unittest.TestCase):
def test_owner_delivery_test_is_bound_to_one_sender_recipient_and_exchange(self):
with tempfile.TemporaryDirectory() as directory:
ledger = Ledger(Path(directory) / "ledger.sqlite3")
transaction_id = ledger.create_authorized_owner_delivery_test(
transaction_id="authorized-owner-delivery-test-unit-v1",
sender="Petra - Scheduling <[email protected]>",
recipient="[email protected]",
expires_at=datetime(2031, 1, 1, tzinfo=UTC),
)
lease = ledger.claim(transaction_id, 60, datetime(2030, 12, 31, tzinfo=UTC)).lease_token
first = ledger.record_postmark_send(transaction_id, lease, "test-send", "Petra - Scheduling <[email protected]>", "[email protected]", now=datetime(2030, 12, 31, tzinfo=UTC))
self.assertEqual(first["state"], "pending")
with self.assertRaises(ValueError):
ledger.record_postmark_send(transaction_id, lease, "second-send", "Petra - Scheduling <[email protected]>", "[email protected]", now=datetime(2030, 12, 31, tzinfo=UTC))
def test_owner_reply_evidence_is_digest_only_and_not_a_transaction(self):
with tempfile.TemporaryDirectory() as directory:
ledger = Ledger(Path(directory) / "ledger.sqlite3")
ledger.record_owner_transport_evidence(
"authorized-owner-delivery-test-gmail-reply-v1",
{"message_id": "m", "thread_id": "t", "sender": "[email protected]", "recipient": "[email protected]", "observed_at": "2030-01-01T00:00:00Z"},
)
with ledger._session() as connection:
evidence = connection.execute("SELECT evidence_digest FROM test_evidence").fetchone()
transactions = connection.execute("SELECT COUNT(*) AS count FROM transactions").fetchone()
self.assertIsNotNone(evidence["evidence_digest"])
self.assertEqual(transactions["count"], 0)
def test_readonly_request_uses_the_documented_server_token_contract(self):
request = build_readonly_request("/server", "dummy-token")
self.assertEqual(request.full_url, "https://api.postmarkapp.com/server")
self.assertEqual(request.get_header("Accept"), "application/json")
self.assertEqual(request.get_header("X-postmark-server-token"), "dummy-token")
def test_bound_execution_forces_message_stream_contract(self):
with tempfile.TemporaryDirectory() as directory:
ledger = Ledger(Path(directory) / "ledger.sqlite3")
config = PolicyConfig(revision="test", fixed_sender="[email protected]")
now = datetime(2030, 12, 31, 16, tzinfo=UTC)
intent = evaluate_intent({"transaction_id":"tx","requester":"[email protected]","sender":"[email protected]","start_at":"2031-01-01T18:00:00Z","end_at":"2031-01-01T19:00:00Z","expires_at":"2031-01-02T00:00:00Z","authentication_evidence_id":"a","availability_evidence_id":"b"}, config, now, [])
ledger.create_transaction(intent, config.revision, config.max_exchange_messages)
client = CapturingClient()
lease = ledger.claim("tx", 60).lease_token
result = PostmarkAdapter(ledger, client).send_bound_message("tx", lease, "send", "[email protected]", "[email protected]", BoundMessage("Offer", "Select a slot."))
self.assertEqual(result, "postmark-id")
self.assertEqual(client.calls[0]["reply_to"], "[email protected]")
self.assertEqual(client.calls[0]["recipient"], "[email protected]")
self.assertRegex(client.calls[0]["rfc_message_id"], r"^<sched-[0-9a-f-]+@example\.test>$")
operation = ledger.operation_for_key("send")
self.assertEqual(operation["provider_id"], "postmark-id")
self.assertEqual(operation["rfc_message_id"], client.calls[0]["rfc_message_id"])