Nextcloud et Android affichent la photo en carré découpé au milieu d'une photo verticale, ce qui coupe les cheveux. La photo est désormais recadrée en carré autour du visage, détecté localement par YuNet (OpenCV, modèle MIT versionné dans le paquet) : les yeux sont placés à la même hauteur sur toutes les photos. Sans visage détecté, le carré est aligné en haut et compté dans le journal (-v). Réglages dans mapping.yaml (fields.photo.crop : eyes_at, side, ou false). Ils entrent dans l'empreinte des seules fiches avec photo : celles-ci seront renvoyées une fois au prochain passage. Les tests utilisent un visage généré par thispersondoesnotexist.com (tests/data/README.md). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
147 lines
4.8 KiB
Python
147 lines
4.8 KiB
Python
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from ldap2nextcloud.config import (
|
|
ConfigError,
|
|
EmailRule,
|
|
PhoneRule,
|
|
PhotoCrop,
|
|
load_directions,
|
|
load_mapping,
|
|
load_settings,
|
|
)
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
|
|
ALL_VARS = [
|
|
"LDAP_URL", "LDAP_BIND_DN", "LDAP_BIND_PASSWORD", "LDAP_BASE_DN", "LDAP_FILTER",
|
|
"LDAP_BIRTHDAY_FILTER", "LDAP_GROUP_DNS", "LDAP_CA_CERT", "NEXTCLOUD_URL",
|
|
"NEXTCLOUD_USER", "NEXTCLOUD_APP_PASSWORD", "NEXTCLOUD_ADDRESSBOOK", "STATE_DB",
|
|
"MAPPING_FILE", "DIRECTIONS_FILE", "PHOTO_MAX_SIZE", "PHOTO_QUALITY",
|
|
"MAX_DELETE_RATIO", "LOG_LEVEL",
|
|
]
|
|
|
|
MINIMAL_ENV = """\
|
|
LDAP_URL=ldap://ldap.test
|
|
LDAP_BASE_DN=ou=personnes,dc=test
|
|
LDAP_FILTER=(objectClass=person)
|
|
NEXTCLOUD_URL=https://cloud.test/
|
|
NEXTCLOUD_USER=annuaire
|
|
NEXTCLOUD_APP_PASSWORD=secret
|
|
NEXTCLOUD_ADDRESSBOOK=annuaire
|
|
"""
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def clean_env(monkeypatch):
|
|
for name in ALL_VARS:
|
|
monkeypatch.delenv(name, raising=False)
|
|
|
|
|
|
def write_env(tmp_path, content):
|
|
path = tmp_path / ".env"
|
|
path.write_text(content)
|
|
return path
|
|
|
|
|
|
def test_load_settings_reads_env_file(tmp_path):
|
|
settings = load_settings(write_env(tmp_path, MINIMAL_ENV))
|
|
assert settings.addressbook_url == (
|
|
"https://cloud.test/remote.php/dav/addressbooks/users/annuaire/annuaire/"
|
|
)
|
|
assert settings.photo_max_size == 512
|
|
assert settings.photo_quality == 80
|
|
assert settings.max_delete_ratio == 0.10
|
|
assert settings.ldap_bind_dn is None
|
|
assert settings.ldap_birthday_filter is None
|
|
assert settings.ldap_group_dns == []
|
|
|
|
|
|
def test_load_settings_missing_variable_names_it(tmp_path):
|
|
content = MINIMAL_ENV.replace("NEXTCLOUD_APP_PASSWORD=secret\n", "")
|
|
with pytest.raises(ConfigError, match="NEXTCLOUD_APP_PASSWORD"):
|
|
load_settings(write_env(tmp_path, content))
|
|
|
|
|
|
def test_load_settings_group_dns_split_on_semicolon(tmp_path):
|
|
content = MINIMAL_ENV + "LDAP_GROUP_DNS=cn=a,dc=x;cn=b,dc=x\n"
|
|
settings = load_settings(write_env(tmp_path, content))
|
|
assert settings.ldap_group_dns == ["cn=a,dc=x", "cn=b,dc=x"]
|
|
|
|
|
|
def test_environment_overrides_env_file(tmp_path, monkeypatch):
|
|
monkeypatch.setenv("NEXTCLOUD_USER", "autre")
|
|
settings = load_settings(write_env(tmp_path, MINIMAL_ENV))
|
|
assert settings.nextcloud_user == "autre"
|
|
|
|
|
|
def test_invalid_number_is_reported(tmp_path):
|
|
content = MINIMAL_ENV + "PHOTO_MAX_SIZE=grand\n"
|
|
with pytest.raises(ConfigError, match="PHOTO_MAX_SIZE"):
|
|
load_settings(write_env(tmp_path, content))
|
|
|
|
|
|
def test_example_env_file_is_loadable(monkeypatch):
|
|
monkeypatch.setenv("NEXTCLOUD_APP_PASSWORD", "x")
|
|
settings = load_settings(ROOT / ".env.example")
|
|
assert settings.ldap_birthday_filter == "(usrDirectionId=620)"
|
|
assert settings.ldap_bind_dn is None
|
|
|
|
|
|
def test_load_mapping_example_file():
|
|
m = load_mapping(ROOT / "mapping.example.yaml")
|
|
assert m.key == "uid"
|
|
assert m.organization == "Exemple"
|
|
assert m.group_attr == "usrDirectionId"
|
|
assert m.phones == [PhoneRule("telephoneNumber", "work", "+3312345", 4)]
|
|
assert m.emails == [EmailRule("mail", "work", "example.org")]
|
|
assert m.birthday_attr == "usrDateNaissance"
|
|
assert m.photo == "jpegPhoto"
|
|
assert m.photo_crop == PhotoCrop(eyes_at=0.42, side=5.5)
|
|
assert m.title == "usrCadre"
|
|
|
|
|
|
def mapping_with_photo(tmp_path, photo):
|
|
path = tmp_path / "mapping.yaml"
|
|
path.write_text(f"key: uid\nfields:\n given_name: givenName\n family_name: sn\n{photo}")
|
|
return load_mapping(path)
|
|
|
|
|
|
def test_photo_crop_is_on_by_default(tmp_path):
|
|
m = mapping_with_photo(tmp_path, " photo: jpegPhoto\n")
|
|
assert m.photo == "jpegPhoto" and m.photo_crop == PhotoCrop()
|
|
|
|
|
|
def test_photo_crop_can_be_disabled(tmp_path):
|
|
m = mapping_with_photo(tmp_path, " photo: {attr: jpegPhoto, crop: false}\n")
|
|
assert m.photo == "jpegPhoto" and m.photo_crop is None
|
|
|
|
|
|
def test_photo_crop_settings_are_read(tmp_path):
|
|
m = mapping_with_photo(tmp_path, " photo: {attr: jpegPhoto, crop: {eyes_at: 0.4, side: 6}}\n")
|
|
assert m.photo_crop == PhotoCrop(eyes_at=0.4, side=6.0)
|
|
|
|
|
|
def test_no_photo_means_no_crop(tmp_path):
|
|
assert mapping_with_photo(tmp_path, "").photo_crop is None
|
|
|
|
|
|
@pytest.mark.parametrize("crop", ["{eyes_at: 1.5}", "{eyes_at: 0}", "{side: 0}", "{side: abc}", "oui"])
|
|
def test_invalid_photo_crop_is_error(tmp_path, crop):
|
|
with pytest.raises(ConfigError, match="crop"):
|
|
mapping_with_photo(tmp_path, f" photo: {{attr: jpegPhoto, crop: {crop}}}\n")
|
|
|
|
|
|
def test_load_mapping_missing_key_is_error(tmp_path):
|
|
path = tmp_path / "mapping.example.yaml"
|
|
path.write_text("fields:\n given_name: givenName\n")
|
|
with pytest.raises(ConfigError, match="key"):
|
|
load_mapping(path)
|
|
|
|
|
|
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"
|