Ticket #12148: auth_get_user_patch.diff

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

     
    11import datetime
     2from django.conf import settings
    23from django.core.exceptions import ImproperlyConfigured
    34from django.utils.importlib import import_module
    45
     
    2223    return cls()
    2324
    2425def get_backends():
    25     from django.conf import settings
    2626    backends = []
    2727    for backend_path in settings.AUTHENTICATION_BACKENDS:
    2828        backends.append(load_backend(backend_path))
     
    8383    try:
    8484        user_id = request.session[SESSION_KEY]
    8585        backend_path = request.session[BACKEND_SESSION_KEY]
    86         backend = load_backend(backend_path)
    87         user = backend.get_user(user_id) or AnonymousUser()
     86        if backend_path in settings.AUTHENTICATION_BACKENDS:
     87            backend = load_backend(backend_path)
     88            user = backend.get_user(user_id) or AnonymousUser()
     89        else:
     90            user = AnonymousUser()
    8891    except KeyError:
    8992        user = AnonymousUser()
    9093    return user
  • tests/regressiontests/auth_backends/tests.py

     
    33except NameError:
    44    from sets import Set as set     # Python 2.3 fallback
    55
     6from unittest import TestCase
     7from django.contrib import auth
     8from django.http import HttpRequest
     9from django.contrib.auth.models import User, AnonymousUser
     10from django.conf import settings
     11
     12class StubBackend:
     13    TestUser_1 = User()
     14    def get_user(self, id):
     15        if id == 1:
     16            return StubBackend.TestUser_1
     17   
     18class GetUserTests(TestCase):
     19    def load_backend(self, backend_path):
     20        if backend_path == "some.backend":
     21            return StubBackend()
     22
     23    def setUp(self):
     24        self.old_load_backend = auth.load_backend 
     25        self.old_auth_backends = settings.AUTHENTICATION_BACKENDS 
     26
     27        auth.load_backend = self.load_backend
     28        settings.AUTHENTICATION_BACKENDS = ("some.backend",)
     29
     30        self.request = HttpRequest()
     31        self.request.session = {}
     32
     33    def set_up_session(self, uid, backend):
     34        self.request.session[auth.SESSION_KEY] = uid
     35        self.request.session[auth.BACKEND_SESSION_KEY] = backend
     36
     37    def tearDown(self):
     38        auth.load_backend = self.old_load_backend
     39        settings.AUTHENTICATION_BACKENDS = self.old_auth_backends
     40
     41    def test_user_loaded_with_backend(self):
     42        self.set_up_session(uid=1, backend="some.backend")
     43        user = auth.get_user(self.request)
     44        self.assertEquals(user, StubBackend.TestUser_1)
     45
     46    def test_user_loaded_with_backend_invalid_uid(self):
     47        self.set_up_session(uid='INVALID', backend="some.backend")
     48        user = auth.get_user(self.request)
     49        self.assert_(isinstance(user,AnonymousUser))
     50
     51    def test_user_loaded_with_invalid_backend(self):
     52        self.set_up_session(uid=1, backend="invalid.backend")
     53        user = auth.get_user(self.request)
     54        self.assert_(isinstance(user,AnonymousUser))
     55
    656__test__ = {'API_TESTS': """
    757>>> from django.contrib.auth.models import User, Group, Permission, AnonymousUser
    858>>> from django.contrib.contenttypes.models import ContentType
     
    75125False
    76126>>> user.has_perms(['auth.test2', 'auth.test3'])
    77127False
    78 """}
     128""",
     129'GetUserTests': GetUserTests}
     130
     131
Back to Top