Files
security-alert-center/backend/tests/test_winrm_connect.py
T

69 lines
2.5 KiB
Python

"""WinRM command builder and CLIXML error parsing tests."""
from app.services.winrm_connect import (
_clixml_to_plain,
_rdp_update_powershell_script,
_winrm_failure_detail,
)
def test_rdp_update_default_script_uses_netlogon_and_git_fallback():
script = _rdp_update_powershell_script("")
assert "$ProgressPreference = 'SilentlyContinue'" in script
assert "NETLOGON\\RDP-login-monitor" in script
assert "Deploy-LoginMonitor.ps1" in script
assert "_sac_git" in script
assert "git.kalinamall.ru/PapaTramp/RDP-login-monitor.git" in script
assert "ProgramData\\RDP-login-monitor\\Deploy-LoginMonitor.ps1" not in script
def test_rdp_update_default_script_honors_sac_git_settings():
script = _rdp_update_powershell_script(
"",
repo_url="https://git.example.com/org/rdp.git",
git_branch="release",
)
assert "git.example.com/org/rdp.git" in script
assert "$branch = 'release'" in script
def test_rdp_update_custom_script_uses_literal_path():
script = _rdp_update_powershell_script(r"\\b26\NETLOGON\RDP-login-monitor\Deploy-LoginMonitor.ps1")
assert r"\\b26\NETLOGON\RDP-login-monitor\Deploy-LoginMonitor.ps1" in script
assert "Test-Path -LiteralPath" in script
def test_rdp_update_custom_script_escapes_single_quotes():
script = _rdp_update_powershell_script(r"C:\Users\O'Brien\Deploy-LoginMonitor.ps1")
assert "O''Brien" in script
def test_winrm_failure_detail_extracts_message_from_clixml():
clixml = (
"#< CLIXML\n"
'<Objs><Obj><MS><S N="Message">Preparing modules for first use.</S>'
'<S N="Message">Deploy-LoginMonitor.ps1 not found on client PC</S></MS></Obj></Objs>'
)
detail = _winrm_failure_detail("", clixml, 1)
assert detail == "Deploy-LoginMonitor.ps1 not found on client PC"
def test_winrm_failure_detail_prefers_plain_stderr():
detail = _winrm_failure_detail("", "Access is denied", 5)
assert detail == "Access is denied"
def test_winrm_failure_detail_never_returns_raw_clixml_progress():
clixml = (
"#< CLIXML <Objs Version=\"1.1.0.1\">"
'<Obj S="progress"><MS><PR N="Record"><AV>Preparing modules for first use.</AV></PR></MS></Obj>'
)
detail = _winrm_failure_detail("", clixml, 1)
assert "#< CLIXML" not in detail
assert "exit code" in detail.lower()
def test_clixml_to_plain_strips_progress_only_blob():
clixml = "#< CLIXML <Objs><Obj S=\"progress\"><AV>Preparing modules for first use.</AV></Obj></Objs>"
assert _clixml_to_plain(clixml) == ""