Skip to content

Commit ca8fd10

Browse files
committed
Add test for home folder resolution when sam is unavailable
1 parent 4fa7c75 commit ca8fd10

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

tests/plugins/os/windows/test__os.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from pathlib import PureWindowsPath
34
from typing import TYPE_CHECKING, Any
45
from unittest.mock import Mock
56

@@ -308,6 +309,55 @@ def test_windows_user_from_sam(target_win_users: Target) -> None:
308309
assert users[1].home == windows_path("C:\\Users\\John")
309310

310311

312+
def test_windows_user_registry_parsing(target_win_users: Target) -> None:
313+
"""Verify ProfileList parsing relies on resolved path objects."""
314+
315+
# 1. Setup the Registry Mock Structure
316+
sid_str = "S-1-5-21-99999-88888-77777-1001"
317+
raw_path_str = "C:\\Users\\DuaLipa"
318+
319+
# Create the subkey (The user SID)
320+
subkey_mock = Mock()
321+
subkey_mock.name = sid_str
322+
323+
# Create the value for ProfileImagePath
324+
value_mock = Mock()
325+
value_mock.value = raw_path_str
326+
subkey_mock.value.return_value = value_mock
327+
328+
# Create the registry key
329+
key_mock = Mock()
330+
key_mock.subkeys.return_value = [subkey_mock]
331+
332+
# We must overwrite the real .registry attribute with a Mock object first
333+
target_win_users.registry = Mock()
334+
# Now we can configure the .keys() method on that mock
335+
target_win_users.registry.keys.return_value = [key_mock]
336+
337+
# 2. Setup the Resolve Mock
338+
resolved_path = PureWindowsPath("C:/Users/DuaLipa")
339+
340+
# Overwrite the real .resolve method with a Mock
341+
target_win_users.resolve = Mock(return_value=resolved_path)
342+
343+
# Disable SAM and Machine SID
344+
target_win_users.sam = Mock(return_value=[])
345+
target_win_users.machine_sid = Mock(return_value=iter([]))
346+
347+
# 4. Run the function
348+
users = list(target_win_users.users())
349+
350+
assert len(users) == 1
351+
record = users[0]
352+
353+
assert record.sid == sid_str
354+
assert record.name == "DuaLipa"
355+
assert record.home == resolved_path
356+
357+
# Verify resolve was called correctly
358+
target_win_users.resolve.assert_called_with(raw_path_str)
359+
360+
311361
@pytest.mark.parametrize(
312362
("registry_value", "expected_hostname"),
313363
[

0 commit comments

Comments
 (0)