2012-09-20 19:03:24 +00:00
|
|
|
import logging
|
2016-09-25 22:25:38 +00:00
|
|
|
from contextlib import contextmanager
|
2017-01-07 11:11:46 +00:00
|
|
|
from io import StringIO
|
2011-06-22 06:01:44 +00:00
|
|
|
|
2015-01-28 12:35:27 +00:00
|
|
|
from admin_scripts.tests import AdminScriptTestCase
|
|
|
|
|
2015-09-15 18:13:34 +00:00
|
|
|
from django.conf import settings
|
2011-10-13 21:34:56 +00:00
|
|
|
from django.core import mail
|
2017-07-13 04:09:18 +00:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2014-10-21 22:32:46 +00:00
|
|
|
from django.core.files.temp import NamedTemporaryFile
|
2016-09-25 22:25:38 +00:00
|
|
|
from django.core.management import color
|
2017-07-13 04:09:18 +00:00
|
|
|
from django.http.multipartparser import MultiPartParserError
|
2015-04-17 21:38:20 +00:00
|
|
|
from django.test import RequestFactory, SimpleTestCase, override_settings
|
2017-07-13 04:09:18 +00:00
|
|
|
from django.test.utils import LoggingCaptureMixin
|
2014-11-10 21:21:31 +00:00
|
|
|
from django.utils.log import (
|
2015-09-15 18:13:34 +00:00
|
|
|
DEFAULT_LOGGING, AdminEmailHandler, CallbackFilter, RequireDebugFalse,
|
2016-09-25 22:25:38 +00:00
|
|
|
RequireDebugTrue, ServerFormatter,
|
2014-11-10 21:21:31 +00:00
|
|
|
)
|
2011-10-13 21:34:56 +00:00
|
|
|
|
2017-07-13 04:09:18 +00:00
|
|
|
from . import views
|
2012-11-19 17:57:40 +00:00
|
|
|
from .logconfig import MyEmailBackend
|
|
|
|
|
2011-06-22 06:01:44 +00:00
|
|
|
# logging config prior to using filter with mail_admins
|
|
|
|
OLD_LOGGING = {
|
|
|
|
'version': 1,
|
|
|
|
'disable_existing_loggers': False,
|
|
|
|
'handlers': {
|
|
|
|
'mail_admins': {
|
|
|
|
'level': 'ERROR',
|
|
|
|
'class': 'django.utils.log.AdminEmailHandler'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'loggers': {
|
|
|
|
'django.request': {
|
|
|
|
'handlers': ['mail_admins'],
|
|
|
|
'level': 'ERROR',
|
|
|
|
'propagate': True,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
class LoggingFiltersTest(SimpleTestCase):
|
2011-09-16 16:41:38 +00:00
|
|
|
def test_require_debug_false_filter(self):
|
|
|
|
"""
|
|
|
|
Test the RequireDebugFalse filter class.
|
|
|
|
"""
|
|
|
|
filter_ = RequireDebugFalse()
|
2011-06-22 06:01:44 +00:00
|
|
|
|
|
|
|
with self.settings(DEBUG=True):
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIs(filter_.filter("record is not used"), False)
|
2011-06-22 06:01:44 +00:00
|
|
|
|
|
|
|
with self.settings(DEBUG=False):
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIs(filter_.filter("record is not used"), True)
|
2011-06-22 06:01:44 +00:00
|
|
|
|
2012-12-24 21:32:41 +00:00
|
|
|
def test_require_debug_true_filter(self):
|
2011-06-22 06:01:44 +00:00
|
|
|
"""
|
2012-12-24 21:32:41 +00:00
|
|
|
Test the RequireDebugTrue filter class.
|
2011-06-22 06:01:44 +00:00
|
|
|
"""
|
2012-12-24 21:32:41 +00:00
|
|
|
filter_ = RequireDebugTrue()
|
2011-06-22 06:01:44 +00:00
|
|
|
|
2012-12-24 21:32:41 +00:00
|
|
|
with self.settings(DEBUG=True):
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIs(filter_.filter("record is not used"), True)
|
2011-06-22 06:01:44 +00:00
|
|
|
|
2012-12-24 21:32:41 +00:00
|
|
|
with self.settings(DEBUG=False):
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIs(filter_.filter("record is not used"), False)
|
2011-06-22 06:01:44 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class SetupDefaultLoggingMixin:
|
2012-09-26 17:56:21 +00:00
|
|
|
|
2015-09-15 18:13:34 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2017-01-21 13:13:44 +00:00
|
|
|
super().setUpClass()
|
2015-09-15 18:13:34 +00:00
|
|
|
cls._logging = settings.LOGGING
|
|
|
|
logging.config.dictConfig(DEFAULT_LOGGING)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
2017-01-21 13:13:44 +00:00
|
|
|
super().tearDownClass()
|
2015-09-15 18:13:34 +00:00
|
|
|
logging.config.dictConfig(cls._logging)
|
|
|
|
|
2016-04-13 21:38:17 +00:00
|
|
|
|
|
|
|
class DefaultLoggingTests(SetupDefaultLoggingMixin, LoggingCaptureMixin, SimpleTestCase):
|
|
|
|
|
2012-09-26 17:56:21 +00:00
|
|
|
def test_django_logger(self):
|
|
|
|
"""
|
|
|
|
The 'django' base logger only output anything when DEBUG=True.
|
|
|
|
"""
|
|
|
|
self.logger.error("Hey, this is an error.")
|
2015-03-23 22:17:43 +00:00
|
|
|
self.assertEqual(self.logger_output.getvalue(), '')
|
2012-09-26 17:56:21 +00:00
|
|
|
|
|
|
|
with self.settings(DEBUG=True):
|
|
|
|
self.logger.error("Hey, this is an error.")
|
2015-03-23 22:17:43 +00:00
|
|
|
self.assertEqual(self.logger_output.getvalue(), 'Hey, this is an error.\n')
|
2012-09-26 17:56:21 +00:00
|
|
|
|
2016-04-20 18:35:46 +00:00
|
|
|
@override_settings(DEBUG=True)
|
2015-09-15 18:13:34 +00:00
|
|
|
def test_django_logger_warning(self):
|
2016-04-20 18:35:46 +00:00
|
|
|
self.logger.warning('warning')
|
|
|
|
self.assertEqual(self.logger_output.getvalue(), 'warning\n')
|
2015-09-15 18:13:34 +00:00
|
|
|
|
2016-04-20 18:35:46 +00:00
|
|
|
@override_settings(DEBUG=True)
|
2015-09-15 18:13:34 +00:00
|
|
|
def test_django_logger_info(self):
|
2016-04-20 18:35:46 +00:00
|
|
|
self.logger.info('info')
|
|
|
|
self.assertEqual(self.logger_output.getvalue(), 'info\n')
|
2015-09-15 18:13:34 +00:00
|
|
|
|
2016-04-20 18:35:46 +00:00
|
|
|
@override_settings(DEBUG=True)
|
2015-09-15 18:13:34 +00:00
|
|
|
def test_django_logger_debug(self):
|
2016-04-20 18:35:46 +00:00
|
|
|
self.logger.debug('debug')
|
|
|
|
self.assertEqual(self.logger_output.getvalue(), '')
|
2015-09-15 18:13:34 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2017-07-13 04:09:18 +00:00
|
|
|
class LoggingAssertionMixin(object):
|
|
|
|
|
|
|
|
def assertLogsRequest(self, url, level, msg, status_code, logger='django.request', exc_class=None):
|
|
|
|
with self.assertLogs(logger, level) as cm:
|
|
|
|
try:
|
|
|
|
self.client.get(url)
|
|
|
|
except views.UncaughtException:
|
|
|
|
pass
|
|
|
|
self.assertEqual(
|
|
|
|
len(cm.records), 1,
|
|
|
|
"Wrong number of calls for logger %r in %r level." % (logger, level)
|
|
|
|
)
|
|
|
|
record = cm.records[0]
|
|
|
|
self.assertEqual(record.getMessage(), msg)
|
|
|
|
self.assertEqual(record.status_code, status_code)
|
|
|
|
if exc_class:
|
|
|
|
self.assertIsNotNone(record.exc_info)
|
|
|
|
self.assertEqual(record.exc_info[0], exc_class)
|
|
|
|
|
|
|
|
|
2016-04-13 21:38:17 +00:00
|
|
|
@override_settings(DEBUG=True, ROOT_URLCONF='logging_tests.urls')
|
2017-07-13 04:09:18 +00:00
|
|
|
class HandlerLoggingTests(SetupDefaultLoggingMixin, LoggingAssertionMixin, LoggingCaptureMixin, SimpleTestCase):
|
2016-04-13 21:38:17 +00:00
|
|
|
|
|
|
|
def test_page_found_no_warning(self):
|
|
|
|
self.client.get('/innocent/')
|
|
|
|
self.assertEqual(self.logger_output.getvalue(), '')
|
|
|
|
|
2017-07-13 04:09:18 +00:00
|
|
|
def test_redirect_no_warning(self):
|
|
|
|
self.client.get('/redirect/')
|
|
|
|
self.assertEqual(self.logger_output.getvalue(), '')
|
|
|
|
|
2016-04-13 21:38:17 +00:00
|
|
|
def test_page_not_found_warning(self):
|
2017-07-13 04:09:18 +00:00
|
|
|
self.assertLogsRequest(
|
|
|
|
url='/does_not_exist/',
|
|
|
|
level='WARNING',
|
|
|
|
status_code=404,
|
|
|
|
msg='Not Found: /does_not_exist/',
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_page_not_found_raised(self):
|
|
|
|
self.assertLogsRequest(
|
|
|
|
url='/does_not_exist_raised/',
|
|
|
|
level='WARNING',
|
|
|
|
status_code=404,
|
|
|
|
msg='Not Found: /does_not_exist_raised/',
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_uncaught_exception(self):
|
|
|
|
self.assertLogsRequest(
|
|
|
|
url='/uncaught_exception/',
|
|
|
|
level='ERROR',
|
|
|
|
status_code=500,
|
|
|
|
msg='Internal Server Error: /uncaught_exception/',
|
|
|
|
exc_class=views.UncaughtException,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_internal_server_error(self):
|
|
|
|
self.assertLogsRequest(
|
|
|
|
url='/internal_server_error/',
|
|
|
|
level='ERROR',
|
|
|
|
status_code=500,
|
|
|
|
msg='Internal Server Error: /internal_server_error/',
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_internal_server_error_599(self):
|
|
|
|
self.assertLogsRequest(
|
|
|
|
url='/internal_server_error/?status=599',
|
|
|
|
level='ERROR',
|
|
|
|
status_code=599,
|
|
|
|
msg='Unknown Status Code: /internal_server_error/',
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_permission_denied(self):
|
|
|
|
self.assertLogsRequest(
|
|
|
|
url='/permission_denied/',
|
|
|
|
level='WARNING',
|
|
|
|
status_code=403,
|
|
|
|
msg='Forbidden (Permission denied): /permission_denied/',
|
|
|
|
exc_class=PermissionDenied,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_multi_part_parser_error(self):
|
|
|
|
self.assertLogsRequest(
|
|
|
|
url='/multi_part_parser_error/',
|
|
|
|
level='WARNING',
|
|
|
|
status_code=400,
|
|
|
|
msg='Bad request (Unable to parse request body): /multi_part_parser_error/',
|
|
|
|
exc_class=MultiPartParserError,
|
|
|
|
)
|
2016-04-13 21:38:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
@override_settings(
|
|
|
|
DEBUG=True,
|
|
|
|
USE_I18N=True,
|
|
|
|
LANGUAGES=[('en', 'English')],
|
2015-11-07 15:12:37 +00:00
|
|
|
MIDDLEWARE=[
|
2016-04-13 21:38:17 +00:00
|
|
|
'django.middleware.locale.LocaleMiddleware',
|
|
|
|
'django.middleware.common.CommonMiddleware',
|
|
|
|
],
|
|
|
|
ROOT_URLCONF='logging_tests.urls_i18n',
|
|
|
|
)
|
|
|
|
class I18nLoggingTests(SetupDefaultLoggingMixin, LoggingCaptureMixin, SimpleTestCase):
|
|
|
|
|
2016-04-14 15:12:35 +00:00
|
|
|
def test_i18n_page_found_no_warning(self):
|
|
|
|
self.client.get('/exists/')
|
|
|
|
self.client.get('/en/exists/')
|
|
|
|
self.assertEqual(self.logger_output.getvalue(), '')
|
|
|
|
|
2016-04-13 21:38:17 +00:00
|
|
|
def test_i18n_page_not_found_warning(self):
|
|
|
|
self.client.get('/this_does_not/')
|
|
|
|
self.client.get('/en/nor_this/')
|
|
|
|
self.assertEqual(self.logger_output.getvalue(), 'Not Found: /this_does_not/\nNot Found: /en/nor_this/\n')
|
|
|
|
|
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
class CallbackFilterTest(SimpleTestCase):
|
2011-06-22 06:01:44 +00:00
|
|
|
def test_sense(self):
|
|
|
|
f_false = CallbackFilter(lambda r: False)
|
|
|
|
f_true = CallbackFilter(lambda r: True)
|
|
|
|
|
|
|
|
self.assertEqual(f_false.filter("record"), False)
|
|
|
|
self.assertEqual(f_true.filter("record"), True)
|
|
|
|
|
|
|
|
def test_passes_on_record(self):
|
|
|
|
collector = []
|
2011-09-16 16:41:38 +00:00
|
|
|
|
2011-06-22 06:01:44 +00:00
|
|
|
def _callback(record):
|
|
|
|
collector.append(record)
|
|
|
|
return True
|
|
|
|
f = CallbackFilter(_callback)
|
|
|
|
|
|
|
|
f.filter("a record")
|
|
|
|
|
|
|
|
self.assertEqual(collector, ["a record"])
|
2011-09-02 03:04:02 +00:00
|
|
|
|
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
class AdminEmailHandlerTest(SimpleTestCase):
|
2015-03-23 22:17:43 +00:00
|
|
|
logger = logging.getLogger('django')
|
2011-09-02 03:04:02 +00:00
|
|
|
|
2012-02-12 12:19:02 +00:00
|
|
|
def get_admin_email_handler(self, logger):
|
2016-10-27 07:53:39 +00:00
|
|
|
# AdminEmailHandler does not get filtered out
|
2012-02-12 12:19:02 +00:00
|
|
|
# even with DEBUG=True.
|
|
|
|
admin_email_handler = [
|
|
|
|
h for h in logger.handlers
|
|
|
|
if h.__class__.__name__ == "AdminEmailHandler"
|
2013-10-18 09:02:43 +00:00
|
|
|
][0]
|
2012-02-12 12:19:02 +00:00
|
|
|
return admin_email_handler
|
|
|
|
|
2013-01-21 19:27:38 +00:00
|
|
|
def test_fail_silently(self):
|
|
|
|
admin_email_handler = self.get_admin_email_handler(self.logger)
|
|
|
|
self.assertTrue(admin_email_handler.connection().fail_silently)
|
|
|
|
|
2011-09-02 03:04:02 +00:00
|
|
|
@override_settings(
|
2015-01-21 16:55:57 +00:00
|
|
|
ADMINS=[('whatever admin', 'admin@example.com')],
|
2013-10-19 23:33:10 +00:00
|
|
|
EMAIL_SUBJECT_PREFIX='-SuperAwesomeSubject-'
|
2013-10-18 09:02:43 +00:00
|
|
|
)
|
2011-09-02 03:04:02 +00:00
|
|
|
def test_accepts_args(self):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
User-supplied arguments and the EMAIL_SUBJECT_PREFIX setting are used
|
|
|
|
to compose the email subject (#16736).
|
2011-09-02 03:04:02 +00:00
|
|
|
"""
|
|
|
|
message = "Custom message that says '%s' and '%s'"
|
|
|
|
token1 = 'ping'
|
|
|
|
token2 = 'pong'
|
|
|
|
|
2012-09-26 17:56:21 +00:00
|
|
|
admin_email_handler = self.get_admin_email_handler(self.logger)
|
2011-09-02 03:04:02 +00:00
|
|
|
# Backup then override original filters
|
|
|
|
orig_filters = admin_email_handler.filters
|
2012-02-12 12:19:02 +00:00
|
|
|
try:
|
|
|
|
admin_email_handler.filters = []
|
2011-09-02 03:04:02 +00:00
|
|
|
|
2012-09-26 17:56:21 +00:00
|
|
|
self.logger.error(message, token1, token2)
|
2011-09-02 03:04:02 +00:00
|
|
|
|
2012-02-12 12:19:02 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
@override_settings(
|
2015-01-21 16:55:57 +00:00
|
|
|
ADMINS=[('whatever admin', 'admin@example.com')],
|
2013-10-19 23:33:10 +00:00
|
|
|
EMAIL_SUBJECT_PREFIX='-SuperAwesomeSubject-',
|
2015-01-21 16:55:57 +00:00
|
|
|
INTERNAL_IPS=['127.0.0.1'],
|
2013-10-18 09:02:43 +00:00
|
|
|
)
|
2012-02-12 12:19:02 +00:00
|
|
|
def test_accepts_args_and_request(self):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
The subject is also handled if being passed a request object.
|
2012-02-12 12:19:02 +00:00
|
|
|
"""
|
|
|
|
message = "Custom message that says '%s' and '%s'"
|
|
|
|
token1 = 'ping'
|
|
|
|
token2 = 'pong'
|
2011-09-02 03:04:02 +00:00
|
|
|
|
2012-09-26 17:56:21 +00:00
|
|
|
admin_email_handler = self.get_admin_email_handler(self.logger)
|
2012-02-12 12:19:02 +00:00
|
|
|
# Backup then override original filters
|
|
|
|
orig_filters = admin_email_handler.filters
|
|
|
|
try:
|
|
|
|
admin_email_handler.filters = []
|
|
|
|
rf = RequestFactory()
|
|
|
|
request = rf.get('/')
|
2016-04-08 02:04:45 +00:00
|
|
|
self.logger.error(
|
|
|
|
message, token1, token2,
|
2012-02-12 12:19:02 +00:00
|
|
|
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
|
2012-02-11 09:31:18 +00:00
|
|
|
|
|
|
|
@override_settings(
|
2015-01-21 16:55:57 +00:00
|
|
|
ADMINS=[('admin', 'admin@example.com')],
|
2013-10-19 23:33:10 +00:00
|
|
|
EMAIL_SUBJECT_PREFIX='',
|
|
|
|
DEBUG=False,
|
2013-10-18 09:02:43 +00:00
|
|
|
)
|
2012-02-11 09:31:18 +00:00
|
|
|
def test_subject_accepts_newlines(self):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
Newlines in email reports' subjects are escaped to prevent
|
|
|
|
AdminErrorHandler from failing (#17281).
|
2012-02-11 09:31:18 +00:00
|
|
|
"""
|
2012-06-07 16:08:47 +00:00
|
|
|
message = 'Message \r\n with newlines'
|
|
|
|
expected_subject = 'ERROR: Message \\r\\n with newlines'
|
2012-02-11 09:31:18 +00:00
|
|
|
|
|
|
|
self.assertEqual(len(mail.outbox), 0)
|
|
|
|
|
2012-09-26 17:56:21 +00:00
|
|
|
self.logger.error(message)
|
2012-02-11 09:31:18 +00:00
|
|
|
|
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertNotIn('\n', mail.outbox[0].subject)
|
|
|
|
self.assertNotIn('\r', mail.outbox[0].subject)
|
2012-02-11 09:31:18 +00:00
|
|
|
self.assertEqual(mail.outbox[0].subject, expected_subject)
|
|
|
|
|
2012-11-19 17:57:40 +00:00
|
|
|
@override_settings(
|
2015-01-21 16:55:57 +00:00
|
|
|
ADMINS=[('admin', 'admin@example.com')],
|
2013-10-19 23:33:10 +00:00
|
|
|
DEBUG=False,
|
2013-10-18 09:02:43 +00:00
|
|
|
)
|
2012-11-19 17:57:40 +00:00
|
|
|
def test_uses_custom_email_backend(self):
|
|
|
|
"""
|
|
|
|
Refs #19325
|
|
|
|
"""
|
|
|
|
message = 'All work and no play makes Jack a dull boy'
|
|
|
|
admin_email_handler = self.get_admin_email_handler(self.logger)
|
|
|
|
mail_admins_called = {'called': False}
|
|
|
|
|
|
|
|
def my_mail_admins(*args, **kwargs):
|
|
|
|
connection = kwargs['connection']
|
2013-05-21 09:42:15 +00:00
|
|
|
self.assertIsInstance(connection, MyEmailBackend)
|
2012-11-19 17:57:40 +00:00
|
|
|
mail_admins_called['called'] = True
|
|
|
|
|
|
|
|
# Monkeypatches
|
|
|
|
orig_mail_admins = mail.mail_admins
|
|
|
|
orig_email_backend = admin_email_handler.email_backend
|
|
|
|
mail.mail_admins = my_mail_admins
|
|
|
|
admin_email_handler.email_backend = (
|
2013-02-26 12:19:18 +00:00
|
|
|
'logging_tests.logconfig.MyEmailBackend')
|
2012-11-19 17:57:40 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
self.logger.error(message)
|
|
|
|
self.assertTrue(mail_admins_called['called'])
|
|
|
|
finally:
|
|
|
|
# Revert Monkeypatches
|
|
|
|
mail.mail_admins = orig_mail_admins
|
|
|
|
admin_email_handler.email_backend = orig_email_backend
|
|
|
|
|
2014-10-06 20:07:20 +00:00
|
|
|
@override_settings(
|
2015-01-21 16:55:57 +00:00
|
|
|
ADMINS=[('whatever admin', 'admin@example.com')],
|
2014-10-06 20:07:20 +00:00
|
|
|
)
|
|
|
|
def test_emit_non_ascii(self):
|
|
|
|
"""
|
|
|
|
#23593 - AdminEmailHandler should allow Unicode characters in the
|
|
|
|
request.
|
|
|
|
"""
|
|
|
|
handler = self.get_admin_email_handler(self.logger)
|
|
|
|
record = self.logger.makeRecord('name', logging.ERROR, 'function', 'lno', 'message', None, None)
|
|
|
|
rf = RequestFactory()
|
|
|
|
url_path = '/º'
|
|
|
|
record.request = rf.get(url_path)
|
|
|
|
handler.emit(record)
|
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
|
|
|
msg = mail.outbox[0]
|
|
|
|
self.assertEqual(msg.to, ['admin@example.com'])
|
|
|
|
self.assertEqual(msg.subject, "[Django] ERROR (EXTERNAL IP): message")
|
2015-07-09 15:27:07 +00:00
|
|
|
self.assertIn("Report at %s" % url_path, msg.body)
|
2014-10-06 20:07:20 +00:00
|
|
|
|
2014-11-10 21:21:31 +00:00
|
|
|
@override_settings(
|
2015-01-21 16:55:57 +00:00
|
|
|
MANAGERS=[('manager', 'manager@example.com')],
|
2014-11-10 21:21:31 +00:00
|
|
|
DEBUG=False,
|
|
|
|
)
|
|
|
|
def test_customize_send_mail_method(self):
|
|
|
|
class ManagerEmailHandler(AdminEmailHandler):
|
|
|
|
def send_mail(self, subject, message, *args, **kwargs):
|
|
|
|
mail.mail_managers(subject, message, *args, connection=self.connection(), **kwargs)
|
|
|
|
|
|
|
|
handler = ManagerEmailHandler()
|
|
|
|
record = self.logger.makeRecord('name', logging.ERROR, 'function', 'lno', 'message', None, None)
|
|
|
|
self.assertEqual(len(mail.outbox), 0)
|
|
|
|
handler.emit(record)
|
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
|
|
|
self.assertEqual(mail.outbox[0].to, ['manager@example.com'])
|
|
|
|
|
2015-09-03 18:52:49 +00:00
|
|
|
@override_settings(ALLOWED_HOSTS='example.com')
|
|
|
|
def test_disallowed_host_doesnt_crash(self):
|
|
|
|
admin_email_handler = self.get_admin_email_handler(self.logger)
|
|
|
|
old_include_html = admin_email_handler.include_html
|
|
|
|
|
|
|
|
# Text email
|
|
|
|
admin_email_handler.include_html = False
|
|
|
|
try:
|
|
|
|
self.client.get('/', HTTP_HOST='evil.com')
|
|
|
|
finally:
|
|
|
|
admin_email_handler.include_html = old_include_html
|
|
|
|
|
|
|
|
# HTML email
|
|
|
|
admin_email_handler.include_html = True
|
|
|
|
try:
|
|
|
|
self.client.get('/', HTTP_HOST='evil.com')
|
|
|
|
finally:
|
|
|
|
admin_email_handler.include_html = old_include_html
|
|
|
|
|
2012-09-24 20:11:42 +00:00
|
|
|
|
|
|
|
class SettingsConfigTest(AdminScriptTestCase):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
Accessing settings in a custom logging handler does not trigger
|
2012-09-24 20:11:42 +00:00
|
|
|
a circular import error.
|
|
|
|
"""
|
|
|
|
def setUp(self):
|
|
|
|
log_config = """{
|
|
|
|
'version': 1,
|
|
|
|
'handlers': {
|
|
|
|
'custom_handler': {
|
|
|
|
'level': 'INFO',
|
|
|
|
'class': 'logging_tests.logconfig.MyHandler',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
self.write_settings('settings.py', sdict={'LOGGING': log_config})
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.remove_settings('settings.py')
|
|
|
|
|
|
|
|
def test_circular_dependency(self):
|
|
|
|
# validate is just an example command to trigger settings configuration
|
2015-01-18 00:00:12 +00:00
|
|
|
out, err = self.run_manage(['check'])
|
2012-09-24 20:11:42 +00:00
|
|
|
self.assertNoOutput(err)
|
2014-01-22 19:09:02 +00:00
|
|
|
self.assertOutput(out, "System check identified no issues (0 silenced).")
|
2012-11-10 11:05:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def dictConfig(config):
|
|
|
|
dictConfig.called = True
|
2016-11-12 17:11:23 +00:00
|
|
|
|
|
|
|
|
2012-11-10 11:05:58 +00:00
|
|
|
dictConfig.called = False
|
|
|
|
|
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
class SetupConfigureLogging(SimpleTestCase):
|
2012-11-10 11:05:58 +00:00
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
Calling django.setup() initializes the logging configuration.
|
2012-11-10 11:05:58 +00:00
|
|
|
"""
|
2018-03-16 09:54:34 +00:00
|
|
|
@override_settings(
|
|
|
|
LOGGING_CONFIG='logging_tests.tests.dictConfig',
|
|
|
|
LOGGING=OLD_LOGGING,
|
|
|
|
)
|
2012-11-10 11:05:58 +00:00
|
|
|
def test_configure_initializes_logging(self):
|
2013-12-31 11:28:01 +00:00
|
|
|
from django import setup
|
|
|
|
setup()
|
2012-11-10 11:05:58 +00:00
|
|
|
self.assertTrue(dictConfig.called)
|
2013-05-15 23:14:28 +00:00
|
|
|
|
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(DEBUG=True, ROOT_URLCONF='logging_tests.urls')
|
2017-07-13 04:09:18 +00:00
|
|
|
class SecurityLoggerTest(LoggingAssertionMixin, SimpleTestCase):
|
2013-05-15 23:14:28 +00:00
|
|
|
|
|
|
|
def test_suspicious_operation_creates_log_message(self):
|
2017-07-13 04:09:18 +00:00
|
|
|
self.assertLogsRequest(
|
|
|
|
url='/suspicious/',
|
|
|
|
level='ERROR',
|
|
|
|
msg='dubious',
|
|
|
|
status_code=400,
|
|
|
|
logger='django.security.SuspiciousOperation',
|
|
|
|
)
|
2013-05-15 23:14:28 +00:00
|
|
|
|
|
|
|
def test_suspicious_operation_uses_sublogger(self):
|
2017-07-13 04:09:18 +00:00
|
|
|
self.assertLogsRequest(
|
|
|
|
url='/suspicious_spec/',
|
|
|
|
level='ERROR',
|
|
|
|
msg='dubious',
|
|
|
|
status_code=400,
|
|
|
|
logger='django.security.DisallowedHost',
|
|
|
|
)
|
2013-11-09 11:11:58 +00:00
|
|
|
|
|
|
|
@override_settings(
|
2015-01-21 16:55:57 +00:00
|
|
|
ADMINS=[('admin', 'admin@example.com')],
|
2013-11-09 11:11:58 +00:00
|
|
|
DEBUG=False,
|
|
|
|
)
|
|
|
|
def test_suspicious_email_admins(self):
|
|
|
|
self.client.get('/suspicious/')
|
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
2015-07-09 15:27:07 +00:00
|
|
|
self.assertIn('Report at /suspicious/', mail.outbox[0].body)
|
2014-09-29 21:09:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SettingsCustomLoggingTest(AdminScriptTestCase):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
Using a logging defaults are still applied when using a custom
|
2014-09-29 21:09:03 +00:00
|
|
|
callable in LOGGING_CONFIG (i.e., logging.config.fileConfig).
|
|
|
|
"""
|
|
|
|
def setUp(self):
|
|
|
|
logging_conf = """
|
|
|
|
[loggers]
|
|
|
|
keys=root
|
|
|
|
[handlers]
|
|
|
|
keys=stream
|
|
|
|
[formatters]
|
|
|
|
keys=simple
|
|
|
|
[logger_root]
|
|
|
|
handlers=stream
|
|
|
|
[handler_stream]
|
|
|
|
class=StreamHandler
|
|
|
|
formatter=simple
|
|
|
|
args=(sys.stdout,)
|
|
|
|
[formatter_simple]
|
|
|
|
format=%(message)s
|
|
|
|
"""
|
2014-10-21 22:32:46 +00:00
|
|
|
self.temp_file = NamedTemporaryFile()
|
2017-02-07 17:05:47 +00:00
|
|
|
self.temp_file.write(logging_conf.encode())
|
2014-09-29 21:09:03 +00:00
|
|
|
self.temp_file.flush()
|
|
|
|
sdict = {'LOGGING_CONFIG': '"logging.config.fileConfig"',
|
2014-10-21 01:49:44 +00:00
|
|
|
'LOGGING': 'r"%s"' % self.temp_file.name}
|
2014-09-29 21:09:03 +00:00
|
|
|
self.write_settings('settings.py', sdict=sdict)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.temp_file.close()
|
|
|
|
self.remove_settings('settings.py')
|
|
|
|
|
|
|
|
def test_custom_logging(self):
|
2015-01-18 00:00:12 +00:00
|
|
|
out, err = self.run_manage(['check'])
|
2014-09-29 21:09:03 +00:00
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertOutput(out, "System check identified no issues (0 silenced).")
|
2016-03-14 10:38:38 +00:00
|
|
|
|
|
|
|
|
2016-09-25 22:25:38 +00:00
|
|
|
class LogFormattersTests(SimpleTestCase):
|
|
|
|
|
|
|
|
def test_server_formatter_styles(self):
|
|
|
|
color_style = color.make_style('')
|
|
|
|
formatter = ServerFormatter()
|
|
|
|
formatter.style = color_style
|
|
|
|
log_msg = 'log message'
|
|
|
|
status_code_styles = [
|
|
|
|
(200, 'HTTP_SUCCESS'),
|
|
|
|
(100, 'HTTP_INFO'),
|
|
|
|
(304, 'HTTP_NOT_MODIFIED'),
|
|
|
|
(300, 'HTTP_REDIRECT'),
|
|
|
|
(404, 'HTTP_NOT_FOUND'),
|
|
|
|
(400, 'HTTP_BAD_REQUEST'),
|
|
|
|
(500, 'HTTP_SERVER_ERROR'),
|
|
|
|
]
|
|
|
|
for status_code, style in status_code_styles:
|
|
|
|
record = logging.makeLogRecord({'msg': log_msg, 'status_code': status_code})
|
|
|
|
self.assertEqual(formatter.format(record), getattr(color_style, style)(log_msg))
|
|
|
|
record = logging.makeLogRecord({'msg': log_msg})
|
|
|
|
self.assertEqual(formatter.format(record), log_msg)
|
|
|
|
|
|
|
|
def test_server_formatter_default_format(self):
|
|
|
|
server_time = '2016-09-25 10:20:30'
|
|
|
|
log_msg = 'log message'
|
|
|
|
logger = logging.getLogger('django.server')
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def patch_django_server_logger():
|
|
|
|
old_stream = logger.handlers[0].stream
|
2017-01-07 11:11:46 +00:00
|
|
|
new_stream = StringIO()
|
2016-09-25 22:25:38 +00:00
|
|
|
logger.handlers[0].stream = new_stream
|
|
|
|
yield new_stream
|
|
|
|
logger.handlers[0].stream = old_stream
|
|
|
|
|
|
|
|
with patch_django_server_logger() as logger_output:
|
|
|
|
logger.info(log_msg, extra={'server_time': server_time})
|
|
|
|
self.assertEqual('[%s] %s\n' % (server_time, log_msg), logger_output.getvalue())
|
|
|
|
|
|
|
|
with patch_django_server_logger() as logger_output:
|
|
|
|
logger.info(log_msg)
|
2016-12-07 22:42:31 +00:00
|
|
|
self.assertRegex(logger_output.getvalue(), r'^\[[-:,.\s\d]+\] %s' % log_msg)
|