From 42b558674e0cfcc04bd5cd3d775fad113624e7d9 Mon Sep 17 00:00:00 2001 From: Shreya Date: Tue, 20 Jul 2021 16:11:35 +0530 Subject: [PATCH] tests: Fix/add unit tests based on addition of `config_manipulator.py` --- .../cc/resources/test_island_mode.py | 2 +- .../monkey_island/cc/services/conftest.py | 22 ++++++++++ .../monkey_island/cc/services/test_config.py | 44 ++----------------- .../cc/services/test_config_manipulator.py | 26 +++++++++++ 4 files changed, 52 insertions(+), 42 deletions(-) create mode 100644 monkey/tests/unit_tests/monkey_island/cc/services/conftest.py create mode 100644 monkey/tests/unit_tests/monkey_island/cc/services/test_config_manipulator.py diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/test_island_mode.py b/monkey/tests/unit_tests/monkey_island/cc/resources/test_island_mode.py index dc1899e8c..435680bfe 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/resources/test_island_mode.py +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/test_island_mode.py @@ -15,7 +15,7 @@ def uses_database(): @pytest.mark.parametrize("mode", ["ransomware", "advanced"]) def test_island_mode_post(flask_client, mode, monkeypatch): monkeypatch.setattr( - "monkey_island.cc.services.mode.set_island_mode_service.ConfigService.update_config_on_mode_set", # noqa: E501 + "monkey_island.cc.services.mode.set_island_mode_service.update_config_on_mode_set", lambda _: None, ) resp = flask_client.post( diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/conftest.py b/monkey/tests/unit_tests/monkey_island/cc/services/conftest.py new file mode 100644 index 000000000..7b56c0c13 --- /dev/null +++ b/monkey/tests/unit_tests/monkey_island/cc/services/conftest.py @@ -0,0 +1,22 @@ +import pytest + +from monkey_island.cc.environment import Environment +from monkey_island.cc.services.config import ConfigService + + +@pytest.fixture +def IPS(): + return ["0.0.0.0", "9.9.9.9"] + + +@pytest.fixture +def PORT(): + return 9999 + + +@pytest.fixture +def config(monkeypatch, IPS, PORT): + monkeypatch.setattr("monkey_island.cc.services.config.local_ip_addresses", lambda: IPS) + monkeypatch.setattr(Environment, "_ISLAND_PORT", PORT) + config = ConfigService.get_default_config(True) + return config diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_config.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_config.py index 26addb732..799fc40e1 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_config.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_config.py @@ -1,68 +1,30 @@ import pytest -from monkey_island.cc.environment import Environment from monkey_island.cc.services.config import ConfigService -from monkey_island.cc.services.mode.mode_enum import IslandModeEnum - -IPS = ["0.0.0.0", "9.9.9.9"] -PORT = 9999 - # If tests fail because config path is changed, sync with # monkey/monkey_island/cc/ui/src/components/pages/RunMonkeyPage/RunOptions.js -@pytest.fixture -def config(monkeypatch): - monkeypatch.setattr("monkey_island.cc.services.config.local_ip_addresses", lambda: IPS) - monkeypatch.setattr(Environment, "_ISLAND_PORT", PORT) - config = ConfigService.get_default_config(True) - return config - - class MockClass: pass @pytest.fixture(scope="function", autouse=True) -def mock_port_in_env_singleton(monkeypatch): +def mock_port_in_env_singleton(monkeypatch, PORT): mock_singleton = MockClass() mock_singleton.env = MockClass() mock_singleton.env.get_island_port = lambda: PORT monkeypatch.setattr("monkey_island.cc.services.config.env_singleton", mock_singleton) -def test_set_server_ips_in_config_command_servers(config): +def test_set_server_ips_in_config_command_servers(config, IPS, PORT): ConfigService.set_server_ips_in_config(config) expected_config_command_servers = [f"{ip}:{PORT}" for ip in IPS] assert config["internal"]["island_server"]["command_servers"] == expected_config_command_servers -def test_set_server_ips_in_config_current_server(config): +def test_set_server_ips_in_config_current_server(config, IPS, PORT): ConfigService.set_server_ips_in_config(config) expected_config_current_server = f"{IPS[0]}:{PORT}" assert config["internal"]["island_server"]["current_server"] == expected_config_current_server - - -def test_update_config_on_mode_set_advanced(config, monkeypatch): - monkeypatch.setattr("monkey_island.cc.services.config.ConfigService.get_config", lambda: config) - monkeypatch.setattr( - "monkey_island.cc.services.config.ConfigService.update_config", - lambda config_json, should_encrypt: config_json, - ) - - mode = IslandModeEnum.ADVANCED - manipulated_config = ConfigService.update_config_on_mode_set(mode) - assert manipulated_config == config - - -def test_update_config_on_mode_set_ransomware(config, monkeypatch): - monkeypatch.setattr("monkey_island.cc.services.config.ConfigService.get_config", lambda: config) - monkeypatch.setattr( - "monkey_island.cc.services.config.ConfigService.update_config", - lambda config_json, should_encrypt: config_json, - ) - - mode = IslandModeEnum.RANSOMWARE - manipulated_config = ConfigService.update_config_on_mode_set(mode) - assert manipulated_config["monkey"]["post_breach"]["post_breach_actions"] == [] diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_config_manipulator.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_config_manipulator.py new file mode 100644 index 000000000..12cd44c10 --- /dev/null +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_config_manipulator.py @@ -0,0 +1,26 @@ +from monkey_island.cc.services.config_manipulator import update_config_on_mode_set +from monkey_island.cc.services.mode.mode_enum import IslandModeEnum + + +def test_update_config_on_mode_set_advanced(config, monkeypatch): + monkeypatch.setattr("monkey_island.cc.services.config.ConfigService.get_config", lambda: config) + monkeypatch.setattr( + "monkey_island.cc.services.config.ConfigService.update_config", + lambda config_json, should_encrypt: config_json, + ) + + mode = IslandModeEnum.ADVANCED + manipulated_config = update_config_on_mode_set(mode) + assert manipulated_config == config + + +def test_update_config_on_mode_set_ransomware(config, monkeypatch): + monkeypatch.setattr("monkey_island.cc.services.config.ConfigService.get_config", lambda: config) + monkeypatch.setattr( + "monkey_island.cc.services.config.ConfigService.update_config", + lambda config_json, should_encrypt: config_json, + ) + + mode = IslandModeEnum.RANSOMWARE + manipulated_config = update_config_on_mode_set(mode) + assert manipulated_config["monkey"]["post_breach"]["post_breach_actions"] == []