test_exceptions.py 5.0 KB

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