Skip to content

Commit 793b04d

Browse files
authored
Revert "Make log_data required in mailer.send"
This reverts commit c33f557.
1 parent c33f557 commit 793b04d

File tree

4 files changed

+90
-60
lines changed

4 files changed

+90
-60
lines changed

h/services/email.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def __init__(self, request: Request, mailer: IMailer) -> None:
6262
self._request = request
6363
self._mailer = mailer
6464

65-
def send(self, email_data: EmailData, log_data: LogData) -> None:
65+
def send(self, email_data: EmailData, log_data: LogData | None = None) -> None:
6666
if self._request.debug: # pragma: no cover
6767
logger.info("emailing in debug mode: check the `mail/` directory")
6868
try:
@@ -75,15 +75,16 @@ def send(self, email_data: EmailData, log_data: LogData) -> None:
7575
except smtplib.SMTPException:
7676
raise
7777

78-
separator = ", " if log_data.extra_msg else ""
79-
logger.info(
80-
"Sent email: tag=%r, sender_id=%s, recipient_ids=%s%s%s",
81-
log_data.tag,
82-
log_data.sender_id,
83-
log_data.recipient_ids,
84-
separator,
85-
log_data.extra_msg,
86-
)
78+
if log_data:
79+
separator = ", " if log_data.extra_msg else ""
80+
logger.info(
81+
"Sent email: tag=%r, sender_id=%s, recipient_ids=%s%s%s",
82+
log_data.tag,
83+
log_data.sender_id,
84+
log_data.recipient_ids,
85+
separator,
86+
log_data.extra_msg,
87+
)
8788

8889

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

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],
27+
log_data: dict[str, Any] | None = None,
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))
34+
service.send(EmailData(**email_data), LogData(**log_data) if log_data else None)

tests/unit/h/services/email_test.py

+47-32
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77

88

99
class TestEmailService:
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)
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)
1418

1519
pyramid_mailer.message.Message.assert_called_once_with(
1620
recipients=["[email protected]"],
@@ -21,7 +25,7 @@ def test_send_creates_email_message(
2125
)
2226

2327
def test_send_creates_email_message_with_html_body(
24-
self, log_data, email_service, pyramid_mailer
28+
self, email_service, pyramid_mailer
2529
):
2630
email = EmailData(
2731
recipients=["[email protected]"],
@@ -30,7 +34,7 @@ def test_send_creates_email_message_with_html_body(
3034
tag=EmailTag.TEST,
3135
html="<p>An HTML body</p>",
3236
)
33-
email_service.send(email, log_data)
37+
email_service.send(email)
3438

3539
pyramid_mailer.message.Message.assert_called_once_with(
3640
recipients=["[email protected]"],
@@ -41,32 +45,60 @@ def test_send_creates_email_message_with_html_body(
4145
)
4246

4347
def test_send_dispatches_email_using_request_mailer(
44-
self, email_data, log_data, email_service, pyramid_mailer
48+
self, email_service, pyramid_mailer
4549
):
4650
request_mailer = pyramid_mailer.get_mailer.return_value
4751
message = pyramid_mailer.message.Message.return_value
4852

49-
email_service.send(email_data, log_data)
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)
5060

5161
request_mailer.send_immediately.assert_called_once_with(message)
5262

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

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

62-
def test_send_logging(self, email_data, log_data, email_service, info_caplog):
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+
)
83+
user_id = 123
84+
log_data = LogData(
85+
tag=email_data.tag,
86+
sender_id=user_id,
87+
recipient_ids=[user_id],
88+
)
6389
email_service.send(email_data, log_data)
6490

6591
assert info_caplog.messages == [
66-
f"Sent email: tag={log_data.tag!r}, sender_id={log_data.sender_id}, recipient_ids={log_data.recipient_ids}"
92+
f"Sent email: tag={log_data.tag!r}, sender_id={user_id}, recipient_ids={[user_id]}"
6793
]
6894

69-
def test_send_logging_with_extra(self, email_data, email_service, info_caplog):
95+
def test_send_logging_with_extra(self, email_service, info_caplog):
96+
email_data = EmailData(
97+
recipients=["[email protected]"],
98+
subject="My email subject",
99+
body="Some text body",
100+
tag=EmailTag.TEST,
101+
)
70102
user_id = 123
71103
annotation_id = "annotation_id"
72104
log_data = LogData(
@@ -81,23 +113,6 @@ def test_send_logging_with_extra(self, email_data, email_service, info_caplog):
81113
f"Sent email: tag={log_data.tag!r}, sender_id={user_id}, recipient_ids={[user_id]}, annotation_id={annotation_id!r}"
82114
]
83115

84-
@pytest.fixture
85-
def email_data(self):
86-
return EmailData(
87-
recipients=["[email protected]"],
88-
subject="My email subject",
89-
body="Some text body",
90-
tag=EmailTag.TEST,
91-
)
92-
93-
@pytest.fixture
94-
def log_data(self):
95-
return LogData(
96-
tag=EmailTag.TEST,
97-
sender_id=123,
98-
recipient_ids=[123],
99-
)
100-
101116
@pytest.fixture
102117
def pyramid_request(self, pyramid_request):
103118
pyramid_request.debug = False

tests/unit/h/tasks/mailer_test.py

+30-16
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,52 @@
66
from h.tasks import mailer
77

88

9-
def test_send(email_data, log_data, email_service):
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):
22+
email_data = {
23+
"recipients": ["[email protected]"],
24+
"subject": "My email subject",
25+
"body": "Some text body",
26+
"tag": EmailTag.TEST,
27+
}
28+
log_data = {
29+
"sender_id": 123,
30+
"recipient_ids": [456],
31+
"tag": EmailTag.TEST,
32+
"extra": {"annotation_id": "annotation_id"},
33+
}
1034
mailer.send(email_data, log_data)
1135

1236
email_service.send.assert_called_once_with(
1337
EmailData(**email_data), LogData(**log_data)
1438
)
1539

1640

17-
def test_send_retries_if_mailing_fails(email_data, log_data, email_service):
41+
def test_send_retries_if_mailing_fails(email_service):
1842
email_service.send.side_effect = Exception()
1943
mailer.send.retry = mock.Mock(wraps=mailer.send.retry)
2044

21-
with pytest.raises(Exception) as exc_info: # noqa: PT011
22-
mailer.send(email_data, log_data)
23-
assert exc_info.type is Exception
24-
25-
assert mailer.send.retry.called
26-
27-
28-
@pytest.fixture
29-
def email_data():
30-
return {
45+
email_data = {
3146
"recipients": ["[email protected]"],
3247
"subject": "My email subject",
3348
"body": "Some text body",
3449
"tag": EmailTag.TEST,
3550
}
51+
with pytest.raises(Exception): # noqa: B017, PT011
52+
mailer.send(email_data)
3653

37-
38-
@pytest.fixture
39-
def log_data():
40-
return {"tag": EmailTag.TEST, "sender_id": 123, "recipient_ids": [123]}
54+
assert mailer.send.retry.called
4155

4256

4357
@pytest.fixture

0 commit comments

Comments
 (0)