PyLimeRPC/tests/test_config.py

175 lines
6.5 KiB
Python
Raw Permalink Normal View History

"""Unit tests for pylimerpc.config (pytest)."""
from __future__ import annotations
import os
import pytest
from pylimerpc.config import LsConfig, load_dotenv, parse_dotenv
DOTENV = """\
# comment line
LIMESURVEY_BASE_URL=https://ls.example.com/index.php?r=admin/remotecontrol
LIMESURVEY_USERNAME=test_user
LIMESURVEY_PASSWORD="secret with spaces"
LIMESURVEY_TIMEOUT_SECONDS=45
LIMESURVEY_OWNER_ID=123
"""
BASE_ENV = {
"LIMESURVEY_BASE_URL": "https://ls.example.com/index.php?r=admin/remotecontrol",
"LIMESURVEY_USERNAME": "test_user",
"LIMESURVEY_PASSWORD": "geheim",
}
def test_parse_dotenv(tmp_path):
env_file = tmp_path / ".env"
env_file.write_text(DOTENV, encoding="utf-8")
values = parse_dotenv(env_file)
assert values["LIMESURVEY_BASE_URL"] == "https://ls.example.com/index.php?r=admin/remotecontrol"
assert values["LIMESURVEY_USERNAME"] == "test_user"
# quotes stripped, but inner content (spaces) is preserved
assert values["LIMESURVEY_PASSWORD"] == "secret with spaces"
assert values["LIMESURVEY_TIMEOUT_SECONDS"] == "45"
assert values["LIMESURVEY_OWNER_ID"] == "123"
assert not any("comment" in key for key in values)
def test_from_dotenv_leest_bestand(tmp_path):
env_file = tmp_path / ".env"
env_file.write_text(DOTENV, encoding="utf-8")
cfg = LsConfig.from_dotenv(env_file, environ={})
assert cfg.base_url == "https://ls.example.com/index.php?r=admin/remotecontrol"
assert cfg.username == "test_user"
assert cfg.password == "secret with spaces"
assert cfg.timeout == 45
assert cfg.owner_id == 123
assert cfg.group_id is None
def test_from_dotenv_bestaat_niet(tmp_path):
with pytest.raises(FileNotFoundError):
LsConfig.from_dotenv(tmp_path / "missing.env", environ={})
def test_from_dotenv_verplichte_velden_ontbreken(tmp_path):
env_file = tmp_path / ".env"
env_file.write_text("LIMESURVEY_BASE_URL=https://x.example.com\n", encoding="utf-8")
with pytest.raises(ValueError) as excinfo:
LsConfig.from_dotenv(env_file, environ={})
msg = str(excinfo.value)
assert "LIMESURVEY_USERNAME" in msg
assert "LIMESURVEY_PASSWORD" in msg
assert "LIMESURVEY_BASE_URL" not in msg # that one was set
def test_parameters_overschrijven_bestand(tmp_path):
env_file = tmp_path / ".env"
env_file.write_text(DOTENV, encoding="utf-8")
cfg = LsConfig.from_dotenv(env_file, environ={}, username="ander", timeout=99)
assert cfg.username == "ander"
assert cfg.timeout == 99
# non-overridden fields come from the file
assert cfg.base_url == "https://ls.example.com/index.php?r=admin/remotecontrol"
def test_environment_wint_van_bestand(tmp_path):
env_file = tmp_path / ".env"
env_file.write_text(DOTENV, encoding="utf-8")
environ = dict(BASE_ENV, LIMESURVEY_USERNAME="env_user")
cfg = LsConfig.from_dotenv(env_file, environ=environ)
assert cfg.username == "env_user"
def test_from_env(monkeypatch):
for key, value in BASE_ENV.items():
monkeypatch.setenv(key, value)
monkeypatch.setenv("LIMESURVEY_TIMEOUT_SECONDS", "15")
cfg = LsConfig.from_env()
assert cfg.base_url == BASE_ENV["LIMESURVEY_BASE_URL"]
assert cfg.username == "test_user"
assert cfg.timeout == 15
assert cfg.owner_id is None
assert cfg.group_id is None
def test_from_env_ontbreekt(monkeypatch):
monkeypatch.delenv("LIMESURVEY_BASE_URL", raising=False)
monkeypatch.delenv("LIMESURVEY_USERNAME", raising=False)
monkeypatch.delenv("LIMESURVEY_PASSWORD", raising=False)
with pytest.raises(ValueError) as excinfo:
LsConfig.from_env(environ={})
assert "LIMESURVEY_BASE_URL" in str(excinfo.value)
def test_timeout_default_en_foutief(tmp_path):
env_file = tmp_path / ".env"
basis = "LIMESURVEY_BASE_URL=https://x.example.com\nLIMESURVEY_USERNAME=u\nLIMESURVEY_PASSWORD=p\n"
env_file.write_text(basis, encoding="utf-8")
assert LsConfig.from_dotenv(env_file, environ={}).timeout == 30
env_file.write_text(basis + "LIMESURVEY_TIMEOUT_SECONDS=abc\n", encoding="utf-8")
with pytest.raises(ValueError):
LsConfig.from_dotenv(env_file, environ={})
def test_owner_group_optioneel(tmp_path):
env_file = tmp_path / ".env"
basis = "LIMESURVEY_BASE_URL=https://x.example.com\nLIMESURVEY_USERNAME=u\nLIMESURVEY_PASSWORD=p\n"
env_file.write_text(basis + "LIMESURVEY_GROUP_ID=7\n", encoding="utf-8")
cfg = LsConfig.from_dotenv(env_file, environ={})
assert cfg.owner_id is None
assert cfg.group_id == 7
env_file.write_text(basis + "LIMESURVEY_OWNER_ID=abc\n", encoding="utf-8")
with pytest.raises(ValueError):
LsConfig.from_dotenv(env_file, environ={})
def test_onbekende_parameter(tmp_path):
env_file = tmp_path / ".env"
env_file.write_text(DOTENV, encoding="utf-8")
with pytest.raises(TypeError):
LsConfig.from_dotenv(env_file, environ={}, typo_veld=1)
def test_base_url_moet_http_zijn(tmp_path):
env_file = tmp_path / ".env"
env_file.write_text(
"LIMESURVEY_BASE_URL=ftp://x.example.com\nLIMESURVEY_USERNAME=u\nLIMESURVEY_PASSWORD=p\n",
encoding="utf-8",
)
with pytest.raises(ValueError):
LsConfig.from_dotenv(env_file, environ={})
def test_load_dotenv_setdefault_en_override(tmp_path, monkeypatch):
env_file = tmp_path / ".env"
env_file.write_text(DOTENV, encoding="utf-8")
monkeypatch.setenv("LIMESURVEY_USERNAME", "already_set")
load_dotenv(env_file)
assert os.environ["LIMESURVEY_USERNAME"] == "already_set" # env wins (setdefault)
load_dotenv(env_file, override=True)
assert os.environ["LIMESURVEY_USERNAME"] == "test_user" # file wins
def test_verify_ssl_parsing(tmp_path):
env_file = tmp_path / ".env"
basis = "LIMESURVEY_BASE_URL=https://x.example.com\nLIMESURVEY_USERNAME=u\nLIMESURVEY_PASSWORD=p\n"
env_file.write_text(basis, encoding="utf-8")
assert LsConfig.from_dotenv(env_file, environ={}).verify_ssl is True # default = on
for raw, expected in [("false", False), ("0", False), ("FALSE", False), ("no", False), ("true", True), ("1", True)]:
env_file.write_text(basis + f"LIMESURVEY_VERIFY_SSL={raw}\n", encoding="utf-8")
assert LsConfig.from_dotenv(env_file, environ={}).verify_ssl is expected
env_file.write_text(basis + "LIMESURVEY_VERIFY_SSL=jawel\n", encoding="utf-8")
with pytest.raises(ValueError):
LsConfig.from_dotenv(env_file, environ={})
# explicit parameter wins
env_file.write_text(basis + "LIMESURVEY_VERIFY_SSL=true\n", encoding="utf-8")
assert LsConfig.from_dotenv(env_file, environ={}, verify_ssl=False).verify_ssl is False