test_exceptions.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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(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. class HandleHttp404ExceptionTests(TestCase):
  49. def setUp(self):
  50. self.exception = Http404()
  51. self.request = RequestFactory().get('/')
  52. def test_get_handle_http404_exception(self):
  53. """get_exception_handler returns correct Http404 exception handler"""
  54. found_handler = exceptionhandler.get_exception_handler(self.exception)
  55. self.assertEqual(
  56. found_handler, exceptionhandler.handle_http404_exception)
  57. response = found_handler(self.request, self.exception)
  58. self.assertEqual(response.status_code, 404)
  59. def test_handle_http404_exception(self):
  60. """handle_misago_exception handles Http404 exception correctly"""
  61. response = exceptionhandler.handle_misago_exception(
  62. self.request, self.exception)
  63. self.assertEqual(response.status_code, 404)
  64. class HandleOutdatedSlugExceptionTests(TestCase):
  65. def setUp(self):
  66. self.exception = OutdatedSlug()
  67. self.request = RequestFactory().get('/')
  68. def test_get_handle_outdated_slug_exception(self):
  69. """
  70. get_exception_handler returns correct OutdatedSlug exception handler
  71. """
  72. found_handler = exceptionhandler.get_exception_handler(self.exception)
  73. self.assertEqual(
  74. found_handler, exceptionhandler.handle_outdated_slug_exception)
  75. response = found_handler(self.request, self.exception)
  76. self.assertEqual(response.status_code, 301)
  77. def test_handle_outdated_slug_exception(self):
  78. """
  79. handle_misago_exception handles OutdatedSlug exception correctly
  80. """
  81. response = exceptionhandler.handle_misago_exception(
  82. self.request, self.exception)
  83. self.assertEqual(response.status_code, 301)
  84. class HandlePermissionDeniedExceptionTests(TestCase):
  85. def setUp(self):
  86. self.exception_message = "Page access not allowed"
  87. self.exception = PermissionDenied(self.exception_message)
  88. self.request = RequestFactory().get('/')
  89. def test_get_handle_permission_denied_exception(self):
  90. """
  91. get_exception_handler returns correct PermissionDenied exception
  92. handler
  93. """
  94. found_handler = exceptionhandler.get_exception_handler(self.exception)
  95. self.assertEqual(
  96. found_handler, exceptionhandler.handle_permission_denied_exception)
  97. response = found_handler(self.request, self.exception)
  98. self.assertIn(self.exception_message, response.content)
  99. self.assertEqual(response.status_code, 403)
  100. def test_handle_permission_denied_exception(self):
  101. """
  102. handle_misago_exception handles PermissionDenied exception correctly
  103. """
  104. response = exceptionhandler.handle_misago_exception(
  105. self.request, self.exception)
  106. self.assertIn(self.exception_message, response.content)
  107. self.assertEqual(response.status_code, 403)