1
1
import smtplib
2
+ from unittest import mock
2
3
from unittest .mock import sentinel
3
4
4
5
import pytest
5
6
6
- from h .services .email import EmailData , EmailService , EmailTag , TaskData , factory
7
+ from h .services .email import (
8
+ DAILY_SENDER_MENTION_LIMIT ,
9
+ EmailData ,
10
+ EmailService ,
11
+ EmailTag ,
12
+ TaskData ,
13
+ factory ,
14
+ )
7
15
8
16
9
17
class TestEmailService :
@@ -21,16 +29,17 @@ def test_send_creates_email_message(
21
29
)
22
30
23
31
def test_send_creates_email_message_with_html_body (
24
- self , task_data , email_service , pyramid_mailer
32
+ self , email_service , task_data , pyramid_mailer
25
33
):
26
- email = EmailData (
34
+ email_data = EmailData (
27
35
28
36
subject = "My email subject" ,
29
37
body = "Some text body" ,
30
38
tag = EmailTag .TEST ,
31
39
html = "<p>An HTML body</p>" ,
32
40
)
33
- email_service .send (email , task_data )
41
+
42
+ email_service .send (email_data , task_data )
34
43
35
44
pyramid_mailer .message .Message .assert_called_once_with (
36
45
@@ -60,6 +69,48 @@ def test_send_creates_email_message_with_subaccount(
60
69
extra_headers = {"X-MC-Tags" : EmailTag .TEST , "X-MC-Subaccount" : "subaccount" },
61
70
)
62
71
72
+ def test_send_creates_mention_email_when_sender_limit_not_reached (
73
+ self ,
74
+ mention_email_data ,
75
+ mention_task_data ,
76
+ email_service ,
77
+ pyramid_mailer ,
78
+ task_done_service ,
79
+ ):
80
+ task_done_service .sender_mention_count .return_value = (
81
+ DAILY_SENDER_MENTION_LIMIT - 1
82
+ )
83
+
84
+ email_service .send (mention_email_data , mention_task_data )
85
+
86
+ task_done_service .sender_mention_count .assert_called_once_with (
87
+ mention_task_data .sender_id , mock .ANY
88
+ )
89
+ pyramid_mailer .message .Message .assert_called_once_with (
90
+
91
+ subject = "My email subject" ,
92
+ body = "Some text body" ,
93
+ html = None ,
94
+ extra_headers = {"X-MC-Tags" : EmailTag .MENTION_NOTIFICATION },
95
+ )
96
+
97
+ def test_send_does_not_create_mention_email_when_sender_limit_reached (
98
+ self ,
99
+ mention_email_data ,
100
+ mention_task_data ,
101
+ email_service ,
102
+ pyramid_mailer ,
103
+ task_done_service ,
104
+ ):
105
+ task_done_service .sender_mention_count .return_value = DAILY_SENDER_MENTION_LIMIT
106
+
107
+ email_service .send (mention_email_data , mention_task_data )
108
+
109
+ task_done_service .sender_mention_count .assert_called_once_with (
110
+ mention_task_data .sender_id , mock .ANY
111
+ )
112
+ pyramid_mailer .message .Message .assert_not_called ()
113
+
63
114
def test_send_dispatches_email_using_request_mailer (
64
115
self , email_data , task_data , email_service , pyramid_mailer
65
116
):
@@ -87,18 +138,36 @@ def test_send_logging(self, email_data, task_data, email_service, info_caplog):
87
138
]
88
139
89
140
def test_send_logging_with_extra (self , email_data , email_service , info_caplog ):
90
- user_id = 123
141
+ sender_id = 123
142
+ recipient_id = 124
91
143
annotation_id = "annotation_id"
92
144
task_data = TaskData (
93
145
tag = email_data .tag ,
94
- sender_id = user_id ,
95
- recipient_ids = [user_id ],
146
+ sender_id = sender_id ,
147
+ recipient_ids = [recipient_id ],
96
148
extra = {"annotation_id" : annotation_id },
97
149
)
150
+
98
151
email_service .send (email_data , task_data )
99
152
100
153
assert info_caplog .messages == [
101
- f"Sent email: tag={ task_data .tag !r} , sender_id={ user_id } , recipient_ids={ [user_id ]} , annotation_id={ annotation_id !r} "
154
+ f"Sent email: tag={ task_data .tag !r} , sender_id={ sender_id } , recipient_ids={ [recipient_id ]} , annotation_id={ annotation_id !r} "
155
+ ]
156
+
157
+ def test_sender_limit_reached_logging (
158
+ self ,
159
+ mention_email_data ,
160
+ mention_task_data ,
161
+ email_service ,
162
+ task_done_service ,
163
+ info_caplog ,
164
+ ):
165
+ task_done_service .sender_mention_count .return_value = DAILY_SENDER_MENTION_LIMIT
166
+
167
+ email_service .send (mention_email_data , mention_task_data )
168
+
169
+ assert info_caplog .messages == [
170
+ f"Not sending email: tag={ mention_task_data .tag !r} sender_id={ mention_task_data .sender_id } recipient_ids={ mention_task_data .recipient_ids } . Sender limit reached."
102
171
]
103
172
104
173
def test_send_creates_task_done (
@@ -107,9 +176,10 @@ def test_send_creates_task_done(
107
176
task_data = TaskData (
108
177
tag = email_data .tag ,
109
178
sender_id = 123 ,
110
- recipient_ids = [123 ],
179
+ recipient_ids = [124 ],
111
180
extra = {"annotation_id" : "annotation_id" },
112
181
)
182
+
113
183
email_service .send (email_data , task_data )
114
184
115
185
task_done_service .create .assert_called_once_with (task_data )
@@ -128,7 +198,24 @@ def task_data(self):
128
198
return TaskData (
129
199
tag = EmailTag .TEST ,
130
200
sender_id = 123 ,
131
- recipient_ids = [123 ],
201
+ recipient_ids = [124 ],
202
+ )
203
+
204
+ @pytest .fixture
205
+ def mention_email_data (self ):
206
+ return EmailData (
207
+
208
+ subject = "My email subject" ,
209
+ body = "Some text body" ,
210
+ tag = EmailTag .MENTION_NOTIFICATION ,
211
+ )
212
+
213
+ @pytest .fixture
214
+ def mention_task_data (self ):
215
+ return TaskData (
216
+ tag = EmailTag .MENTION_NOTIFICATION ,
217
+ sender_id = 123 ,
218
+ recipient_ids = [124 ],
132
219
)
133
220
134
221
@pytest .fixture
0 commit comments