from datetime import date, datetime, timezone import vobject from ldap2nextcloud.model import Contact, Email, Phone, contact_uid from ldap2nextcloud.vcard import read_sync_hash, render_vcard NOW = datetime(2026, 11, 17, 8, 30, tzinfo=timezone.utc) FULL = Contact( key="omalmare", given_name="Oscar", family_name="MALMARE", full_name="MALMARE Oscar", organization="Exemple", title="Ingénieur", emails=(Email("o.malmare@example.org", "work", True),), phones=(Phone("+33123459052", "work"),), category="Direction des systèmes d'information", birthday=date(1968, 6, 15), photo=b"source", ) MINIMAL = Contact( key="x", given_name="A", family_name="B", full_name="A B", organization=None, title=None, emails=(), phones=(), category=None, birthday=None, photo=None, ) LEGACY = "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:Ancien\r\nN:Ancien;;;;\r\nEND:VCARD\r\n" def test_render_contains_expected_properties(): card = vobject.readOne(render_vcard(FULL, b"\xff\xd8jpeg", "abc", NOW)) assert card.version.value == "3.0" assert card.uid.value == contact_uid("omalmare") assert card.n.value.family == "MALMARE" and card.n.value.given == "Oscar" assert card.fn.value == "MALMARE Oscar" assert card.org.value == ["Exemple"] assert card.title.value == "Ingénieur" assert card.tel.value == "+33123459052" assert {t.upper() for t in card.tel.params["TYPE"]} == {"VOICE", "WORK"} assert card.email.value == "o.malmare@example.org" assert {t.upper() for t in card.email.params["TYPE"]} == {"INTERNET", "WORK", "PREF"} assert card.categories.value == ["Direction des systèmes d'information"] assert card.bday.value == "1968-06-15" assert card.photo.value == b"\xff\xd8jpeg" assert card.x_ldapsync_hash.value == "abc" assert card.rev.value == "20261117T083000Z" assert card.prodid.value == "-//ldap2nextcloud//FR" def test_optional_fields_omitted(): text = render_vcard(MINIMAL, None, "abc", NOW) for prop in ("TITLE", "TEL", "EMAIL", "CATEGORIES", "BDAY", "PHOTO", "ORG"): assert f"\r\n{prop}" not in text def test_no_legacy_properties(): text = render_vcard(FULL, None, "abc", NOW) assert "KIND" not in text and "X-ABUID" not in text and "ADR" not in text def test_accents_and_long_lines_roundtrip(): long_name = "Élodie-Françoise " * 8 contact = Contact(**{**FULL.__dict__, "full_name": long_name.strip()}) text = render_vcard(contact, None, "abc", NOW) assert vobject.readOne(text).fn.value == long_name.strip() assert all(len(line.encode("utf-8")) <= 75 for line in text.split("\r\n")) def test_crlf_line_endings(): text = render_vcard(FULL, b"\xff\xd8jpeg", "abc", NOW) assert "\r\n" in text assert "\n" not in text.replace("\r\n", "") def test_category_with_comma_stays_single_group(): contact = Contact(**{**FULL.__dict__, "category": "Recherche, Innovation"}) card = vobject.readOne(render_vcard(contact, None, "abc", NOW)) assert card.categories.value == ["Recherche, Innovation"] def test_read_sync_hash_roundtrip(): assert read_sync_hash(render_vcard(FULL, None, "abc", NOW)) == "abc" def test_read_sync_hash_absent(): assert read_sync_hash(LEGACY) is None def test_read_sync_hash_unreadable(): assert read_sync_hash("pas une vcard") is None