Merge branch 'Aufinal/ldap_mail' into 'master'

Utilise le mail LDAP lors de l'inscription d'utilisateurs

Closes #268

See merge request klub-dev-ens/gestioCOF!437
This commit is contained in:
Martin Pepin 2020-08-25 19:08:25 +02:00
commit 2cf0ccbb6b
6 changed files with 20 additions and 12 deletions

View file

@ -47,7 +47,7 @@ class BDSOthersSearch(autocomplete.ModelSearch):
class BDSLDAPSearch(autocomplete.LDAPSearch): class BDSLDAPSearch(autocomplete.LDAPSearch):
def result_link(self, clipper): def result_link(self, clipper):
url = reverse("bds:user.create.fromclipper", args=(clipper.clipper,)) url = reverse("bds:user.create.fromclipper", args=(clipper.clipper,))
get = {"fullname": clipper.fullname} get = {"fullname": clipper.fullname, "mail": clipper.mail}
return "{}?{}".format(url, urlencode(get)) return "{}?{}".format(url, urlencode(get))

View file

@ -47,7 +47,6 @@ class TestRegistrationView(TestCase):
def test_get(self, mock_messages): def test_get(self, mock_messages):
user = User.objects.create_user(username="toto") user = User.objects.create_user(username="toto")
url = reverse("bds:user.update", args=(user.id,)) url = reverse("bds:user.update", args=(user.id,))
print(url)
client = Client() client = Client()
# Anonymous GET # Anonymous GET

View file

@ -73,7 +73,7 @@ class UserCreateView(StaffRequiredMixin, MultipleFormView):
def get_user_initial(self): def get_user_initial(self):
if "clipper" in self.kwargs: if "clipper" in self.kwargs:
clipper = self.kwargs["clipper"] clipper = self.kwargs["clipper"]
email = "{}@clipper.ens.fr".format(clipper) email = self.request.GET.get("mail", "{}@clipper.ens.fr".format(clipper))
fullname = self.request.GET.get("fullname", None) fullname = self.request.GET.get("fullname", None)
if fullname: if fullname:

View file

@ -329,15 +329,15 @@ class RegistrationAutocompleteViewTests(MockLDAPMixin, ViewTestCaseMixin, TestCa
self._test("wy bd", [self.u2], [self.m1], []) self._test("wy bd", [self.u2], [self.m1], [])
def test_clipper(self): 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( mock_ldap.ldap_obj.search_s.assert_called_once_with(
"dc=spi,dc=ens,dc=fr", "dc=spi,dc=ens,dc=fr",
mock_ldap.SCOPE_SUBTREE, mock_ldap.SCOPE_SUBTREE,
"(&(|(cn=*aa*)(uid=*aa*))(|(cn=*bb*)(uid=*bb*)))", "(&(|(cn=*aa*)(uid=*aa*))(|(cn=*bb*)(uid=*bb*)))",
["cn", "uid"], ["cn", "uid", "mail"],
) )
def test_clipper_escaped(self): def test_clipper_escaped(self):
@ -348,9 +348,9 @@ class RegistrationAutocompleteViewTests(MockLDAPMixin, ViewTestCaseMixin, TestCa
mock_ldap.ldap_obj.search_s.assert_not_called() mock_ldap.ldap_obj.search_s.assert_not_called()
def test_clipper_no_duplicate(self): 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.username = "uid"
self.u1.save() self.u1.save()

View file

@ -129,13 +129,14 @@ class ModelSearch(SearchUnit):
# LDAP search # LDAP search
# --- # ---
Clipper = namedtuple("Clipper", ["clipper", "fullname"]) Clipper = namedtuple("Clipper", ["clipper", "fullname", "mail"])
class LDAPSearch(SearchUnit): class LDAPSearch(SearchUnit):
ldap_server_url = getattr(settings, "LDAP_SERVER_URL", None) ldap_server_url = getattr(settings, "LDAP_SERVER_URL", None)
domain_component = "dc=spi,dc=ens,dc=fr" domain_component = "dc=spi,dc=ens,dc=fr"
search_fields = ["cn", "uid"] search_fields = ["cn", "uid"]
attr_list = ["cn", "uid", "mail"]
verbose_name = _("Comptes clippers") verbose_name = _("Comptes clippers")
@ -172,12 +173,13 @@ class LDAPSearch(SearchUnit):
try: try:
ldap_obj = ldap.initialize(self.ldap_server_url) ldap_obj = ldap.initialize(self.ldap_server_url)
res = ldap_obj.search_s( 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 [ return [
Clipper( Clipper(
clipper=attrs["uid"][0].decode("utf-8"), clipper=attrs["uid"][0].decode("utf-8"),
fullname=attrs["cn"][0].decode("utf-8"), fullname=attrs["cn"][0].decode("utf-8"),
mail=attrs["mail"][0].decode("utf-8"),
) )
for (_, attrs) in res for (_, attrs) in res
] ]

View file

@ -34,8 +34,15 @@ class MockLDAPMixin:
def mockLDAP(self, results): def mockLDAP(self, results):
entries = [ 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 object whose `search_s` method always returns the same results.
mock_ldap_obj = mock.Mock() mock_ldap_obj = mock.Mock()