summaryrefslogtreecommitdiff
path: root/tests/auth_tests
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2017-02-24 10:15:41 -0500
committerTim Graham <timograham@gmail.com>2017-02-24 13:59:34 -0500
commit53f5dc10cdc0c1fbeb48b2d98fbdd26c1996ec59 (patch)
tree619c49c5d96373976673c4a7079f24fc66b05c4b /tests/auth_tests
parent0417bf47a6fd00d9a6a4c711780ce01f35f62d59 (diff)
[1.11.x] Refs #25187 -- Fixed AuthBackend.authenticate() compatibility for signatures that accept a request kwarg.
Backport of c31e7ab5a4b062225bc4f6b5cae065325dd30f1f from master
Diffstat (limited to 'tests/auth_tests')
-rw-r--r--tests/auth_tests/test_auth_backends_deprecation.py24
1 files changed, 23 insertions, 1 deletions
diff --git a/tests/auth_tests/test_auth_backends_deprecation.py b/tests/auth_tests/test_auth_backends_deprecation.py
index 6178936535..0d45fee7a4 100644
--- a/tests/auth_tests/test_auth_backends_deprecation.py
+++ b/tests/auth_tests/test_auth_backends_deprecation.py
@@ -3,6 +3,8 @@ import warnings
from django.contrib.auth import authenticate
from django.test import SimpleTestCase, override_settings
+mock_request = object()
+
class NoRequestBackend(object):
def authenticate(self, username=None, password=None):
@@ -10,12 +12,20 @@ class NoRequestBackend(object):
pass
+class RequestNotPositionArgBackend:
+ def authenticate(self, username=None, password=None, request=None):
+ assert username == 'username'
+ assert password == 'pass'
+ assert request is mock_request
+
+
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__
+ request_not_positional_backend = '%s.RequestNotPositionArgBackend' % __name__
@override_settings(AUTHENTICATION_BACKENDS=[no_request_backend])
def test_no_request_deprecation_warning(self):
@@ -25,6 +35,18 @@ class AcceptsRequestBackendTest(SimpleTestCase):
self.assertEqual(len(warns), 1)
self.assertEqual(
str(warns[0].message),
- "Update authentication backend %s to accept a positional `request` "
+ "Update %s.authenticate() to accept a positional `request` "
"argument." % self.no_request_backend
)
+
+ @override_settings(AUTHENTICATION_BACKENDS=[request_not_positional_backend])
+ def test_request_keyword_arg_deprecation_warning(self):
+ with warnings.catch_warnings(record=True) as warns:
+ warnings.simplefilter('always')
+ authenticate(username='username', password='pass', request=mock_request)
+ 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.request_not_positional_backend
+ )