tests: Fix/add unit tests based on addition of `config_manipulator.py`

This commit is contained in:
Shreya 2021-07-20 16:11:35 +05:30
parent eabbca4f32
commit 42b558674e
4 changed files with 52 additions and 42 deletions

View File

@ -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(

View File

@ -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

View File

@ -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"] == []

View File

@ -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"] == []