debug.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import inspect
  2. import sys
  3. import warnings
  4. from django.conf import settings
  5. from django.core.files.uploadedfile import SimpleUploadedFile
  6. from django.test import TestCase, RequestFactory
  7. from django.core.urlresolvers import reverse
  8. from django.template import TemplateSyntaxError
  9. from django.views.debug import ExceptionReporter
  10. from regressiontests.views import BrokenException, except_args
  11. class DebugViewTests(TestCase):
  12. def setUp(self):
  13. self.save_warnings_state()
  14. warnings.filterwarnings('ignore', category=DeprecationWarning,
  15. module='django.views.generic.simple')
  16. warnings.filterwarnings('ignore', category=DeprecationWarning,
  17. module='django.views.generic.create_update')
  18. warnings.filterwarnings('ignore', category=DeprecationWarning,
  19. module='django.views.generic.date_based')
  20. warnings.filterwarnings('ignore', category=DeprecationWarning,
  21. module='django.views.generic.list_detail')
  22. self.old_debug = settings.DEBUG
  23. settings.DEBUG = True
  24. self.old_template_debug = settings.TEMPLATE_DEBUG
  25. settings.TEMPLATE_DEBUG = True
  26. def tearDown(self):
  27. settings.DEBUG = self.old_debug
  28. settings.TEMPLATE_DEBUG = self.old_template_debug
  29. self.restore_warnings_state()
  30. def test_files(self):
  31. response = self.client.get('/views/raises/')
  32. self.assertEqual(response.status_code, 500)
  33. data = {
  34. 'file_data.txt': SimpleUploadedFile('file_data.txt', 'haha'),
  35. }
  36. response = self.client.post('/views/raises/', data)
  37. self.assertTrue('file_data.txt' in response.content)
  38. self.assertFalse('haha' in response.content)
  39. def test_404(self):
  40. response = self.client.get('/views/raises404/')
  41. self.assertEqual(response.status_code, 404)
  42. def test_view_exceptions(self):
  43. for n in range(len(except_args)):
  44. self.assertRaises(BrokenException, self.client.get,
  45. reverse('view_exception', args=(n,)))
  46. def test_template_exceptions(self):
  47. for n in range(len(except_args)):
  48. try:
  49. self.client.get(reverse('template_exception', args=(n,)))
  50. except TemplateSyntaxError, e:
  51. raising_loc = inspect.trace()[-1][-2][0].strip()
  52. self.assertFalse(raising_loc.find('raise BrokenException') == -1,
  53. "Failed to find 'raise BrokenException' in last frame of traceback, instead found: %s" %
  54. raising_loc)
  55. def test_template_loader_postmortem(self):
  56. response = self.client.get(reverse('raises_template_does_not_exist'))
  57. self.assertContains(response, 'templates/i_dont_exist.html</code> (File does not exist)</li>', status_code=500)
  58. class ExceptionReporterTests(TestCase):
  59. rf = RequestFactory()
  60. def test_request_and_exception(self):
  61. "A simple exception report can be generated"
  62. try:
  63. request = self.rf.get('/test_view/')
  64. raise ValueError("Can't find my keys")
  65. except ValueError:
  66. exc_type, exc_value, tb = sys.exc_info()
  67. reporter = ExceptionReporter(request, exc_type, exc_value, tb)
  68. html = reporter.get_traceback_html()
  69. self.assertIn('<h1>ValueError at /test_view/</h1>', html)
  70. self.assertIn('<pre class="exception_value">Can&#39;t find my keys</pre>', html)
  71. self.assertIn('<th>Request Method:</th>', html)
  72. self.assertIn('<th>Request URL:</th>', html)
  73. self.assertIn('<th>Exception Type:</th>', html)
  74. self.assertIn('<th>Exception Value:</th>', html)
  75. self.assertIn('<h2>Traceback ', html)
  76. self.assertIn('<h2>Request information</h2>', html)
  77. self.assertNotIn('<p>Request data not supplied</p>', html)
  78. def test_no_request(self):
  79. "An exception report can be generated without request"
  80. try:
  81. raise ValueError("Can't find my keys")
  82. except ValueError:
  83. exc_type, exc_value, tb = sys.exc_info()
  84. reporter = ExceptionReporter(None, exc_type, exc_value, tb)
  85. html = reporter.get_traceback_html()
  86. self.assertIn('<h1>ValueError</h1>', html)
  87. self.assertIn('<pre class="exception_value">Can&#39;t find my keys</pre>', html)
  88. self.assertNotIn('<th>Request Method:</th>', html)
  89. self.assertNotIn('<th>Request URL:</th>', html)
  90. self.assertIn('<th>Exception Type:</th>', html)
  91. self.assertIn('<th>Exception Value:</th>', html)
  92. self.assertIn('<h2>Traceback ', html)
  93. self.assertIn('<h2>Request information</h2>', html)
  94. self.assertIn('<p>Request data not supplied</p>', html)
  95. def test_no_exception(self):
  96. "An exception report can be generated for just a request"
  97. request = self.rf.get('/test_view/')
  98. reporter = ExceptionReporter(request, None, None, None)
  99. html = reporter.get_traceback_html()
  100. self.assertIn('<h1>Report at /test_view/</h1>', html)
  101. self.assertIn('<pre class="exception_value">No exception supplied</pre>', html)
  102. self.assertIn('<th>Request Method:</th>', html)
  103. self.assertIn('<th>Request URL:</th>', html)
  104. self.assertNotIn('<th>Exception Type:</th>', html)
  105. self.assertNotIn('<th>Exception Value:</th>', html)
  106. self.assertNotIn('<h2>Traceback ', html)
  107. self.assertIn('<h2>Request information</h2>', html)
  108. self.assertNotIn('<p>Request data not supplied</p>', html)
  109. def test_request_and_message(self):
  110. "A message can be provided in addition to a request"
  111. request = self.rf.get('/test_view/')
  112. reporter = ExceptionReporter(request, None, "I'm a little teapot", None)
  113. html = reporter.get_traceback_html()
  114. self.assertIn('<h1>Report at /test_view/</h1>', html)
  115. self.assertIn('<pre class="exception_value">I&#39;m a little teapot</pre>', html)
  116. self.assertIn('<th>Request Method:</th>', html)
  117. self.assertIn('<th>Request URL:</th>', html)
  118. self.assertNotIn('<th>Exception Type:</th>', html)
  119. self.assertNotIn('<th>Exception Value:</th>', html)
  120. self.assertNotIn('<h2>Traceback ', html)
  121. self.assertIn('<h2>Request information</h2>', html)
  122. self.assertNotIn('<p>Request data not supplied</p>', html)
  123. def test_message_only(self):
  124. reporter = ExceptionReporter(None, None, "I'm a little teapot", None)
  125. html = reporter.get_traceback_html()
  126. self.assertIn('<h1>Report</h1>', html)
  127. self.assertIn('<pre class="exception_value">I&#39;m a little teapot</pre>', html)
  128. self.assertNotIn('<th>Request Method:</th>', html)
  129. self.assertNotIn('<th>Request URL:</th>', html)
  130. self.assertNotIn('<th>Exception Type:</th>', html)
  131. self.assertNotIn('<th>Exception Value:</th>', html)
  132. self.assertNotIn('<h2>Traceback ', html)
  133. self.assertIn('<h2>Request information</h2>', html)
  134. self.assertIn('<p>Request data not supplied</p>', html)