mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #24526 -- Combined django.request/security loggers with the root logger.
Thanks Carl Meyer for review.
This commit is contained in:
@@ -9,7 +9,7 @@ from admin_scripts.tests import AdminScriptTestCase
|
||||
from django.core import mail
|
||||
from django.core.files.temp import NamedTemporaryFile
|
||||
from django.test import RequestFactory, TestCase, override_settings
|
||||
from django.test.utils import patch_logger
|
||||
from django.test.utils import LoggingCaptureMixin, patch_logger
|
||||
from django.utils.deprecation import RemovedInNextVersionWarning
|
||||
from django.utils.encoding import force_text
|
||||
from django.utils.log import (
|
||||
@@ -65,26 +65,18 @@ class LoggingFiltersTest(TestCase):
|
||||
self.assertEqual(filter_.filter("record is not used"), False)
|
||||
|
||||
|
||||
class DefaultLoggingTest(TestCase):
|
||||
def setUp(self):
|
||||
self.logger = logging.getLogger('django')
|
||||
self.old_stream = self.logger.handlers[0].stream
|
||||
|
||||
def tearDown(self):
|
||||
self.logger.handlers[0].stream = self.old_stream
|
||||
class DefaultLoggingTest(LoggingCaptureMixin, TestCase):
|
||||
|
||||
def test_django_logger(self):
|
||||
"""
|
||||
The 'django' base logger only output anything when DEBUG=True.
|
||||
"""
|
||||
output = StringIO()
|
||||
self.logger.handlers[0].stream = output
|
||||
self.logger.error("Hey, this is an error.")
|
||||
self.assertEqual(output.getvalue(), '')
|
||||
self.assertEqual(self.logger_output.getvalue(), '')
|
||||
|
||||
with self.settings(DEBUG=True):
|
||||
self.logger.error("Hey, this is an error.")
|
||||
self.assertEqual(output.getvalue(), 'Hey, this is an error.\n')
|
||||
self.assertEqual(self.logger_output.getvalue(), 'Hey, this is an error.\n')
|
||||
|
||||
|
||||
class WarningLoggerTests(TestCase):
|
||||
@@ -167,11 +159,10 @@ class CallbackFilterTest(TestCase):
|
||||
|
||||
|
||||
class AdminEmailHandlerTest(TestCase):
|
||||
logger = logging.getLogger('django.request')
|
||||
logger = logging.getLogger('django')
|
||||
|
||||
def get_admin_email_handler(self, logger):
|
||||
# Inspired from views/views.py: send_log()
|
||||
# ensuring the AdminEmailHandler does not get filtered out
|
||||
# Ensure that AdminEmailHandler does not get filtered out
|
||||
# even with DEBUG=True.
|
||||
admin_email_handler = [
|
||||
h for h in logger.handlers
|
||||
|
||||
@@ -19,6 +19,7 @@ from django.utils._os import upath
|
||||
from django.utils.deprecation import (
|
||||
RemovedInDjango20Warning, RemovedInDjango21Warning,
|
||||
)
|
||||
from django.utils.log import DEFAULT_LOGGING
|
||||
|
||||
warnings.simplefilter("error", RemovedInDjango20Warning)
|
||||
warnings.simplefilter("error", RemovedInDjango21Warning)
|
||||
@@ -144,6 +145,11 @@ def setup(verbosity, test_labels):
|
||||
'auth': 'django.contrib.auth.tests.migrations',
|
||||
'contenttypes': 'contenttypes_tests.migrations',
|
||||
}
|
||||
log_config = DEFAULT_LOGGING
|
||||
# Filter out non-error logging so we don't have to capture it in lots of
|
||||
# tests.
|
||||
log_config['loggers']['django']['level'] = 'ERROR'
|
||||
settings.LOGGING = log_config
|
||||
|
||||
if verbosity > 0:
|
||||
# Ensure any warnings captured to logging are piped through a verbose
|
||||
|
||||
@@ -16,6 +16,7 @@ from django.core.files.uploadedfile import SimpleUploadedFile
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.template.base import TemplateDoesNotExist
|
||||
from django.test import RequestFactory, TestCase, override_settings
|
||||
from django.test.utils import LoggingCaptureMixin
|
||||
from django.utils import six
|
||||
from django.utils.encoding import force_bytes, force_text
|
||||
from django.utils.functional import SimpleLazyObject
|
||||
@@ -48,7 +49,7 @@ class CallableSettingWrapperTests(TestCase):
|
||||
|
||||
|
||||
@override_settings(DEBUG=True, ROOT_URLCONF="view_tests.urls")
|
||||
class DebugViewTests(TestCase):
|
||||
class DebugViewTests(LoggingCaptureMixin, TestCase):
|
||||
|
||||
def test_files(self):
|
||||
response = self.client.get('/raises/')
|
||||
@@ -642,7 +643,7 @@ class ExceptionReportTestMixin(object):
|
||||
|
||||
|
||||
@override_settings(ROOT_URLCONF='view_tests.urls')
|
||||
class ExceptionReporterFilterTests(TestCase, ExceptionReportTestMixin):
|
||||
class ExceptionReporterFilterTests(ExceptionReportTestMixin, LoggingCaptureMixin, TestCase):
|
||||
"""
|
||||
Ensure that sensitive information can be filtered out of error reports.
|
||||
Refs #14614.
|
||||
@@ -833,7 +834,7 @@ class ExceptionReporterFilterTests(TestCase, ExceptionReportTestMixin):
|
||||
self.assertNotContains(response, 'should not be displayed', status_code=500)
|
||||
|
||||
|
||||
class AjaxResponseExceptionReporterFilter(TestCase, ExceptionReportTestMixin):
|
||||
class AjaxResponseExceptionReporterFilter(ExceptionReportTestMixin, LoggingCaptureMixin, TestCase):
|
||||
"""
|
||||
Ensure that sensitive information can be filtered out of error reports.
|
||||
|
||||
|
||||
@@ -102,7 +102,7 @@ def render_no_template(request):
|
||||
|
||||
|
||||
def send_log(request, exc_info):
|
||||
logger = logging.getLogger('django.request')
|
||||
logger = logging.getLogger('django')
|
||||
# The default logging config has a logging filter to ensure admin emails are
|
||||
# only sent with DEBUG=False, but since someone might choose to remove that
|
||||
# filter, we still want to be able to test the behavior of error emails
|
||||
|
||||
Reference in New Issue
Block a user