|
| 1 | +#pylint: disable=redefined-outer-name |
| 2 | +import uuid |
| 3 | +import pytest |
| 4 | +import requests |
| 5 | +from allocation import config |
| 6 | +from allocation.domain import commands |
| 7 | +from allocation.adapters import notifications |
| 8 | +from allocation.service_layer import messagebus, unit_of_work |
| 9 | + |
| 10 | +email_config = config.get_email_host_and_port() |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def bus(sqlite_session_factory): |
| 14 | + uow = unit_of_work.SqlAlchemyUnitOfWork(sqlite_session_factory) |
| 15 | + bus = messagebus.MessageBus( |
| 16 | + uow=uow, |
| 17 | + notifications=notifications.EmailNotifications( |
| 18 | + smtp_host=email_config['host'], |
| 19 | + port=email_config['port'], |
| 20 | + ), |
| 21 | + publish=lambda *_, **__: None |
| 22 | + ) |
| 23 | + uow.bus = bus |
| 24 | + return bus |
| 25 | + |
| 26 | + |
| 27 | +def random_sku(): |
| 28 | + return uuid.uuid4().hex[:6] |
| 29 | + |
| 30 | + |
| 31 | +def test_out_of_stock_email(bus): |
| 32 | + sku = random_sku() |
| 33 | + bus.handle(commands.CreateBatch('batch1', sku, 9, None)) |
| 34 | + bus.handle(commands.Allocate('order1', sku, 10)) |
| 35 | + messages = requests.get( |
| 36 | + f'http://{email_config["host"]}:{email_config["http_port"]}/api/v2/messages' |
| 37 | + ).json() |
| 38 | + message = next( |
| 39 | + m for m in messages['items'] |
| 40 | + if sku in str(m) |
| 41 | + ) |
| 42 | + assert message['Raw']['From'] == 'allocations@example.com' |
| 43 | + assert message['Raw']['To'] == ['stock@made.com'] |
| 44 | + assert f'Out of stock for {sku}' in message['Raw']['Data'] |
0 commit comments