54c1d96933
Co-authored-by: Cursor <cursoragent@cursor.com>
56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
"""Tests for host session list/parse helpers."""
|
|
|
|
from app.services.host_sessions import (
|
|
event_supports_session_terminate,
|
|
parse_loginctl_sessions,
|
|
parse_qwinsta_sessions,
|
|
)
|
|
|
|
|
|
def test_parse_loginctl_sessions():
|
|
stdout = "c1 1000 alice seat0 pts/0 active -\n 2 1001 bob - tty2 active -\n"
|
|
rows = parse_loginctl_sessions(stdout)
|
|
assert len(rows) == 2
|
|
assert rows[0].session_id == "c1"
|
|
assert rows[0].user == "alice"
|
|
assert rows[0].tty == "pts/0"
|
|
|
|
|
|
def test_parse_loginctl_sessions_ignores_motd_noise():
|
|
stdout = """This server is powered by FASTPANEL
|
|
Ubuntu Operating LTS
|
|
configuration By can be not
|
|
11090 1000 papatramp - - active -
|
|
11102 1000 papatramp - - active -
|
|
"""
|
|
rows = parse_loginctl_sessions(stdout)
|
|
assert len(rows) == 2
|
|
assert rows[0].session_id == "11090"
|
|
assert rows[1].session_id == "11102"
|
|
assert all(row.user == "papatramp" for row in rows)
|
|
|
|
|
|
def test_parse_qwinsta_sessions_filters_user():
|
|
stdout = """SESSIONNAME USERNAME ID STATE
|
|
console Administrator 1 Active
|
|
rdp-tcp#0 B26\\alice 2 Active
|
|
"""
|
|
all_rows = parse_qwinsta_sessions(stdout)
|
|
assert len(all_rows) == 2
|
|
filtered = parse_qwinsta_sessions(stdout, filter_user="alice")
|
|
assert len(filtered) == 1
|
|
assert filtered[0].session_id == "2"
|
|
|
|
|
|
def test_event_supports_session_terminate_types():
|
|
class HostStub:
|
|
os_family = "linux"
|
|
product = "ssh-monitor"
|
|
|
|
class EventStub:
|
|
def __init__(self, event_type: str):
|
|
self.type = event_type
|
|
self.host = HostStub()
|
|
|
|
assert event_supports_session_terminate(EventStub("ssh.login.success")) is True
|