From 0d9f1cd7e0f597ab45f57459f6cc69a64e6f2311 Mon Sep 17 00:00:00 2001 From: dandds Date: Thu, 11 Oct 2018 10:59:07 -0400 Subject: [PATCH] update mailer, add tests --- atst/app.py | 2 +- atst/utils/mailer.py | 8 ++---- tests/utils/test_mailer.py | 54 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 tests/utils/test_mailer.py diff --git a/atst/app.py b/atst/app.py index c8795f61..36aca6d0 100644 --- a/atst/app.py +++ b/atst/app.py @@ -87,7 +87,7 @@ def map_config(config): return { **config["default"], "ENV": config["default"]["ENVIRONMENT"], - "DEBUG": config["default"]["DEBUG"], + "DEBUG": config["default"].getboolean("DEBUG"), "PORT": int(config["default"]["PORT"]), "SQLALCHEMY_DATABASE_URI": config["default"]["DATABASE_URI"], "SQLALCHEMY_TRACK_MODIFICATIONS": False, diff --git a/atst/utils/mailer.py b/atst/utils/mailer.py index 32918bfa..e3a6a094 100644 --- a/atst/utils/mailer.py +++ b/atst/utils/mailer.py @@ -26,20 +26,17 @@ class _HostConnection: class Mailer: - def __init__( - self, server, port, sender, username, password, use_tls=False, debug=False - ): + def __init__(self, server, port, sender, password, use_tls=False, debug=False): self.server = server self.port = port self.sender = sender - self.username = username self.password = password self.use_tls = use_tls self.debug = debug self.messages = deque(maxlen=50) def connection(self): - return _HostConnection(self.server, self.port, self.username, self.password) + return _HostConnection(self.server, self.port, self.sender, self.password) def _message(self, recipients, subject, body): msg = EmailMessage() @@ -64,7 +61,6 @@ def _map_config(config): "server": config.get("MAIL_SERVER"), "port": config.get("MAIL_PORT"), "sender": config.get("MAIL_SENDER"), - "username": config.get("MAIL_USERNAME"), "password": config.get("MAIL_PASSWORD"), "use_tls": config.get("MAIL_TLS", False), "debug": config.get("DEBUG", False), diff --git a/tests/utils/test_mailer.py b/tests/utils/test_mailer.py new file mode 100644 index 00000000..1a820b6b --- /dev/null +++ b/tests/utils/test_mailer.py @@ -0,0 +1,54 @@ +import pytest +from atst.utils.mailer import Mailer + + +class MockHost: + def __init__(self): + self.messages = [] + + def __enter__(self): + return self + + def __exit__(self, *args): + pass + + def send_message(self, message): + self.messages.append(message) + + +@pytest.fixture +def mail_host(): + return MockHost() + + +def test_can_send_mail(monkeypatch, mail_host): + monkeypatch.setattr("atst.utils.mailer.Mailer.connection", lambda *args: mail_host) + mailer = Mailer("localhost", 456, "leia@rebellion.net", "droidsyourelookingfor") + message_data = { + "recipients": ["ben@tattoine.org"], + "subject": "help", + "body": "you're my only hope", + } + mailer.send(**message_data) + assert len(mail_host.messages) == 1 + message = mail_host.messages[0] + assert message["To"] == message_data["recipients"][0] + assert message["Subject"] == message_data["subject"] + assert message.get_content().strip() == message_data["body"] + + +def test_can_save_messages(): + mailer = Mailer( + "localhost", 456, "leia@rebellion.net", "droidsyourelookingfor", debug=True + ) + message_data = { + "recipients": ["ben@tattoine.org"], + "subject": "help", + "body": "you're my only hope", + } + mailer.send(**message_data) + assert len(mailer.messages) == 1 + message = mailer.messages[0] + assert message["To"] == message_data["recipients"][0] + assert message["Subject"] == message_data["subject"] + assert message.get_content().strip() == message_data["body"]