|
1 | 1 | from io import StringIO |
| 2 | +from pathlib import Path |
| 3 | +from tempfile import TemporaryDirectory |
2 | 4 | from unittest import mock |
3 | | - |
| 5 | +import pytest |
4 | 6 | from django.core import mail |
5 | 7 | from django.core.management import call_command |
6 | | -from django.test import TestCase |
7 | 8 |
|
8 | 9 | from certification.certifications.models import Certification, QuizCertification |
9 | 10 | from lab.tests.factories import StaffUserFactory |
10 | 11 |
|
11 | 12 | from ..models import CertificationNotification, NotificationType |
12 | 13 |
|
13 | | -TEMPLATE = "certification/email/radioprotection_invitation.html" |
14 | 14 |
|
| 15 | +@pytest.fixture(name="temp_template_path") |
| 16 | +def temp_template_path_fixture(): |
| 17 | + """Create a temporary directory with a template file for testing.""" |
| 18 | + with TemporaryDirectory() as temp_dir: |
| 19 | + # Create the template directory structure |
| 20 | + template_dir = Path(temp_dir) / "certification" / "email" |
| 21 | + template_dir.mkdir(parents=True) |
15 | 22 |
|
16 | | -class ClosepollTest(TestCase): |
17 | | - def test_command_send_notification(self): |
18 | | - user = StaffUserFactory() |
19 | | - notification = CertificationNotification.objects.create( |
20 | | - user=user, |
21 | | - certification=Certification.objects.create( |
22 | | - name="certification", |
23 | | - invitation_to_complete_email_template_path=TEMPLATE, |
24 | | - ), |
25 | | - type_of=NotificationType.INVITATION_TO_COMPLETE, |
26 | | - ) |
27 | | - QuizCertification.objects.create( |
28 | | - certification=notification.certification, url="url", passing_score=1 |
29 | | - ) |
| 23 | + # Create a simple template file |
| 24 | + template_path = template_dir / "radioprotection_invitation.html" |
| 25 | + template_path.write_text("<html><body>Test template</body></html>") |
30 | 26 |
|
31 | | - out = StringIO() |
32 | | - call_command("send_notifications", stdout=out) |
| 27 | + # Add the temp directory to template dirs |
| 28 | + with mock.patch( |
| 29 | + "django.template.loaders.filesystem.Loader.get_dirs" |
| 30 | + ) as mock_get_dirs: |
| 31 | + mock_get_dirs.return_value = [temp_dir] |
| 32 | + yield str(template_path) |
33 | 33 |
|
34 | | - notification.refresh_from_db() |
35 | | - assert notification.is_sent |
36 | 34 |
|
37 | | - assert ( |
38 | | - f"Notification for certification certification sent to {user.email}." |
39 | | - in out.getvalue() |
40 | | - ) |
| 35 | +@pytest.mark.django_db |
| 36 | +def test_command_send_notification(temp_template_path: str): |
| 37 | + user = StaffUserFactory() |
| 38 | + notification = CertificationNotification.objects.create( |
| 39 | + user=user, |
| 40 | + certification=Certification.objects.create( |
| 41 | + name="certification", |
| 42 | + invitation_to_complete_email_template_path=temp_template_path, |
| 43 | + ), |
| 44 | + type_of=NotificationType.INVITATION_TO_COMPLETE, |
| 45 | + ) |
| 46 | + QuizCertification.objects.create( |
| 47 | + certification=notification.certification, url="url", passing_score=1 |
| 48 | + ) |
41 | 49 |
|
42 | | - assert len(mail.outbox) == 1 |
43 | | - assert mail.outbox[0].recipients() == [user.email] |
| 50 | + out = StringIO() |
| 51 | + call_command("send_notifications", stdout=out) |
44 | 52 |
|
45 | | - def test_command_continue_if_error(self): |
46 | | - certification = Certification.objects.create( |
47 | | - name="certification", |
48 | | - invitation_to_complete_email_template_path=TEMPLATE, |
49 | | - ) |
50 | | - QuizCertification.objects.create( |
51 | | - certification=certification, url="url", passing_score=1 |
52 | | - ) |
| 53 | + notification.refresh_from_db() |
| 54 | + assert notification.is_sent |
53 | 55 |
|
54 | | - notifications = [] |
55 | | - with mock.patch( |
56 | | - # pylint: disable=line-too-long |
57 | | - "certification.management.commands.send_notifications.CertificationNotification.send_notification" |
58 | | - ) as mock_fn: |
59 | | - mock_fn.side_effect = [Exception("Error"), None] |
60 | | - users = [StaffUserFactory(), StaffUserFactory()] |
61 | | - for user in users: |
62 | | - notifications.append( |
63 | | - CertificationNotification.objects.create( |
64 | | - user=user, |
65 | | - certification=certification, |
66 | | - type_of=NotificationType.INVITATION_TO_COMPLETE, |
67 | | - ) |
| 56 | + assert ( |
| 57 | + f"Notification for certification certification sent to {user.email}." |
| 58 | + in out.getvalue() |
| 59 | + ) |
| 60 | + |
| 61 | + assert len(mail.outbox) == 1 |
| 62 | + assert mail.outbox[0].recipients() == [user.email] |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.django_db |
| 66 | +def test_command_continue_if_error(temp_template_path: str): |
| 67 | + certification = Certification.objects.create( |
| 68 | + name="certification", |
| 69 | + invitation_to_complete_email_template_path=temp_template_path, |
| 70 | + ) |
| 71 | + QuizCertification.objects.create( |
| 72 | + certification=certification, url="url", passing_score=1 |
| 73 | + ) |
| 74 | + |
| 75 | + notifications = [] |
| 76 | + with mock.patch( |
| 77 | + # pylint: disable=line-too-long |
| 78 | + "certification.management.commands.send_notifications.CertificationNotification.send_notification" |
| 79 | + ) as mock_fn: |
| 80 | + mock_fn.side_effect = [Exception("Error"), None] |
| 81 | + users = [StaffUserFactory(), StaffUserFactory()] |
| 82 | + for user in users: |
| 83 | + notifications.append( |
| 84 | + CertificationNotification.objects.create( |
| 85 | + user=user, |
| 86 | + certification=certification, |
| 87 | + type_of=NotificationType.INVITATION_TO_COMPLETE, |
68 | 88 | ) |
| 89 | + ) |
69 | 90 |
|
70 | | - out = StringIO() |
71 | | - call_command("send_notifications", stdout=out) |
| 91 | + out = StringIO() |
| 92 | + call_command("send_notifications", stdout=out) |
72 | 93 |
|
73 | | - for notification in notifications: |
74 | | - notification.refresh_from_db() |
| 94 | + for notification in notifications: |
| 95 | + notification.refresh_from_db() |
75 | 96 |
|
76 | | - assert not notifications[0].is_sent |
77 | | - assert notifications[1].is_sent |
| 97 | + assert not notifications[0].is_sent |
| 98 | + assert notifications[1].is_sent |
0 commit comments