123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- import inspect
- import sys
- import warnings
- from django.conf import settings
- from django.core.files.uploadedfile import SimpleUploadedFile
- from django.test import TestCase, RequestFactory
- from django.core.urlresolvers import reverse
- from django.template import TemplateSyntaxError
- from django.views.debug import ExceptionReporter
- from regressiontests.views import BrokenException, except_args
- class DebugViewTests(TestCase):
- def setUp(self):
- self.save_warnings_state()
- warnings.filterwarnings('ignore', category=DeprecationWarning,
- module='django.views.generic.simple')
- warnings.filterwarnings('ignore', category=DeprecationWarning,
- module='django.views.generic.create_update')
- warnings.filterwarnings('ignore', category=DeprecationWarning,
- module='django.views.generic.date_based')
- warnings.filterwarnings('ignore', category=DeprecationWarning,
- module='django.views.generic.list_detail')
- self.old_debug = settings.DEBUG
- settings.DEBUG = True
- self.old_template_debug = settings.TEMPLATE_DEBUG
- settings.TEMPLATE_DEBUG = True
- def tearDown(self):
- settings.DEBUG = self.old_debug
- settings.TEMPLATE_DEBUG = self.old_template_debug
- self.restore_warnings_state()
- def test_files(self):
- response = self.client.get('/views/raises/')
- self.assertEqual(response.status_code, 500)
- data = {
- 'file_data.txt': SimpleUploadedFile('file_data.txt', 'haha'),
- }
- response = self.client.post('/views/raises/', data)
- self.assertTrue('file_data.txt' in response.content)
- self.assertFalse('haha' in response.content)
- def test_404(self):
- response = self.client.get('/views/raises404/')
- self.assertEqual(response.status_code, 404)
- def test_view_exceptions(self):
- for n in range(len(except_args)):
- self.assertRaises(BrokenException, self.client.get,
- reverse('view_exception', args=(n,)))
- def test_template_exceptions(self):
- for n in range(len(except_args)):
- try:
- self.client.get(reverse('template_exception', args=(n,)))
- except TemplateSyntaxError, e:
- raising_loc = inspect.trace()[-1][-2][0].strip()
- self.assertFalse(raising_loc.find('raise BrokenException') == -1,
- "Failed to find 'raise BrokenException' in last frame of traceback, instead found: %s" %
- raising_loc)
- def test_template_loader_postmortem(self):
- response = self.client.get(reverse('raises_template_does_not_exist'))
- self.assertContains(response, 'templates/i_dont_exist.html</code> (File does not exist)</li>', status_code=500)
- class ExceptionReporterTests(TestCase):
- rf = RequestFactory()
- def test_request_and_exception(self):
- "A simple exception report can be generated"
- try:
- request = self.rf.get('/test_view/')
- raise ValueError("Can't find my keys")
- except ValueError:
- exc_type, exc_value, tb = sys.exc_info()
- reporter = ExceptionReporter(request, exc_type, exc_value, tb)
- html = reporter.get_traceback_html()
- self.assertIn('<h1>ValueError at /test_view/</h1>', html)
- self.assertIn('<pre class="exception_value">Can't find my keys</pre>', html)
- self.assertIn('<th>Request Method:</th>', html)
- self.assertIn('<th>Request URL:</th>', html)
- self.assertIn('<th>Exception Type:</th>', html)
- self.assertIn('<th>Exception Value:</th>', html)
- self.assertIn('<h2>Traceback ', html)
- self.assertIn('<h2>Request information</h2>', html)
- self.assertNotIn('<p>Request data not supplied</p>', html)
- def test_no_request(self):
- "An exception report can be generated without request"
- try:
- raise ValueError("Can't find my keys")
- except ValueError:
- exc_type, exc_value, tb = sys.exc_info()
- reporter = ExceptionReporter(None, exc_type, exc_value, tb)
- html = reporter.get_traceback_html()
- self.assertIn('<h1>ValueError</h1>', html)
- self.assertIn('<pre class="exception_value">Can't find my keys</pre>', html)
- self.assertNotIn('<th>Request Method:</th>', html)
- self.assertNotIn('<th>Request URL:</th>', html)
- self.assertIn('<th>Exception Type:</th>', html)
- self.assertIn('<th>Exception Value:</th>', html)
- self.assertIn('<h2>Traceback ', html)
- self.assertIn('<h2>Request information</h2>', html)
- self.assertIn('<p>Request data not supplied</p>', html)
- def test_no_exception(self):
- "An exception report can be generated for just a request"
- request = self.rf.get('/test_view/')
- reporter = ExceptionReporter(request, None, None, None)
- html = reporter.get_traceback_html()
- self.assertIn('<h1>Report at /test_view/</h1>', html)
- self.assertIn('<pre class="exception_value">No exception supplied</pre>', html)
- self.assertIn('<th>Request Method:</th>', html)
- self.assertIn('<th>Request URL:</th>', html)
- self.assertNotIn('<th>Exception Type:</th>', html)
- self.assertNotIn('<th>Exception Value:</th>', html)
- self.assertNotIn('<h2>Traceback ', html)
- self.assertIn('<h2>Request information</h2>', html)
- self.assertNotIn('<p>Request data not supplied</p>', html)
- def test_request_and_message(self):
- "A message can be provided in addition to a request"
- request = self.rf.get('/test_view/')
- reporter = ExceptionReporter(request, None, "I'm a little teapot", None)
- html = reporter.get_traceback_html()
- self.assertIn('<h1>Report at /test_view/</h1>', html)
- self.assertIn('<pre class="exception_value">I'm a little teapot</pre>', html)
- self.assertIn('<th>Request Method:</th>', html)
- self.assertIn('<th>Request URL:</th>', html)
- self.assertNotIn('<th>Exception Type:</th>', html)
- self.assertNotIn('<th>Exception Value:</th>', html)
- self.assertNotIn('<h2>Traceback ', html)
- self.assertIn('<h2>Request information</h2>', html)
- self.assertNotIn('<p>Request data not supplied</p>', html)
- def test_message_only(self):
- reporter = ExceptionReporter(None, None, "I'm a little teapot", None)
- html = reporter.get_traceback_html()
- self.assertIn('<h1>Report</h1>', html)
- self.assertIn('<pre class="exception_value">I'm a little teapot</pre>', html)
- self.assertNotIn('<th>Request Method:</th>', html)
- self.assertNotIn('<th>Request URL:</th>', html)
- self.assertNotIn('<th>Exception Type:</th>', html)
- self.assertNotIn('<th>Exception Value:</th>', html)
- self.assertNotIn('<h2>Traceback ', html)
- self.assertIn('<h2>Request information</h2>', html)
- self.assertIn('<p>Request data not supplied</p>', html)
|