test_exceptions.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from django.test import TestCase
  2. from django.http import Http404 as DjHttp404
  3. from django.core import exceptions as django_exceptions
  4. from misago.views import exceptions as misago_exceptions
  5. from misago.views.exceptions import handler
  6. DJANGO_EXCEPTIONS = (
  7. django_exceptions.PermissionDenied,
  8. django_exceptions.ViewDoesNotExist,
  9. DjHttp404,
  10. )
  11. PYTHON_EXCEPTIONS = (
  12. TypeError,
  13. ValueError,
  14. KeyError,
  15. )
  16. class MisagoExceptionHandlerTests(TestCase):
  17. def test_misago_exception_list_valid(self):
  18. """MISAGO_EXCEPTIONS list is valid"""
  19. self.assertEqual(len(misago_exceptions.MISAGO_EXCEPTIONS),
  20. len(misago_exceptions.__all__))
  21. for exception in misago_exceptions.MISAGO_EXCEPTIONS:
  22. if exception.__name__ not in misago_exceptions.__all__:
  23. self.fail("%s is registered in "
  24. "misago.exceptions.MISAGO_EXCEPTIONS but not in "
  25. "misago.exceptions.__all__" % exception.__name__)
  26. def test_misago_exceptions_detection(self):
  27. """Misago exception handler recognizes Misago exceptions"""
  28. for exception in misago_exceptions.MISAGO_EXCEPTIONS:
  29. try:
  30. raise exception()
  31. except exception as e:
  32. self.assertTrue(handler.is_misago_exception(e))
  33. def test_django_exceptions_detection(self):
  34. """Misago exception handler fails to recognize Django exceptions"""
  35. for exception in DJANGO_EXCEPTIONS:
  36. try:
  37. raise exception()
  38. except exception as e:
  39. self.assertFalse(handler.is_misago_exception(e))
  40. def test_python_exceptions_detection(self):
  41. """Misago exception handler fails to recognize Python exceptions"""
  42. for exception in PYTHON_EXCEPTIONS:
  43. try:
  44. raise exception()
  45. except exception as e:
  46. self.assertFalse(handler.is_misago_exception(e))
  47. def test_exception_handlers_list(self):
  48. """EXCEPTION_HANDLERS length matches that of MISAGO_EXCEPTIONS"""
  49. self.assertEqual(len(misago_exceptions.MISAGO_EXCEPTIONS),
  50. len(handler.EXCEPTION_HANDLERS))
  51. def test_get_exception_handler(self):
  52. """Exception handler has handler for every Misago exception"""
  53. for exception in misago_exceptions.MISAGO_EXCEPTIONS:
  54. try:
  55. handler.get_exception_handler(exception())
  56. except ValueError:
  57. self.fail(
  58. "%s has no exception handler defined " % exception.__name__)
  59. def test_handle_http404_exception(self):
  60. """Exception handler corrently turns Http404 exception into response"""
  61. self.fail("misago.views.exceptions.handler.handle_http404_exception "
  62. "has not been implmented.")
  63. def test_handle_outdated_url_exception(self):
  64. """Exception handler corrently turns Misago exceptions into responses"""
  65. self.fail("misago.views.exceptionshandler.handle_outdated_url_exception "
  66. "has not been implmented.")
  67. def test_handle_permission_denied_exception(self):
  68. """Exception handler corrently turns Misago exceptions into responses"""
  69. self.fail("misago.views.exceptions.handler.handle_"
  70. "permission_denied_exception has not been implmented.")
  71. def test_handle_misago_exception(self):
  72. """Exception handler corrently turns Misago exceptions into responses"""
  73. self.fail("misago.views.exceptions.handler.handle_misago_exception has "
  74. "not been implmented.")