class CustomViewProcessMiddlewareTest(unittest.TestCase):

    def setUp(self):
        self.cv = CustomViewProcessMiddleware()
        self.request = RequestMock()

    def test_view_status_200(self):
        # CASE: User is not authenticated.
        self.request.user.is_authenticated = lambda: False

        view_result = self.cv.process_view(self.request, views_test.home, (), {})
        self.assertEqual(view_result.status_code, 200)

        view_result = self.cv.process_view(self.request, views_test.index, (), {})
        self.assertEqual(view_result.status_code, 200)

        # CASE: User is authenticated, permission doesn't exist
        self.request.user.is_authenticated = lambda: True
        view_acl.middleware.Permission.objects.filter = lambda codename=None: PermissionObjMock()

        view_result = self.cv.process_view(self.request, views_test.home, (), {})
        self.assertEqual(view_result.status_code, 200)

        view_result = self.cv.process_view(self.request, views_test.index, (), {})
        self.assertEqual(view_result.status_code, 200)

    def test_view_status_403(self):
        # Permission exists, User is authenticated, doesn't have access to view.
        self.request.user.is_authenticated = lambda: True
        view_acl.middleware.Permission.objects.filter = lambda codename=None: PermissionObjMockExist()

        # CASE: User is authenticated, Permission exists, user doesn't have access.
        view_result = self.cv.process_view(self.request, views_test.home, (), {})
        self.assertEqual(view_result.status_code, 403)

        view_result = self.cv.process_view(self.request, views_test.index, (), {})
        self.assertEqual(view_result.status_code, 403)
 def setUp(self):
     self.cv = CustomViewProcessMiddleware()
     self.request = RequestMock()