import json 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": "fake"}]}, } 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 assert "is valid" in result.stdout def test_validate_without_config_fails() -> None: result = runner.invoke(cli.app, ["validate"]) assert result.exit_code != 0 assert "either --config or --config-env" in result.output def test_list_modules() -> None: result = runner.invoke(cli.app, ["list-modules"]) assert result.exit_code == 0 assert set(result.stdout.splitlines()) == { "apk", "apt", "certificates", "java", "npm", "php", "python", "rust", "yum", } 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 assert "skipping module apk" in caplog.text assert "skipping module java" in caplog.text assert "skipping module apt" in caplog.text assert "skipping module certificates" in caplog.text assert "skipping module yum" in caplog.text assert "skipping module python" in caplog.text def test_apply_runs_relevant_module(tmp_path) -> None: 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 assert (dst / "usr/local/share/ca-certificates/my-ca.crt").read_bytes() == b"fake" assert (dst / "etc/ssl/certs/ca-certificates.crt").read_bytes() == b"fake\n" 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()