from dataclasses import replace from datetime import date from ldap2nextcloud.config import PhotoCrop from ldap2nextcloud.model import Contact, Email, Phone, contact_href, contact_uid, fingerprint BASE = 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"\xff\xd8photo", ) def test_href_is_stable_and_derived_from_key(): assert contact_href("omalmare") == contact_href("omalmare") assert contact_href("omalmare") == f"{contact_uid('omalmare')}.vcf" assert contact_href("omalmare") != contact_href("cbarssac") def test_uid_is_known_uuid5(): # Valeur figée : changer le namespace casserait l'identité de toutes les fiches. assert contact_uid("omalmare") == contact_uid("omalmare") assert len(contact_uid("omalmare")) == 36 assert contact_uid("omalmare")[14] == "5" def test_fingerprint_stable_for_equal_contacts(): assert fingerprint(BASE, 512, 80) == fingerprint(replace(BASE), 512, 80) def test_fingerprint_changes_with_any_field(): reference = fingerprint(BASE, 512, 80) for changed in ( replace(BASE, category="Autre"), replace(BASE, photo=b"autre"), replace(BASE, photo=None), replace(BASE, birthday=None), replace(BASE, phones=()), replace(BASE, title="Chef"), ): assert fingerprint(changed, 512, 80) != reference def test_fingerprint_changes_with_photo_settings(): assert fingerprint(BASE, 512, 80) != fingerprint(BASE, 720, 80) assert fingerprint(BASE, 512, 80) != fingerprint(BASE, 512, 90) def test_fingerprint_changes_with_crop_settings_when_contact_has_photo(): plain = fingerprint(BASE, 512, 80) cropped = fingerprint(BASE, 512, 80, PhotoCrop()) assert plain != cropped assert cropped != fingerprint(BASE, 512, 80, PhotoCrop(eyes_at=0.5)) assert cropped != fingerprint(BASE, 512, 80, PhotoCrop(side=6)) def test_crop_settings_do_not_touch_contacts_without_photo(): no_photo = replace(BASE, photo=None) assert fingerprint(no_photo, 512, 80) == fingerprint(no_photo, 512, 80, PhotoCrop())