UT: Move a ransomware test to integration_tests

This commit is contained in:
Kekoa Kaaikala 2022-08-18 17:46:55 +00:00
parent baa1687487
commit b5c6240190
5 changed files with 37 additions and 40 deletions

6
monkey/tests/conftest.py Normal file
View File

@ -0,0 +1,6 @@
import pytest
@pytest.fixture(params=[".m0nk3y", ".test", ""], ids=["monkeyext", "testext", "noext"])
def ransomware_file_extension(request):
return request.param

View File

@ -0,0 +1,29 @@
import threading
import pytest
import infection_monkey.payload.ransomware.ransomware_builder as ransomware_builder
from monkey.common.agent_configuration.default_agent_configuration import RANSOMWARE_OPTIONS
@pytest.fixture
def ransomware_options_dict(ransomware_file_extension):
options = RANSOMWARE_OPTIONS
options["encryption"]["file_extension"] = ransomware_file_extension
return options
def test_uses_correct_extension(ransomware_options_dict, tmp_path, ransomware_file_extension):
target_dir = tmp_path
ransomware_directories = ransomware_options_dict["encryption"]["directories"]
ransomware_directories["linux_target_dir"] = target_dir
ransomware_directories["windows_target_dir"] = target_dir
ransomware = ransomware_builder.build_ransomware(ransomware_options_dict)
file = target_dir / "file.txt"
file.write_text("Do your worst!")
ransomware.run(threading.Event())
# Verify that the file has been encrypted with the correct ending
encrypted_file = file.with_suffix(file.suffix + ransomware_file_extension)
assert encrypted_file.is_file()

View File

@ -11,11 +11,6 @@ def patched_home_env(monkeypatch, tmp_path):
return tmp_path return tmp_path
@pytest.fixture(params=[".m0nk3y", ".test", ""], ids=["monkeyext", "testext", "noext"])
def ransomware_file_extension(request):
return request.param
@pytest.fixture @pytest.fixture
def ransomware_test_data(data_for_tests_dir): def ransomware_test_data(data_for_tests_dir):
return Path(data_for_tests_dir) / "ransomware_targets" return Path(data_for_tests_dir) / "ransomware_targets"

View File

@ -1,6 +1,5 @@
import tempfile
import threading import threading
from pathlib import Path, PurePosixPath from pathlib import PurePosixPath
from unittest.mock import MagicMock from unittest.mock import MagicMock
import pytest import pytest
@ -10,11 +9,9 @@ from tests.unit_tests.infection_monkey.payload.ransomware.ransomware_target_file
TEST_KEYBOARD_TXT, TEST_KEYBOARD_TXT,
) )
import infection_monkey.payload.ransomware.ransomware_builder as ransomware_builder
from infection_monkey.payload.ransomware.consts import README_FILE_NAME, README_SRC from infection_monkey.payload.ransomware.consts import README_FILE_NAME, README_SRC
from infection_monkey.payload.ransomware.ransomware import Ransomware from infection_monkey.payload.ransomware.ransomware import Ransomware
from infection_monkey.payload.ransomware.ransomware_options import RansomwareOptions from infection_monkey.payload.ransomware.ransomware_options import RansomwareOptions
from monkey.common.agent_configuration.default_agent_configuration import RANSOMWARE_OPTIONS
@pytest.fixture @pytest.fixture
@ -55,19 +52,6 @@ def ransomware_options(ransomware_file_extension, ransomware_test_data):
return RansomwareOptionsStub(True, False, ransomware_file_extension, ransomware_test_data) return RansomwareOptionsStub(True, False, ransomware_file_extension, ransomware_test_data)
@pytest.fixture
def ransomware_options_dict(ransomware_file_extension):
options = RANSOMWARE_OPTIONS
options["encryption"]["file_extension"] = ransomware_file_extension
return options
@pytest.fixture
def encrypt_target_directory():
with tempfile.TemporaryDirectory() as tmpdir:
yield Path(tmpdir)
@pytest.fixture @pytest.fixture
def mock_file_encryptor(): def mock_file_encryptor():
return MagicMock() return MagicMock()
@ -235,20 +219,3 @@ def test_leave_readme_exceptions_handled(build_ransomware, ransomware_options):
# Test will fail if exception is raised and not handled # Test will fail if exception is raised and not handled
ransomware.run(threading.Event()) ransomware.run(threading.Event())
def test_uses_correct_extension(
ransomware_options_dict, encrypt_target_directory, ransomware_file_extension
):
ransomware_directories = ransomware_options_dict["encryption"]["directories"]
ransomware_directories["linux_target_dir"] = encrypt_target_directory
ransomware_directories["windows_target_dir"] = encrypt_target_directory
ransomware = ransomware_builder.build_ransomware(ransomware_options_dict)
file = encrypt_target_directory / "file.txt"
file.write_text("Do your worst!")
ransomware.run(threading.Event())
# Verify that the file has been encrypted with the correct ending
encrypted_file = file.with_suffix(file.suffix + ransomware_file_extension)
assert encrypted_file.is_file()

View File

@ -20,7 +20,7 @@ log_cli = 1
log_cli_level = "DEBUG" log_cli_level = "DEBUG"
log_cli_format = "%(asctime)s [%(levelname)s] %(module)s.%(funcName)s.%(lineno)d: %(message)s" log_cli_format = "%(asctime)s [%(levelname)s] %(module)s.%(funcName)s.%(lineno)d: %(message)s"
log_cli_date_format = "%H:%M:%S" log_cli_date_format = "%H:%M:%S"
addopts = "-v --capture=sys tests/unit_tests" addopts = "-v --capture=sys tests/unit_tests tests/integration_tests"
norecursedirs = "node_modules dist" norecursedirs = "node_modules dist"
markers = ["slow: mark test as slow"] markers = ["slow: mark test as slow"]
pythonpath = "./monkey" pythonpath = "./monkey"