2014-10-07 04:07:20 +08:00
|
|
|
# -*- coding:utf-8 -*-
|
2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2012-09-21 03:03:24 +08:00
|
|
|
import logging
|
2012-05-03 21:27:01 +08:00
|
|
|
import warnings
|
2011-06-22 14:01:44 +08:00
|
|
|
|
2011-10-14 05:34:56 +08:00
|
|
|
from django.core import mail
|
2014-10-22 06:32:46 +08:00
|
|
|
from django.core.files.temp import NamedTemporaryFile
|
2013-12-23 23:01:13 +08:00
|
|
|
from django.test import TestCase, RequestFactory, override_settings
|
|
|
|
from django.test.utils import patch_logger
|
2012-12-08 18:13:52 +08:00
|
|
|
from django.utils.encoding import force_text
|
2014-03-08 18:13:45 +08:00
|
|
|
from django.utils.deprecation import RemovedInNextVersionWarning
|
2014-11-11 05:21:31 +08:00
|
|
|
from django.utils.log import (
|
|
|
|
AdminEmailHandler, CallbackFilter, RequireDebugFalse, RequireDebugTrue,
|
|
|
|
)
|
2012-09-27 01:56:21 +08:00
|
|
|
from django.utils.six import StringIO
|
2011-10-14 05:34:56 +08:00
|
|
|
|
2013-02-26 20:19:18 +08:00
|
|
|
from admin_scripts.tests import AdminScriptTestCase
|
2012-09-25 04:11:42 +08:00
|
|
|
|
2012-11-20 01:57:40 +08:00
|
|
|
from .logconfig import MyEmailBackend
|
|
|
|
|
2011-09-02 11:04:02 +08:00
|
|
|
|
2011-06-22 14:01:44 +08: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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-25 05:32:41 +08:00
|
|
|
class LoggingFiltersTest(TestCase):
|
2011-09-17 00:41:38 +08:00
|
|
|
def test_require_debug_false_filter(self):
|
|
|
|
"""
|
|
|
|
Test the RequireDebugFalse filter class.
|
|
|
|
"""
|
|
|
|
filter_ = RequireDebugFalse()
|
2011-06-22 14:01:44 +08:00
|
|
|
|
|
|
|
with self.settings(DEBUG=True):
|
2011-09-17 00:41:38 +08:00
|
|
|
self.assertEqual(filter_.filter("record is not used"), False)
|
2011-06-22 14:01:44 +08:00
|
|
|
|
|
|
|
with self.settings(DEBUG=False):
|
2011-09-17 00:41:38 +08:00
|
|
|
self.assertEqual(filter_.filter("record is not used"), True)
|
2011-06-22 14:01:44 +08:00
|
|
|
|
2012-12-25 05:32:41 +08:00
|
|
|
def test_require_debug_true_filter(self):
|
2011-06-22 14:01:44 +08:00
|
|
|
"""
|
2012-12-25 05:32:41 +08:00
|
|
|
Test the RequireDebugTrue filter class.
|
2011-06-22 14:01:44 +08:00
|
|
|
"""
|
2012-12-25 05:32:41 +08:00
|
|
|
filter_ = RequireDebugTrue()
|
2011-06-22 14:01:44 +08:00
|
|
|
|
2012-12-25 05:32:41 +08:00
|
|
|
with self.settings(DEBUG=True):
|
|
|
|
self.assertEqual(filter_.filter("record is not used"), True)
|
2011-06-22 14:01:44 +08:00
|
|
|
|
2012-12-25 05:32:41 +08:00
|
|
|
with self.settings(DEBUG=False):
|
|
|
|
self.assertEqual(filter_.filter("record is not used"), False)
|
2011-06-22 14:01:44 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2012-09-27 01:56:21 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
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(), '')
|
|
|
|
|
|
|
|
with self.settings(DEBUG=True):
|
|
|
|
self.logger.error("Hey, this is an error.")
|
|
|
|
self.assertEqual(output.getvalue(), 'Hey, this is an error.\n')
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2012-11-17 08:50:50 +08:00
|
|
|
class WarningLoggerTests(TestCase):
|
|
|
|
"""
|
2014-03-08 18:13:45 +08:00
|
|
|
Tests that warnings output for RemovedInDjangoXXWarning (XX being the next
|
|
|
|
Django version) is enabled and captured to the logging system
|
2012-11-17 08:50:50 +08:00
|
|
|
"""
|
|
|
|
def setUp(self):
|
2013-03-28 01:05:31 +08:00
|
|
|
# If tests are invoke with "-Wall" (or any -W flag actually) then
|
2014-01-02 05:55:50 +08:00
|
|
|
# warning logging gets disabled (see configure_logging in django/utils/log.py).
|
|
|
|
# However, these tests expect warnings to be logged, so manually force warnings
|
2013-03-28 06:01:11 +08:00
|
|
|
# to the logs. Use getattr() here because the logging capture state is
|
|
|
|
# undocumented and (I assume) brittle.
|
|
|
|
self._old_capture_state = bool(getattr(logging, '_warnings_showwarning', False))
|
2013-03-28 01:05:31 +08:00
|
|
|
logging.captureWarnings(True)
|
|
|
|
|
2013-01-03 08:00:39 +08:00
|
|
|
# this convoluted setup is to avoid printing this deprecation to
|
|
|
|
# stderr during test running - as the test runner forces deprecations
|
|
|
|
# to be displayed at the global py.warnings level
|
2012-11-17 08:50:50 +08:00
|
|
|
self.logger = logging.getLogger('py.warnings')
|
2013-01-03 08:00:39 +08:00
|
|
|
self.outputs = []
|
|
|
|
self.old_streams = []
|
|
|
|
for handler in self.logger.handlers:
|
|
|
|
self.old_streams.append(handler.stream)
|
|
|
|
self.outputs.append(StringIO())
|
|
|
|
handler.stream = self.outputs[-1]
|
2012-11-17 08:50:50 +08:00
|
|
|
|
|
|
|
def tearDown(self):
|
2013-01-03 08:00:39 +08:00
|
|
|
for i, handler in enumerate(self.logger.handlers):
|
|
|
|
self.logger.handlers[i].stream = self.old_streams[i]
|
2012-11-17 08:50:50 +08:00
|
|
|
|
2013-03-28 01:05:31 +08:00
|
|
|
# Reset warnings state.
|
2013-03-28 06:01:11 +08:00
|
|
|
logging.captureWarnings(self._old_capture_state)
|
2013-03-28 01:05:31 +08:00
|
|
|
|
2012-11-17 08:50:50 +08:00
|
|
|
@override_settings(DEBUG=True)
|
|
|
|
def test_warnings_capture(self):
|
2015-01-08 07:49:02 +08:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings('always')
|
|
|
|
warnings.warn('Foo Deprecated', RemovedInNextVersionWarning)
|
|
|
|
output = force_text(self.outputs[0].getvalue())
|
|
|
|
self.assertIn('Foo Deprecated', output)
|
2012-11-17 08:50:50 +08:00
|
|
|
|
|
|
|
def test_warnings_capture_debug_false(self):
|
2015-01-08 07:49:02 +08:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings('always')
|
|
|
|
warnings.warn('Foo Deprecated', RemovedInNextVersionWarning)
|
|
|
|
output = force_text(self.outputs[0].getvalue())
|
|
|
|
self.assertNotIn('Foo Deprecated', output)
|
2012-11-17 08:50:50 +08:00
|
|
|
|
2014-03-08 18:51:08 +08:00
|
|
|
@override_settings(DEBUG=True)
|
|
|
|
def test_error_filter_still_raises(self):
|
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings(
|
|
|
|
'error',
|
|
|
|
category=RemovedInNextVersionWarning
|
|
|
|
)
|
|
|
|
with self.assertRaises(RemovedInNextVersionWarning):
|
|
|
|
warnings.warn('Foo Deprecated', RemovedInNextVersionWarning)
|
|
|
|
|
2012-09-27 01:56:21 +08:00
|
|
|
|
2011-06-22 14:01:44 +08:00
|
|
|
class CallbackFilterTest(TestCase):
|
|
|
|
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-17 00:41:38 +08:00
|
|
|
|
2011-06-22 14:01:44 +08:00
|
|
|
def _callback(record):
|
|
|
|
collector.append(record)
|
|
|
|
return True
|
|
|
|
f = CallbackFilter(_callback)
|
|
|
|
|
|
|
|
f.filter("a record")
|
|
|
|
|
|
|
|
self.assertEqual(collector, ["a record"])
|
2011-09-02 11:04:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
class AdminEmailHandlerTest(TestCase):
|
2012-09-27 01:56:21 +08:00
|
|
|
logger = logging.getLogger('django.request')
|
2011-09-02 11:04:02 +08:00
|
|
|
|
2012-02-12 20:19:02 +08:00
|
|
|
def get_admin_email_handler(self, logger):
|
2013-02-26 20:19:18 +08:00
|
|
|
# Inspired from views/views.py: send_log()
|
2012-02-12 20:19:02 +08:00
|
|
|
# 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"
|
2013-10-18 17:02:43 +08:00
|
|
|
][0]
|
2012-02-12 20:19:02 +08:00
|
|
|
return admin_email_handler
|
|
|
|
|
2013-01-22 03:27:38 +08: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 11:04:02 +08:00
|
|
|
@override_settings(
|
2013-10-20 07:33:10 +08:00
|
|
|
ADMINS=(('whatever admin', 'admin@example.com'),),
|
|
|
|
EMAIL_SUBJECT_PREFIX='-SuperAwesomeSubject-'
|
2013-10-18 17:02:43 +08:00
|
|
|
)
|
2011-09-02 11:04:02 +08:00
|
|
|
def test_accepts_args(self):
|
|
|
|
"""
|
|
|
|
Ensure that user-supplied arguments and the EMAIL_SUBJECT_PREFIX
|
|
|
|
setting are used to compose the email subject.
|
|
|
|
Refs #16736.
|
|
|
|
"""
|
|
|
|
message = "Custom message that says '%s' and '%s'"
|
|
|
|
token1 = 'ping'
|
|
|
|
token2 = 'pong'
|
|
|
|
|
2012-09-27 01:56:21 +08:00
|
|
|
admin_email_handler = self.get_admin_email_handler(self.logger)
|
2011-09-02 11:04:02 +08:00
|
|
|
# Backup then override original filters
|
|
|
|
orig_filters = admin_email_handler.filters
|
2012-02-12 20:19:02 +08:00
|
|
|
try:
|
|
|
|
admin_email_handler.filters = []
|
2011-09-02 11:04:02 +08:00
|
|
|
|
2012-09-27 01:56:21 +08:00
|
|
|
self.logger.error(message, token1, token2)
|
2011-09-02 11:04:02 +08:00
|
|
|
|
2012-02-12 20:19:02 +08: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(
|
2013-10-20 07:33:10 +08:00
|
|
|
ADMINS=(('whatever admin', 'admin@example.com'),),
|
|
|
|
EMAIL_SUBJECT_PREFIX='-SuperAwesomeSubject-',
|
|
|
|
INTERNAL_IPS=('127.0.0.1',),
|
2013-10-18 17:02:43 +08:00
|
|
|
)
|
2012-02-12 20:19:02 +08:00
|
|
|
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'
|
2011-09-02 11:04:02 +08:00
|
|
|
|
2012-09-27 01:56:21 +08:00
|
|
|
admin_email_handler = self.get_admin_email_handler(self.logger)
|
2012-02-12 20:19:02 +08:00
|
|
|
# Backup then override original filters
|
|
|
|
orig_filters = admin_email_handler.filters
|
|
|
|
try:
|
|
|
|
admin_email_handler.filters = []
|
|
|
|
rf = RequestFactory()
|
|
|
|
request = rf.get('/')
|
2012-09-27 01:56:21 +08:00
|
|
|
self.logger.error(message, token1, token2,
|
2012-02-12 20:19:02 +08: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 17:31:18 +08:00
|
|
|
|
|
|
|
@override_settings(
|
2013-10-20 07:33:10 +08:00
|
|
|
ADMINS=(('admin', 'admin@example.com'),),
|
|
|
|
EMAIL_SUBJECT_PREFIX='',
|
|
|
|
DEBUG=False,
|
2013-10-18 17:02:43 +08:00
|
|
|
)
|
2012-02-11 17:31:18 +08:00
|
|
|
def test_subject_accepts_newlines(self):
|
|
|
|
"""
|
|
|
|
Ensure that newlines in email reports' subjects are escaped to avoid
|
|
|
|
AdminErrorHandler to fail.
|
|
|
|
Refs #17281.
|
|
|
|
"""
|
2012-06-08 00:08:47 +08:00
|
|
|
message = 'Message \r\n with newlines'
|
|
|
|
expected_subject = 'ERROR: Message \\r\\n with newlines'
|
2012-02-11 17:31:18 +08:00
|
|
|
|
|
|
|
self.assertEqual(len(mail.outbox), 0)
|
|
|
|
|
2012-09-27 01:56:21 +08:00
|
|
|
self.logger.error(message)
|
2012-02-11 17:31:18 +08:00
|
|
|
|
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertNotIn('\n', mail.outbox[0].subject)
|
|
|
|
self.assertNotIn('\r', mail.outbox[0].subject)
|
2012-02-11 17:31:18 +08:00
|
|
|
self.assertEqual(mail.outbox[0].subject, expected_subject)
|
|
|
|
|
|
|
|
@override_settings(
|
2013-10-20 07:33:10 +08:00
|
|
|
ADMINS=(('admin', 'admin@example.com'),),
|
|
|
|
EMAIL_SUBJECT_PREFIX='',
|
|
|
|
DEBUG=False,
|
2013-10-18 17:02:43 +08:00
|
|
|
)
|
2012-02-11 17:31:18 +08:00
|
|
|
def test_truncate_subject(self):
|
|
|
|
"""
|
|
|
|
RFC 2822's hard limit is 998 characters per line.
|
|
|
|
So, minus "Subject: ", the actual subject must be no longer than 989
|
|
|
|
characters.
|
|
|
|
Refs #17281.
|
|
|
|
"""
|
|
|
|
message = 'a' * 1000
|
|
|
|
expected_subject = 'ERROR: aa' + 'a' * 980
|
|
|
|
|
|
|
|
self.assertEqual(len(mail.outbox), 0)
|
|
|
|
|
2012-09-27 01:56:21 +08:00
|
|
|
self.logger.error(message)
|
2012-02-11 17:31:18 +08:00
|
|
|
|
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
|
|
|
self.assertEqual(mail.outbox[0].subject, expected_subject)
|
2012-09-25 04:11:42 +08:00
|
|
|
|
2012-11-20 01:57:40 +08:00
|
|
|
@override_settings(
|
2013-10-20 07:33:10 +08:00
|
|
|
ADMINS=(('admin', 'admin@example.com'),),
|
|
|
|
DEBUG=False,
|
2013-10-18 17:02:43 +08:00
|
|
|
)
|
2012-11-20 01:57:40 +08: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 17:42:15 +08:00
|
|
|
self.assertIsInstance(connection, MyEmailBackend)
|
2012-11-20 01:57:40 +08: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 20:19:18 +08:00
|
|
|
'logging_tests.logconfig.MyEmailBackend')
|
2012-11-20 01:57:40 +08: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-07 04:07:20 +08:00
|
|
|
@override_settings(
|
|
|
|
ADMINS=(('whatever admin', 'admin@example.com'),),
|
|
|
|
)
|
|
|
|
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")
|
|
|
|
self.assertIn("path:%s" % url_path, msg.body)
|
|
|
|
|
2014-11-11 05:21:31 +08:00
|
|
|
@override_settings(
|
|
|
|
MANAGERS=(('manager', 'manager@example.com'),),
|
|
|
|
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'])
|
|
|
|
|
2012-09-25 04:11:42 +08:00
|
|
|
|
|
|
|
class SettingsConfigTest(AdminScriptTestCase):
|
|
|
|
"""
|
|
|
|
Test that accessing settings in a custom logging handler does not trigger
|
|
|
|
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 08:00:12 +08:00
|
|
|
out, err = self.run_manage(['check'])
|
2012-09-25 04:11:42 +08:00
|
|
|
self.assertNoOutput(err)
|
2014-01-23 03:09:02 +08:00
|
|
|
self.assertOutput(out, "System check identified no issues (0 silenced).")
|
2012-11-10 19:05:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
def dictConfig(config):
|
|
|
|
dictConfig.called = True
|
|
|
|
dictConfig.called = False
|
|
|
|
|
|
|
|
|
2013-12-31 19:28:01 +08:00
|
|
|
class SetupConfigureLogging(TestCase):
|
2012-11-10 19:05:58 +08:00
|
|
|
"""
|
2013-12-31 19:28:01 +08:00
|
|
|
Test that calling django.setup() initializes the logging configuration.
|
2012-11-10 19:05:58 +08:00
|
|
|
"""
|
2014-09-30 05:09:03 +08:00
|
|
|
@override_settings(LOGGING_CONFIG='logging_tests.tests.dictConfig',
|
|
|
|
LOGGING=OLD_LOGGING)
|
2012-11-10 19:05:58 +08:00
|
|
|
def test_configure_initializes_logging(self):
|
2013-12-31 19:28:01 +08:00
|
|
|
from django import setup
|
|
|
|
setup()
|
2012-11-10 19:05:58 +08:00
|
|
|
self.assertTrue(dictConfig.called)
|
2013-05-16 07:14:28 +08:00
|
|
|
|
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(DEBUG=True, ROOT_URLCONF='logging_tests.urls')
|
2013-05-16 07:14:28 +08:00
|
|
|
class SecurityLoggerTest(TestCase):
|
|
|
|
|
|
|
|
def test_suspicious_operation_creates_log_message(self):
|
2013-08-17 02:12:10 +08:00
|
|
|
with patch_logger('django.security.SuspiciousOperation', 'error') as calls:
|
2013-10-19 20:31:38 +08:00
|
|
|
self.client.get('/suspicious/')
|
2013-08-17 02:12:10 +08:00
|
|
|
self.assertEqual(len(calls), 1)
|
|
|
|
self.assertEqual(calls[0], 'dubious')
|
2013-05-16 07:14:28 +08:00
|
|
|
|
|
|
|
def test_suspicious_operation_uses_sublogger(self):
|
2013-08-17 02:12:10 +08:00
|
|
|
with patch_logger('django.security.DisallowedHost', 'error') as calls:
|
2013-10-19 20:31:38 +08:00
|
|
|
self.client.get('/suspicious_spec/')
|
2013-08-17 02:12:10 +08:00
|
|
|
self.assertEqual(len(calls), 1)
|
|
|
|
self.assertEqual(calls[0], 'dubious')
|
2013-11-09 19:11:58 +08:00
|
|
|
|
|
|
|
@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)
|
2014-09-30 05:09:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
class SettingsCustomLoggingTest(AdminScriptTestCase):
|
|
|
|
"""
|
|
|
|
Test that using a logging defaults are still applied when using a custom
|
|
|
|
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-22 06:32:46 +08:00
|
|
|
self.temp_file = NamedTemporaryFile()
|
2014-09-30 05:09:03 +08:00
|
|
|
self.temp_file.write(logging_conf.encode('utf-8'))
|
|
|
|
self.temp_file.flush()
|
|
|
|
sdict = {'LOGGING_CONFIG': '"logging.config.fileConfig"',
|
2014-10-21 09:49:44 +08:00
|
|
|
'LOGGING': 'r"%s"' % self.temp_file.name}
|
2014-09-30 05:09:03 +08: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 08:00:12 +08:00
|
|
|
out, err = self.run_manage(['check'])
|
2014-09-30 05:09:03 +08:00
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertOutput(out, "System check identified no issues (0 silenced).")
|