From ca2300360c0304867a5fcfbfc1f4ed4147e745ff Mon Sep 17 00:00:00 2001 From: vincent Date: Tue, 21 Mar 2023 20:33:19 +0100 Subject: [PATCH] fix issue with ldap entry_dn call --- supysonic/managers/ldap.py | 3 ++- tests/managers/test_manager_ldap.py | 8 +++++++- tests/managers/test_manager_user.py | 5 +++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/supysonic/managers/ldap.py b/supysonic/managers/ldap.py index 33f7278..adf7343 100644 --- a/supysonic/managers/ldap.py +++ b/supysonic/managers/ldap.py @@ -43,8 +43,9 @@ def try_auth(self, user, password): if not entrie: return False try: + logger.debug(type(entrie)) with ldap3.Connection( - self.server, entrie["entry_dn"], password, read_only=True + self.server, entrie.entry_dn, password, read_only=True ) as conn: return { "uid": entrie[self.username_attr], diff --git a/tests/managers/test_manager_ldap.py b/tests/managers/test_manager_ldap.py index f2d39c1..9a4fe11 100644 --- a/tests/managers/test_manager_ldap.py +++ b/tests/managers/test_manager_ldap.py @@ -14,6 +14,12 @@ "email_attr": "mail", } +class MockEntrie (): + def __init__(self,dn,attr): + self.entry_dn=dn + self.attribute=attr + def __getitem__(self, item): + return self.attribute[item] class LdapManagerTestCase(unittest.TestCase): @@ -37,7 +43,7 @@ def test_ldapManager_searchUser(self, mock_object): @patch('supysonic.managers.ldap.ldap3.Connection') def test_ldapManager_try_auth(self, mock_object): mock_object.return_value.__enter__.return_value.entries = [ - {LDAP["username_attr"]:"toto", "entry_dn":"cn=toto", "mail":"toto@example.com"}] + MockEntrie ("cn=toto",{LDAP["username_attr"]:"toto", "mail":"toto@example.com"})] ldap = LdapManager(**LDAP) ldap_user = ldap.try_auth("toto", "toto") self.assertFalse(ldap_user["admin"]) diff --git a/tests/managers/test_manager_user.py b/tests/managers/test_manager_user.py index 018b311..41fa186 100644 --- a/tests/managers/test_manager_user.py +++ b/tests/managers/test_manager_user.py @@ -13,6 +13,7 @@ from unittest.mock import patch import uuid +from .test_manager_ldap import MockEntrie class UserManagerTestCase(unittest.TestCase): def setUp(self): @@ -149,7 +150,7 @@ def test_try_auth_ldap(self,mock_object): config=get_current_config() config.LDAP["ldap_server"]="fakeserver" mock_object.return_value.__enter__.return_value.entries = [ - {"uid":"toto", "entry_dn":"cn=toto", "mail":"toto@example.com"}] + MockEntrie ("cn=toto",{config.LDAP["username_attr"]:"toto", "mail":"toto@example.com"})] authed= UserManager.try_auth('toto','toto') user = db.User.get(name="toto") self.assertEqual(authed, user) @@ -157,7 +158,7 @@ def test_try_auth_ldap(self,mock_object): # test admin and mail change config.LDAP["admin_filter"]="fake_admin_filer" mock_object.return_value.__enter__.return_value.entries = [ - {"uid":"toto", "entry_dn":"cn=toto", "mail":"toto2@example.com"}] + MockEntrie ("cn=toto",{config.LDAP["username_attr"]:"toto", "mail":"toto2@example.com"})] authed= UserManager.try_auth('toto','toto') self.assertEqual(authed.mail,"toto2@example.com") self.assertEqual(authed.admin,True)