feat: add pneumatube gateway bridging smolmail and SMTP via LMTP
This commit is contained in:
commit
722394a7bf
16 changed files with 954 additions and 0 deletions
20
tests/test_email_to_smol.py
Normal file
20
tests/test_email_to_smol.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
from email.message import EmailMessage
|
||||
|
||||
from pneumatube.email_to_smol import EmailToSmolBridge, _single_line
|
||||
|
||||
|
||||
def test_single_line_strips_crlf():
|
||||
assert _single_line("alice@example.org\r\nAccept: forged") == "alice@example.org Accept: forged"
|
||||
|
||||
|
||||
def test_extract_text_plain_message():
|
||||
message = EmailMessage()
|
||||
message.set_content("hello there")
|
||||
assert EmailToSmolBridge.extract_text(message) == "hello there\n"
|
||||
|
||||
|
||||
def test_extract_text_multipart_prefers_plain_part():
|
||||
message = EmailMessage()
|
||||
message.set_content("plain body")
|
||||
message.add_alternative("<p>html body</p>", subtype="html")
|
||||
assert "plain body" in EmailToSmolBridge.extract_text(message)
|
||||
18
tests/test_smtp_client.py
Normal file
18
tests/test_smtp_client.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import pytest
|
||||
|
||||
from pneumatube.smtp_client import InvalidAddress, validate
|
||||
|
||||
|
||||
def test_validate_accepts_plain_address():
|
||||
assert validate(" bob@example.org ") == "bob@example.org"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("address", [
|
||||
"not-an-email",
|
||||
"bob@localhost",
|
||||
"bob@example.org\r\nBcc: evil@example.org",
|
||||
"bob@example.org\nX-Injected: 1",
|
||||
])
|
||||
def test_validate_rejects_bad_addresses(address):
|
||||
with pytest.raises(InvalidAddress):
|
||||
validate(address)
|
||||
60
tests/test_smtp_server.py
Normal file
60
tests/test_smtp_server.py
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import asyncio
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from pneumatube.smtp_server import GatewayHandler
|
||||
|
||||
|
||||
class _StubBridge:
|
||||
def __init__(self):
|
||||
self.forwarded = []
|
||||
|
||||
def forward(self, username, sender, subject, body):
|
||||
self.forwarded.append(username)
|
||||
if username == "broken":
|
||||
raise RuntimeError("smolmail server unreachable")
|
||||
|
||||
@staticmethod
|
||||
def extract_text(message):
|
||||
return message.get_payload(decode=True).decode("utf-8", "replace")
|
||||
|
||||
|
||||
class _InlineWorker:
|
||||
"""Runs synchronously: handle_DATA's per-recipient logic doesn't depend
|
||||
on real threading, just on worker.run's (fn, *args) -> result contract."""
|
||||
|
||||
async def run(self, fn, *args):
|
||||
return fn(*args)
|
||||
|
||||
|
||||
def _envelope(rcpt_tos):
|
||||
return SimpleNamespace(
|
||||
content=b"Subject: hi\r\n\r\nbody text\r\n",
|
||||
mail_from="sender@realmail.example",
|
||||
rcpt_tos=rcpt_tos,
|
||||
)
|
||||
|
||||
|
||||
def test_handle_data_replies_once_per_recipient():
|
||||
bridge = _StubBridge()
|
||||
handler = GatewayHandler(bridge, _InlineWorker(), config=SimpleNamespace())
|
||||
envelope = _envelope(["alice@gateway.example", "broken@gateway.example"])
|
||||
|
||||
reply = asyncio.run(handler.handle_DATA(None, None, envelope))
|
||||
lines = reply.split("\r\n")
|
||||
|
||||
assert len(lines) == 2
|
||||
assert lines[0].startswith("250")
|
||||
assert lines[1].startswith("451")
|
||||
assert bridge.forwarded == ["alice", "broken"]
|
||||
|
||||
|
||||
def test_handle_data_all_succeed():
|
||||
bridge = _StubBridge()
|
||||
handler = GatewayHandler(bridge, _InlineWorker(), config=SimpleNamespace())
|
||||
envelope = _envelope(["alice@gateway.example", "bob@gateway.example"])
|
||||
|
||||
reply = asyncio.run(handler.handle_DATA(None, None, envelope))
|
||||
|
||||
assert reply == "250 2.1.5 delivered\r\n250 2.1.5 delivered"
|
||||
Loading…
Add table
Add a link
Reference in a new issue