def get_email_from_mailhog(sku): host, port = map(config.get_email_host_and_port().get, ['host', 'http_port']) all_emails = requests.get(f'http://{host}:{port}/api/v2/messages').json() return next(m for m in all_emails['items'] if sku in str(m))
# pylint: disable=too-few-public-methods import abc import smtplib from allocation import config class AbstractNotifications(abc.ABC): @abc.abstractmethod def send(self, destination, message): raise NotImplementedError DEFAULT_HOST = config.get_email_host_and_port()["host"] DEFAULT_PORT = config.get_email_host_and_port()["port"] class EmailNotifications(AbstractNotifications): def __init__(self, smtp_host=DEFAULT_HOST, port=DEFAULT_PORT): self.server = smtplib.SMTP(smtp_host, port=port) self.server.noop() def send(self, destination, message): msg = f"Subject: allocation service notification\n{message}" self.server.sendmail( from_addr="*****@*****.**", to_addrs=[destination], msg=msg, )
def get_email_from_mailhog(sku): host, port = map(config.get_email_host_and_port().get, ["host", "http_port"]) all_emails = requests.get(f"http://{host}:{port}/api/v2/messages").json() return next(m for m in all_emails["items"] if sku in str(m))
#pylint: disable=redefined-outer-name import uuid import pytest import requests from allocation import config, commands, messagebus, notifications, unit_of_work cfg = config.get_email_host_and_port() @pytest.fixture def bus(sqlite_session_factory): return messagebus.MessageBus( uow=unit_of_work.SqlAlchemyUnitOfWork(sqlite_session_factory), notifications=notifications.EmailNotifications( smtp_host=cfg['host'], port=cfg['port'], ), publish=lambda *_, **__: None) def random_sku(): return uuid.uuid4().hex[:6] def test_out_of_stock_email(bus): sku = random_sku() bus.handle([ commands.CreateBatch('batch1', sku, 9, None), commands.Allocate('order1', sku, 10), ]) messages = requests.get(
@license: (C) Copyright 2019-2020, Alfred Yuan Limited. @time: 2020/9/10 3:08 PM @desc: """ import abc import smtplib from allocation import config class AbstractNotifications(abc.ABC): @abc.abstractmethod def send(self, destination, message): raise NotImplementedError DEFAULT_HOST = config.get_email_host_and_port()['host'] DEFAULT_PORT = config.get_email_host_and_port()['port'] class EmailNotifications(AbstractNotifications): def __init__(self, smtp_host=DEFAULT_HOST, smtp_port=DEFAULT_PORT): self.server = smtplib.SMTP(smtp_host, port=smtp_port) self.server.noop() def send(self, destination, message): msg = f'Subject: allocation service notification\n{message}' self.server.sendmail(from_addr='*****@*****.**', to_addrs=[destination], msg=msg)