mirror of
https://github.com/d9pouces/id_configurator.git
synced 2026-11-09 01:47:21 +01:00
feat: initial commit
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
import base64
|
||||
import json
|
||||
import subprocess
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from typer.testing import CliRunner
|
||||
|
||||
from id_configurator import cli
|
||||
|
||||
runner = CliRunner()
|
||||
|
||||
MINIMAL_CONFIG = {
|
||||
"uuid": "12345678-1234-5678-1234-567812345678",
|
||||
"organisation": "InterDiode",
|
||||
}
|
||||
CONFIG_WITH_CERT = {
|
||||
**MINIMAL_CONFIG,
|
||||
"configurations": {
|
||||
"certificates": [
|
||||
{"name": "my-ca", "content": base64.b64encode(b"fake").decode("ascii")}
|
||||
]
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def write_config(tmp_path, data) -> str:
|
||||
path = tmp_path / "config.json"
|
||||
path.write_text(json.dumps(data), encoding="utf-8")
|
||||
return str(path)
|
||||
|
||||
|
||||
def test_validate_valid_config(tmp_path) -> None:
|
||||
config_path = write_config(tmp_path, MINIMAL_CONFIG)
|
||||
result = runner.invoke(cli.app, ["validate", "--config", config_path])
|
||||
assert result.exit_code == 0 # nosec B101
|
||||
assert "is valid" in result.stdout # nosec B101
|
||||
|
||||
|
||||
def test_validate_without_config_fails() -> None:
|
||||
result = runner.invoke(cli.app, ["validate"])
|
||||
assert result.exit_code != 0 # nosec B101
|
||||
assert "either --config or --config-env" in result.output # nosec B101
|
||||
|
||||
|
||||
def test_list_modules() -> None:
|
||||
result = runner.invoke(cli.app, ["list-modules"])
|
||||
assert result.exit_code == 0 # nosec B101
|
||||
assert set(result.stdout.splitlines()) == {"apt", "certificates"} # nosec B101
|
||||
|
||||
|
||||
def test_apply_skips_irrelevant_modules(tmp_path, caplog) -> None:
|
||||
config_path = write_config(tmp_path, MINIMAL_CONFIG)
|
||||
dst = tmp_path / "dst"
|
||||
with caplog.at_level("DEBUG", logger="id_configurator"):
|
||||
result = runner.invoke(
|
||||
cli.app, ["apply", "--config", config_path, "--dst", str(dst), "-v"]
|
||||
)
|
||||
assert result.exit_code == 0 # nosec B101
|
||||
assert "skipping module apt" in caplog.text # nosec B101
|
||||
assert "skipping module certificates" in caplog.text # nosec B101
|
||||
|
||||
|
||||
def test_apply_runs_relevant_module(tmp_path, monkeypatch) -> None:
|
||||
mock_run = MagicMock(
|
||||
return_value=subprocess.CompletedProcess(
|
||||
args=[], returncode=0, stdout="", stderr=""
|
||||
)
|
||||
)
|
||||
monkeypatch.setattr("id_configurator.modules.certificates.subprocess.run", mock_run)
|
||||
|
||||
config_path = write_config(tmp_path, CONFIG_WITH_CERT)
|
||||
dst = tmp_path / "dst"
|
||||
result = runner.invoke(
|
||||
cli.app, ["apply", "--config", config_path, "--dst", str(dst)]
|
||||
)
|
||||
|
||||
assert result.exit_code == 0 # nosec B101
|
||||
assert (dst / "usr/local/share/ca-certificates/my-ca.crt").read_bytes() == b"fake" # nosec B101
|
||||
mock_run.assert_called_once()
|
||||
|
||||
|
||||
def test_main_default_name_calls_app(monkeypatch) -> None:
|
||||
mock_app = MagicMock()
|
||||
monkeypatch.setattr(cli, "app", mock_app)
|
||||
cli.main()
|
||||
mock_app.assert_called_once()
|
||||
|
||||
|
||||
def test_main_other_name_does_not_call_app(monkeypatch) -> None:
|
||||
mock_app = MagicMock()
|
||||
monkeypatch.setattr(cli, "app", mock_app)
|
||||
cli.main("id_configurator.__main__")
|
||||
mock_app.assert_not_called()
|
||||
Reference in New Issue
Block a user