from __future__ import annotations import types import pytest from ariadne.services.mailer import Mailer, MailerError class DummySMTP: def __init__(self, host=None, port=None, timeout=None): self.calls = [] self.message = None def __enter__(self): return self def __exit__(self, exc_type, exc, tb): return False def ehlo(self): self.calls.append("ehlo") def starttls(self): self.calls.append("starttls") def login(self, username, password): self.calls.append(("login", username, password)) def send_message(self, message): self.message = message def test_mailer_requires_host(monkeypatch) -> None: dummy = types.SimpleNamespace( smtp_host="", smtp_port=25, smtp_username="", smtp_password="", smtp_from="test@bstein.dev", smtp_starttls=False, smtp_use_tls=False, smtp_timeout_sec=5.0, ) monkeypatch.setattr("ariadne.services.mailer.settings", dummy) svc = Mailer() with pytest.raises(MailerError): svc.send("subject", ["a@bstein.dev"], "body") def test_send_welcome_calls_send(monkeypatch) -> None: dummy = types.SimpleNamespace( smtp_host="smtp", smtp_port=25, smtp_username="", smtp_password="", smtp_from="test@bstein.dev", smtp_starttls=False, smtp_use_tls=False, smtp_timeout_sec=5.0, ) monkeypatch.setattr("ariadne.services.mailer.settings", dummy) svc = Mailer() called = {} def _send(subject, to_addrs, text_body, html_body=None): called["subject"] = subject called["to"] = to_addrs return types.SimpleNamespace(ok=True, detail="sent") monkeypatch.setattr(svc, "send", _send) svc.send_welcome("user@bstein.dev", "CODE", "https://bstein.dev/onboarding?code=CODE", username="user") assert called["subject"] == "Welcome to Titan Lab" def test_mailer_send_uses_starttls(monkeypatch) -> None: dummy = types.SimpleNamespace( smtp_host="smtp", smtp_port=25, smtp_username="user", smtp_password="pass", smtp_from="test@bstein.dev", smtp_starttls=True, smtp_use_tls=False, smtp_timeout_sec=5.0, ) monkeypatch.setattr("ariadne.services.mailer.settings", dummy) monkeypatch.setattr("ariadne.services.mailer.smtplib.SMTP", DummySMTP) svc = Mailer() result = svc.send("subject", ["a@bstein.dev"], "body", html_body="
hi
") assert result.ok is True def test_mailer_send_uses_tls(monkeypatch) -> None: dummy = types.SimpleNamespace( smtp_host="smtp", smtp_port=465, smtp_username="user", smtp_password="pass", smtp_from="test@bstein.dev", smtp_starttls=False, smtp_use_tls=True, smtp_timeout_sec=5.0, ) monkeypatch.setattr("ariadne.services.mailer.settings", dummy) monkeypatch.setattr("ariadne.services.mailer.smtplib.SMTP_SSL", DummySMTP) svc = Mailer() result = svc.send("subject", ["a@bstein.dev"], "body") assert result.ok is True def test_mailer_send_raises_error(monkeypatch) -> None: dummy = types.SimpleNamespace( smtp_host="smtp", smtp_port=25, smtp_username="user", smtp_password="pass", smtp_from="test@bstein.dev", smtp_starttls=False, smtp_use_tls=False, smtp_timeout_sec=5.0, ) monkeypatch.setattr("ariadne.services.mailer.settings", dummy) class BrokenSMTP(DummySMTP): def send_message(self, message): raise RuntimeError("boom") monkeypatch.setattr("ariadne.services.mailer.smtplib.SMTP", BrokenSMTP) svc = Mailer() with pytest.raises(MailerError): svc.send("subject", ["a@bstein.dev"], "body")