1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
from django.core import mail
from django.core.mail import MailerDoesNotExist
from django.core.management import CommandError, call_command
from django.test import SimpleTestCase, override_settings
@override_settings(
ADMINS=["admin@example.com", "admin_and_manager@example.com"],
MANAGERS=["manager@example.com", "admin_and_manager@example.com"],
MAILERS={
"default": {"BACKEND": "django.core.mail.backends.locmem.EmailBackend"},
"notifications": {"BACKEND": "django.core.mail.backends.locmem.EmailBackend"},
},
)
class SendTestEmailManagementCommand(SimpleTestCase):
"""
Test the sending of a test email using the `sendtestemail` command.
"""
def test_single_receiver(self):
"""
The mail is sent with the correct subject and recipient.
"""
recipient = "joe@example.com"
call_command("sendtestemail", recipient)
self.assertEqual(len(mail.outbox), 1)
mail_message = mail.outbox[0]
self.assertEqual(mail_message.subject[0:15], "Test email from")
self.assertEqual(mail_message.recipients(), [recipient])
def test_multiple_receivers(self):
"""
The mail may be sent with multiple recipients.
"""
recipients = ["joe@example.com", "jane@example.com"]
call_command("sendtestemail", recipients[0], recipients[1])
self.assertEqual(len(mail.outbox), 1)
mail_message = mail.outbox[0]
self.assertEqual(mail_message.subject[0:15], "Test email from")
self.assertEqual(
sorted(mail_message.recipients()),
[
"jane@example.com",
"joe@example.com",
],
)
def test_missing_receivers(self):
"""
The command should complain if no receivers are given (and --admins or
--managers are not set).
"""
msg = (
"You must specify some email recipients, or pass the --managers or "
"--admin options."
)
with self.assertRaisesMessage(CommandError, msg):
call_command("sendtestemail")
def test_manager_receivers(self):
"""
The mail should be sent to the email addresses specified in
settings.MANAGERS.
"""
call_command("sendtestemail", "--managers")
self.assertEqual(len(mail.outbox), 1)
mail_message = mail.outbox[0]
self.assertEqual(
sorted(mail_message.recipients()),
[
"admin_and_manager@example.com",
"manager@example.com",
],
)
def test_admin_receivers(self):
"""
The mail should be sent to the email addresses specified in
settings.ADMIN.
"""
call_command("sendtestemail", "--admins")
self.assertEqual(len(mail.outbox), 1)
mail_message = mail.outbox[0]
self.assertEqual(
sorted(mail_message.recipients()),
[
"admin@example.com",
"admin_and_manager@example.com",
],
)
def test_manager_and_admin_receivers(self):
"""
The mail should be sent to the email addresses specified in both
settings.MANAGERS and settings.ADMINS.
"""
call_command("sendtestemail", "--managers", "--admins")
self.assertEqual(len(mail.outbox), 2)
manager_mail = mail.outbox[0]
self.assertEqual(
sorted(manager_mail.recipients()),
[
"admin_and_manager@example.com",
"manager@example.com",
],
)
admin_mail = mail.outbox[1]
self.assertEqual(
sorted(admin_mail.recipients()),
[
"admin@example.com",
"admin_and_manager@example.com",
],
)
def test_using_option(self):
recipient = "joe@example.com"
call_command("sendtestemail", "--using", "notifications", recipient)
self.assertEqual(len(mail.outbox), 1)
mail_message = mail.outbox[0]
self.assertEqual(mail_message.sent_using, "notifications")
def test_using_default(self):
recipient = "joe@example.com"
call_command("sendtestemail", recipient)
self.assertEqual(len(mail.outbox), 1)
mail_message = mail.outbox[0]
self.assertEqual(mail_message.sent_using, "default")
def test_using_option_with_managers(self):
call_command("sendtestemail", "--using", "notifications", "--managers")
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].sent_using, "notifications")
def test_using_option_with_admins(self):
call_command("sendtestemail", "--using", "notifications", "--admins")
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].sent_using, "notifications")
def test_using_nonexistent_mailer(self):
msg = "The mailer 'nonexistent' is not configured."
with self.assertRaisesMessage(MailerDoesNotExist, msg):
call_command("sendtestemail", "--using", "nonexistent", "joe@example.com")
|