Merge pull request #1436 from guardicore/1410/ut-autowindowsuser

Add unit test for deactivate and delete new auto windows user
This commit is contained in:
Shreya Malviya 2021-08-31 20:23:48 +05:30 committed by GitHub
commit 24b6c751cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 10 deletions

View File

@ -2,8 +2,18 @@ import logging
import subprocess
from infection_monkey.utils.auto_new_user import AutoNewUser
from infection_monkey.utils.environment import is_windows_os
from infection_monkey.utils.new_user_error import NewUserError
if is_windows_os():
import win32api
import win32con
import win32event
import win32process
import win32security
from winsys import _advapi32
ACTIVE_NO_NET_USER = "/ACTIVE:NO"
WAIT_TIMEOUT_IN_MILLISECONDS = 60 * 1000
@ -42,10 +52,6 @@ class AutoNewWindowsUser(AutoNewUser):
_ = subprocess.check_output(windows_cmds, stderr=subprocess.STDOUT)
def __enter__(self):
# Importing these only on windows, as they won't exist on linux.
import win32con
import win32security
try:
# Logon as new user: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf
# -winbase-logonusera
@ -62,12 +68,6 @@ class AutoNewWindowsUser(AutoNewUser):
return self
def run_as(self, command):
# Importing these only on windows, as they won't exist on linux.
import win32api
import win32event
import win32process
from winsys import _advapi32
exit_code = -1
process_handle = None
thread_handle = None

View File

@ -0,0 +1,41 @@
import os
import subprocess
import pytest
from infection_monkey.utils.windows.users import AutoNewWindowsUser
TEST_USER = "test_user"
@pytest.fixture
def subprocess_check_output_spy(monkeypatch):
def mock_check_output(command, stderr):
mock_check_output.command = command
mock_check_output.command = ""
monkeypatch.setattr(subprocess, "check_output", mock_check_output)
return mock_check_output
class StubLogonUser:
def __init__(self):
pass
def Close():
return None
@pytest.mark.skipif(os.name == "posix", reason="This test only runs on Windows.")
def test_new_user_delete_windows(subprocess_check_output_spy, monkeypatch):
monkeypatch.setattr(
"infection_monkey.utils.windows.users.win32security.LogonUser",
lambda _, __, ___, ____, _____: StubLogonUser,
)
with (AutoNewWindowsUser(TEST_USER, "password")):
pass
assert f"net user {TEST_USER} /delete" in " ".join(subprocess_check_output_spy.command)