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
@@ -40,6 +49,60 @@ def test_send_creates_email_message_with_html_body(
40
49
extra_headers = {"X-MC-Tags" : EmailTag .TEST },
41
50
)
42
51
52
+ def test_send_creates_email_with_mention (
53
+ self , email_data , task_data , email_service , pyramid_mailer , task_done_service
54
+ ):
55
+ email_data = EmailData (
56
+
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 ],
65
+ )
66
+ task_done_service .sender_mention_count .return_value = DAILY_SENDER_MENTION_LIMIT
67
+
68
+ email_service .send (email_data , task_data )
69
+
70
+ task_done_service .sender_mention_count .assert_called_once_with (
71
+ task_data .sender_id , mock .ANY
72
+ )
73
+ pyramid_mailer .message .Message .assert_called_once_with (
74
+
75
+ subject = "My email subject" ,
76
+ body = "Some text body" ,
77
+ html = None ,
78
+ extra_headers = {"X-MC-Tags" : EmailTag .MENTION_NOTIFICATION },
79
+ )
80
+
81
+ def test_send_does_not_create_email_with_mention (
82
+ self , email_data , task_data , email_service , pyramid_mailer , task_done_service
83
+ ):
84
+ email_data = EmailData (
85
+
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
+ )
98
+
99
+ email_service .send (email_data , task_data )
100
+
101
+ task_done_service .sender_mention_count .assert_called_once_with (
102
+ task_data .sender_id , mock .ANY
103
+ )
104
+ pyramid_mailer .message .Message .assert_not_called ()
105
+
43
106
def test_send_dispatches_email_using_request_mailer (
44
107
self , email_data , task_data , email_service , pyramid_mailer
45
108
):
@@ -67,18 +130,19 @@ def test_send_logging(self, email_data, task_data, email_service, info_caplog):
67
130
]
68
131
69
132
def test_send_logging_with_extra (self , email_data , email_service , info_caplog ):
70
- user_id = 123
133
+ sender_id = 123
134
+ recipient_id = 124
71
135
annotation_id = "annotation_id"
72
136
task_data = TaskData (
73
137
tag = email_data .tag ,
74
- sender_id = user_id ,
75
- recipient_ids = [user_id ],
138
+ sender_id = sender_id ,
139
+ recipient_ids = [recipient_id ],
76
140
extra = {"annotation_id" : annotation_id },
77
141
)
78
142
email_service .send (email_data , task_data )
79
143
80
144
assert info_caplog .messages == [
81
- f"Sent email: tag={ task_data .tag !r} , sender_id={ user_id } , recipient_ids={ [user_id ]} , annotation_id={ annotation_id !r} "
145
+ f"Sent email: tag={ task_data .tag !r} , sender_id={ sender_id } , recipient_ids={ [recipient_id ]} , annotation_id={ annotation_id !r} "
82
146
]
83
147
84
148
def test_send_creates_task_done (
@@ -87,7 +151,7 @@ def test_send_creates_task_done(
87
151
task_data = TaskData (
88
152
tag = email_data .tag ,
89
153
sender_id = 123 ,
90
- recipient_ids = [123 ],
154
+ recipient_ids = [124 ],
91
155
extra = {"annotation_id" : "annotation_id" },
92
156
)
93
157
email_service .send (email_data , task_data )
@@ -108,7 +172,7 @@ def task_data(self):
108
172
return TaskData (
109
173
tag = EmailTag .TEST ,
110
174
sender_id = 123 ,
111
- recipient_ids = [123 ],
175
+ recipient_ids = [124 ],
112
176
)
113
177
114
178
@pytest .fixture
0 commit comments