diff --git a/monkey/tests/unit_tests/monkey_island/cc/server_utils/test_file_utils.py b/monkey/tests/unit_tests/monkey_island/cc/server_utils/test_file_utils.py index e912ca043..6297aada9 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/server_utils/test_file_utils.py +++ b/monkey/tests/unit_tests/monkey_island/cc/server_utils/test_file_utils.py @@ -21,40 +21,32 @@ def test_expand_vars(patched_home_env): @pytest.mark.skipif(os.name != "posix", reason="Tests Posix (not Windows) permissions.") -def test_has_expected_permissions_true_linux(tmpdir, create_empty_file): - file_name = f"{tmpdir}/test" - - create_empty_file(file_name) +def test_has_expected_permissions_true_linux(tmpdir, create_empty_tmp_file): + file_name = create_empty_tmp_file("test") os.chmod(file_name, 0o754) assert file_utils.has_expected_permissions(file_name, 0o754) @pytest.mark.skipif(os.name != "posix", reason="Tests Posix (not Windows) permissions.") -def test_has_expected_permissions_false_linux(tmpdir, create_empty_file): - file_name = f"{tmpdir}/test" - - create_empty_file(file_name) +def test_has_expected_permissions_false_linux(tmpdir, create_empty_tmp_file): + file_name = create_empty_tmp_file("test") os.chmod(file_name, 0o755) assert not file_utils.has_expected_permissions(file_name, 0o700) @pytest.mark.skipif(os.name == "posix", reason="Tests Windows (not Posix) permissions.") -def test_has_expected_permissions_true_windows(tmpdir, create_empty_file): - file_name = f"{tmpdir}/test" - - create_empty_file(file_name) +def test_has_expected_permissions_true_windows(tmpdir, create_empty_tmp_file): + file_name = create_empty_tmp_file("test") subprocess.run(f"echo y| cacls {file_name} /p %USERNAME%:F", shell=True) # noqa: DUO116 assert file_utils.has_expected_permissions(file_name, 2032127) @pytest.mark.skipif(os.name == "posix", reason="Tests Windows (not Posix) permissions.") -def test_has_expected_permissions_false_windows(tmpdir, create_empty_file): - file_name = f"{tmpdir}/test" - - create_empty_file(file_name) +def test_has_expected_permissions_false_windows(tmpdir, create_empty_tmp_file): + file_name = create_empty_tmp_file("test") subprocess.run(f"echo y| cacls {file_name} /p %USERNAME%:R", shell=True) # noqa: DUO116 assert not file_utils.has_expected_permissions(file_name, 2032127) diff --git a/monkey/tests/unit_tests/monkey_island/cc/setup/test_island_config_options_validator.py b/monkey/tests/unit_tests/monkey_island/cc/setup/test_island_config_options_validator.py index 8f79e0009..b6d9eeb85 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/setup/test_island_config_options_validator.py +++ b/monkey/tests/unit_tests/monkey_island/cc/setup/test_island_config_options_validator.py @@ -32,13 +32,12 @@ def linux_island_config_options(create_read_only_linux_file: Callable): @pytest.fixture -def create_read_only_linux_file(tmpdir: str, create_empty_file: Callable) -> Callable: +def create_read_only_linux_file(tmpdir: str, create_empty_tmp_file: Callable) -> Callable: def inner(file_name: str) -> str: - new_file = os.path.join(tmpdir, file_name) - create_empty_file(new_file) - os.chmod(new_file, LINUX_READ_ONLY_BY_USER) + full_file_path = create_empty_tmp_file(file_name) + os.chmod(full_file_path, LINUX_READ_ONLY_BY_USER) - return new_file + return full_file_path return inner @@ -93,14 +92,13 @@ def windows_island_config_options(tmpdir: str, create_read_only_windows_file: Ca @pytest.fixture -def create_read_only_windows_file(tmpdir: str, create_empty_file: Callable) -> Callable: +def create_read_only_windows_file(tmpdir: str, create_empty_tmp_file: Callable) -> Callable: def inner(file_name: str) -> str: - new_file = os.path.join(tmpdir, file_name) - create_empty_file(new_file) - cmd_to_change_permissions = get_windows_cmd_to_change_permissions(new_file, "R") + full_file_path = create_empty_tmp_file(file_name) + cmd_to_change_permissions = get_windows_cmd_to_change_permissions(full_file_path, "R") subprocess.run(cmd_to_change_permissions, shell=True) # noqa DUO116 - return new_file + return full_file_path return inner diff --git a/monkey/tests/unit_tests/monkey_island/conftest.py b/monkey/tests/unit_tests/monkey_island/conftest.py index 2baa97a02..538576aef 100644 --- a/monkey/tests/unit_tests/monkey_island/conftest.py +++ b/monkey/tests/unit_tests/monkey_island/conftest.py @@ -1,4 +1,5 @@ import os +from collections.abc import Callable import pytest @@ -16,9 +17,12 @@ def patched_home_env(monkeypatch, tmpdir): @pytest.fixture -def create_empty_file(): - def inner(file_name): - with open(file_name, "w"): +def create_empty_tmp_file(tmpdir: str) -> Callable: + def inner(file_name: str): + new_file = os.path.join(tmpdir, file_name) + with open(new_file, "w"): pass + return new_file + return inner