From d17be88afd5f5e1059491e10408ba239e2e99fe2 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Sat, 7 Sep 2019 15:58:49 +0200 Subject: [PATCH] Refs #30037 -- Required the RemoteUserBackend.configure_user() to have request as the first positional argument. Per deprecation timeline. --- django/contrib/auth/backends.py | 16 +----- docs/releases/3.1.txt | 3 ++ .../test_remote_user_deprecation.py | 50 ------------------- 3 files changed, 4 insertions(+), 65 deletions(-) delete mode 100644 tests/auth_tests/test_remote_user_deprecation.py diff --git a/django/contrib/auth/backends.py b/django/contrib/auth/backends.py index 559d06fe33..ac86749e53 100644 --- a/django/contrib/auth/backends.py +++ b/django/contrib/auth/backends.py @@ -1,10 +1,6 @@ -import inspect -import warnings - from django.contrib.auth import get_user_model from django.contrib.auth.models import Permission from django.db.models import Exists, OuterRef, Q -from django.utils.deprecation import RemovedInDjango31Warning UserModel = get_user_model() @@ -206,17 +202,7 @@ class RemoteUserBackend(ModelBackend): UserModel.USERNAME_FIELD: username }) if created: - args = (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) + user = self.configure_user(request, user) else: try: user = UserModel._default_manager.get_by_natural_key(username) diff --git a/docs/releases/3.1.txt b/docs/releases/3.1.txt index 3f7e7f261b..5f6af788ce 100644 --- a/docs/releases/3.1.txt +++ b/docs/releases/3.1.txt @@ -241,5 +241,8 @@ to remove usage of these features. * ``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 ``TransactionTestCase.multi_db`` is removed. diff --git a/tests/auth_tests/test_remote_user_deprecation.py b/tests/auth_tests/test_remote_user_deprecation.py deleted file mode 100644 index 1b31d9f038..0000000000 --- a/tests/auth_tests/test_remote_user_deprecation.py +++ /dev/null @@ -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')