Ticket #12148: auth_get_user_patch_v2.diff

File auth_get_user_patch_v2.diff, 4.2 KB (added by Tamas Szabo, 15 years ago)
  • django/contrib/auth/__init__.py

     
    11import datetime
    22from warnings import warn
     3from django.conf import settings
    34from django.core.exceptions import ImproperlyConfigured
    45from django.utils.importlib import import_module
    56
     
    3536    return cls()
    3637
    3738def get_backends():
    38     from django.conf import settings
    3939    backends = []
    4040    for backend_path in settings.AUTHENTICATION_BACKENDS:
    4141        backends.append(load_backend(backend_path))
     
    9696    try:
    9797        user_id = request.session[SESSION_KEY]
    9898        backend_path = request.session[BACKEND_SESSION_KEY]
    99         backend = load_backend(backend_path)
    100         user = backend.get_user(user_id) or AnonymousUser()
     99        if backend_path in settings.AUTHENTICATION_BACKENDS:
     100            backend = load_backend(backend_path)
     101            user = backend.get_user(user_id) or AnonymousUser()
     102        else:
     103            user = AnonymousUser()
    101104    except KeyError:
    102105        user = AnonymousUser()
    103106    return user
  • django/contrib/auth/tests/auth_backends.py

     
    11from django.conf import settings
     2from django.contrib import auth
    23from django.contrib.auth.models import User, Group, Permission, AnonymousUser
    34from django.contrib.contenttypes.models import ContentType
     5from django.http import HttpRequest
    46from django.test import TestCase
    57
    68
     
    245247
    246248    def test_get_all_permissions(self):
    247249        self.assertEqual(self.user1.get_all_permissions(TestObj()), set())
     250
     251
     252class StubBackend(object):
     253    USER_1 = User()
     254    def get_user(self, id):
     255        if id == 1: return self.USER_1
     256        return None
     257
     258class BackendCachedTest(TestCase):
     259    """
     260    Tests that User is loaded with the backend cached in the session.
     261    """
     262
     263    backend = 'django.contrib.auth.tests.auth_backends.StubBackend'
     264    valid_uid = 1
     265    invalid_uid = -1
     266
     267    def setUp(self):
     268        self.curr_auth = settings.AUTHENTICATION_BACKENDS
     269        settings.AUTHENTICATION_BACKENDS = (self.backend,)
     270        self.request = HttpRequest()
     271        self.request.session = {}
     272     
     273    def tearDown(self):
     274        settings.AUTHENTICATION_BACKENDS = self.curr_auth
     275
     276    def set_up_session(self, uid, backend):
     277        self.request.session[auth.SESSION_KEY] = uid
     278        self.request.session[auth.BACKEND_SESSION_KEY] = backend
     279       
     280    def test_valid_user_valid_backend(self):
     281        self.set_up_session(uid=self.valid_uid, backend=self.backend)
     282        user = auth.get_user(self.request)
     283        self.assert_(user is StubBackend.USER_1)
     284       
     285    def test_invalid_user_valid_backend(self):
     286        self.set_up_session(uid=self.invalid_uid, backend=self.backend)
     287        user = auth.get_user(self.request)
     288        self.assert_(isinstance(user,AnonymousUser))
     289       
     290    def test_valid_user_backend_not_in_settings(self):
     291        self.set_up_session(uid=self.invalid_uid, backend=self.backend)
     292        settings.AUTHENTICATION_BACKENDS = tuple()
     293        user = auth.get_user(self.request)
     294        self.assert_(isinstance(user,AnonymousUser))
     295
  • django/contrib/auth/tests/__init__.py

     
    44from django.contrib.auth.tests.forms import FORM_TESTS
    55from django.contrib.auth.tests.remote_user \
    66        import RemoteUserTest, RemoteUserNoCreateTest, RemoteUserCustomTest
    7 from django.contrib.auth.tests.auth_backends import BackendTest, RowlevelBackendTest, AnonymousUserBackendTest, NoAnonymousUserBackendTest
     7from django.contrib.auth.tests.auth_backends import BackendTest, RowlevelBackendTest, AnonymousUserBackendTest, NoAnonymousUserBackendTest, BackendCachedTest
    88from django.contrib.auth.tests.tokens import TOKEN_GENERATOR_TESTS
    99
    1010# The password for the fixture data users is 'password'
Back to Top