2eb06acb5b
Silent SSH probe on host card open; manual test feedback auto-hides. Persist ssh_admin_ok in DB (migration 019). After agent update read version from host and refresh UI.
159 lines
5.4 KiB
Python
159 lines
5.4 KiB
Python
"""Additional Linux admin API tests."""
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
def test_get_linux_admin_settings_env_default(jwt_headers, client, monkeypatch):
|
|
monkeypatch.setenv("SAC_LINUX_ADMIN_USER", "")
|
|
monkeypatch.setenv("SAC_LINUX_ADMIN_PASSWORD", "")
|
|
from app.config import get_settings
|
|
|
|
get_settings.cache_clear()
|
|
|
|
response = client.get("/api/v1/settings/linux-admin", headers=jwt_headers)
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["configured"] is False
|
|
assert body["source"] == "env"
|
|
|
|
|
|
def test_host_ssh_test_requires_admin(jwt_monitor_headers, client):
|
|
response = client.post("/api/v1/hosts/1/actions/ssh-test", headers=jwt_monitor_headers)
|
|
assert response.status_code == 403
|
|
|
|
|
|
def test_host_ssh_test_not_linux(jwt_headers, client, db_session, monkeypatch):
|
|
monkeypatch.setenv("SAC_LINUX_ADMIN_USER", "root")
|
|
monkeypatch.setenv("SAC_LINUX_ADMIN_PASSWORD", "pw")
|
|
from app.config import get_settings
|
|
from app.models import Host
|
|
|
|
get_settings.cache_clear()
|
|
|
|
host = Host(hostname="PC", os_family="windows", product="rdp-login-monitor", ipv4="10.0.0.1")
|
|
db_session.add(host)
|
|
db_session.commit()
|
|
db_session.refresh(host)
|
|
|
|
response = client.post(f"/api/v1/hosts/{host.id}/actions/ssh-test", headers=jwt_headers)
|
|
assert response.status_code == 400
|
|
assert "not Linux" in response.json()["detail"]
|
|
|
|
|
|
def test_host_agent_update_not_configured(jwt_headers, client, db_session, monkeypatch):
|
|
monkeypatch.setenv("SAC_LINUX_ADMIN_USER", "")
|
|
monkeypatch.setenv("SAC_LINUX_ADMIN_PASSWORD", "")
|
|
from app.config import get_settings
|
|
from app.models import Host
|
|
|
|
get_settings.cache_clear()
|
|
|
|
host = Host(hostname="ubabuba", os_family="linux", product="ssh-monitor", ipv4="10.0.0.5")
|
|
db_session.add(host)
|
|
db_session.commit()
|
|
db_session.refresh(host)
|
|
|
|
response = client.post(f"/api/v1/hosts/{host.id}/actions/agent-update", headers=jwt_headers)
|
|
assert response.status_code == 400
|
|
assert "not configured" in response.json()["detail"].lower()
|
|
|
|
|
|
def test_put_linux_admin_settings_persists(jwt_headers, client, db_session, monkeypatch):
|
|
monkeypatch.setenv("SAC_LINUX_ADMIN_USER", "")
|
|
monkeypatch.setenv("SAC_LINUX_ADMIN_PASSWORD", "")
|
|
from app.config import get_settings
|
|
from app.models.ui_settings import UI_SETTINGS_ROW_ID, UiSettings
|
|
|
|
get_settings.cache_clear()
|
|
|
|
response = client.put(
|
|
"/api/v1/settings/linux-admin",
|
|
headers=jwt_headers,
|
|
json={"user": "root", "password": "secret-pass"},
|
|
)
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["configured"] is True
|
|
assert body["user"] == "root"
|
|
assert body["source"] == "db"
|
|
assert body["password_hint"]
|
|
|
|
row = db_session.get(UiSettings, UI_SETTINGS_ROW_ID)
|
|
assert row is not None
|
|
assert row.linux_admin_user == "root"
|
|
assert row.linux_admin_password == "secret-pass"
|
|
|
|
|
|
def test_host_ssh_test_success(jwt_headers, client, db_session, monkeypatch):
|
|
monkeypatch.setenv("SAC_LINUX_ADMIN_USER", "root")
|
|
monkeypatch.setenv("SAC_LINUX_ADMIN_PASSWORD", "pw")
|
|
from app.config import get_settings
|
|
from app.models import Host
|
|
from app.services.ssh_connect import SshCommandResult
|
|
|
|
get_settings.cache_clear()
|
|
|
|
host = Host(hostname="ubabuba", os_family="linux", product="ssh-monitor", ipv4="10.0.0.5")
|
|
db_session.add(host)
|
|
db_session.commit()
|
|
db_session.refresh(host)
|
|
|
|
with patch("app.api.v1.hosts.test_ssh_connection") as mock_test:
|
|
mock_test.return_value = SshCommandResult(
|
|
ok=True,
|
|
message="SSH OK, hostname=ubabuba",
|
|
target="ubabuba",
|
|
stdout="ubabuba\n",
|
|
exit_code=0,
|
|
)
|
|
response = client.post(
|
|
f"/api/v1/hosts/{host.id}/actions/ssh-test",
|
|
headers=jwt_headers,
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["ok"] is True
|
|
assert "hostname=ubabuba" in body["message"]
|
|
assert body["target"] == "ubabuba"
|
|
assert body["ssh_admin_ok"] is True
|
|
|
|
db_session.refresh(host)
|
|
assert host.ssh_admin_ok is True
|
|
assert host.ssh_admin_checked_at is not None
|
|
|
|
|
|
def test_host_agent_update_success(jwt_headers, client, db_session, monkeypatch):
|
|
monkeypatch.setenv("SAC_LINUX_ADMIN_USER", "root")
|
|
monkeypatch.setenv("SAC_LINUX_ADMIN_PASSWORD", "pw")
|
|
from app.config import get_settings
|
|
from app.models import Host
|
|
from app.services.ssh_connect import SshCommandResult
|
|
|
|
get_settings.cache_clear()
|
|
|
|
host = Host(hostname="ubabuba", os_family="linux", product="ssh-monitor", ipv4="10.0.0.5")
|
|
db_session.add(host)
|
|
db_session.commit()
|
|
db_session.refresh(host)
|
|
|
|
with patch("app.api.v1.hosts.run_ssh_monitor_update") as mock_update:
|
|
mock_update.return_value = SshCommandResult(
|
|
ok=True,
|
|
message="SSH OK (ubabuba), exit 0\nSUMMARY updated",
|
|
target="ubabuba",
|
|
stdout="SUMMARY updated\n",
|
|
exit_code=0,
|
|
)
|
|
response = client.post(
|
|
f"/api/v1/hosts/{host.id}/actions/agent-update",
|
|
headers=jwt_headers,
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["ok"] is True
|
|
assert body["target"] == "ubabuba"
|
|
if "product_version" in body:
|
|
assert body["product_version"] is None or isinstance(body["product_version"], str)
|