From 4b9330ccc04575f9e5126529ec355a450d12e77c Mon Sep 17 00:00:00 2001 From: Aleksej Manaev Date: Mon, 11 Jul 2016 16:40:39 +0200 Subject: Fixed #25187 -- Made request available in authentication backends. --- tests/auth_tests/test_auth_backends.py | 4 +-- tests/auth_tests/test_auth_backends_deprecation.py | 30 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 tests/auth_tests/test_auth_backends_deprecation.py (limited to 'tests/auth_tests') diff --git a/tests/auth_tests/test_auth_backends.py b/tests/auth_tests/test_auth_backends.py index e850772f35..ea40992a61 100644 --- a/tests/auth_tests/test_auth_backends.py +++ b/tests/auth_tests/test_auth_backends.py @@ -475,7 +475,7 @@ class PermissionDeniedBackend(object): Always raises PermissionDenied in `authenticate`, `has_perm` and `has_module_perms`. """ - def authenticate(self, username=None, password=None): + def authenticate(self, request, username=None, password=None): raise PermissionDenied def has_perm(self, user_obj, perm, obj=None): @@ -585,7 +585,7 @@ class TypeErrorBackend(object): Always raises TypeError. """ - def authenticate(self, username=None, password=None): + def authenticate(self, request, username=None, password=None): raise TypeError diff --git a/tests/auth_tests/test_auth_backends_deprecation.py b/tests/auth_tests/test_auth_backends_deprecation.py new file mode 100644 index 0000000000..6178936535 --- /dev/null +++ b/tests/auth_tests/test_auth_backends_deprecation.py @@ -0,0 +1,30 @@ +import warnings + +from django.contrib.auth import authenticate +from django.test import SimpleTestCase, override_settings + + +class NoRequestBackend(object): + def authenticate(self, username=None, password=None): + # Doesn't accept a request parameter. + pass + + +class AcceptsRequestBackendTest(SimpleTestCase): + """ + A deprecation warning is shown for backends that have an authenticate() + method without a request parameter. + """ + no_request_backend = '%s.NoRequestBackend' % __name__ + + @override_settings(AUTHENTICATION_BACKENDS=[no_request_backend]) + def test_no_request_deprecation_warning(self): + with warnings.catch_warnings(record=True) as warns: + warnings.simplefilter('always') + authenticate(username='test', password='test') + self.assertEqual(len(warns), 1) + self.assertEqual( + str(warns[0].message), + "Update authentication backend %s to accept a positional `request` " + "argument." % self.no_request_backend + ) -- cgit v1.3