1
0
mirror of https://github.com/django/django.git synced 2025-04-24 17:24:37 +00:00
django/tests/mail/test_sendtestemail.py
Mike Edmunds e295033144 Fixed #36138 -- Changed ADMINS and MANAGERS settings to lists of strings.
Previously, the ADMINS and MANAGERS settings were lists of (name, address)
tuples (where the name had been unused). Deprecated use of tuples.

Updated settings value sanity checks, and changed from ValueError to
ImproperlyConfigured.
2025-03-21 10:17:57 +01:00

110 lines
3.6 KiB
Python

from django.core import mail
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"],
)
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",
],
)