test_exceptionhandler.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. with self.assertRaises(ValueError):
  53. exceptionhandler.get_exception_handler(exception())
  54. class HandleHttp404ExceptionTests(TestCase):
  55. def setUp(self):
  56. self.exception = Http404()
  57. self.request = RequestFactory().get(reverse('forum_index'))
  58. def test_get_handle_http404_exception(self):
  59. """get_exception_handler returns correct Http404 exception handler"""
  60. found_handler = exceptionhandler.get_exception_handler(self.exception)
  61. self.assertEqual(
  62. found_handler, exceptionhandler.handle_http404_exception)
  63. response = found_handler(self.request, self.exception)
  64. self.assertEqual(response.status_code, 404)
  65. def test_handle_http404_exception(self):
  66. """handle_misago_exception handles Http404 exception correctly"""
  67. response = exceptionhandler.handle_misago_exception(
  68. self.request, self.exception)
  69. self.assertEqual(response.status_code, 404)
  70. class HandleOutdatedSlugExceptionTests(TestCase):
  71. def setUp(self):
  72. self.exception = OutdatedSlug()
  73. self.request = RequestFactory().get(reverse('forum_index'))
  74. def test_get_handle_outdated_slug_exception(self):
  75. """
  76. get_exception_handler returns correct OutdatedSlug exception handler
  77. """
  78. found_handler = exceptionhandler.get_exception_handler(self.exception)
  79. self.assertEqual(
  80. found_handler, exceptionhandler.handle_outdated_slug_exception)
  81. response = found_handler(self.request, self.exception)
  82. self.assertEqual(response.status_code, 301)
  83. def test_handle_outdated_slug_exception(self):
  84. """
  85. handle_misago_exception handles OutdatedSlug exception correctly
  86. """
  87. response = exceptionhandler.handle_misago_exception(
  88. self.request, self.exception)
  89. self.assertEqual(response.status_code, 301)
  90. class HandlePermissionDeniedExceptionTests(TestCase):
  91. def setUp(self):
  92. self.exception_message = "Page access not allowed"
  93. self.exception = PermissionDenied(self.exception_message)
  94. self.request = RequestFactory().get(reverse('forum_index'))
  95. def test_get_handle_permission_denied_exception(self):
  96. """
  97. get_exception_handler returns correct PermissionDenied exception
  98. handler
  99. """
  100. found_handler = exceptionhandler.get_exception_handler(self.exception)
  101. self.assertEqual(
  102. found_handler, exceptionhandler.handle_permission_denied_exception)
  103. response = found_handler(self.request, self.exception)
  104. self.assertIn(self.exception_message, response.content)
  105. self.assertEqual(response.status_code, 403)
  106. def test_handle_permission_denied_exception(self):
  107. """
  108. handle_misago_exception handles PermissionDenied exception correctly
  109. """
  110. response = exceptionhandler.handle_misago_exception(
  111. self.request, self.exception)
  112. self.assertIn(self.exception_message, response.content)
  113. self.assertEqual(response.status_code, 403)
  114. class GetExceptionMessageTests(TestCase):
  115. def test_get_exception_message(self):
  116. """_get_exception_message returns message for exception with one"""
  117. message = "Fish's name Eric"
  118. exception = Http404(message)
  119. self.assertEqual(exceptionhandler._get_exception_message(exception),
  120. message)
  121. def test_get_no_exception_message(self):
  122. """_get_exception_message returns None for exception without one"""
  123. exception = Http404()
  124. self.assertEqual(exceptionhandler._get_exception_message(exception),
  125. None)