import logging from datetime import date from pathlib import Path from ldap2nextcloud.config import PhoneRule, load_directions, load_mapping from ldap2nextcloud.mapping import entry_to_contact, format_phone, parse_birthday from ldap2nextcloud.model import Email, Phone ROOT = Path(__file__).resolve().parent.parent MAPPING = load_mapping(ROOT / "mapping.example.yaml") DIRECTIONS = load_directions(ROOT / "directions.example.yaml") RULE = PhoneRule("telephoneNumber", "work", "+3312345", 4) def entry(**attrs): full = { "uid": ["omalmare"], "cn": ["MALMARE Oscar"], "sn": ["MALMARE"], "givenName": ["Oscar"], "mail": ["o.malmare@example.org"], "telephoneNumber": ["9052"], "usrDirectionId": ["620"], "usrCadre": ["Ingénieur"], "usrDateNaissance": ["19680615000000Z"], } full.update(attrs) raw = {k: [v.encode("utf-8") for v in vals] for k, vals in full.items() if vals is not None} raw["jpegPhoto"] = [b"\xff\xd8photo"] return raw def test_internal_number_gets_prefix(): assert format_phone("9052", RULE) == "+33123459052" def test_full_number_is_kept_without_spaces(): assert format_phone("01 23 45 90 52", RULE) == "0123459052" assert format_phone("+33 1 23.45.90.52", RULE) == "+33123459052" def test_parse_birthday_generalized_time(): assert parse_birthday("19680615000000Z") == date(1968, 6, 15) def test_parse_birthday_invalid_returns_none(): assert parse_birthday("pas une date") is None assert parse_birthday("19681315000000Z") is None def test_full_entry_maps_all_fields(): c = entry_to_contact(entry(), MAPPING, DIRECTIONS, with_birthday=True) assert c.key == "omalmare" assert c.given_name == "Oscar" and c.family_name == "MALMARE" assert c.full_name == "MALMARE Oscar" assert c.organization == "Exemple" assert c.title == "Ingénieur" assert c.emails == (Email("o.malmare@example.org", "work", True),) assert c.phones == (Phone("+33123459052", "work"),) assert c.category == "Direction des systèmes d'information" assert c.birthday == date(1968, 6, 15) assert c.photo == b"\xff\xd8photo" def test_birthday_only_when_requested(): c = entry_to_contact(entry(), MAPPING, DIRECTIONS, with_birthday=False) assert c.birthday is None def test_unknown_direction_code_gives_no_category(caplog): with caplog.at_level(logging.WARNING): c = entry_to_contact(entry(usrDirectionId=["999"]), MAPPING, DIRECTIONS, True) assert c.category is None assert "999" in caplog.text and "omalmare" in caplog.text assert "Oscar" not in caplog.text and "MALMARE" not in caplog.text def test_missing_optional_attributes(): raw = entry(mail=None, telephoneNumber=None, usrDirectionId=None, usrCadre=None) del raw["jpegPhoto"] c = entry_to_contact(raw, MAPPING, DIRECTIONS, True) assert c.emails == () and c.phones == () assert c.photo is None and c.category is None and c.title is None def test_entry_without_key_is_skipped(caplog): raw = entry() del raw["uid"] with caplog.at_level(logging.WARNING): assert entry_to_contact(raw, MAPPING, DIRECTIONS, True) is None assert caplog.records def test_accented_names_decoded(): c = entry_to_contact(entry(givenName=["Cédric"]), MAPPING, DIRECTIONS, True) assert c.given_name == "Cédric" def test_attribute_lookup_is_case_insensitive(): raw = entry() raw["givenname"] = raw.pop("givenName") c = entry_to_contact(raw, MAPPING, DIRECTIONS, True) assert c.given_name == "Oscar" def test_full_name_fallback_without_cn(): c = entry_to_contact(entry(cn=None), MAPPING, DIRECTIONS, True) assert c.full_name == "Oscar MALMARE" def test_preferred_email_domain(): c = entry_to_contact(entry(mail=["x@autre.fr", "x@example.org"]), MAPPING, DIRECTIONS, True) assert c.emails == (Email("x@example.org", "work", True), Email("x@autre.fr", "work", False))