[1.6.x] Propagate get_user_model exception from get_user
Fixes #21439
Backport of 3560ef04
from master.
Conflicts:
django/contrib/auth/tests/test_auth_backends.py
This commit is contained in:
parent
c37b2e46b9
commit
0aa06bd378
|
@ -64,8 +64,8 @@ class ModelBackend(object):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def get_user(self, user_id):
|
def get_user(self, user_id):
|
||||||
try:
|
|
||||||
UserModel = get_user_model()
|
UserModel = get_user_model()
|
||||||
|
try:
|
||||||
return UserModel._default_manager.get(pk=user_id)
|
return UserModel._default_manager.get(pk=user_id)
|
||||||
except UserModel.DoesNotExist:
|
except UserModel.DoesNotExist:
|
||||||
return None
|
return None
|
||||||
|
|
|
@ -480,3 +480,27 @@ class ChangedBackendSettingsTest(TestCase):
|
||||||
# anonymous as the backend is not longer available.
|
# anonymous as the backend is not longer available.
|
||||||
self.assertIsNotNone(user)
|
self.assertIsNotNone(user)
|
||||||
self.assertTrue(user.is_anonymous())
|
self.assertTrue(user.is_anonymous())
|
||||||
|
|
||||||
|
|
||||||
|
@skipIfCustomUser
|
||||||
|
class ImproperlyConfiguredUserModelTest(TestCase):
|
||||||
|
"""
|
||||||
|
Tests that an exception from within get_user_model is propagated and doesn't
|
||||||
|
raise an UnboundLocalError.
|
||||||
|
|
||||||
|
Regression test for ticket #21439
|
||||||
|
"""
|
||||||
|
def setUp(self):
|
||||||
|
self.user1 = User.objects.create_user('test', 'test@example.com', 'test')
|
||||||
|
self.client.login(
|
||||||
|
username='test',
|
||||||
|
password='test'
|
||||||
|
)
|
||||||
|
|
||||||
|
@override_settings(AUTH_USER_MODEL='thismodel.doesntexist')
|
||||||
|
def test_does_not_shadow_exception(self):
|
||||||
|
# Prepare a request object
|
||||||
|
request = HttpRequest()
|
||||||
|
request.session = self.client.session
|
||||||
|
|
||||||
|
self.assertRaises(ImproperlyConfigured, get_user, request)
|
||||||
|
|
Loading…
Reference in New Issue