Ticket #15671: no-hasattr3.diff
File no-hasattr3.diff, 3.3 KB (added by , 14 years ago) |
---|
-
django/contrib/auth/tests/remote_user.py
1 1 from datetime import datetime 2 2 3 3 from django.conf import settings 4 from django.contrib.auth import middleware 4 5 from django.contrib.auth.backends import RemoteUserBackend 5 6 from django.contrib.auth.models import User 7 from django.core.exceptions import ImproperlyConfigured 8 from django.core.handlers.wsgi import WSGIRequest 6 9 from django.test import TestCase 7 10 8 11 … … 92 95 response = self.client.get('/remote_user/', REMOTE_USER=self.known_user) 93 96 self.assertEqual(default_login, response.context['user'].last_login) 94 97 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 95 131 def tearDown(self): 96 132 """Restores settings to avoid breaking other tests.""" 97 133 settings.MIDDLEWARE_CLASSES = self.curr_middleware -
django/contrib/auth/middleware.py
38 38 39 39 def process_request(self, request): 40 40 # 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: 42 46 raise ImproperlyConfigured( 43 47 "The Django remote user auth middleware requires the" 44 48 " authentication middleware to be installed. Edit your"