test_exceptionhandler.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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.core.exceptions import OutdatedSlug
  7. from misago.core 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_for_handled_exceptions(self):
  41. """Exception handler has correct handler for every Misago exception"""
  42. for exception in exceptionhandler.HANDLED_EXCEPTIONS:
  43. exceptionhandler.get_exception_handler(exception())
  44. def test_get_exception_handler_for_non_handled_exceptio(self):
  45. """Exception handler has no handler for non-supported exception"""
  46. for exception in INVALID_EXCEPTIONS:
  47. with self.assertRaises(ValueError):
  48. exceptionhandler.get_exception_handler(exception())