2014-04-14 21:18:03 +00:00
|
|
|
import logging
|
|
|
|
|
2015-09-11 23:35:21 +00:00
|
|
|
from django.template import Context, Engine, Variable, VariableDoesNotExist
|
2016-09-09 01:24:22 +00:00
|
|
|
from django.test import SimpleTestCase, ignore_warnings
|
|
|
|
from django.utils.deprecation import RemovedInDjango21Warning
|
2014-04-14 21:18:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestHandler(logging.Handler):
|
|
|
|
def __init__(self):
|
|
|
|
super(TestHandler, self).__init__()
|
|
|
|
self.log_record = None
|
|
|
|
|
|
|
|
def emit(self, record):
|
|
|
|
self.log_record = record
|
|
|
|
|
|
|
|
|
2015-09-11 23:35:21 +00:00
|
|
|
class BaseTemplateLoggingTestCase(SimpleTestCase):
|
2014-04-14 21:18:03 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.test_handler = TestHandler()
|
|
|
|
self.logger = logging.getLogger('django.template')
|
|
|
|
self.original_level = self.logger.level
|
|
|
|
self.logger.addHandler(self.test_handler)
|
2015-09-11 23:35:21 +00:00
|
|
|
self.logger.setLevel(self.loglevel)
|
2014-04-14 21:18:03 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.logger.removeHandler(self.test_handler)
|
|
|
|
self.logger.level = self.original_level
|
|
|
|
|
2015-09-11 23:35:21 +00:00
|
|
|
|
|
|
|
class VariableResolveLoggingTests(BaseTemplateLoggingTestCase):
|
|
|
|
loglevel = logging.DEBUG
|
|
|
|
|
2014-04-14 21:18:03 +00:00
|
|
|
def test_log_on_variable_does_not_exist_silent(self):
|
2017-01-19 07:39:46 +00:00
|
|
|
class TestObject:
|
2014-04-14 21:18:03 +00:00
|
|
|
class SilentDoesNotExist(Exception):
|
|
|
|
silent_variable_failure = True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def template_name(self):
|
2015-09-21 19:20:19 +00:00
|
|
|
return "template_name"
|
2014-04-14 21:18:03 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def template(self):
|
2015-04-11 23:41:45 +00:00
|
|
|
return Engine().from_string('')
|
2014-04-14 21:18:03 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def article(self):
|
|
|
|
raise TestObject.SilentDoesNotExist("Attribute does not exist.")
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(attr for attr in dir(TestObject) if attr[:2] != "__")
|
|
|
|
|
|
|
|
def __getitem__(self, item):
|
|
|
|
return self.__dict__[item]
|
|
|
|
|
|
|
|
Variable('article').resolve(TestObject())
|
2015-09-21 19:20:19 +00:00
|
|
|
|
2014-04-14 21:18:03 +00:00
|
|
|
self.assertEqual(
|
2015-09-21 19:20:19 +00:00
|
|
|
self.test_handler.log_record.getMessage(),
|
|
|
|
"Exception while resolving variable 'article' in template 'template_name'."
|
2014-04-14 21:18:03 +00:00
|
|
|
)
|
2015-09-21 19:20:19 +00:00
|
|
|
self.assertIsNotNone(self.test_handler.log_record.exc_info)
|
|
|
|
raised_exception = self.test_handler.log_record.exc_info[1]
|
|
|
|
self.assertEqual(str(raised_exception), 'Attribute does not exist.')
|
2014-04-14 21:18:03 +00:00
|
|
|
|
|
|
|
def test_log_on_variable_does_not_exist_not_silent(self):
|
|
|
|
with self.assertRaises(VariableDoesNotExist):
|
|
|
|
Variable('article.author').resolve({'article': {'section': 'News'}})
|
|
|
|
|
|
|
|
self.assertEqual(
|
2015-09-21 19:20:19 +00:00
|
|
|
self.test_handler.log_record.getMessage(),
|
|
|
|
"Exception while resolving variable 'author' in template 'unknown'."
|
|
|
|
)
|
|
|
|
self.assertIsNotNone(self.test_handler.log_record.exc_info)
|
|
|
|
raised_exception = self.test_handler.log_record.exc_info[1]
|
|
|
|
self.assertEqual(
|
|
|
|
str(raised_exception),
|
|
|
|
'Failed lookup for key [author] in %r' % ("{%r: %r}" % ('section', 'News'))
|
2014-04-14 21:18:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_no_log_when_variable_exists(self):
|
|
|
|
Variable('article.section').resolve({'article': {'section': 'News'}})
|
|
|
|
self.assertIsNone(self.test_handler.log_record)
|
2015-09-11 23:35:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class IncludeNodeLoggingTests(BaseTemplateLoggingTestCase):
|
|
|
|
loglevel = logging.WARN
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
super(IncludeNodeLoggingTests, cls).setUpClass()
|
|
|
|
cls.engine = Engine(loaders=[
|
|
|
|
('django.template.loaders.locmem.Loader', {
|
|
|
|
'child': '{{ raises_exception }}',
|
|
|
|
}),
|
|
|
|
], debug=False)
|
|
|
|
|
|
|
|
def error_method():
|
|
|
|
raise IndexError("some generic exception")
|
|
|
|
|
|
|
|
cls.ctx = Context({'raises_exception': error_method})
|
|
|
|
|
|
|
|
def test_logs_exceptions_during_rendering_with_debug_disabled(self):
|
|
|
|
template = self.engine.from_string('{% include "child" %}')
|
|
|
|
template.name = 'template_name'
|
2016-09-09 01:24:22 +00:00
|
|
|
with ignore_warnings(category=RemovedInDjango21Warning):
|
|
|
|
self.assertEqual(template.render(self.ctx), '')
|
2015-09-11 23:35:21 +00:00
|
|
|
self.assertEqual(
|
|
|
|
self.test_handler.log_record.getMessage(),
|
|
|
|
"Exception raised while rendering {% include %} for template "
|
|
|
|
"'template_name'. Empty string rendered instead."
|
|
|
|
)
|
|
|
|
self.assertIsNotNone(self.test_handler.log_record.exc_info)
|
|
|
|
self.assertEqual(self.test_handler.log_record.levelno, logging.WARN)
|
|
|
|
|
|
|
|
def test_logs_exceptions_during_rendering_with_no_template_name(self):
|
|
|
|
template = self.engine.from_string('{% include "child" %}')
|
2016-09-09 01:24:22 +00:00
|
|
|
with ignore_warnings(category=RemovedInDjango21Warning):
|
|
|
|
self.assertEqual(template.render(self.ctx), '')
|
2015-09-11 23:35:21 +00:00
|
|
|
self.assertEqual(
|
|
|
|
self.test_handler.log_record.getMessage(),
|
|
|
|
"Exception raised while rendering {% include %} for template "
|
|
|
|
"'unknown'. Empty string rendered instead."
|
|
|
|
)
|
|
|
|
self.assertIsNotNone(self.test_handler.log_record.exc_info)
|
|
|
|
self.assertEqual(self.test_handler.log_record.levelno, logging.WARN)
|