test_exceptions.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. """exceptionhandler.is_misago_exception recognizes handled exceptions"""
  18. for exception in exceptionhandler.HANDLED_EXCEPTIONS:
  19. try:
  20. raise exception()
  21. except exception as e:
  22. self.assertTrue(exceptionhandler.is_misago_exception(e))
  23. def test_is_misago_exception_false_for_not_handled_exceptions(self):
  24. """Misago exception handler fails to recognize other exceptions"""
  25. for exception in INVALID_EXCEPTIONS:
  26. try:
  27. raise exception()
  28. except exception as e:
  29. self.assertFalse(exceptionhandler.is_misago_exception(e))
  30. class GetExceptionHandlerTests(TestCase):
  31. def test_exception_handlers_list(self):
  32. """HANDLED_EXCEPTIONS length matches that of EXCEPTION_HANDLERS"""
  33. self.assertEqual(len(exceptionhandler.HANDLED_EXCEPTIONS),
  34. len(exceptionhandler.EXCEPTION_HANDLERS))
  35. def test_get_exception_handler(self):
  36. """Exception handler has correct handler for every Misago exception"""
  37. for exception in exceptionhandler.HANDLED_EXCEPTIONS:
  38. try:
  39. exceptionhandler.get_exception_handler(exception())
  40. except ValueError:
  41. self.fail(
  42. "%s has no exception handler defined " % exception.__name__)
  43. class HandleHttp404ExceptionTests(TestCase):
  44. def setUp(self):
  45. self.exception_message = "Text page could not be found"
  46. self.exception = Http404(self.exception_message)
  47. self.request = RequestFactory().get('/')
  48. def test_get_exception_handler(self):
  49. """get_exception_handler returns correct Http404 exception handler"""
  50. found_handler = exceptionhandler.get_exception_handler(self.exception)
  51. self.assertEqual(
  52. found_handler, exceptionhandler.handle_http404_exception)
  53. response = found_handler(self.request, self.exception)
  54. self.assertIn(self.exception_message, response.content)
  55. self.assertEqual(response.status_code, 404)
  56. def test_handle_misago_exception(self):
  57. """handle_misago_exception handles Http404 exception correctly"""
  58. response = exceptionhandler.handle_misago_exception(
  59. self.request, self.exception)
  60. self.assertIn(self.exception_message, response.content)
  61. self.assertEqual(response.status_code, 404)
  62. class HandleOutdatedSlugExceptionTests(TestCase):
  63. def setUp(self):
  64. self.exception = OutdatedSlug()
  65. self.request = RequestFactory().get('/')
  66. def test_get_exception_handler(self):
  67. """
  68. get_exception_handler returns correct OutdatedSlug exception handler
  69. """
  70. found_handler = exceptionhandler.get_exception_handler(self.exception)
  71. self.assertEqual(
  72. found_handler, exceptionhandler.handle_outdated_slug_exception)
  73. response = found_handler(self.request, self.exception)
  74. self.assertEqual(response.status_code, 301)
  75. def test_handle_misago_exception(self):
  76. """
  77. handle_misago_exception handles OutdatedSlug exception correctly
  78. """
  79. response = exceptionhandler.handle_misago_exception(
  80. self.request, self.exception)
  81. self.assertEqual(response.status_code, 301)
  82. class HandlePermissionDeniedExceptionTests(TestCase):
  83. def setUp(self):
  84. self.exception_message = "Page access not allowed"
  85. self.exception = PermissionDenied(self.exception_message)
  86. self.request = RequestFactory().get('/')
  87. def test_get_exception_handler(self):
  88. """
  89. get_exception_handler returns correct PermissionDenied exception handler
  90. """
  91. found_handler = exceptionhandler.get_exception_handler(self.exception)
  92. self.assertEqual(
  93. found_handler, exceptionhandler.handle_permission_denied_exception)
  94. response = found_handler(self.request, self.exception)
  95. self.assertIn(self.exception_message, response.content)
  96. self.assertEqual(response.status_code, 403)
  97. def test_handle_misago_exception(self):
  98. """
  99. handle_misago_exception handles PermissionDenied exception correctly
  100. """
  101. response = exceptionhandler.handle_misago_exception(
  102. self.request, self.exception)
  103. self.assertIn(self.exception_message, response.content)
  104. self.assertEqual(response.status_code, 403)