From 78bebd00361615a1e3f0c6f7087fa80603939da3 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Thu, 1 Apr 2021 17:06:39 +0200 Subject: [PATCH] utils: set LDAP auth backend when resetting password of an LDAP account (#52638) --- src/authentic2/utils/__init__.py | 15 ++++++++------- tests/test_ldap.py | 1 + 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/authentic2/utils/__init__.py b/src/authentic2/utils/__init__.py index aedf9ee5..2c3e515a 100644 --- a/src/authentic2/utils/__init__.py +++ b/src/authentic2/utils/__init__.py @@ -1150,13 +1150,14 @@ def same_origin(url1, url2): return True -def simulate_authentication( - request, user, method, backend='authentic2.backends.models_backend.ModelBackend', service=None, **kwargs -): - '''Simulate a normal login by forcing a backend attribute on the user instance''' - # do not modify the passed user - user = copy.deepcopy(user) - user.backend = backend +def simulate_authentication(request, user, method, backend=None, service=None, **kwargs): + '''Simulate a normal login by eventually forcing a backend attribute on the + user instance''' + if not getattr(user, 'backend', None) and not backend: + backend = 'authentic2.backends.models_backend.ModelBackend' + if backend: + user = copy.deepcopy(user) + user.backend = backend return login(request, user, method, service=service, record=False, **kwargs) diff --git a/tests/test_ldap.py b/tests/test_ldap.py index edc4b9a6..4ca8b4e3 100644 --- a/tests/test_ldap.py +++ b/tests/test_ldap.py @@ -878,6 +878,7 @@ def reset_password_ldap_user(settings, app): response.form['new_password1'] = new_password response.form['new_password2'] = new_password response = response.form.submit(status=302).maybe_follow() + assert app.session['_auth_user_backend'] == 'authentic2.backends.ldap_backend.LDAPBackendPasswordLost' # logout response = response.click('Logout').maybe_follow() return new_password -- 2.30.1