Ticket #15671: no-hasattr2.diff

File no-hasattr2.diff, 2.0 KB (added by Matt McDonald, 14 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
    67from django.test import TestCase
     
    9293        response = self.client.get('/remote_user/', REMOTE_USER=self.known_user)
    9394        self.assertEqual(default_login, response.context['user'].last_login)
    9495
     96    def test_raises_exceptions(self):
     97        """
     98        The exception raised by a failure when checking for request.user should
     99        bubble up.
     100        """
     101        class BrokenLazyUser(object):
     102            def __get__(self, request, obj_type=None):
     103                raise TypeError
     104        curr_lazy_user = middleware.LazyUser
     105        middleware.LazyUser = BrokenLazyUser
     106        try:
     107            self.assertRaises(TypeError, self.client.get, '/remote_user/',
     108                              REMOTE_USER=self.known_user)
     109        finally:
     110            middleware.LazyUser = curr_lazy_user
     111
    95112    def tearDown(self):
    96113        """Restores settings to avoid breaking other tests."""
    97114        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        try:
     42            request.user
     43        except AttributeError:
    4244            raise ImproperlyConfigured(
    4345                "The Django remote user auth middleware requires the"
    4446                " authentication middleware to be installed.  Edit your"
Back to Top