Skip to content

Commit c9f07f3

Browse files
committed
Rate-limit emails sent by the same user in a period of time
1 parent ab582e9 commit c9f07f3

File tree

1 file changed

+56
-33
lines changed

1 file changed

+56
-33
lines changed

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

+56-33
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,22 @@ def test_send_creates_email_message_with_html_body(
4949
extra_headers={"X-MC-Tags": EmailTag.TEST},
5050
)
5151

52-
def test_send_creates_email_with_mention(
53-
self, email_data, task_data, email_service, pyramid_mailer, task_done_service
52+
def test_send_creates_mention_email_when_sender_limit_not_reached(
53+
self,
54+
mention_email_data,
55+
mention_task_data,
56+
email_service,
57+
pyramid_mailer,
58+
task_done_service,
5459
):
55-
email_data = EmailData(
56-
recipients=["[email protected]"],
57-
subject="My email subject",
58-
body="Some text body",
59-
tag=EmailTag.MENTION_NOTIFICATION,
60-
)
61-
task_data = TaskData(
62-
tag=email_data.tag,
63-
sender_id=123,
64-
recipient_ids=[124],
60+
task_done_service.sender_mention_count.return_value = (
61+
DAILY_SENDER_MENTION_LIMIT - 1
6562
)
66-
task_done_service.sender_mention_count.return_value = DAILY_SENDER_MENTION_LIMIT
6763

68-
email_service.send(email_data, task_data)
64+
email_service.send(mention_email_data, mention_task_data)
6965

7066
task_done_service.sender_mention_count.assert_called_once_with(
71-
task_data.sender_id, mock.ANY
67+
mention_task_data.sender_id, mock.ANY
7268
)
7369
pyramid_mailer.message.Message.assert_called_once_with(
7470
recipients=["[email protected]"],
@@ -78,28 +74,20 @@ def test_send_creates_email_with_mention(
7874
extra_headers={"X-MC-Tags": EmailTag.MENTION_NOTIFICATION},
7975
)
8076

81-
def test_send_does_not_create_email_with_mention(
82-
self, email_data, task_data, email_service, pyramid_mailer, task_done_service
77+
def test_send_does_not_create_mention_email_when_sender_limit_reached(
78+
self,
79+
mention_email_data,
80+
mention_task_data,
81+
email_service,
82+
pyramid_mailer,
83+
task_done_service,
8384
):
84-
email_data = EmailData(
85-
recipients=["[email protected]"],
86-
subject="My email subject",
87-
body="Some text body",
88-
tag=EmailTag.MENTION_NOTIFICATION,
89-
)
90-
task_data = TaskData(
91-
tag=email_data.tag,
92-
sender_id=123,
93-
recipient_ids=[124],
94-
)
95-
task_done_service.sender_mention_count.return_value = (
96-
DAILY_SENDER_MENTION_LIMIT + 1
97-
)
85+
task_done_service.sender_mention_count.return_value = DAILY_SENDER_MENTION_LIMIT
9886

99-
email_service.send(email_data, task_data)
87+
email_service.send(mention_email_data, mention_task_data)
10088

10189
task_done_service.sender_mention_count.assert_called_once_with(
102-
task_data.sender_id, mock.ANY
90+
mention_task_data.sender_id, mock.ANY
10391
)
10492
pyramid_mailer.message.Message.assert_not_called()
10593

@@ -139,12 +127,29 @@ def test_send_logging_with_extra(self, email_data, email_service, info_caplog):
139127
recipient_ids=[recipient_id],
140128
extra={"annotation_id": annotation_id},
141129
)
130+
142131
email_service.send(email_data, task_data)
143132

144133
assert info_caplog.messages == [
145134
f"Sent email: tag={task_data.tag!r}, sender_id={sender_id}, recipient_ids={[recipient_id]}, annotation_id={annotation_id!r}"
146135
]
147136

137+
def test_sender_limit_reached_logging(
138+
self,
139+
mention_email_data,
140+
mention_task_data,
141+
email_service,
142+
task_done_service,
143+
info_caplog,
144+
):
145+
task_done_service.sender_mention_count.return_value = DAILY_SENDER_MENTION_LIMIT
146+
147+
email_service.send(mention_email_data, mention_task_data)
148+
149+
assert info_caplog.messages == [
150+
f"Email not sent for sender_id={mention_task_data.sender_id}: limit reached"
151+
]
152+
148153
def test_send_creates_task_done(
149154
self, email_data, task_data, email_service, task_done_service
150155
):
@@ -154,6 +159,7 @@ def test_send_creates_task_done(
154159
recipient_ids=[124],
155160
extra={"annotation_id": "annotation_id"},
156161
)
162+
157163
email_service.send(email_data, task_data)
158164

159165
task_done_service.create.assert_called_once_with(task_data)
@@ -175,6 +181,23 @@ def task_data(self):
175181
recipient_ids=[124],
176182
)
177183

184+
@pytest.fixture
185+
def mention_email_data(self):
186+
return EmailData(
187+
recipients=["[email protected]"],
188+
subject="My email subject",
189+
body="Some text body",
190+
tag=EmailTag.MENTION_NOTIFICATION,
191+
)
192+
193+
@pytest.fixture
194+
def mention_task_data(self):
195+
return TaskData(
196+
tag=EmailTag.MENTION_NOTIFICATION,
197+
sender_id=123,
198+
recipient_ids=[124],
199+
)
200+
178201
@pytest.fixture
179202
def email_service(self, pyramid_request, pyramid_mailer, task_done_service):
180203
request_mailer = pyramid_mailer.get_mailer.return_value

0 commit comments

Comments
 (0)