Skip to content

Commit b95959e

Browse files
committed
integration test for emails [chapter_13_dependency_injection_ends]
1 parent cfce836 commit b95959e

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

tests/integration/test_email.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#pylint: disable=redefined-outer-name
2+
import pytest
3+
import requests
4+
from sqlalchemy.orm import clear_mappers
5+
from allocation import bootstrap, config
6+
from allocation.domain import commands
7+
from allocation.adapters import notifications
8+
from allocation.service_layer import unit_of_work
9+
from ..random_refs import random_sku
10+
11+
12+
@pytest.fixture
13+
def bus(sqlite_session_factory):
14+
bus = bootstrap.bootstrap(
15+
start_orm=True,
16+
uow=unit_of_work.SqlAlchemyUnitOfWork(sqlite_session_factory),
17+
notifications=notifications.EmailNotifications(),
18+
publish=lambda *args: None,
19+
)
20+
yield bus
21+
clear_mappers()
22+
23+
24+
def get_email_from_mailhog(sku):
25+
host, port = map(config.get_email_host_and_port().get, ['host', 'http_port'])
26+
all_emails = requests.get(f'http://{host}:{port}/api/v2/messages').json()
27+
return next(m for m in all_emails['items'] if sku in str(m))
28+
29+
30+
def test_out_of_stock_email(bus):
31+
sku = random_sku()
32+
bus.handle(commands.CreateBatch('batch1', sku, 9, None))
33+
bus.handle(commands.Allocate('order1', sku, 10))
34+
email = get_email_from_mailhog(sku)
35+
assert email['Raw']['From'] == '[email protected]'
36+
assert email['Raw']['To'] == ['[email protected]']
37+
assert f'Out of stock for {sku}' in email['Raw']['Data']

0 commit comments

Comments
 (0)