fix: WinRM RDP update via run_ps, strip CLIXML noise (0.20.11)
This commit is contained in:
@@ -1,33 +1,56 @@
|
||||
"""WinRM command builder tests."""
|
||||
"""WinRM command builder and CLIXML error parsing tests."""
|
||||
|
||||
import base64
|
||||
|
||||
from app.services.winrm_connect import _winrm_rdp_update_remote_cmd
|
||||
from app.services.winrm_connect import (
|
||||
_clixml_to_plain,
|
||||
_rdp_update_powershell_script,
|
||||
_winrm_failure_detail,
|
||||
)
|
||||
|
||||
|
||||
def test_rdp_update_default_uses_encoded_command_without_broken_quotes():
|
||||
cmd = _winrm_rdp_update_remote_cmd("")
|
||||
assert "-EncodedCommand" in cmd
|
||||
assert "-Command" not in cmd
|
||||
assert '"& {' not in cmd
|
||||
|
||||
encoded = cmd.rsplit(" ", 1)[-1]
|
||||
script = base64.b64decode(encoded).decode("utf-16-le")
|
||||
assert "Deploy-LoginMonitor.ps1" in script
|
||||
assert "Join-Path $env:ProgramData" in script
|
||||
def test_rdp_update_default_script_uses_rdp_login_monitor_path():
|
||||
script = _rdp_update_powershell_script("")
|
||||
assert "$ProgressPreference = 'SilentlyContinue'" in script
|
||||
assert "try {" in script
|
||||
assert "Write-Output ('ERROR: '" in script
|
||||
assert "RDP-login-monitor\\Deploy-LoginMonitor.ps1" in script
|
||||
|
||||
|
||||
def test_rdp_update_custom_script_uses_encoded_command():
|
||||
cmd = _winrm_rdp_update_remote_cmd(r"C:\ProgramData\LoginMonitor\Deploy-LoginMonitor.ps1")
|
||||
assert "-EncodedCommand" in cmd
|
||||
encoded = cmd.rsplit(" ", 1)[-1]
|
||||
script = base64.b64decode(encoded).decode("utf-16-le")
|
||||
assert r"C:\ProgramData\LoginMonitor\Deploy-LoginMonitor.ps1" in script
|
||||
def test_rdp_update_custom_script_uses_literal_path():
|
||||
script = _rdp_update_powershell_script(r"C:\ProgramData\RDP-login-monitor\Deploy-LoginMonitor.ps1")
|
||||
assert r"C:\ProgramData\RDP-login-monitor\Deploy-LoginMonitor.ps1" in script
|
||||
assert "Test-Path -LiteralPath" in script
|
||||
|
||||
|
||||
def test_rdp_update_custom_script_escapes_single_quotes():
|
||||
cmd = _winrm_rdp_update_remote_cmd(r"C:\Users\O'Brien\Deploy-LoginMonitor.ps1")
|
||||
encoded = cmd.rsplit(" ", 1)[-1]
|
||||
script = base64.b64decode(encoded).decode("utf-16-le")
|
||||
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) == ""
|
||||
|
||||
Reference in New Issue
Block a user