From 81a85a0c2b6741b82819687db6a5bc092d7c4508 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Sat, 4 Dec 2010 07:05:04 +0000 Subject: [PATCH] =?UTF-8?q?[1.2.X]=20Fixed=20#13190=20--=20Improved=20erro?= =?UTF-8?q?r=20handling=20for=20the=20case=20where=20no=20authentication?= =?UTF-8?q?=20backends=20are=20defined.=20Thanks=20to=20Joel3000=20for=20t?= =?UTF-8?q?he=20report,=20and=20=C5=81ukasz=20Rekucki=20for=20the=20final?= =?UTF-8?q?=20patch.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Backport of r14793 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14799 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/auth/__init__.py | 2 ++ django/contrib/auth/tests/__init__.py | 2 +- django/contrib/auth/tests/auth_backends.py | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/django/contrib/auth/__init__.py b/django/contrib/auth/__init__.py index 169ae01070..0eb197b416 100644 --- a/django/contrib/auth/__init__.py +++ b/django/contrib/auth/__init__.py @@ -39,6 +39,8 @@ def get_backends(): backends = [] for backend_path in settings.AUTHENTICATION_BACKENDS: backends.append(load_backend(backend_path)) + if not backends: + raise ImproperlyConfigured('No authentication backends have been defined. Does AUTHENTICATION_BACKENDS contain anything?') return backends def authenticate(**credentials): diff --git a/django/contrib/auth/tests/__init__.py b/django/contrib/auth/tests/__init__.py index 98061a157a..9bf7d83147 100644 --- a/django/contrib/auth/tests/__init__.py +++ b/django/contrib/auth/tests/__init__.py @@ -1,4 +1,4 @@ -from django.contrib.auth.tests.auth_backends import BackendTest, RowlevelBackendTest, AnonymousUserBackendTest, NoAnonymousUserBackendTest +from django.contrib.auth.tests.auth_backends import BackendTest, RowlevelBackendTest, AnonymousUserBackendTest, NoAnonymousUserBackendTest, NoBackendsTest from django.contrib.auth.tests.basic import BasicTestCase from django.contrib.auth.tests.decorators import LoginRequiredTestCase from django.contrib.auth.tests.forms import UserCreationFormTest, AuthenticationFormTest, SetPasswordFormTest, PasswordChangeFormTest, UserChangeFormTest, PasswordResetFormTest diff --git a/django/contrib/auth/tests/auth_backends.py b/django/contrib/auth/tests/auth_backends.py index bc61e99c6b..0c1d112ac8 100644 --- a/django/contrib/auth/tests/auth_backends.py +++ b/django/contrib/auth/tests/auth_backends.py @@ -1,6 +1,7 @@ from django.conf import settings from django.contrib.auth.models import User, Group, Permission, AnonymousUser from django.contrib.contenttypes.models import ContentType +from django.core.exceptions import ImproperlyConfigured from django.test import TestCase @@ -245,3 +246,18 @@ class NoAnonymousUserBackendTest(TestCase): def test_get_all_permissions(self): self.assertEqual(self.user1.get_all_permissions(TestObj()), set()) + +class NoBackendsTest(TestCase): + """ + Tests that an appropriate error is raised if no auth backends are provided. + """ + def setUp(self): + self.old_AUTHENTICATION_BACKENDS = settings.AUTHENTICATION_BACKENDS + settings.AUTHENTICATION_BACKENDS = [] + self.user = User.objects.create_user('test', 'test@example.com', 'test') + + def tearDown(self): + settings.AUTHENTICATION_BACKENDS = self.old_AUTHENTICATION_BACKENDS + + def test_raises_exception(self): + self.assertRaises(ImproperlyConfigured, self.user.has_perm, ('perm', TestObj(),))