Ticket #15671: no-hasattr3.diff

File no-hasattr3.diff, 3.3 KB (added by Matt McDonald, 13 years ago)
  • django/contrib/auth/tests/remote_user.py

     
    11from datetime import datetime
    22
    33from django.conf import settings
     4from django.contrib.auth import middleware
    45from django.contrib.auth.backends import RemoteUserBackend
    56from django.contrib.auth.models import User
     7from django.core.exceptions import ImproperlyConfigured
     8from django.core.handlers.wsgi import WSGIRequest
    69from django.test import TestCase
    710
    811
     
    9295        response = self.client.get('/remote_user/', REMOTE_USER=self.known_user)
    9396        self.assertEqual(default_login, response.context['user'].last_login)
    9497
     98    def test_auth_middleware_required(self):
     99        """
     100        Tests that an ImproperlyConfigured exception is raised when the
     101        AuthenticationMiddleware is not properly installed.
     102        """
     103        # Remove the AuthenticationMiddleware.
     104        middleware = 'django.contrib.auth.middleware.AuthenticationMiddleware'
     105        settings.MIDDLEWARE_CLASSES = filter(lambda x: x != middleware,
     106                                             settings.MIDDLEWARE_CLASSES)
     107        # Cleanup the side-effects of having the AuthenticationMiddleware
     108        # installed in a previous test run.
     109        if WSGIRequest.__dict__.get('user'):
     110          del WSGIRequest.user
     111        self.assertRaises(ImproperlyConfigured, self.client.get,
     112                          '/remote_user/', REMOTE_USER=self.known_user)
     113
     114    def test_raises_attribute_error(self):
     115        """
     116        Tests that an AttributeError exception raised when accessing
     117        request.user should bubble up when the AuthenticationMiddleware is
     118        properly installed.
     119        """
     120        class BrokenLazyUser(object):
     121            def __get__(self, request, obj_type=None):
     122                raise AttributeError
     123        curr_lazy_user = middleware.LazyUser
     124        middleware.LazyUser = BrokenLazyUser
     125        try:
     126            self.assertRaises(AttributeError, self.client.get, '/remote_user/',
     127                              REMOTE_USER=self.known_user)
     128        finally:
     129            middleware.LazyUser = curr_lazy_user
     130
    95131    def tearDown(self):
    96132        """Restores settings to avoid breaking other tests."""
    97133        settings.MIDDLEWARE_CLASSES = self.curr_middleware
  • django/contrib/auth/middleware.py

     
    3838
    3939    def process_request(self, request):
    4040        # AuthenticationMiddleware is required so that request.user exists.
    41         if not hasattr(request, 'user'):
     41        # Accessing the request.user attribute itself may result in an
     42        # AttributeError being raised during the process of lazily loading the
     43        # real User object.  We want to allow that exception to raise, so we
     44        # only check that the LazyUser is present on the request class.
     45        if request.__class__.__dict__.get('user', None) is None:
    4246            raise ImproperlyConfigured(
    4347                "The Django remote user auth middleware requires the"
    4448                " authentication middleware to be installed.  Edit your"
Back to Top