1
0
mirror of https://github.com/django/django.git synced 2025-06-05 03:29:12 +00:00

Refs #36138 -- Improved tests for mail_admins() and mail_managers().

- Separated MailTests.test_connection_arg test cases.
- Expanded test cases for incorrect values of ADMINS/MANAGERS settings.
- Added test case verifying correct values of ADMINS/MANAGERS settings.
This commit is contained in:
Mike Edmunds 2025-02-04 13:32:49 -08:00 committed by Sarah Boyce
parent 6a9db1e626
commit 62ad970c39

View File

@ -1104,16 +1104,10 @@ class MailTests(MailTestsMixin, SimpleTestCase):
) )
self.assertIsInstance(mail.get_connection(), locmem.EmailBackend) self.assertIsInstance(mail.get_connection(), locmem.EmailBackend)
@override_settings( @override_settings(EMAIL_BACKEND="django.core.mail.backends.locmem.EmailBackend")
EMAIL_BACKEND="django.core.mail.backends.locmem.EmailBackend", def test_connection_arg_send_mail(self):
ADMINS=[("nobody", "nobody@example.com")],
MANAGERS=[("nobody", "nobody@example.com")],
)
def test_connection_arg(self):
"""Test connection argument to send_mail(), et. al."""
mail.outbox = [] mail.outbox = []
# Send using non-default connection.
# Send using non-default connection
connection = mail.get_connection("mail.custombackend.EmailBackend") connection = mail.get_connection("mail.custombackend.EmailBackend")
send_mail( send_mail(
"Subject", "Subject",
@ -1126,6 +1120,10 @@ class MailTests(MailTestsMixin, SimpleTestCase):
self.assertEqual(len(connection.test_outbox), 1) self.assertEqual(len(connection.test_outbox), 1)
self.assertEqual(connection.test_outbox[0].subject, "Subject") self.assertEqual(connection.test_outbox[0].subject, "Subject")
@override_settings(EMAIL_BACKEND="django.core.mail.backends.locmem.EmailBackend")
def test_connection_arg_send_mass_mail(self):
mail.outbox = []
# Send using non-default connection.
connection = mail.get_connection("mail.custombackend.EmailBackend") connection = mail.get_connection("mail.custombackend.EmailBackend")
send_mass_mail( send_mass_mail(
[ [
@ -1139,12 +1137,26 @@ class MailTests(MailTestsMixin, SimpleTestCase):
self.assertEqual(connection.test_outbox[0].subject, "Subject1") self.assertEqual(connection.test_outbox[0].subject, "Subject1")
self.assertEqual(connection.test_outbox[1].subject, "Subject2") self.assertEqual(connection.test_outbox[1].subject, "Subject2")
@override_settings(
EMAIL_BACKEND="django.core.mail.backends.locmem.EmailBackend",
ADMINS=[("nobody", "nobody@example.com")],
)
def test_connection_arg_mail_admins(self):
mail.outbox = []
# Send using non-default connection.
connection = mail.get_connection("mail.custombackend.EmailBackend") connection = mail.get_connection("mail.custombackend.EmailBackend")
mail_admins("Admin message", "Content", connection=connection) mail_admins("Admin message", "Content", connection=connection)
self.assertEqual(mail.outbox, []) self.assertEqual(mail.outbox, [])
self.assertEqual(len(connection.test_outbox), 1) self.assertEqual(len(connection.test_outbox), 1)
self.assertEqual(connection.test_outbox[0].subject, "[Django] Admin message") self.assertEqual(connection.test_outbox[0].subject, "[Django] Admin message")
@override_settings(
EMAIL_BACKEND="django.core.mail.backends.locmem.EmailBackend",
MANAGERS=[("nobody", "nobody@example.com")],
)
def test_connection_arg_mail_managers(self):
mail.outbox = []
# Send using non-default connection.
connection = mail.get_connection("mail.custombackend.EmailBackend") connection = mail.get_connection("mail.custombackend.EmailBackend")
mail_managers("Manager message", "Content", connection=connection) mail_managers("Manager message", "Content", connection=connection)
self.assertEqual(mail.outbox, []) self.assertEqual(mail.outbox, [])
@ -1765,6 +1777,31 @@ class BaseEmailBackendTests(MailTestsMixin):
self.assertEqual(message.get_payload(1).get_payload(), "HTML Content") self.assertEqual(message.get_payload(1).get_payload(), "HTML Content")
self.assertEqual(message.get_payload(1).get_content_type(), "text/html") self.assertEqual(message.get_payload(1).get_content_type(), "text/html")
def test_mail_admins_and_managers(self):
tests = (
# The ADMINS and MANAGERS settings are lists of (name, address) tuples.
[("Name, Full", "test@example.com")],
# Lists and tuples are interchangeable.
[["Name, Full", "test@example.com"], ["ignored", "other@example.com"]],
(("", "test@example.com"), ("", "other@example.com")),
# Lazy strings are supported.
[(gettext_lazy("Name, Full"), gettext_lazy("test@example.com"))],
)
for setting, mail_func in (
("ADMINS", mail_admins),
("MANAGERS", mail_managers),
):
for value in tests:
self.flush_mailbox()
with (
self.subTest(setting=setting, value=value),
self.settings(**{setting: value}),
):
mail_func("subject", "content")
message = self.get_the_message()
expected_to = ", ".join([str(address) for _, address in value])
self.assertEqual(message.get_all("to"), [expected_to])
@override_settings(MANAGERS=[("nobody", "nobody@example.com")]) @override_settings(MANAGERS=[("nobody", "nobody@example.com")])
def test_html_mail_managers(self): def test_html_mail_managers(self):
"""Test html_message argument to mail_managers""" """Test html_message argument to mail_managers"""
@ -1804,14 +1841,12 @@ class BaseEmailBackendTests(MailTestsMixin):
String prefix + lazy translated subject = bad output String prefix + lazy translated subject = bad output
Regression for #13494 Regression for #13494
""" """
mail_managers(gettext_lazy("Subject"), "Content") for mail_func in [mail_managers, mail_admins]:
message = self.get_the_message() with self.subTest(mail_func=mail_func):
self.assertEqual(message.get("subject"), "[Django] Subject") mail_func(gettext_lazy("Subject"), "Content")
message = self.get_the_message()
self.flush_mailbox() self.assertEqual(message.get("subject"), "[Django] Subject")
mail_admins(gettext_lazy("Subject"), "Content") self.flush_mailbox()
message = self.get_the_message()
self.assertEqual(message.get("subject"), "[Django] Subject")
@override_settings(ADMINS=[], MANAGERS=[]) @override_settings(ADMINS=[], MANAGERS=[])
def test_empty_admins(self): def test_empty_admins(self):
@ -1819,17 +1854,21 @@ class BaseEmailBackendTests(MailTestsMixin):
mail_admins/mail_managers doesn't connect to the mail server mail_admins/mail_managers doesn't connect to the mail server
if there are no recipients (#9383) if there are no recipients (#9383)
""" """
mail_admins("hi", "there") for mail_func in [mail_managers, mail_admins]:
self.assertEqual(self.get_mailbox_content(), []) with self.subTest(mail_func=mail_func):
mail_managers("hi", "there") mail_func("hi", "there")
self.assertEqual(self.get_mailbox_content(), []) self.assertEqual(self.get_mailbox_content(), [])
def test_wrong_admins_managers(self): def test_wrong_admins_managers(self):
tests = ( tests = (
"test@example.com", "test@example.com",
gettext_lazy("test@example.com"),
("test@example.com",), ("test@example.com",),
["test@example.com", "other@example.com"], ["test@example.com", "other@example.com"],
("test@example.com", "other@example.com"), ("test@example.com", "other@example.com"),
[("name", "test", "example.com")],
[("Name <test@example.com",)],
[[]],
) )
for setting, mail_func in ( for setting, mail_func in (
("ADMINS", mail_admins), ("ADMINS", mail_admins),