117 lines
4.3 KiB
Python
117 lines
4.3 KiB
Python
import json
|
|
import os
|
|
import textwrap
|
|
import pytest
|
|
from unittest.mock import patch
|
|
from apim_apps.cli import main
|
|
|
|
|
|
SAMPLE_CONFIG = json.dumps({
|
|
"environments": [{"name": "DEV_LAN", "url": "https://dev.example.com:8075"}]
|
|
})
|
|
|
|
SAMPLE_MANIFEST = textwrap.dedent("""\
|
|
organizations: []
|
|
applications: []
|
|
""")
|
|
|
|
|
|
@pytest.fixture
|
|
def project_files(tmp_path):
|
|
(tmp_path / "config.json").write_text(SAMPLE_CONFIG)
|
|
(tmp_path / "apps.yaml").write_text(SAMPLE_MANIFEST)
|
|
return tmp_path
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Credential validation
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def test_exits_when_apim_user_missing(project_files):
|
|
argv = ["apim_apps.py", "--config", str(project_files / "config.json"),
|
|
"--manifest", str(project_files / "apps.yaml"), "--dry-run", "--env", "DEV_LAN"]
|
|
env = {"APIM_PASSWORD": "secret"}
|
|
with patch("sys.argv", argv), patch.dict(os.environ, env, clear=True):
|
|
with pytest.raises(SystemExit) as exc:
|
|
main()
|
|
assert exc.value.code == 1
|
|
|
|
|
|
def test_exits_when_apim_password_missing(project_files):
|
|
argv = ["apim_apps.py", "--config", str(project_files / "config.json"),
|
|
"--manifest", str(project_files / "apps.yaml"), "--dry-run", "--env", "DEV_LAN"]
|
|
env = {"APIM_USER": "admin"}
|
|
with patch("sys.argv", argv), patch.dict(os.environ, env, clear=True):
|
|
with pytest.raises(SystemExit) as exc:
|
|
main()
|
|
assert exc.value.code == 1
|
|
|
|
|
|
def test_exits_when_both_credentials_missing(project_files):
|
|
argv = ["apim_apps.py", "--config", str(project_files / "config.json"),
|
|
"--manifest", str(project_files / "apps.yaml"), "--dry-run", "--env", "DEV_LAN"]
|
|
with patch("sys.argv", argv), patch.dict(os.environ, {}, clear=True):
|
|
with pytest.raises(SystemExit) as exc:
|
|
main()
|
|
assert exc.value.code == 1
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# --env filtering
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def test_exits_when_env_not_found(project_files):
|
|
argv = ["apim_apps.py", "--config", str(project_files / "config.json"),
|
|
"--manifest", str(project_files / "apps.yaml"), "--env", "UNKNOWN"]
|
|
env = {"APIM_USER": "admin", "APIM_PASSWORD": "secret"}
|
|
with patch("sys.argv", argv), patch.dict(os.environ, env, clear=True):
|
|
with pytest.raises(SystemExit) as exc:
|
|
main()
|
|
assert exc.value.code == 1
|
|
|
|
|
|
def test_dry_run_succeeds_with_valid_env(project_files):
|
|
argv = ["apim_apps.py", "--config", str(project_files / "config.json"),
|
|
"--manifest", str(project_files / "apps.yaml"), "--dry-run", "--env", "DEV_LAN"]
|
|
env = {"APIM_USER": "admin", "APIM_PASSWORD": "secret"}
|
|
with patch("sys.argv", argv), patch.dict(os.environ, env, clear=True):
|
|
with patch("apim_apps.engine.AxwayClient") as MockClient:
|
|
instance = MockClient.return_value
|
|
instance.list_orgs.return_value = {}
|
|
instance.list_apps.return_value = {}
|
|
with pytest.raises(SystemExit) as exc:
|
|
main()
|
|
assert exc.value.code == 0
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# --init-* helpers
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def test_init_manifest_creates_file(tmp_path):
|
|
out = tmp_path / "apps.yaml"
|
|
with patch("sys.argv", ["apim_apps.py", "--init-manifest", "--manifest", str(out)]):
|
|
main()
|
|
assert out.exists()
|
|
assert out.stat().st_size > 0
|
|
|
|
|
|
def test_init_config_creates_file(tmp_path):
|
|
out = tmp_path / "config.json"
|
|
with patch("sys.argv", ["apim_apps.py", "--init-config", "--config", str(out)]):
|
|
main()
|
|
assert out.exists()
|
|
data = json.loads(out.read_text())
|
|
assert "environments" in data
|
|
|
|
|
|
def test_init_config_no_credentials_in_template(tmp_path):
|
|
"""Generated config.json must not contain username or password."""
|
|
out = tmp_path / "config.json"
|
|
with patch("sys.argv", ["apim_apps.py", "--init-config", "--config", str(out)]):
|
|
main()
|
|
data = json.loads(out.read_text())
|
|
for entry in data["environments"]:
|
|
assert "username" not in entry
|
|
assert "password" not in entry
|