1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

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.
This commit is contained in:
Mike Edmunds
2025-02-14 10:58:01 -08:00
committed by Sarah Boyce
parent aed303aff5
commit e295033144
10 changed files with 102 additions and 52 deletions

View File

@@ -19,6 +19,7 @@ from textwrap import dedent
from unittest import mock, skipUnless
from django.core import mail
from django.core.exceptions import ImproperlyConfigured
from django.core.mail import (
DNS_NAME,
BadHeaderError,
@@ -35,6 +36,7 @@ from django.core.mail.backends import console, dummy, filebased, locmem, smtp
from django.core.mail.message import sanitize_address
from django.test import SimpleTestCase, override_settings
from django.test.utils import requires_tz_support
from django.utils.deprecation import RemovedInDjango70Warning
from django.utils.translation import gettext_lazy
try:
@@ -1139,7 +1141,7 @@ class MailTests(MailTestsMixin, SimpleTestCase):
@override_settings(
EMAIL_BACKEND="django.core.mail.backends.locmem.EmailBackend",
ADMINS=[("nobody", "nobody@example.com")],
ADMINS=["nobody@example.com"],
)
def test_connection_arg_mail_admins(self):
mail.outbox = []
@@ -1152,7 +1154,7 @@ class MailTests(MailTestsMixin, SimpleTestCase):
@override_settings(
EMAIL_BACKEND="django.core.mail.backends.locmem.EmailBackend",
MANAGERS=[("nobody", "nobody@example.com")],
MANAGERS=["nobody@example.com"],
)
def test_connection_arg_mail_managers(self):
mail.outbox = []
@@ -1779,13 +1781,13 @@ class BaseEmailBackendTests(MailTestsMixin):
def test_mail_admins_and_managers(self):
tests = (
# The ADMINS and MANAGERS settings are lists of (name, address) tuples.
[("Name, Full", "test@example.com")],
# The ADMINS and MANAGERS settings are lists of email strings.
['"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")),
["test@example.com", "other@example.com"],
("test@example.com", "other@example.com"),
# Lazy strings are supported.
[(gettext_lazy("Name, Full"), gettext_lazy("test@example.com"))],
[gettext_lazy("test@example.com")],
)
for setting, mail_func in (
("ADMINS", mail_admins),
@@ -1799,10 +1801,10 @@ class BaseEmailBackendTests(MailTestsMixin):
):
mail_func("subject", "content")
message = self.get_the_message()
expected_to = ", ".join([str(address) for _, address in value])
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@example.com"])
def test_html_mail_managers(self):
"""Test html_message argument to mail_managers"""
mail_managers("Subject", "Content", html_message="HTML Content")
@@ -1817,7 +1819,7 @@ class BaseEmailBackendTests(MailTestsMixin):
self.assertEqual(message.get_payload(1).get_payload(), "HTML Content")
self.assertEqual(message.get_payload(1).get_content_type(), "text/html")
@override_settings(ADMINS=[("nobody", "nobody@example.com")])
@override_settings(ADMINS=["nobody@example.com"])
def test_html_mail_admins(self):
"""Test html_message argument to mail_admins"""
mail_admins("Subject", "Content", html_message="HTML Content")
@@ -1833,8 +1835,8 @@ class BaseEmailBackendTests(MailTestsMixin):
self.assertEqual(message.get_payload(1).get_content_type(), "text/html")
@override_settings(
ADMINS=[("nobody", "nobody+admin@example.com")],
MANAGERS=[("nobody", "nobody+manager@example.com")],
ADMINS=["nobody+admin@example.com"],
MANAGERS=["nobody+manager@example.com"],
)
def test_manager_and_admin_mail_prefix(self):
"""
@@ -1859,13 +1861,40 @@ class BaseEmailBackendTests(MailTestsMixin):
mail_func("hi", "there")
self.assertEqual(self.get_mailbox_content(), [])
# RemovedInDjango70Warning.
def test_deprecated_admins_managers_tuples(self):
tests = (
[("nobody", "nobody@example.com"), ("other", "other@example.com")],
[["nobody", "nobody@example.com"], ["other", "other@example.com"]],
)
for setting, mail_func in (
("ADMINS", mail_admins),
("MANAGERS", mail_managers),
):
msg = (
f"Using (name, address) pairs in the {setting} setting is deprecated."
" Replace with a list of email address strings."
)
for value in tests:
self.flush_mailbox()
with (
self.subTest(setting=setting, value=value),
self.settings(**{setting: value}),
):
with self.assertWarnsMessage(RemovedInDjango70Warning, msg):
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])
def test_wrong_admins_managers(self):
tests = (
"test@example.com",
gettext_lazy("test@example.com"),
("test@example.com",),
["test@example.com", "other@example.com"],
("test@example.com", "other@example.com"),
# RemovedInDjango70Warning: uncomment these cases when support for
# deprecated (name, address) tuples is removed.
# [("nobody", "nobody@example.com"), ("other", "other@example.com")],
# [["nobody", "nobody@example.com"], ["other", "other@example.com"]],
[("name", "test", "example.com")],
[("Name <test@example.com",)],
[[]],
@@ -1874,13 +1903,13 @@ class BaseEmailBackendTests(MailTestsMixin):
("ADMINS", mail_admins),
("MANAGERS", mail_managers),
):
msg = "The %s setting must be a list of 2-tuples." % setting
msg = f"The {setting} setting must be a list of email address strings."
for value in tests:
with (
self.subTest(setting=setting, value=value),
self.settings(**{setting: value}),
):
with self.assertRaisesMessage(ValueError, msg):
with self.assertRaisesMessage(ImproperlyConfigured, msg):
mail_func("subject", "content")
def test_message_cc_header(self):