Refs #30037 -- Required the RemoteUserBackend.configure_user() to have request as the first positional argument.

Per deprecation timeline.
This commit is contained in:
Mariusz Felisiak 2019-09-07 15:58:49 +02:00
parent b61ea56789
commit d17be88afd
3 changed files with 4 additions and 65 deletions

View File

@ -1,10 +1,6 @@
import inspect
import warnings
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission from django.contrib.auth.models import Permission
from django.db.models import Exists, OuterRef, Q from django.db.models import Exists, OuterRef, Q
from django.utils.deprecation import RemovedInDjango31Warning
UserModel = get_user_model() UserModel = get_user_model()
@ -206,17 +202,7 @@ class RemoteUserBackend(ModelBackend):
UserModel.USERNAME_FIELD: username UserModel.USERNAME_FIELD: username
}) })
if created: if created:
args = (request, user) user = self.configure_user(request, user)
try:
inspect.getcallargs(self.configure_user, request, user)
except TypeError:
args = (user,)
warnings.warn(
'Update %s.configure_user() to accept `request` as '
'the first argument.'
% self.__class__.__name__, RemovedInDjango31Warning
)
user = self.configure_user(*args)
else: else:
try: try:
user = UserModel._default_manager.get_by_natural_key(username) user = UserModel._default_manager.get_by_natural_key(username)

View File

@ -241,5 +241,8 @@ to remove usage of these features.
* ``django.contrib.staticfiles.storage.CachedStaticFilesStorage`` is removed. * ``django.contrib.staticfiles.storage.CachedStaticFilesStorage`` is removed.
* The ``RemoteUserBackend.configure_user()`` method requires ``request`` as the
first positional argument.
* Support for ``SimpleTestCase.allow_database_queries`` and * Support for ``SimpleTestCase.allow_database_queries`` and
``TransactionTestCase.multi_db`` is removed. ``TransactionTestCase.multi_db`` is removed.

View File

@ -1,50 +0,0 @@
import warnings
from django.contrib.auth.backends import RemoteUserBackend
from django.contrib.auth.models import User
from django.test import TestCase, modify_settings, override_settings
class CustomRemoteUserBackend(RemoteUserBackend):
"""Override configure_user() without a request argument."""
def configure_user(self, user):
user.email = 'user@example.com'
user.save()
return user
@override_settings(ROOT_URLCONF='auth_tests.urls')
class RemoteUserCustomTest(TestCase):
middleware = 'django.contrib.auth.middleware.RemoteUserMiddleware'
backend = 'auth_tests.test_remote_user_deprecation.CustomRemoteUserBackend'
header = 'REMOTE_USER'
def setUp(self):
self.patched_settings = modify_settings(
AUTHENTICATION_BACKENDS={'append': self.backend},
MIDDLEWARE={'append': self.middleware},
)
self.patched_settings.enable()
def tearDown(self):
self.patched_settings.disable()
def test_configure_user_deprecation_warning(self):
"""
A deprecation warning is shown for RemoteUserBackend that have a
configure_user() method without a request parameter.
"""
num_users = User.objects.count()
with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter('always')
response = self.client.get('/remote_user/', **{self.header: 'newuser'})
self.assertEqual(response.context['user'].username, 'newuser')
self.assertEqual(len(warns), 1)
self.assertEqual(
str(warns[0].message),
'Update CustomRemoteUserBackend.configure_user() to accept '
'`request` as the first argument.'
)
self.assertEqual(User.objects.count(), num_users + 1)
user = User.objects.get(username='newuser')
self.assertEqual(user.email, 'user@example.com')