Fixed #30604 -- Made mail_admins()/mail_managers() raise ValueError if ADMINS/MANAGERS is set incorrectly.

This commit is contained in:
Hasan Ramezani 2019-06-29 01:50:43 +02:00 committed by Mariusz Felisiak
parent 29240a9952
commit 090ca6512f
3 changed files with 22 additions and 1 deletions

View File

@ -91,6 +91,8 @@ def mail_admins(subject, message, fail_silently=False, connection=None,
"""Send a message to the admins, as defined by the ADMINS setting.""" """Send a message to the admins, as defined by the ADMINS setting."""
if not settings.ADMINS: if not settings.ADMINS:
return return
if not all(isinstance(a, (list, tuple)) and len(a) == 2 for a in settings.ADMINS):
raise ValueError('The ADMINS setting must be a list of 2-tuples.')
mail = EmailMultiAlternatives( mail = EmailMultiAlternatives(
'%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject), message, '%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject), message,
settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS], settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
@ -106,6 +108,8 @@ def mail_managers(subject, message, fail_silently=False, connection=None,
"""Send a message to the managers, as defined by the MANAGERS setting.""" """Send a message to the managers, as defined by the MANAGERS setting."""
if not settings.MANAGERS: if not settings.MANAGERS:
return return
if not all(isinstance(a, (list, tuple)) and len(a) == 2 for a in settings.MANAGERS):
raise ValueError('The MANAGERS setting must be a list of 2-tuples.')
mail = EmailMultiAlternatives( mail = EmailMultiAlternatives(
'%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject), message, '%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject), message,
settings.SERVER_EMAIL, [a[1] for a in settings.MANAGERS], settings.SERVER_EMAIL, [a[1] for a in settings.MANAGERS],

View File

@ -991,6 +991,23 @@ class BaseEmailBackendTests(HeadersCheckMixin):
mail_managers('hi', 'there') mail_managers('hi', 'there')
self.assertEqual(self.get_mailbox_content(), []) self.assertEqual(self.get_mailbox_content(), [])
def test_wrong_admins_managers(self):
tests = (
'test@example.com',
('test@example.com',),
['test@example.com', 'other@example.com'],
('test@example.com', 'other@example.com'),
)
for setting, mail_func in (
('ADMINS', mail_admins),
('MANAGERS', mail_managers),
):
msg = 'The %s setting must be a list of 2-tuples.' % setting
for value in tests:
with self.subTest(setting=setting, value=value), self.settings(**{setting: value}):
with self.assertRaisesMessage(ValueError, msg):
mail_func('subject', 'content')
def test_message_cc_header(self): def test_message_cc_header(self):
""" """
Regression test for #7722 Regression test for #7722

View File

@ -340,7 +340,7 @@ class CommonMiddlewareTest(SimpleTestCase):
@override_settings( @override_settings(
IGNORABLE_404_URLS=[re.compile(r'foo')], IGNORABLE_404_URLS=[re.compile(r'foo')],
MANAGERS=['PHB@dilbert.com'], MANAGERS=[('PHD', 'PHB@dilbert.com')],
) )
class BrokenLinkEmailsMiddlewareTest(SimpleTestCase): class BrokenLinkEmailsMiddlewareTest(SimpleTestCase):