test_exceptionhandler.py 5.7 KB

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