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,60 @@ 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_email_with_mention (
73
+ self , email_data , task_data , email_service , pyramid_mailer , task_done_service
74
+ ):
75
+ email_data = EmailData (
76
+
77
+ subject = "My email subject" ,
78
+ body = "Some text body" ,
79
+ tag = EmailTag .MENTION_NOTIFICATION ,
80
+ )
81
+ task_data = TaskData (
82
+ tag = email_data .tag ,
83
+ sender_id = 123 ,
84
+ recipient_ids = [124 ],
85
+ )
86
+ task_done_service .sender_mention_count .return_value = DAILY_SENDER_MENTION_LIMIT
87
+
88
+ email_service .send (email_data , task_data )
89
+
90
+ task_done_service .sender_mention_count .assert_called_once_with (
91
+ task_data .sender_id , mock .ANY
92
+ )
93
+ pyramid_mailer .message .Message .assert_called_once_with (
94
+
95
+ subject = "My email subject" ,
96
+ body = "Some text body" ,
97
+ html = None ,
98
+ extra_headers = {"X-MC-Tags" : EmailTag .MENTION_NOTIFICATION },
99
+ )
100
+
101
+ def test_send_does_not_create_email_with_mention (
102
+ self , email_data , task_data , email_service , pyramid_mailer , task_done_service
103
+ ):
104
+ email_data = EmailData (
105
+
106
+ subject = "My email subject" ,
107
+ body = "Some text body" ,
108
+ tag = EmailTag .MENTION_NOTIFICATION ,
109
+ )
110
+ task_data = TaskData (
111
+ tag = email_data .tag ,
112
+ sender_id = 123 ,
113
+ recipient_ids = [124 ],
114
+ )
115
+ task_done_service .sender_mention_count .return_value = (
116
+ DAILY_SENDER_MENTION_LIMIT + 1
117
+ )
118
+
119
+ email_service .send (email_data , task_data )
120
+
121
+ task_done_service .sender_mention_count .assert_called_once_with (
122
+ task_data .sender_id , mock .ANY
123
+ )
124
+ pyramid_mailer .message .Message .assert_not_called ()
125
+
63
126
def test_send_dispatches_email_using_request_mailer (
64
127
self , email_data , task_data , email_service , pyramid_mailer
65
128
):
@@ -87,18 +150,19 @@ def test_send_logging(self, email_data, task_data, email_service, info_caplog):
87
150
]
88
151
89
152
def test_send_logging_with_extra (self , email_data , email_service , info_caplog ):
90
- user_id = 123
153
+ sender_id = 123
154
+ recipient_id = 124
91
155
annotation_id = "annotation_id"
92
156
task_data = TaskData (
93
157
tag = email_data .tag ,
94
- sender_id = user_id ,
95
- recipient_ids = [user_id ],
158
+ sender_id = sender_id ,
159
+ recipient_ids = [recipient_id ],
96
160
extra = {"annotation_id" : annotation_id },
97
161
)
98
162
email_service .send (email_data , task_data )
99
163
100
164
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} "
165
+ f"Sent email: tag={ task_data .tag !r} , sender_id={ sender_id } , recipient_ids={ [recipient_id ]} , annotation_id={ annotation_id !r} "
102
166
]
103
167
104
168
def test_send_creates_task_done (
@@ -107,7 +171,7 @@ def test_send_creates_task_done(
107
171
task_data = TaskData (
108
172
tag = email_data .tag ,
109
173
sender_id = 123 ,
110
- recipient_ids = [123 ],
174
+ recipient_ids = [124 ],
111
175
extra = {"annotation_id" : "annotation_id" },
112
176
)
113
177
email_service .send (email_data , task_data )
@@ -128,7 +192,7 @@ def task_data(self):
128
192
return TaskData (
129
193
tag = EmailTag .TEST ,
130
194
sender_id = 123 ,
131
- recipient_ids = [123 ],
195
+ recipient_ids = [124 ],
132
196
)
133
197
134
198
@pytest .fixture
0 commit comments