Skip to content

Commit

Permalink
fix issue with ldap entry_dn call
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentDcmps committed Mar 21, 2023
1 parent 5fb5633 commit ca23003
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
3 changes: 2 additions & 1 deletion supysonic/managers/ldap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
8 changes: 7 additions & 1 deletion tests/managers/test_manager_ldap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand All @@ -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":"[email protected]"}]
MockEntrie ("cn=toto",{LDAP["username_attr"]:"toto", "mail":"[email protected]"})]
ldap = LdapManager(**LDAP)
ldap_user = ldap.try_auth("toto", "toto")
self.assertFalse(ldap_user["admin"])
Expand Down
5 changes: 3 additions & 2 deletions tests/managers/test_manager_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from unittest.mock import patch
import uuid

from .test_manager_ldap import MockEntrie

class UserManagerTestCase(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -149,15 +150,15 @@ 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":"[email protected]"}]
MockEntrie ("cn=toto",{config.LDAP["username_attr"]:"toto", "mail":"[email protected]"})]
authed= UserManager.try_auth('toto','toto')
user = db.User.get(name="toto")
self.assertEqual(authed, user)

# 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":"[email protected]"}]
MockEntrie ("cn=toto",{config.LDAP["username_attr"]:"toto", "mail":"[email protected]"})]
authed= UserManager.try_auth('toto','toto')
self.assertEqual(authed.mail,"[email protected]")
self.assertEqual(authed.admin,True)
Expand Down

0 comments on commit ca23003

Please sign in to comment.