From e6dd70b4dba3b7d812fecda7288c9707990ea71b Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 9 Nov 2013 12:11:58 +0100 Subject: [PATCH] Fixed #21383 -- Added request details in SuspiciousOperation messages --- django/core/handlers/base.py | 7 ++++++- django/test/utils.py | 4 ++-- tests/logging_tests/tests.py | 9 +++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/django/core/handlers/base.py b/django/core/handlers/base.py index bf65687d5c..587cbae2c3 100644 --- a/django/core/handlers/base.py +++ b/django/core/handlers/base.py @@ -174,7 +174,12 @@ class BaseHandler(object): # The security logger receives events for all SuspiciousOperations security_logger = logging.getLogger('django.security.%s' % e.__class__.__name__) - security_logger.error(force_text(e)) + security_logger.error( + force_text(e), + extra={ + 'status_code': 400, + 'request': request + }) try: callback, param_dict = resolver.resolve400() diff --git a/django/test/utils.py b/django/test/utils.py index 781fa37beb..1599a5358b 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -407,8 +407,8 @@ def patch_logger(logger_name, log_level): """ calls = [] - def replacement(msg): - calls.append(msg) + def replacement(msg, *args, **kwargs): + calls.append(msg % args) logger = logging.getLogger(logger_name) orig = getattr(logger, log_level) setattr(logger, log_level, replacement) diff --git a/tests/logging_tests/tests.py b/tests/logging_tests/tests.py index 1b3a20cc24..1f9c8920c9 100644 --- a/tests/logging_tests/tests.py +++ b/tests/logging_tests/tests.py @@ -370,3 +370,12 @@ class SecurityLoggerTest(TestCase): self.client.get('/suspicious_spec/') self.assertEqual(len(calls), 1) self.assertEqual(calls[0], 'dubious') + + @override_settings( + ADMINS=(('admin', 'admin@example.com'),), + DEBUG=False, + ) + def test_suspicious_email_admins(self): + self.client.get('/suspicious/') + self.assertEqual(len(mail.outbox), 1) + self.assertIn('path:/suspicious/,', mail.outbox[0].body)