Historique regroupé en un seul commit pour ne pas publier de données de production. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
114 lines
3.6 KiB
Python
114 lines
3.6 KiB
Python
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from ldap2nextcloud.config import (
|
|
ConfigError,
|
|
EmailRule,
|
|
PhoneRule,
|
|
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.title == "usrCadre"
|
|
|
|
|
|
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"
|