UT: Use a mock IAgentRepository instead of monkeypatching open()

This commit is contained in:
Mike Salvatore 2022-03-14 12:38:06 -04:00 committed by Ilija Lazoroski
parent 020dbbf2fe
commit 5be0a3d6f9
1 changed files with 7 additions and 2 deletions

View File

@ -1,3 +1,4 @@
from io import BytesIO
from unittest.mock import MagicMock
import pytest
@ -21,6 +22,10 @@ class AuthenticationErrorForTests(Exception):
pass
mock_agent_repository = MagicMock()
mock_agent_repository.get_agent_binary.return_value = BytesIO(b"BINARY_EXECUTABLE")
@pytest.fixture
def powershell_arguments():
options = {
@ -37,7 +42,7 @@ def powershell_arguments():
"options": options,
"current_depth": 2,
"telemetry_messenger": MagicMock(),
"agent_repository": MagicMock(),
"agent_repository": mock_agent_repository,
}
return arguments
@ -48,7 +53,6 @@ def powershell_exploiter(monkeypatch):
monkeypatch.setattr(powershell, "AuthenticationError", AuthenticationErrorForTests)
monkeypatch.setattr(powershell, "is_windows_os", lambda: True)
monkeypatch.setattr("builtins.open", lambda _, __: MagicMock())
return pe
@ -160,6 +164,7 @@ def test_successful_propagation(monkeypatch, powershell_exploiter, powershell_ar
assert exploit_result.exploitation_success
assert exploit_result.propagation_success
assert not exploit_result.error_message
def test_login_attempts_correctly_reported(monkeypatch, powershell_exploiter, powershell_arguments):