import fcntl from pathlib import Path import pytest import truststore from ldap2nextcloud import cli from ldap2nextcloud.carddav import CardDavError from ldap2nextcloud.planner import GuardError from ldap2nextcloud.sync import Summary ROOT = Path(__file__).resolve().parent.parent ENV = """\ LDAP_URL=ldap://ldap.test LDAP_BASE_DN=dc=test LDAP_FILTER=(objectClass=person) NEXTCLOUD_URL=https://cloud.test NEXTCLOUD_USER=annuaire NEXTCLOUD_APP_PASSWORD=secret NEXTCLOUD_ADDRESSBOOK=annuaire STATE_DB={state} MAPPING_FILE={root}/mapping.example.yaml DIRECTIONS_FILE={root}/directions.example.yaml """ @pytest.fixture def env_file(tmp_path, monkeypatch): for name in ("LDAP_URL", "NEXTCLOUD_APP_PASSWORD", "STATE_DB", "MAPPING_FILE"): monkeypatch.delenv(name, raising=False) path = tmp_path / ".env" path.write_text(ENV.format(state=tmp_path / "state" / "sync.sqlite", root=ROOT)) return path @pytest.fixture def calls(monkeypatch): record = [] monkeypatch.setattr(truststore, "inject_into_ssl", lambda: record.append("truststore")) monkeypatch.setattr(cli, "LdapSource", lambda settings: object()) monkeypatch.setattr(cli, "CardDavClient", lambda *a, **k: object()) return record def fake_run(result): def run(*args, **kwargs): if isinstance(result, Exception): raise result return result return run def test_truststore_injected_before_anything(env_file, calls, monkeypatch): real_load = cli.load_settings def spy(path): calls.append("settings") return real_load(path) monkeypatch.setattr(cli, "load_settings", spy) monkeypatch.setattr(cli, "run_sync", fake_run(Summary())) assert cli.main(["--env", str(env_file)]) == 0 assert calls[:2] == ["truststore", "settings"] assert calls.count("truststore") == 1 def test_success_returns_0(env_file, calls, monkeypatch): monkeypatch.setattr(cli, "run_sync", fake_run(Summary(created=2))) assert cli.main(["--env", str(env_file)]) == 0 def test_card_errors_return_1(env_file, calls, monkeypatch): monkeypatch.setattr(cli, "run_sync", fake_run(Summary(errors=1))) assert cli.main(["--env", str(env_file)]) == 1 def test_server_error_returns_1(env_file, calls, monkeypatch): monkeypatch.setattr(cli, "run_sync", fake_run(CardDavError("HTTP 401", 401))) assert cli.main(["--env", str(env_file)]) == 1 def test_guard_error_returns_2(env_file, calls, monkeypatch): monkeypatch.setattr(cli, "run_sync", fake_run(GuardError("trop"))) assert cli.main(["--env", str(env_file)]) == 2 def test_config_error_returns_3(tmp_path, calls, monkeypatch): monkeypatch.delenv("LDAP_URL", raising=False) empty = tmp_path / ".env" empty.write_text("") assert cli.main(["--env", str(empty)]) == 3 def test_lock_held_returns_4(env_file, calls, monkeypatch, tmp_path): monkeypatch.setattr(cli, "run_sync", fake_run(Summary())) lock_path = tmp_path / "state" / "sync.sqlite.lock" lock_path.parent.mkdir(parents=True) with open(lock_path, "w") as handle: fcntl.flock(handle, fcntl.LOCK_EX | fcntl.LOCK_NB) assert cli.main(["--env", str(env_file)]) == 4 def test_options_passed_to_run_sync(env_file, calls, monkeypatch): seen = {} def run(*args, **kwargs): seen.update(kwargs) return Summary() monkeypatch.setattr(cli, "run_sync", run) cli.main(["--env", str(env_file), "--dry-run", "--initial", "--force"]) assert seen["dry_run"] and seen["initial"] and seen["force"] def test_progress_follows_terminal(env_file, calls, monkeypatch): seen = {} def run(*args, **kwargs): seen["progress"] = kwargs["progress"] return Summary() monkeypatch.setattr(cli, "run_sync", run) monkeypatch.setattr(cli.sys.stderr, "isatty", lambda: False) cli.main(["--env", str(env_file)]) from ldap2nextcloud.progress import LogProgress assert isinstance(seen["progress"], LogProgress)