Coverage for website/registrations/tests/test_emails.py: 100.00%
95 statements
« prev ^ index » next coverage.py v7.6.7, created at 2025-08-14 10:31 +0000
« prev ^ index » next coverage.py v7.6.7, created at 2025-08-14 10:31 +0000
1import uuid
2from unittest import mock
4from django.conf import settings
5from django.core import mail
6from django.template import loader
7from django.template.defaultfilters import floatformat
8from django.test import TestCase
9from django.urls import reverse
10from django.utils import timezone, translation
12from freezegun import freeze_time
14from members.models import Member, Profile
15from members.models.membership import Membership
16from registrations import emails
17from registrations.models import Entry, Registration, Renewal
18from registrations.tasks import notify_old_entries
19from utils.snippets import send_email
22class EmailsTest(TestCase):
23 def setUp(self):
24 translation.activate("en")
26 @mock.patch("registrations.emails.send_email")
27 def test_send_registration_email_confirmation(self, send_email):
28 reg = Registration(
29 email="test@example.org",
30 first_name="John",
31 last_name="Doe",
32 pk=uuid.uuid4(),
33 )
35 emails.send_registration_email_confirmation(reg)
37 send_email.assert_called_once_with(
38 to=[reg.email],
39 subject="Confirm email address",
40 txt_template="registrations/email/registration_confirm_mail.txt",
41 html_template="registrations/email/registration_confirm_mail.html",
42 context={
43 "name": reg.get_full_name(),
44 "confirm_link": (
45 "http://localhost:8000"
46 + reverse("registrations:confirm-email", args=[reg.pk])
47 ),
48 },
49 )
51 @mock.patch("registrations.emails.send_email")
52 def test_send_registration_accepted_message(self, send_email):
53 reg = Registration(
54 email="test@example.org",
55 first_name="John",
56 last_name="Doe",
57 pk=0,
58 contribution=2,
59 )
61 emails.send_registration_accepted_message(reg)
63 send_email.assert_called_once_with(
64 to=[reg.email],
65 subject="Registration accepted",
66 txt_template="registrations/email/registration_accepted.txt",
67 html_template="registrations/email/registration_accepted.html",
68 context={
69 "name": reg.get_full_name(),
70 "fees": floatformat(reg.contribution, 2),
71 },
72 )
74 @mock.patch("registrations.emails.send_email")
75 def test_send_registration_rejected_message(self, send_email):
76 reg = Registration(
77 email="test@example.org",
78 first_name="John",
79 last_name="Doe",
80 pk=0,
81 )
83 emails.send_registration_rejected_message(reg)
85 send_email.assert_called_once_with(
86 to=[reg.email],
87 subject="Registration rejected",
88 txt_template="registrations/email/registration_rejected.txt",
89 html_template="registrations/email/registration_rejected.html",
90 context={"name": reg.get_full_name()},
91 )
93 @mock.patch("registrations.emails.send_email")
94 def test_send_new_registration_board_message(self, send_email):
95 registration = Registration(
96 email="test@example.org",
97 first_name="John",
98 last_name="Doe",
99 pk=0,
100 )
102 emails.send_new_registration_board_message(registration)
104 send_email.assert_called_once_with(
105 to=[settings.BOARD_NOTIFICATION_ADDRESS],
106 subject="New registration",
107 txt_template="registrations/email/registration_board.txt",
108 html_template="registrations/email/registration_board.html",
109 context={
110 "name": registration.get_full_name(),
111 "url": (
112 "http://localhost:8000"
113 + reverse(
114 "admin:registrations_registration_change",
115 args=[registration.pk],
116 )
117 ),
118 },
119 )
121 @mock.patch("registrations.emails.send_email")
122 def test_send_renewal_accepted_message(self, send_email):
123 member = Member(
124 email="test@example.org",
125 first_name="John",
126 last_name="Doe",
127 profile=Profile(),
128 )
130 renewal = Renewal(pk=0, member=member, contribution=2)
132 emails.send_renewal_accepted_message(renewal)
134 send_email.assert_called_once_with(
135 to=[renewal.member.email],
136 subject="Renewal accepted",
137 txt_template="registrations/email/renewal_accepted.txt",
138 html_template="registrations/email/renewal_accepted.html",
139 context={
140 "name": renewal.member.get_full_name(),
141 "fees": floatformat(renewal.contribution, 2),
142 "thalia_pay_enabled": settings.THALIA_PAY_ENABLED_PAYMENT_METHOD,
143 "url": (
144 settings.BASE_URL
145 + reverse(
146 "registrations:renew",
147 )
148 ),
149 },
150 )
152 @mock.patch("registrations.emails.send_email")
153 def test_send_renewal_rejected_message(self, send_email):
154 member = Member(
155 email="test@example.org",
156 first_name="John",
157 last_name="Doe",
158 profile=Profile(),
159 )
161 renewal = Renewal(pk=0, member=member)
163 emails.send_renewal_rejected_message(renewal)
165 send_email.assert_called_once_with(
166 to=[renewal.member.email],
167 subject="Renewal rejected",
168 txt_template="registrations/email/renewal_rejected.txt",
169 html_template="registrations/email/renewal_rejected.html",
170 context={"name": renewal.member.get_full_name()},
171 )
173 @mock.patch("registrations.emails.send_email")
174 def test_send_renewal_complete_message(self, send_email):
175 member = Member(
176 email="test@example.org",
177 first_name="John",
178 last_name="Doe",
179 profile=Profile(),
180 )
182 renewal = Renewal(pk=0, member=member)
184 emails.send_renewal_complete_message(renewal)
186 send_email.assert_called_once_with(
187 to=[renewal.member.email],
188 subject="Renewal successful",
189 txt_template="registrations/email/renewal_complete.txt",
190 html_template="registrations/email/renewal_complete.html",
191 context={"name": renewal.member.get_full_name()},
192 )
194 @mock.patch("registrations.emails.send_email")
195 def test_send_new_renewal_board_message(self, send_email):
196 member = Member(
197 email="test@example.org",
198 first_name="John",
199 last_name="Doe",
200 profile=Profile(),
201 )
203 renewal = Renewal(pk=0, member=member)
205 emails.send_new_renewal_board_message(renewal)
207 send_email.assert_called_once_with(
208 to=[settings.BOARD_NOTIFICATION_ADDRESS],
209 subject="New renewal",
210 txt_template="registrations/email/renewal_board.txt",
211 html_template="registrations/email/renewal_board.html",
212 context={
213 "name": renewal.member.get_full_name(),
214 "url": (
215 "http://localhost:8000"
216 + reverse("admin:registrations_renewal_change", args=[renewal.pk])
217 ),
218 },
219 )
221 @mock.patch("registrations.emails.send_email")
222 def test_send_reminder_open_registration(self, send_email):
223 with freeze_time("2024-01-01"):
224 registration = Registration.objects.create(
225 first_name="John",
226 last_name="Doe",
227 email="johndoe@example.com",
228 programme="computingscience",
229 starting_year=2014,
230 address_street="Heyendaalseweg 135",
231 address_street2="",
232 address_postal_code="6525AJ",
233 address_city="Nijmegen",
234 address_country="NL",
235 phone_number="06123456789",
236 birthday=timezone.now().replace(year=1990),
237 length=Entry.MEMBERSHIP_YEAR,
238 membership_type=Membership.MEMBER,
239 status=Entry.STATUS_CONFIRM,
240 )
242 with freeze_time("2024-02-10"):
243 notify_old_entries()
245 send_email.assert_called_once_with(
246 to=[settings.BOARD_NOTIFICATION_ADDRESS],
247 subject="Open registration for more than one month",
248 txt_template="registrations/email/reminder_open_registration.txt",
249 html_template="registrations/email/reminder_open_registration.html",
250 context={
251 "name": registration.get_full_name(),
252 "created_at": registration.created_at,
253 },
254 )
256 @mock.patch("registrations.emails.send_email")
257 def test_send_reminder_open_renewal(self, send_email):
258 with freeze_time("2024-01-01"):
259 member = Member.objects.create(
260 email="test@example.org",
261 first_name="John",
262 last_name="Doe",
263 )
265 renewal = Renewal.objects.create(
266 pk=0,
267 member=member,
268 length=Entry.MEMBERSHIP_YEAR,
269 membership_type=Membership.MEMBER,
270 status=Entry.STATUS_CONFIRM,
271 )
273 with freeze_time("2024-02-10"):
274 notify_old_entries()
276 send_email.assert_called_once_with(
277 to=[settings.BOARD_NOTIFICATION_ADDRESS],
278 subject="Open renewal for more than one month",
279 txt_template="registrations/email/reminder_open_renewal.txt",
280 html_template="registrations/email/reminder_open_renewal.html",
281 context={
282 "name": renewal.member.get_full_name(),
283 "created_at": renewal.created_at,
284 },
285 )
287 @mock.patch("registrations.emails.send_email")
288 def test_send_references_information_message(self, send_email):
289 with self.subTest("Registrations"):
290 registration = Registration(
291 email="test@example.org",
292 first_name="John",
293 last_name="Doe",
294 pk=uuid.uuid4(),
295 )
297 emails.send_references_information_message(registration)
299 send_email.assert_called_once_with(
300 to=["test@example.org"],
301 subject="Information about references",
302 txt_template="registrations/email/references_information.txt",
303 html_template="registrations/email/references_information.html",
304 context={
305 "name": registration.get_full_name(),
306 "reference_link": (
307 "http://localhost:8000"
308 + reverse("registrations:reference", args=[registration.pk])
309 ),
310 },
311 )
313 send_email.reset_mock()
315 with self.subTest("Renewals"):
316 member = Member(
317 email="test@example.org",
318 first_name="John",
319 last_name="Doe",
320 profile=Profile(),
321 )
323 renewal = Renewal(pk=uuid.uuid4(), member=member)
325 emails.send_references_information_message(renewal)
327 send_email.assert_called_once_with(
328 to=["test@example.org"],
329 subject="Information about references",
330 txt_template="registrations/email/references_information.txt",
331 html_template="registrations/email/references_information.html",
332 context={
333 "name": renewal.member.get_full_name(),
334 "reference_link": (
335 "http://localhost:8000"
336 + reverse("registrations:reference", args=[renewal.pk])
337 ),
338 },
339 )
341 def test_send_email(self):
342 send_email(
343 to=["test@example.org"],
344 subject="Subject",
345 txt_template="registrations/email/renewal_board.txt",
346 context={
347 "name": "name",
348 "url": "",
349 },
350 )
352 self.assertEqual(mail.outbox[0].subject, "[THALIA] Subject")
353 self.assertEqual(mail.outbox[0].to, ["test@example.org"])
354 self.assertEqual(
355 mail.outbox[0].body,
356 loader.render_to_string(
357 "registrations/email/renewal_board.txt",
358 {
359 "name": "name",
360 "url": "",
361 },
362 ),
363 )