test_exceptionhandler.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. from django.core.urlresolvers import reverse
  2. from django.http import Http404
  3. from django.core import exceptions as django_exceptions
  4. from django.core.exceptions import PermissionDenied
  5. from django.test import TestCase
  6. from django.test.client import RequestFactory
  7. from misago.core.exceptions import OutdatedSlug
  8. from misago.core import exceptionhandler
  9. INVALID_EXCEPTIONS = (
  10. django_exceptions.ObjectDoesNotExist,
  11. django_exceptions.ViewDoesNotExist,
  12. TypeError,
  13. ValueError,
  14. KeyError,
  15. )
  16. class IsMisagoExceptionTests(TestCase):
  17. def test_is_misago_exception_true_for_handled_exceptions(self):
  18. """
  19. exceptionhandler.is_misago_exception recognizes handled exceptions
  20. """
  21. for exception in exceptionhandler.HANDLED_EXCEPTIONS:
  22. try:
  23. raise exception()
  24. except exception as e:
  25. self.assertTrue(exceptionhandler.is_misago_exception(e))
  26. def test_is_misago_exception_false_for_not_handled_exceptions(self):
  27. """
  28. exceptionhandler.is_misago_exception fails to recognize other
  29. exceptions
  30. """
  31. for exception in INVALID_EXCEPTIONS:
  32. try:
  33. raise exception()
  34. except exception as e:
  35. self.assertFalse(exceptionhandler.is_misago_exception(e))
  36. class GetExceptionHandlerTests(TestCase):
  37. def test_exception_handlers_list(self):
  38. """HANDLED_EXCEPTIONS length matches that of EXCEPTION_HANDLERS"""
  39. self.assertEqual(len(exceptionhandler.HANDLED_EXCEPTIONS),
  40. len(exceptionhandler.EXCEPTION_HANDLERS))
  41. def test_get_exception_handler_for_handled_exceptions(self):
  42. """Exception handler has correct handler for every Misago exception"""
  43. for exception in exceptionhandler.HANDLED_EXCEPTIONS:
  44. try:
  45. exceptionhandler.get_exception_handler(exception())
  46. except ValueError:
  47. self.fail(
  48. "%s has no exception handler defined" % exception.__name__)
  49. def test_get_exception_handler_for_non_handled_exceptio(self):
  50. """Exception handler has no handler for non-supported exception"""
  51. for exception in INVALID_EXCEPTIONS:
  52. try:
  53. exceptionhandler.get_exception_handler(exception())
  54. self.fail("%s has exception handler, but it "
  55. "shouldn't" % exception.__name__)
  56. except ValueError:
  57. pass
  58. class HandleHttp404ExceptionTests(TestCase):
  59. def setUp(self):
  60. self.exception = Http404()
  61. self.request = RequestFactory().get(reverse('forum_index'))
  62. def test_get_handle_http404_exception(self):
  63. """get_exception_handler returns correct Http404 exception handler"""
  64. found_handler = exceptionhandler.get_exception_handler(self.exception)
  65. self.assertEqual(
  66. found_handler, exceptionhandler.handle_http404_exception)
  67. response = found_handler(self.request, self.exception)
  68. self.assertEqual(response.status_code, 404)
  69. def test_handle_http404_exception(self):
  70. """handle_misago_exception handles Http404 exception correctly"""
  71. response = exceptionhandler.handle_misago_exception(
  72. self.request, self.exception)
  73. self.assertEqual(response.status_code, 404)
  74. class HandleOutdatedSlugExceptionTests(TestCase):
  75. def setUp(self):
  76. self.exception = OutdatedSlug()
  77. self.request = RequestFactory().get(reverse('forum_index'))
  78. def test_get_handle_outdated_slug_exception(self):
  79. """
  80. get_exception_handler returns correct OutdatedSlug exception handler
  81. """
  82. found_handler = exceptionhandler.get_exception_handler(self.exception)
  83. self.assertEqual(
  84. found_handler, exceptionhandler.handle_outdated_slug_exception)
  85. response = found_handler(self.request, self.exception)
  86. self.assertEqual(response.status_code, 301)
  87. def test_handle_outdated_slug_exception(self):
  88. """
  89. handle_misago_exception handles OutdatedSlug exception correctly
  90. """
  91. response = exceptionhandler.handle_misago_exception(
  92. self.request, self.exception)
  93. self.assertEqual(response.status_code, 301)
  94. class HandlePermissionDeniedExceptionTests(TestCase):
  95. def setUp(self):
  96. self.exception_message = "Page access not allowed"
  97. self.exception = PermissionDenied(self.exception_message)
  98. self.request = RequestFactory().get(reverse('forum_index'))
  99. def test_get_handle_permission_denied_exception(self):
  100. """
  101. get_exception_handler returns correct PermissionDenied exception
  102. handler
  103. """
  104. found_handler = exceptionhandler.get_exception_handler(self.exception)
  105. self.assertEqual(
  106. found_handler, exceptionhandler.handle_permission_denied_exception)
  107. response = found_handler(self.request, self.exception)
  108. self.assertIn(self.exception_message, response.content)
  109. self.assertEqual(response.status_code, 403)
  110. def test_handle_permission_denied_exception(self):
  111. """
  112. handle_misago_exception handles PermissionDenied exception correctly
  113. """
  114. response = exceptionhandler.handle_misago_exception(
  115. self.request, self.exception)
  116. self.assertIn(self.exception_message, response.content)
  117. self.assertEqual(response.status_code, 403)
  118. class GetExceptionMessageTests(TestCase):
  119. def test_get_exception_message(self):
  120. """_get_exception_message returns message for exception with one"""
  121. message = "Fish's name Eric"
  122. exception = Http404(message)
  123. self.assertEqual(exceptionhandler._get_exception_message(exception),
  124. message)
  125. def test_get_no exception_message(self):
  126. """_get_exception_message returns None for exception without one"""
  127. exception = Http404()
  128. self.assertEqual(exceptionhandler._get_exception_message(exception),
  129. None)