From 2ada7241604c240e0bcbce122e4144d3522c6951 Mon Sep 17 00:00:00 2001 From: ZeGuigui Date: Tue, 17 Nov 2026 13:45:43 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Lire=20les=20attributs=20LDAP=20?= =?UTF-8?q?renvoy=C3=A9s=20avec=20une=20option=20(jpegPhoto;binary)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit L'annuaire renvoie la photo de certaines fiches sous jpegPhoto;binary. Ce nom ne correspondait pas à l'attribut demandé : la photo était ignorée. Les attributs sont désormais rattachés à leur nom de base. Co-Authored-By: Claude Opus 5.5 --- src/ldap2nextcloud/ldap_source.py | 14 +++++++++----- tests/test_ldap_source.py | 32 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/ldap2nextcloud/ldap_source.py b/src/ldap2nextcloud/ldap_source.py index 9fd1633..0870d0a 100644 --- a/src/ldap2nextcloud/ldap_source.py +++ b/src/ldap2nextcloud/ldap_source.py @@ -88,13 +88,17 @@ class LdapSource: if self._settings.ldap_group_dns: members = self._group_members() results = [r for r in results if _normalize_dn(r["dn"]) in members] - wanted = {a.lower() for a in attributes} + # Clé : nom de base en minuscules -> orthographe du mapping. + wanted = {a.lower(): a for a in attributes} entries: list[RawEntry] = [] for result in results: - raw = result.get("raw_attributes", {}) - entries.append( - {name: list(values) for name, values in raw.items() if name.lower() in wanted and values} - ) + entry: RawEntry = {} + for name, values in result.get("raw_attributes", {}).items(): + # L'annuaire peut renvoyer l'attribut avec une option (jpegPhoto;binary). + base = wanted.get(name.split(";", 1)[0].lower()) + if base and values: + entry.setdefault(base, []).extend(values) + entries.append(entry) return entries def fetch_birthday_keys(self, key_attr: str) -> set[str]: diff --git a/tests/test_ldap_source.py b/tests/test_ldap_source.py index 585548a..f6c917b 100644 --- a/tests/test_ldap_source.py +++ b/tests/test_ldap_source.py @@ -94,3 +94,35 @@ def test_photo_returned_as_raw_bytes(connection, ldif_entries): if "jpegPhoto" in attrs: assert by_uid[attrs["uid"][0]]["jpegPhoto"] == attrs["jpegPhoto"] break + + +class _StubConnection: + """Connexion minimale qui renvoie les attributs tels que l'annuaire réel les nomme.""" + + def __init__(self, raw_attributes): + stub = self + + class _Standard: + def paged_search(self, **kwargs): + return [{"type": "searchResEntry", "dn": "uid=x,ou=personnes,dc=example,dc=org", + "raw_attributes": stub.raw_attributes}] + + class _Extend: + standard = _Standard() + + self.raw_attributes = raw_attributes + self.extend = _Extend() + + +def test_attribute_with_binary_option_is_kept_under_its_base_name(): + conn = _StubConnection({ + "uid": [b"x"], "jpegPhoto;binary": [b"\xff\xd8photo"], "usrMotifAbsence": [b"ABC"], + }) + entries = LdapSource(SETTINGS, conn).fetch_entries(required_attributes(MAPPING)) + assert entries == [{"uid": [b"x"], "jpegPhoto": [b"\xff\xd8photo"]}] + + +def test_values_of_same_attribute_with_and_without_option_are_merged(): + conn = _StubConnection({"uid": [b"x"], "jpegPhoto": [b"a"], "jpegPhoto;binary": [b"b"]}) + entries = LdapSource(SETTINGS, conn).fetch_entries(required_attributes(MAPPING)) + assert entries[0]["jpegPhoto"] == [b"a", b"b"]