"""Email in integration tests: every email is a queued row the outbox sends (#5506), so a test catches what the mail server would have been handed by replacing the one SMTP conversation, outbox.deliver_sync.""" from __future__ import annotations from email.message import EmailMessage def capture(app, monkeypatch, answer=None) -> list[EmailMessage]: """Set a mail server and collect every message the outbox delivers. `answer(msg)` is the server's reply to each (None: accepted).""" from steward.core import outbox sent: list[EmailMessage] = [] def deliver(cfg, messages): sent.extend(messages) return [answer(m) if answer else None for m in messages] app.config["SMTP"] = {**app.config.get("SMTP", {}), "host": "mx.example", "port": 587, "from": "steward@example.com"} monkeypatch.setattr(outbox, "deliver_sync", deliver) return sent def to(sent: list[EmailMessage], *addresses: str) -> list[EmailMessage]: """The messages among `sent` addressed to any of `addresses` (other tests' queued email can go out in the same tick).""" want = {a.lower() for a in addresses} return [m for m in sent if m["To"].lower() in want]