Browse Source

Updated test suite for exception handler.

Rafał Pitoń 11 years ago
parent
commit
7894b2890c
1 changed files with 96 additions and 58 deletions
  1. 96 58
      misago/views/tests/test_exceptions.py

+ 96 - 58
misago/views/tests/test_exceptions.py

@@ -1,89 +1,127 @@
-from django.test import TestCase
-from django.http import Http404 as DjHttp404
+from django.http import Http404
 from django.core import exceptions as django_exceptions
-from misago.views import exceptions as misago_exceptions
-from misago.views.exceptions import handler
+from django.core.exceptions import PermissionDenied
+from django.test import TestCase
+from django.test.client import RequestFactory
+from misago.views.exceptions import OutdatedSlug
+from misago.views import exceptionhandler
 
 
-DJANGO_EXCEPTIONS = (
-    django_exceptions.PermissionDenied,
+INVALID_EXCEPTIONS = (
+    django_exceptions.ObjectDoesNotExist,
     django_exceptions.ViewDoesNotExist,
-    DjHttp404,
-)
-
-PYTHON_EXCEPTIONS = (
     TypeError,
     ValueError,
     KeyError,
 )
 
 
-class MisagoExceptionHandlerTests(TestCase):
-    def test_misago_exception_list_valid(self):
-        """MISAGO_EXCEPTIONS list is valid"""
-        self.assertEqual(len(misago_exceptions.MISAGO_EXCEPTIONS),
-                         len(misago_exceptions.__all__))
-
-        for exception in misago_exceptions.MISAGO_EXCEPTIONS:
-            if exception.__name__ not in  misago_exceptions.__all__:
-                self.fail("%s is registered in "
-                          "misago.exceptions.MISAGO_EXCEPTIONS but not in "
-                          "misago.exceptions.__all__" % exception.__name__)
-
-    def test_misago_exceptions_detection(self):
-        """Misago exception handler recognizes Misago exceptions"""
-        for exception in misago_exceptions.MISAGO_EXCEPTIONS:
+class IsMisagoExceptionTests(TestCase):
+    def test_is_misago_exception_true_for_handled_exceptions(self):
+        """exceptionhandler.is_misago_exception recognizes handled exceptions"""
+        for exception in exceptionhandler.HANDLED_EXCEPTIONS:
             try:
                 raise exception()
             except exception as e:
-                self.assertTrue(handler.is_misago_exception(e))
+                self.assertTrue(exceptionhandler.is_misago_exception(e))
 
-    def test_django_exceptions_detection(self):
-        """Misago exception handler fails to recognize Django exceptions"""
-        for exception in DJANGO_EXCEPTIONS:
+    def test_is_misago_exception_false_for_not_handled_exceptions(self):
+        """Misago exception handler fails to recognize other exceptions"""
+        for exception in INVALID_EXCEPTIONS:
             try:
                 raise exception()
             except exception as e:
-                self.assertFalse(handler.is_misago_exception(e))
+                self.assertFalse(exceptionhandler.is_misago_exception(e))
 
-    def test_python_exceptions_detection(self):
-        """Misago exception handler fails to recognize Python exceptions"""
-        for exception in PYTHON_EXCEPTIONS:
-            try:
-                raise exception()
-            except exception as e:
-                self.assertFalse(handler.is_misago_exception(e))
 
+class GetExceptionHandlerTests(TestCase):
     def test_exception_handlers_list(self):
-        """EXCEPTION_HANDLERS length matches that of MISAGO_EXCEPTIONS"""
-        self.assertEqual(len(misago_exceptions.MISAGO_EXCEPTIONS),
-                         len(handler.EXCEPTION_HANDLERS))
+        """HANDLED_EXCEPTIONS length matches that of EXCEPTION_HANDLERS"""
+        self.assertEqual(len(exceptionhandler.HANDLED_EXCEPTIONS),
+                         len(exceptionhandler.EXCEPTION_HANDLERS))
 
     def test_get_exception_handler(self):
-        """Exception handler has handler for every Misago exception"""
-        for exception in misago_exceptions.MISAGO_EXCEPTIONS:
+        """Exception handler has correct handler for every Misago exception"""
+        for exception in exceptionhandler.HANDLED_EXCEPTIONS:
             try:
-                handler.get_exception_handler(exception())
+                exceptionhandler.get_exception_handler(exception())
             except ValueError:
                 self.fail(
                     "%s has no exception handler defined " % exception.__name__)
 
-    def test_handle_http404_exception(self):
-        """Exception handler corrently turns Http404 exception into response"""
-        self.fail("misago.views.exceptions.handler.handle_http404_exception "
-                  "has not been implmented.")
 
-    def test_handle_outdated_url_exception(self):
-        """Exception handler corrently turns Misago exceptions into responses"""
-        self.fail("misago.views.exceptionshandler.handle_outdated_url_exception "
-                  "has not been implmented.")
+class HandleHttp404ExceptionTests(TestCase):
+    def setUp(self):
+        self.exception_message = "Text page could not be found"
+        self.exception = Http404(self.exception_message)
+        self.request = RequestFactory().get('/')
+
+    def test_get_exception_handler(self):
+        """get_exception_handler returns correct Http404 exception handler"""
+        found_handler = exceptionhandler.get_exception_handler(self.exception)
+        self.assertEqual(
+            found_handler, exceptionhandler.handle_http404_exception)
+
+        response = found_handler(self.request, self.exception)
+        self.assertIn(self.exception_message, response.content)
+        self.assertEqual(response.status_code, 404)
+
+    def test_handle_misago_exception(self):
+        """handle_misago_exception handles Http404 exception correctly"""
+        response = exceptionhandler.handle_misago_exception(
+            self.request, self.exception)
+        self.assertIn(self.exception_message, response.content)
+        self.assertEqual(response.status_code, 404)
+
+
+class HandleOutdatedSlugExceptionTests(TestCase):
+    def setUp(self):
+        self.exception = OutdatedSlug()
+        self.request = RequestFactory().get('/')
+
+    def test_get_exception_handler(self):
+        """
+        get_exception_handler returns correct OutdatedSlug exception handler
+        """
+        found_handler = exceptionhandler.get_exception_handler(self.exception)
+        self.assertEqual(
+            found_handler, exceptionhandler.handle_outdated_slug_exception)
+
+        response = found_handler(self.request, self.exception)
+        self.assertEqual(response.status_code, 301)
+
+    def test_handle_misago_exception(self):
+        """
+        handle_misago_exception handles OutdatedSlug exception correctly
+        """
+        response = exceptionhandler.handle_misago_exception(
+            self.request, self.exception)
+        self.assertEqual(response.status_code, 301)
+
+
+class HandlePermissionDeniedExceptionTests(TestCase):
+    def setUp(self):
+        self.exception_message = "Page access not allowed"
+        self.exception = PermissionDenied(self.exception_message)
+        self.request = RequestFactory().get('/')
+
+    def test_get_exception_handler(self):
+        """
+        get_exception_handler returns correct PermissionDenied exception handler
+        """
+        found_handler = exceptionhandler.get_exception_handler(self.exception)
+        self.assertEqual(
+            found_handler, exceptionhandler.handle_permission_denied_exception)
 
-    def test_handle_permission_denied_exception(self):
-        """Exception handler corrently turns Misago exceptions into responses"""
-        self.fail("misago.views.exceptions.handler.handle_"
-                  "permission_denied_exception has not been implmented.")
+        response = found_handler(self.request, self.exception)
+        self.assertIn(self.exception_message, response.content)
+        self.assertEqual(response.status_code, 403)
 
     def test_handle_misago_exception(self):
-        """Exception handler corrently turns Misago exceptions into responses"""
-        self.fail("misago.views.exceptions.handler.handle_misago_exception has "
-                  "not been implmented.")
+        """
+        handle_misago_exception handles PermissionDenied exception correctly
+        """
+        response = exceptionhandler.handle_misago_exception(
+            self.request, self.exception)
+        self.assertIn(self.exception_message, response.content)
+        self.assertEqual(response.status_code, 403)