test_exceptionhandler.py 5.3 KB

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