Fixed AdminEmailHandler to format the subject message correctly when arguments are passed.

This bug was hidden before r17480 as it basically didn't take the arguments into account. Refs #14973.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17512 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2012-02-12 12:19:02 +00:00
parent 328c70ef15
commit 1c9c29b5b2
2 changed files with 58 additions and 18 deletions

View File

@ -49,7 +49,7 @@ class AdminEmailHandler(logging.Handler):
record.levelname,
(request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS
and 'internal' or 'EXTERNAL'),
record.msg
record.getMessage()
)
filter = get_exception_reporter_filter(request)
request_repr = filter.get_request_repr(request)

View File

@ -4,7 +4,7 @@ import copy
from django.conf import compat_patch_logging_config
from django.core import mail
from django.test import TestCase
from django.test import TestCase, RequestFactory
from django.test.utils import override_settings
from django.utils.log import CallbackFilter, RequireDebugFalse, getLogger
@ -124,6 +124,16 @@ class CallbackFilterTest(TestCase):
class AdminEmailHandlerTest(TestCase):
def get_admin_email_handler(self, logger):
# Inspired from regressiontests/views/views.py: send_log()
# ensuring the AdminEmailHandler does not get filtered out
# even with DEBUG=True.
admin_email_handler = [
h for h in logger.handlers
if h.__class__.__name__ == "AdminEmailHandler"
][0]
return admin_email_handler
@override_settings(
ADMINS=(('whatever admin', 'admin@example.com'),),
EMAIL_SUBJECT_PREFIX='-SuperAwesomeSubject-'
@ -134,32 +144,62 @@ class AdminEmailHandlerTest(TestCase):
setting are used to compose the email subject.
Refs #16736.
"""
message = "Custom message that says '%s' and '%s'"
token1 = 'ping'
token2 = 'pong'
logger = getLogger('django.request')
# Inspired from regressiontests/views/views.py: send_log()
# ensuring the AdminEmailHandler does not get filtered out
# even with DEBUG=True.
admin_email_handler = [
h for h in logger.handlers
if h.__class__.__name__ == "AdminEmailHandler"
][0]
admin_email_handler = self.get_admin_email_handler(logger)
# Backup then override original filters
orig_filters = admin_email_handler.filters
admin_email_handler.filters = []
try:
admin_email_handler.filters = []
logger.error(message, token1, token2)
logger.error(message, token1, token2)
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].to, ['admin@example.com'])
self.assertEqual(mail.outbox[0].subject,
"-SuperAwesomeSubject-ERROR: Custom message that says 'ping' and 'pong'")
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].to, ['admin@example.com'])
self.assertEqual(mail.outbox[0].subject,
"-SuperAwesomeSubject-ERROR: Custom message that says 'ping' and 'pong'")
finally:
# Restore original filters
admin_email_handler.filters = orig_filters
# Restore original filters
admin_email_handler.filters = orig_filters
@override_settings(
ADMINS=(('whatever admin', 'admin@example.com'),),
EMAIL_SUBJECT_PREFIX='-SuperAwesomeSubject-',
INTERNAL_IPS=('127.0.0.1',),
)
def test_accepts_args_and_request(self):
"""
Ensure that the subject is also handled if being
passed a request object.
"""
message = "Custom message that says '%s' and '%s'"
token1 = 'ping'
token2 = 'pong'
logger = getLogger('django.request')
admin_email_handler = self.get_admin_email_handler(logger)
# Backup then override original filters
orig_filters = admin_email_handler.filters
try:
admin_email_handler.filters = []
rf = RequestFactory()
request = rf.get('/')
logger.error(message, token1, token2,
extra={
'status_code': 403,
'request': request,
}
)
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].to, ['admin@example.com'])
self.assertEqual(mail.outbox[0].subject,
"-SuperAwesomeSubject-ERROR (internal IP): Custom message that says 'ping' and 'pong'")
finally:
# Restore original filters
admin_email_handler.filters = orig_filters
@override_settings(
ADMINS=(('admin', 'admin@example.com'),),