Files
zeguiguiandClaude Opus 5.5 6d8ec287ec 🤖 Option pour écarter les fiches sans Direction ni téléphone
Avec skip_without_group_and_phone: true dans mapping.yaml, une fiche
dont l'attribut Direction est vide et qui n'a aucun téléphone n'est
pas synchronisée (et est supprimée du carnet si elle y est). Un code
Direction inconnu de directions.yaml ne compte pas comme vide.
L'option est désactivée par défaut ; le nombre de fiches écartées est
journalisé.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 19:42:35 +01:00

146 lines
4.9 KiB
Python

import logging
from datetime import date
from pathlib import Path
from ldap2nextcloud.config import PhoneRule, load_directions, load_mapping
from dataclasses import replace
from ldap2nextcloud.mapping import entry_to_contact, format_phone, is_excluded, 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))
# --- Fiches écartées : ni Direction ni téléphone -----------------------------
STRICT = replace(MAPPING, skip_without_group_and_phone=True)
def test_entry_without_direction_nor_phone_is_excluded():
assert is_excluded(entry(usrDirectionId=None, telephoneNumber=None), STRICT)
def test_blank_direction_counts_as_missing():
assert is_excluded(entry(usrDirectionId=[" "], telephoneNumber=[""]), STRICT)
def test_entry_with_phone_but_no_direction_is_kept():
assert not is_excluded(entry(usrDirectionId=None), STRICT)
def test_entry_with_direction_but_no_phone_is_kept():
assert not is_excluded(entry(telephoneNumber=None), STRICT)
def test_unknown_direction_code_is_not_an_empty_direction():
assert not is_excluded(entry(usrDirectionId=["999"], telephoneNumber=None), STRICT)
def test_rule_is_off_by_default():
assert not MAPPING.skip_without_group_and_phone
assert not is_excluded(entry(usrDirectionId=None, telephoneNumber=None), MAPPING)