diff options
| author | Mike Edmunds <medmunds@gmail.com> | 2025-02-14 10:58:01 -0800 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2025-03-21 10:17:57 +0100 |
| commit | e295033144e3085abaf9277d1bb0a6436ce73e01 (patch) | |
| tree | 582ec2ccaf6440a27c5bff5f6b73db2c8e5bd4ea /tests/mail | |
| parent | aed303aff57ac990894b6354af001b0e8ea55f71 (diff) | |
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.
Diffstat (limited to 'tests/mail')
| -rw-r--r-- | tests/mail/test_sendtestemail.py | 10 | ||||
| -rw-r--r-- | tests/mail/tests.py | 63 |
2 files changed, 48 insertions, 25 deletions
diff --git a/tests/mail/test_sendtestemail.py b/tests/mail/test_sendtestemail.py index c871f1a299..c7195ba9a1 100644 --- a/tests/mail/test_sendtestemail.py +++ b/tests/mail/test_sendtestemail.py @@ -4,14 +4,8 @@ from django.test import SimpleTestCase, override_settings @override_settings( - ADMINS=( - ("Admin", "admin@example.com"), - ("Admin and Manager", "admin_and_manager@example.com"), - ), - MANAGERS=( - ("Manager", "manager@example.com"), - ("Admin and Manager", "admin_and_manager@example.com"), - ), + ADMINS=["admin@example.com", "admin_and_manager@example.com"], + MANAGERS=["manager@example.com", "admin_and_manager@example.com"], ) class SendTestEmailManagementCommand(SimpleTestCase): """ diff --git a/tests/mail/tests.py b/tests/mail/tests.py index 950ec7fa35..8180887a8a 100644 --- a/tests/mail/tests.py +++ b/tests/mail/tests.py @@ -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): |
