summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2018-01-30 19:31:25 -0500
committerTim Graham <timograham@gmail.com>2018-01-31 09:27:21 -0500
commit4430b83e4bcefbd7da1831df543f711d9da6bc26 (patch)
tree4e659590b785b803f2a0089655850a120147bd13 /tests
parent649d5eaa8b7b224fc0eb8a3339a589288d5b10e8 (diff)
downloaddjango-4430b83e4bcefbd7da1831df543f711d9da6bc26.tar.gz
[1.11.x] Fixed #29071 -- Fixed contrib.auth.authenticate() crash if a backend doesn't accept a request but a later one does.
Regression in a3ba2662cdaa36183fdfb8a26dfa157e26fca76a. Backport of 55e16f25e9d2050e95e448f9ab2e4b9fc845a9e5 from stable/2.0.x
Diffstat (limited to 'tests')
-rw-r--r--tests/auth_tests/test_auth_backends_deprecation.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/tests/auth_tests/test_auth_backends_deprecation.py b/tests/auth_tests/test_auth_backends_deprecation.py
index 675e185e9f..00b7e19d89 100644
--- a/tests/auth_tests/test_auth_backends_deprecation.py
+++ b/tests/auth_tests/test_auth_backends_deprecation.py
@@ -13,6 +13,18 @@ class NoRequestBackend(object):
pass
+class NoRequestWithKwargs:
+ def authenticate(self, username=None, password=None, **kwargs):
+ pass
+
+
+class RequestPositionalArg:
+ def authenticate(self, request, username=None, password=None, **kwargs):
+ assert username == 'username'
+ assert password == 'pass'
+ assert request is mock_request
+
+
class RequestNotPositionArgBackend:
def authenticate(self, username=None, password=None, request=None):
assert username == 'username'
@@ -34,6 +46,8 @@ class AcceptsRequestBackendTest(SimpleTestCase):
method without a request parameter.
"""
no_request_backend = '%s.NoRequestBackend' % __name__
+ no_request_with_kwargs_backend = '%s.NoRequestWithKwargs' % __name__
+ request_positional_arg_backend = '%s.RequestPositionalArg' % __name__
request_not_positional_backend = '%s.RequestNotPositionArgBackend' % __name__
request_not_positional_with_used_kwarg_backend = '%s.RequestNotPositionArgWithUsedKwargBackend' % __name__
@@ -79,6 +93,21 @@ class AcceptsRequestBackendTest(SimpleTestCase):
"argument." % self.no_request_backend
)
+ @override_settings(AUTHENTICATION_BACKENDS=[no_request_with_kwargs_backend, request_positional_arg_backend])
+ def test_credentials_not_mutated(self):
+ """
+ No problem if a backend doesn't accept `request` and a later one does.
+ """
+ with warnings.catch_warnings(record=True) as warns:
+ warnings.simplefilter('always')
+ authenticate(mock_request, username='username', password='pass')
+ self.assertEqual(len(warns), 1)
+ self.assertEqual(
+ str(warns[0].message),
+ "In %s.authenticate(), move the `request` keyword argument to the "
+ "first positional argument." % self.no_request_with_kwargs_backend
+ )
+
@override_settings(AUTHENTICATION_BACKENDS=[request_not_positional_with_used_kwarg_backend])
def test_handles_backend_in_kwargs(self):
with warnings.catch_warnings(record=True) as warns: