ariadne/tests/test_mailer.py

52 lines
1.4 KiB
Python

from __future__ import annotations
import types
import pytest
from ariadne.services.mailer import Mailer, MailerError
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"