From 8fa07bb84524c29660fc4559e412845c87dfd39c Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Mon, 3 Aug 2020 14:54:58 +0200 Subject: [PATCH] Fix tests --- bds/tests/test_views.py | 1 - gestioncof/tests/test_views.py | 10 +++++----- shared/autocomplete.py | 5 +++-- shared/tests/mixins.py | 11 +++++++++-- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/bds/tests/test_views.py b/bds/tests/test_views.py index 8e30adf4..20ce02a3 100644 --- a/bds/tests/test_views.py +++ b/bds/tests/test_views.py @@ -47,7 +47,6 @@ class TestRegistrationView(TestCase): def test_get(self, mock_messages): user = User.objects.create_user(username="toto") url = reverse("bds:user.update", args=(user.id,)) - print(url) client = Client() # Anonymous GET diff --git a/gestioncof/tests/test_views.py b/gestioncof/tests/test_views.py index 0107b6a1..e5a4e033 100644 --- a/gestioncof/tests/test_views.py +++ b/gestioncof/tests/test_views.py @@ -329,15 +329,15 @@ class RegistrationAutocompleteViewTests(MockLDAPMixin, ViewTestCaseMixin, TestCa self._test("wy bd", [self.u2], [self.m1], []) def test_clipper(self): - mock_ldap = self.mockLDAP([("uid", "first last")]) + mock_ldap = self.mockLDAP([("uid", "first last", "mail")]) - self._test("aa bb", [], [], [Clipper("uid", "first last")]) + self._test("aa bb", [], [], [Clipper("uid", "first last", "mail")]) mock_ldap.ldap_obj.search_s.assert_called_once_with( "dc=spi,dc=ens,dc=fr", mock_ldap.SCOPE_SUBTREE, "(&(|(cn=*aa*)(uid=*aa*))(|(cn=*bb*)(uid=*bb*)))", - ["cn", "uid"], + ["cn", "uid", "mail"], ) def test_clipper_escaped(self): @@ -348,9 +348,9 @@ class RegistrationAutocompleteViewTests(MockLDAPMixin, ViewTestCaseMixin, TestCa mock_ldap.ldap_obj.search_s.assert_not_called() def test_clipper_no_duplicate(self): - self.mockLDAP([("uid", "abc")]) + self.mockLDAP([("uid", "abc", "mail")]) - self._test("abc", [self.u1], [], [Clipper("uid", "abc")]) + self._test("abc", [self.u1], [], [Clipper("uid", "abc", "mail")]) self.u1.username = "uid" self.u1.save() diff --git a/shared/autocomplete.py b/shared/autocomplete.py index e0fa8413..971a8dfb 100644 --- a/shared/autocomplete.py +++ b/shared/autocomplete.py @@ -135,7 +135,8 @@ Clipper = namedtuple("Clipper", ["clipper", "fullname", "mail"]) class LDAPSearch(SearchUnit): ldap_server_url = getattr(settings, "LDAP_SERVER_URL", None) domain_component = "dc=spi,dc=ens,dc=fr" - search_fields = ["cn", "uid", "mail"] + search_fields = ["cn", "uid"] + attr_list = ["cn", "uid", "mail"] verbose_name = _("Comptes clippers") @@ -172,7 +173,7 @@ class LDAPSearch(SearchUnit): try: ldap_obj = ldap.initialize(self.ldap_server_url) res = ldap_obj.search_s( - self.domain_component, ldap.SCOPE_SUBTREE, query, self.search_fields + self.domain_component, ldap.SCOPE_SUBTREE, query, self.attr_list ) return [ Clipper( diff --git a/shared/tests/mixins.py b/shared/tests/mixins.py index 7b5edf6a..ea83616a 100644 --- a/shared/tests/mixins.py +++ b/shared/tests/mixins.py @@ -34,8 +34,15 @@ class MockLDAPMixin: def mockLDAP(self, results): entries = [ - ("whatever", {"cn": [name.encode("utf-8")], "uid": [uid.encode("utf-8")]}) - for uid, name in results + ( + "whatever", + { + "cn": [name.encode("utf-8")], + "uid": [uid.encode("utf-8")], + "mail": [mail.encode("utf-8")], + }, + ) + for uid, name, mail in results ] # Mock ldap object whose `search_s` method always returns the same results. mock_ldap_obj = mock.Mock()