Skip to content

Commit 332b339

Browse files
committed
Make log_data required in mailer.send
1 parent 9a358bb commit 332b339

File tree

4 files changed

+42
-58
lines changed

4 files changed

+42
-58
lines changed

Diff for: h/services/email.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def __init__(self, request: Request, mailer: IMailer) -> None:
5757
self._request = request
5858
self._mailer = mailer
5959

60-
def send(self, email_data: EmailData, log_data: LogData | None = None) -> None:
60+
def send(self, email_data: EmailData, log_data: LogData) -> None:
6161
if self._request.debug: # pragma: no cover
6262
logger.info("emailing in debug mode: check the `mail/` directory")
6363
try:
@@ -70,14 +70,13 @@ def send(self, email_data: EmailData, log_data: LogData | None = None) -> None:
7070
except smtplib.SMTPException:
7171
raise
7272

73-
if log_data:
74-
logger.info(
75-
"Sent email: tag=%r, sender_id=%s, recipient_ids=%s, annotation_id=%r",
76-
log_data.tag,
77-
log_data.sender_id,
78-
log_data.recipient_ids,
79-
log_data.annotation_id,
80-
)
73+
logger.info(
74+
"Sent email: tag=%r, sender_id=%s, recipient_ids=%s, annotation_id=%r",
75+
log_data.tag,
76+
log_data.sender_id,
77+
log_data.recipient_ids,
78+
log_data.annotation_id,
79+
)
8180

8281

8382
def factory(_context, request: Request) -> EmailService:

Diff for: h/tasks/mailer.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
def send(
2525
self, # noqa: ARG001
2626
email_data: dict[str, Any],
27-
log_data: dict[str, Any] | None = None,
27+
log_data: dict[str, Any],
2828
) -> None:
2929
"""Send an email.
3030
3131
:param email_data: A dictionary containing email data compatible with EmailData class.
3232
"""
3333
service: EmailService = celery.request.find_service(EmailService)
34-
service.send(EmailData(**email_data), LogData(**log_data) if log_data else None)
34+
service.send(EmailData(**email_data), LogData(**log_data))

Diff for: tests/unit/h/services/email_test.py

+31-34
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@
77

88

99
class TestEmailService:
10-
def test_send_creates_email_message(self, email_service, pyramid_mailer):
11-
email = EmailData(
12-
recipients=["[email protected]"],
13-
subject="My email subject",
14-
body="Some text body",
15-
tag=EmailTag.TEST,
16-
)
17-
email_service.send(email)
10+
def test_send_creates_email_message(
11+
self, email_data, log_data, email_service, pyramid_mailer
12+
):
13+
email_service.send(email_data, log_data)
1814

1915
pyramid_mailer.message.Message.assert_called_once_with(
2016
recipients=["[email protected]"],
@@ -25,16 +21,16 @@ def test_send_creates_email_message(self, email_service, pyramid_mailer):
2521
)
2622

2723
def test_send_creates_email_message_with_html_body(
28-
self, email_service, pyramid_mailer
24+
self, log_data, email_service, pyramid_mailer
2925
):
30-
email = EmailData(
26+
email_data = EmailData(
3127
recipients=["[email protected]"],
3228
subject="My email subject",
3329
body="Some text body",
3430
tag=EmailTag.TEST,
3531
html="<p>An HTML body</p>",
3632
)
37-
email_service.send(email)
33+
email_service.send(email_data, log_data)
3834

3935
pyramid_mailer.message.Message.assert_called_once_with(
4036
recipients=["[email protected]"],
@@ -45,41 +41,25 @@ def test_send_creates_email_message_with_html_body(
4541
)
4642

4743
def test_send_dispatches_email_using_request_mailer(
48-
self, email_service, pyramid_mailer
44+
self, email_data, log_data, email_service, pyramid_mailer
4945
):
5046
request_mailer = pyramid_mailer.get_mailer.return_value
5147
message = pyramid_mailer.message.Message.return_value
5248

53-
email = EmailData(
54-
recipients=["[email protected]"],
55-
subject="My email subject",
56-
body="Some text body",
57-
tag=EmailTag.TEST,
58-
)
59-
email_service.send(email)
49+
email_service.send(email_data, log_data)
6050

6151
request_mailer.send_immediately.assert_called_once_with(message)
6252

63-
def test_raises_smtplib_exception(self, email_service, pyramid_mailer):
53+
def test_raises_smtplib_exception(
54+
self, email_data, log_data, email_service, pyramid_mailer
55+
):
6456
request_mailer = pyramid_mailer.get_mailer.return_value
6557
request_mailer.send_immediately.side_effect = smtplib.SMTPException()
6658

67-
email = EmailData(
68-
recipients=["[email protected]"],
69-
subject="My email subject",
70-
body="Some text body",
71-
tag=EmailTag.TEST,
72-
)
7359
with pytest.raises(smtplib.SMTPException):
74-
email_service.send(email)
60+
email_service.send(email_data, log_data)
7561

76-
def test_send_logging(self, email_service, info_caplog):
77-
email_data = EmailData(
78-
recipients=["[email protected]"],
79-
subject="My email subject",
80-
body="Some text body",
81-
tag=EmailTag.TEST,
82-
)
62+
def test_send_logging(self, email_service, info_caplog, email_data):
8363
user_id = 123
8464
annotation_id = "annotation_id"
8565
log_data = LogData(
@@ -94,6 +74,23 @@ def test_send_logging(self, email_service, info_caplog):
9474
f"Sent email: tag={email_data.tag!r}, sender_id={user_id}, recipient_ids=[{user_id}], annotation_id={annotation_id!r}"
9575
]
9676

77+
@pytest.fixture
78+
def email_data(self):
79+
return EmailData(
80+
recipients=["[email protected]"],
81+
subject="My email subject",
82+
body="Some text body",
83+
tag=EmailTag.TEST,
84+
)
85+
86+
@pytest.fixture
87+
def log_data(self):
88+
return LogData(
89+
sender_id=123,
90+
tag=EmailTag.TEST,
91+
recipient_ids=[456],
92+
)
93+
9794
@pytest.fixture
9895
def pyramid_request(self, pyramid_request):
9996
pyramid_request.debug = False

Diff for: tests/unit/h/tasks/mailer_test.py

+1-13
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,7 @@
66
from h.tasks import mailer
77

88

9-
def test_send_without_log_data(email_service):
10-
email_data = {
11-
"recipients": ["[email protected]"],
12-
"subject": "My email subject",
13-
"body": "Some text body",
14-
"tag": EmailTag.TEST,
15-
}
16-
mailer.send(email_data)
17-
18-
email_service.send.assert_called_once_with(EmailData(**email_data), None)
19-
20-
21-
def test_send_with_log_data(email_service):
9+
def test_send(email_service):
2210
email_data = {
2311
"recipients": ["[email protected]"],
2412
"subject": "My email subject",

0 commit comments

Comments
 (0)