Switch to python-ldap (instead of ldap3)

This commit is contained in:
Martin Pépin 2020-06-16 17:21:59 +02:00
parent 3ca8b45014
commit 028b6f6cb7
No known key found for this signature in database
GPG key ID: E7520278B1774448
4 changed files with 41 additions and 31 deletions

View file

@ -22,32 +22,33 @@ class MockLDAPMixin:
appeler `with Connection(*args, **kwargs) as foo` pour que le test fonctionne.
"""
class MockLDAPModule:
SCOPE_SUBTREE = None # whatever
def __init__(self, ldap_obj):
self.ldap_obj = ldap_obj
def initialize(self, *args):
"""Always return the same ldap object."""
return self.ldap_obj
def mockLDAP(self, results):
class Elt:
def __init__(self, value):
self.value = value
entries = [
("whatever", {"cn": [name.encode("utf-8")], "uid": [uid.encode("utf-8")]})
for uid, name in results
]
# Mock ldap object whose `search_s` method always returns the same results.
mock_ldap_obj = mock.Mock()
mock_ldap_obj.search_s = mock.Mock(return_value=entries)
class Entry:
def __init__(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, Elt(v))
# Mock ldap module whose `initialize_method` always return the same ldap object.
mock_ldap_module = self.MockLDAPModule(mock_ldap_obj)
results_as_ldap = [Entry(uid=uid, cn=name) for uid, name in results]
mock_connection = mock.MagicMock()
mock_connection.entries = results_as_ldap
# Connection is used as a context manager.
mock_context_manager = mock.MagicMock()
mock_context_manager.return_value.__enter__.return_value = mock_connection
patcher = mock.patch(
"shared.views.autocomplete.Connection", new=mock_context_manager
)
patcher = mock.patch("shared.views.autocomplete.ldap", new=mock_ldap_module)
patcher.start()
self.addCleanup(patcher.stop)
return mock_connection
return mock_ldap_module
class CSVResponseMixin: