2016-07-11 14:40:39 +00:00
|
|
|
import warnings
|
|
|
|
|
|
|
|
from django.contrib.auth import authenticate
|
|
|
|
from django.test import SimpleTestCase, override_settings
|
|
|
|
|
2017-02-24 15:15:41 +00:00
|
|
|
mock_request = object()
|
2017-05-22 15:03:18 +00:00
|
|
|
mock_backend = object()
|
2017-02-24 15:15:41 +00:00
|
|
|
|
2016-07-11 14:40:39 +00:00
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class NoRequestBackend:
|
2016-07-11 14:40:39 +00:00
|
|
|
def authenticate(self, username=None, password=None):
|
|
|
|
# Doesn't accept a request parameter.
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-02-24 15:15:41 +00:00
|
|
|
class RequestNotPositionArgBackend:
|
|
|
|
def authenticate(self, username=None, password=None, request=None):
|
|
|
|
assert username == 'username'
|
|
|
|
assert password == 'pass'
|
|
|
|
assert request is mock_request
|
|
|
|
|
|
|
|
|
2017-05-22 15:03:18 +00:00
|
|
|
class RequestNotPositionArgWithUsedKwargBackend:
|
|
|
|
def authenticate(self, username=None, password=None, request=None, backend=None):
|
|
|
|
assert username == 'username'
|
|
|
|
assert password == 'pass'
|
|
|
|
assert request is mock_request
|
|
|
|
assert backend is mock_backend
|
|
|
|
|
|
|
|
|
2016-07-11 14:40:39 +00:00
|
|
|
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__
|
2017-02-24 15:15:41 +00:00
|
|
|
request_not_positional_backend = '%s.RequestNotPositionArgBackend' % __name__
|
2017-05-22 15:03:18 +00:00
|
|
|
request_not_positional_with_used_kwarg_backend = '%s.RequestNotPositionArgWithUsedKwargBackend' % __name__
|
2016-07-11 14:40:39 +00:00
|
|
|
|
|
|
|
@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),
|
2017-02-24 15:15:41 +00:00
|
|
|
"Update %s.authenticate() to accept a positional `request` "
|
2016-07-11 14:40:39 +00:00
|
|
|
"argument." % self.no_request_backend
|
|
|
|
)
|
2017-02-24 15:15:41 +00:00
|
|
|
|
|
|
|
@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
|
|
|
|
)
|
2017-05-14 22:22:58 +00:00
|
|
|
|
|
|
|
@override_settings(AUTHENTICATION_BACKENDS=[request_not_positional_backend, no_request_backend])
|
|
|
|
def test_both_types_of_deprecation_warning(self):
|
|
|
|
with warnings.catch_warnings(record=True) as warns:
|
|
|
|
warnings.simplefilter('always')
|
|
|
|
authenticate(mock_request, username='username', password='pass')
|
|
|
|
|
|
|
|
self.assertEqual(len(warns), 2)
|
|
|
|
self.assertEqual(
|
|
|
|
str(warns[0].message),
|
|
|
|
"In %s.authenticate(), move the `request` keyword argument to the "
|
|
|
|
"first positional argument." % self.request_not_positional_backend
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
str(warns[1].message),
|
|
|
|
"Update %s.authenticate() to accept a positional `request` "
|
|
|
|
"argument." % self.no_request_backend
|
|
|
|
)
|
2017-05-22 15:03:18 +00:00
|
|
|
|
|
|
|
@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:
|
|
|
|
warnings.simplefilter('always')
|
|
|
|
authenticate(username='username', password='pass', request=mock_request, backend=mock_backend)
|
|
|
|
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_with_used_kwarg_backend
|
|
|
|
)
|