From ae73e94c3e839e634e14def965c77265fde1a57a Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Tue, 4 Oct 2022 02:00:40 +0200 Subject: [PATCH] Friskies pl0x? --- src/authenticators/ldap_admin.py | 4 ++++ src/tests/test_ldapauth.py | 17 ++++------------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/authenticators/ldap_admin.py b/src/authenticators/ldap_admin.py index 704816460..a7fc18da6 100644 --- a/src/authenticators/ldap_admin.py +++ b/src/authenticators/ldap_admin.py @@ -37,6 +37,10 @@ class Authenticator(BaseAuthenticator): os.system("systemctl restart slapd") time.sleep(10) # waits 10 secondes so we are sure that slapd has restarted + # Force-reset existing LDAP interface + from yunohost.utils import ldap as ldaputils + ldaputils._ldap_interface = None + try: admins = _get_ldap_interface().search(ADMIN_GROUP, attrs=["memberUid"])[0].get("memberUid", []) except ldap.SERVER_DOWN: diff --git a/src/tests/test_ldapauth.py b/src/tests/test_ldapauth.py index 0ec0346da..db5229342 100644 --- a/src/tests/test_ldapauth.py +++ b/src/tests/test_ldapauth.py @@ -2,7 +2,6 @@ import pytest import os from yunohost.authenticators.ldap_admin import Authenticator as LDAPAuth -from yunohost.tools import tools_rootpw from yunohost.user import user_create, user_list, user_update, user_delete from yunohost.domain import _get_maindomain @@ -17,7 +16,7 @@ def setup_function(function): maindomain = _get_maindomain() - if os.system("systemctl is-active slapd") != 0: + if os.system("systemctl is-active slapd >/dev/null") != 0: os.system("systemctl start slapd && sleep 3") user_create("alice", "Alice", "White", maindomain, "Yunohost", admin=True) @@ -26,7 +25,7 @@ def setup_function(function): def teardown_function(): - os.system("systemctl is-active slapd || systemctl start slapd && sleep 5") + os.system("systemctl is-active slapd >/dev/null || systemctl start slapd; sleep 5") for u in user_list()["users"]: user_delete(u, purge=True) @@ -67,20 +66,14 @@ def test_authenticate_with_wrong_password(): def test_authenticate_server_down(mocker): os.system("systemctl stop slapd && sleep 5") - # Now if slapd is down, moulinette tries to restart it - mocker.patch("os.system") - mocker.patch("time.sleep") - with pytest.raises(MoulinetteError) as exception: - LDAPAuth().authenticate_credentials(credentials="alice:Yunohost") - - assert "Unable to reach LDAP server" in str(exception) + LDAPAuth().authenticate_credentials(credentials="alice:Yunohost") def test_authenticate_change_password(): LDAPAuth().authenticate_credentials(credentials="alice:Yunohost") - tools_rootpw("plopette", check_strength=False) + user_update("alice", change_password="plopette") with pytest.raises(MoulinetteError) as exception: LDAPAuth().authenticate_credentials(credentials="alice:Yunohost") @@ -89,6 +82,4 @@ def test_authenticate_change_password(): expected_msg = translation.format() assert expected_msg in str(exception) - user_update("alice", password="plopette") - LDAPAuth().authenticate_credentials(credentials="alice:plopette")