2015-03-07 20:55:59 +08:00
|
|
|
from django.core import mail
|
|
|
|
from django.core.management import call_command
|
2015-06-16 20:46:56 +08:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2015-03-07 20:55:59 +08:00
|
|
|
|
|
|
|
|
2015-06-16 20:46:56 +08:00
|
|
|
@override_settings(
|
|
|
|
ADMINS=(
|
|
|
|
("Admin", "admin@example.com"),
|
|
|
|
("Admin and Manager", "admin_and_manager@example.com"),
|
2022-02-04 03:24:19 +08:00
|
|
|
),
|
2015-06-16 20:46:56 +08:00
|
|
|
MANAGERS=(
|
|
|
|
("Manager", "manager@example.com"),
|
|
|
|
("Admin and Manager", "admin_and_manager@example.com"),
|
2022-02-04 03:24:19 +08:00
|
|
|
),
|
2015-06-16 20:46:56 +08:00
|
|
|
)
|
2015-03-07 20:55:59 +08:00
|
|
|
class SendTestEmailManagementCommand(SimpleTestCase):
|
|
|
|
"""
|
|
|
|
Test the sending of a test email using the `sendtestemail` command.
|
|
|
|
"""
|
|
|
|
|
2015-06-16 20:46:56 +08:00
|
|
|
def test_single_receiver(self):
|
2015-03-07 20:55:59 +08:00
|
|
|
"""
|
|
|
|
The mail is sent with the correct subject and recipient.
|
|
|
|
"""
|
2015-06-16 20:46:56 +08:00
|
|
|
recipient = "joe@example.com"
|
|
|
|
call_command("sendtestemail", recipient)
|
2015-03-07 20:55:59 +08:00
|
|
|
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])
|
|
|
|
|
2015-06-16 20:46:56 +08:00
|
|
|
def test_multiple_receivers(self):
|
2015-03-07 20:55:59 +08:00
|
|
|
"""
|
|
|
|
The mail may be sent with multiple recipients.
|
|
|
|
"""
|
2015-06-16 20:46:56 +08:00
|
|
|
recipients = ["joe@example.com", "jane@example.com"]
|
|
|
|
call_command("sendtestemail", recipients[0], recipients[1])
|
2015-03-07 20:55:59 +08:00
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
|
|
|
mail_message = mail.outbox[0]
|
|
|
|
self.assertEqual(mail_message.subject[0:15], "Test email from")
|
2015-06-16 20:46:56 +08:00
|
|
|
self.assertEqual(
|
|
|
|
sorted(mail_message.recipients()),
|
|
|
|
[
|
|
|
|
"jane@example.com",
|
|
|
|
"joe@example.com",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
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",
|
|
|
|
],
|
|
|
|
)
|