"""Local accounts on Windows as ``UserDict`` (NetOrk/netork#2889). One script reads ``Get-LocalUser``, ``Get-LocalGroup`` with their members and the profiles' paths; a fake transport answers it from tests/fixtures/synthetic/os_users.json, a member server as Windows installs it plus one account created (``svc-backup``). Windows names an account by its SID: ``uid`` is its relative identifier, ``sid`` the whole of it (napalm-device-types' ``UserDict.sid``). """ from __future__ import annotations import json from pathlib import Path from typing import Any import pytest from napalm_windows import WindowsDriver from napalm_windows import os_users as mod FIXTURE = Path(__file__).parent / "fixtures" / "synthetic" / "os_users.json" BASE = "S-1-5-21-1004336348-1177238915-682003330" class FakeTransport: def __init__(self, answer: str) -> None: self.answer = answer self.sent: list[str] = [] self.is_open = True def run(self, script: str) -> str: self.sent.append(script) return self.answer def _reading(**changes: Any) -> dict[str, Any]: return {**json.loads(FIXTURE.read_text()), **changes} def _users(reading: dict[str, Any] | None = None) -> dict[str, Any]: d = WindowsDriver("win01", "admin", "secret") d._transport = FakeTransport(json.dumps(reading if reading is not None else _reading())) return {u["username"]: u for u in d.get_os_users()} def test_an_account_created_reads_like_a_posix_one(): assert _users()["svc-backup"] == { "username": "svc-backup", "uid": 1001, "gid": 513, "home": "C:\\Users\\svc-backup", "shell": "", "groups": ["Administrators", "Backup Operators", "Users"], "sid": f"{BASE}-1001", } def test_the_built_in_accounts_keep_their_well_known_rids(): users = _users() assert users["Administrator"]["uid"] == 500 assert users["Guest"]["uid"] == 501 assert users["Guest"]["groups"] == ["Guests"] def test_an_account_that_never_logged_on_has_no_home(): assert _users()["Guest"]["home"] == "" def test_a_group_whose_members_could_not_be_read_names_nobody(): # Get-LocalGroupMember and the ADSI fallback both failed: the script sends null. reading = _reading() reading["groups"][0]["members"] = None assert "Administrators" not in _users(reading)["svc-backup"]["groups"] def test_a_single_account_survives_powershells_unwrapping(): reading = _reading() reading["users"] = reading["users"][0] reading["groups"] = reading["groups"][0] assert _users(reading)["Administrator"]["groups"] == ["Administrators"] @pytest.mark.parametrize("role", [4, 5]) def test_a_domain_controller_has_no_local_accounts_to_list(role): with pytest.raises(NotImplementedError, match="domain controller"): _users(_reading(domain_role=role, users=[], groups=[])) def test_a_host_without_the_local_accounts_cmdlets_cannot_list_them(): with pytest.raises(NotImplementedError, match="Get-LocalUser"): _users(_reading(local_accounts=False, users=[], groups=[])) def test_one_script_reads_accounts_groups_and_profiles(): d = WindowsDriver("win01", "admin", "secret") d._transport = FakeTransport(json.dumps(_reading())) d.get_os_users() (script,) = d._transport.sent # type: ignore[union-attr] assert script == mod._PS_OS_USERS for part in ("Get-LocalUser", "Get-LocalGroupMember", "Win32_UserProfile", "DomainRole"): assert part in script # Probed first, so a host without the module says so instead of failing. assert "Get-Command -Name Get-LocalUser" in script