🤖 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>
This commit is contained in:
zeguiguiandClaude Opus 5.5 committed 2026-11-17 19:42:35 +01:00
1 parent 35f90b8fb1
commit 6d8ec287ec
8 files changed
+102 -4

No files matched your search

+17
View File
@@ -144,3 +144,20 @@ def test_load_directions_example_file():
d = load_directions(ROOT / "directions.example.yaml")
assert len(d) == 3
assert d["620"] == "Direction des systèmes d'information"
def test_skip_without_group_and_phone_defaults_to_false(tmp_path):
assert mapping_with_photo(tmp_path, "").skip_without_group_and_phone is False
def test_skip_without_group_and_phone_is_read(tmp_path):
path = tmp_path / "mapping.yaml"
path.write_text("key: uid\nskip_without_group_and_phone: true\nfields:\n given_name: givenName\n family_name: sn\n")
assert load_mapping(path).skip_without_group_and_phone is True
def test_skip_without_group_and_phone_must_be_boolean(tmp_path):
path = tmp_path / "mapping.yaml"
path.write_text("key: uid\nskip_without_group_and_phone: oui\nfields:\n given_name: givenName\n family_name: sn\n")
with pytest.raises(ConfigError, match="skip_without_group_and_phone"):
load_mapping(path)
+33 -1
View File
@@ -3,7 +3,9 @@ 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 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
@@ -111,3 +113,33 @@ def test_full_name_fallback_without_cn():
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)
+18
View File
@@ -7,6 +7,8 @@ from PIL import Image
from ldap2nextcloud.carddav import CardDavError
from ldap2nextcloud.photo import PreparedPhoto
from dataclasses import replace
from ldap2nextcloud.config import Settings, load_directions, load_mapping
from ldap2nextcloud.model import contact_href
from ldap2nextcloud.planner import GuardError
@@ -278,3 +280,19 @@ def test_dry_run_does_not_start_progress(tmp_path):
progress = RecordingProgress()
run(tmp_path, FakeClient(), dry_run=True, progress=progress)
assert progress.total is None
def test_entries_without_direction_nor_phone_are_not_synced(tmp_path, caplog):
source = FakeSource()
for entry in source.entries[:2]:
del entry["telephoneNumber"]
del source.entries[0]["usrDirectionId"] # seule la première n'a ni l'un ni l'autre
client = FakeClient({contact_href("omalmare"): ("x", "BEGIN:VCARD")})
settings = make_settings(tmp_path)
strict = replace(MAPPING, skip_without_group_and_phone=True)
with caplog.at_level(logging.INFO), StateDB(settings.state_db) as state:
summary = run_sync(settings, strict, DIRECTIONS, source, client, state, force=True)
assert contact_href("omalmare") not in client.cards # fiche déjà présente : supprimée
assert contact_href("cbarssac") in client.cards
assert (summary.created, summary.deleted) == (2, 1)
assert "1 écartées (ni Direction ni téléphone)" in caplog.text