def test_public_view(self): mw = LoginRequiredMiddleware() # Authenticated request: req = self.factory.get(reverse('login_not_required_view')) req.user = self.user self.assertEqual(mw.process_view(req, login_not_required_view, [], {}), None) # Unauthenticated request: req = self.factory.get(reverse('login_not_required_view')) req.user = AnonymousUser() self.assertEqual(mw.process_view(req, login_not_required_view, [], {}), None)
def test_statics(self): mw = LoginRequiredMiddleware() # Unauthenticated request that should have been handled by web server: req = self.factory.get(settings.STATIC_URL + 'logo.png') req.user = AnonymousUser() resp = mw.process_view(req, None, None, None) self.assertEqual(resp.status_code, 401) # Unauthenticated request that should have been handled by web server: req = self.factory.get(settings.MEDIA_URL + 'logo.png') req.user = AnonymousUser() resp = mw.process_view(req, None, None, None) self.assertEqual(resp.status_code, 401)
def test_statics_debug(self): # Statics can be served in DEBUG mode though: mw = LoginRequiredMiddleware() # Unauthenticated request that should have been handled by web server: req = self.factory.get(settings.STATIC_URL + 'logo.png') req.user = AnonymousUser() resp = mw.process_view(req, None, None, None) self.assertEqual(resp, None) # Unauthenticated request that should have been handled by web server: req = self.factory.get(settings.MEDIA_URL + 'logo.png') req.user = AnonymousUser() resp = mw.process_view(req, None, None, None) self.assertEqual(resp, None)
def test_ajax(self): mw = LoginRequiredMiddleware() # Authenticated request: req = self.factory.get(reverse('another_view')) req.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' req.user = self.user self.assertEqual(mw.process_view(req, None, None, None), None) # Unauthenticated request: req = self.factory.get(reverse('another_view')) req.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' req.user = AnonymousUser() resp = mw.process_view(req, None, None, None) self.assertEqual(resp.status_code, 401)
def test_login_required(self): mw = LoginRequiredMiddleware() # Authenticated request: req = self.factory.get(reverse('another_view')) req.user = self.user self.assertEqual(mw.process_view(req, None, None, None), None) # Unauthenticated request: req = self.factory.get(reverse('another_view')) req.user = AnonymousUser() resp = mw.process_view(req, None, None, None) self.assertEqual(resp.status_code, 200) self.assertContains(resp, 'id_username') self.assertContains(resp, 'id_password')