test_exceptionhandlers.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from django.core import exceptions as django_exceptions
  2. from django.core.exceptions import PermissionDenied
  3. from django.http import Http404
  4. from django.test import TestCase
  5. from misago.core import exceptionhandler
  6. INVALID_EXCEPTIONS = (
  7. django_exceptions.ObjectDoesNotExist,
  8. django_exceptions.ViewDoesNotExist,
  9. TypeError,
  10. ValueError,
  11. KeyError,
  12. )
  13. class IsMisagoExceptionTests(TestCase):
  14. def test_is_misago_exception_true_for_handled_exceptions(self):
  15. """
  16. exceptionhandler.is_misago_exception recognizes handled exceptions
  17. """
  18. for exception in exceptionhandler.HANDLED_EXCEPTIONS:
  19. self.assertTrue(exceptionhandler.is_misago_exception(exception()))
  20. def test_is_misago_exception_false_for_not_handled_exceptions(self):
  21. """
  22. exceptionhandler.is_misago_exception fails to recognize other
  23. exceptions
  24. """
  25. for exception in INVALID_EXCEPTIONS:
  26. self.assertFalse(exceptionhandler.is_misago_exception(exception()))
  27. class GetExceptionHandlerTests(TestCase):
  28. def test_exception_handlers_list(self):
  29. """HANDLED_EXCEPTIONS length matches that of EXCEPTION_HANDLERS"""
  30. self.assertEqual(len(exceptionhandler.HANDLED_EXCEPTIONS),
  31. len(exceptionhandler.EXCEPTION_HANDLERS))
  32. def test_get_exception_handler_for_handled_exceptions(self):
  33. """Exception handler has correct handler for every Misago exception"""
  34. for exception in exceptionhandler.HANDLED_EXCEPTIONS:
  35. exceptionhandler.get_exception_handler(exception())
  36. def test_get_exception_handler_for_non_handled_exceptio(self):
  37. """Exception handler has no handler for non-supported exception"""
  38. for exception in INVALID_EXCEPTIONS:
  39. with self.assertRaises(ValueError):
  40. exceptionhandler.get_exception_handler(exception())
  41. class HandleAPIExceptionTests(TestCase):
  42. def test_permission_denied(self):
  43. """permission denied exception is correctly handled"""
  44. response = exceptionhandler.handle_api_exception(PermissionDenied())
  45. self.assertEqual(response.status_code, 403)
  46. self.assertEqual(response.data['detail'], "Permission denied")
  47. def test_permission_message_denied(self):
  48. """permission denied with message is correctly handled"""
  49. exception = PermissionDenied("You shall not pass!")
  50. response = exceptionhandler.handle_api_exception(exception)
  51. self.assertEqual(response.status_code, 403)
  52. self.assertEqual(response.data['detail'], "You shall not pass!")
  53. def test_unhandled_exception(self):
  54. """our exception handler is not interrupting other exceptions"""
  55. for exception in INVALID_EXCEPTIONS:
  56. response = exceptionhandler.handle_api_exception(exception())
  57. self.assertIsNone(response)
  58. response = exceptionhandler.handle_api_exception(Http404())
  59. self.assertEqual(response.status_code, 404)
  60. self.assertEqual(response.data['detail'], "Not found")