Files
ping-mee-self-service-platform/portal/tests/test_config.py
T
ebbe.bassandClaude Sonnet 5.5 96d3e77161 feat: add directory adapter
LDAPS directory adapter with certificate verification, allow-listed reads and
writes, escaped filters, password verification and change, password policy
and account state, plus an in-memory fake. Adds the LDAP_* configuration,
python-ldap with its image libraries, the CA file mount and an opt-in live
test for the test OU.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N3t4WL7bd9bdHHNjEsxzqC
2026-10-17 18:28:50 +02:00

108 lines
3.1 KiB
Python

import os
import subprocess
import sys
from pathlib import Path
import pytest
from portal.config.env import ConfigError, load_config
ROOT = Path(__file__).resolve().parents[2]
def test_valid_environment_uses_safe_defaults(valid_env):
config = load_config(valid_env)
assert config.debug is False
assert config.log_level == "INFO"
assert config.default_timezone == "Europe/Berlin"
assert config.trusted_proxies == []
assert config.allowed_hosts == ["testserver"]
def test_every_problem_is_listed_and_no_value_leaks(valid_env):
secret = "short-secret"
env = {
**valid_env,
"SECRET_KEY": secret,
"LOG_LEVEL": "loud",
"DEBUG": "maybe",
"TRUSTED_PROXIES": "not-an-ip",
"DEFAULT_TIMEZONE": "Nowhere/Land",
}
del env["ALLOWED_HOSTS"]
with pytest.raises(ConfigError) as info:
load_config(env)
message = str(info.value)
for name in (
"SECRET_KEY",
"ALLOWED_HOSTS",
"LOG_LEVEL",
"DEBUG",
"TRUSTED_PROXIES",
"DEFAULT_TIMEZONE",
):
assert name in message
assert secret not in message
def test_missing_data_dir_is_reported(valid_env, tmp_path):
with pytest.raises(ConfigError, match="DATA_DIR"):
load_config({**valid_env, "DATA_DIR": str(tmp_path / "missing")})
def test_process_exits_nonzero_naming_both_variables(tmp_path):
env = {k: v for k, v in os.environ.items() if k not in ("SECRET_KEY", "LOG_LEVEL")}
env.update(
ALLOWED_HOSTS="testserver",
DATA_DIR=str(tmp_path),
LOG_LEVEL="loud",
DJANGO_SETTINGS_MODULE="portal.config.settings",
)
result = subprocess.run( # noqa: S603
[sys.executable, "manage.py", "check"],
cwd=ROOT,
env=env,
capture_output=True,
text=True,
check=False,
)
assert result.returncode != 0
assert "SECRET_KEY" in result.stderr
assert "LOG_LEVEL" in result.stderr
assert "Traceback" not in result.stderr
@pytest.mark.parametrize(
"name",
[
"LDAP_SERVER",
"LDAP_USER_OU_DN",
"LDAP_BIND_DN",
"LDAP_BIND_PASSWORD",
"LDAP_CA_FILE",
"LDAP_PUBLISHER_GROUP_DN",
"LDAP_ADMIN_GROUP_DN",
],
)
def test_each_directory_variable_is_required(valid_env, name):
del valid_env[name]
with pytest.raises(ConfigError, match=name):
load_config(valid_env)
def test_directory_settings_are_loaded_and_the_password_is_not_printed(valid_env):
directory = load_config(valid_env).directory
assert directory.server == "dc.example.test"
assert directory.user_ou_dn == "OU=Users,DC=example,DC=test"
assert "service-password" not in repr(directory)
def test_ldap_server_must_not_be_a_url(valid_env):
with pytest.raises(ConfigError, match="LDAP_SERVER"):
load_config({**valid_env, "LDAP_SERVER": "ldap://dc.example.test"})
def test_a_missing_ca_file_is_reported(valid_env, tmp_path):
with pytest.raises(ConfigError, match="LDAP_CA_FILE: must be an existing file"):
load_config({**valid_env, "LDAP_CA_FILE": str(tmp_path / "missing.pem")})