From a4a0aba0fe20711b26b22c3b45ef89bbc045a473 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Tue, 18 May 2021 15:38:07 +0300 Subject: [PATCH] Refactored UT's fixtures to be on separate files and renamed folders/fixture to be more precise --- monkey/tests/conftest.py | 39 +------------------ .../logger_config.json | 0 .../{resources => mocked_data}/mongo_key.bin | 0 .../server_config_no_credentials.json | 3 ++ .../server_config_partial_credentials.json | 3 ++ .../server_config_standard_env.json | 3 ++ ...rver_config_standard_with_credentials.json | 3 ++ .../server_config_with_credentials.json | 3 ++ .../server_configs/test_server_config.json | 0 .../monkey_island/cc/environment/conftest.py | 23 +++++++++++ .../cc/environment/test_environment.py | 12 +++--- .../tests/monkey_island/cc/test_encryptor.py | 12 +++--- monkey/tests/monkey_island/conftest.py | 13 +++++++ 13 files changed, 65 insertions(+), 49 deletions(-) rename monkey/tests/{resources => mocked_data}/logger_config.json (100%) rename monkey/tests/{resources => mocked_data}/mongo_key.bin (100%) rename monkey/tests/{resources/environment => mocked_data/server_configs}/server_config_no_credentials.json (65%) rename monkey/tests/{resources/environment => mocked_data/server_configs}/server_config_partial_credentials.json (68%) rename monkey/tests/{resources/environment => mocked_data/server_configs}/server_config_standard_env.json (64%) rename monkey/tests/{resources/environment => mocked_data/server_configs}/server_config_standard_with_credentials.json (76%) rename monkey/tests/{resources/environment => mocked_data/server_configs}/server_config_with_credentials.json (73%) rename monkey/tests/{resources => mocked_data}/server_configs/test_server_config.json (100%) create mode 100644 monkey/tests/monkey_island/cc/environment/conftest.py create mode 100644 monkey/tests/monkey_island/conftest.py diff --git a/monkey/tests/conftest.py b/monkey/tests/conftest.py index 20e57f4d0..4cf7471f5 100644 --- a/monkey/tests/conftest.py +++ b/monkey/tests/conftest.py @@ -9,43 +9,8 @@ sys.path.insert(0, MONKEY_BASE_PATH) @pytest.fixture(scope="session") -def resources_dir(pytestconfig): - return os.path.join(pytestconfig.rootdir, "monkey", "tests", "resources") - - -@pytest.fixture(scope="session") -def environment_resources_dir(resources_dir): - return os.path.join(resources_dir, "environment") - - -@pytest.fixture(scope="session") -def with_credentials(environment_resources_dir): - return os.path.join(environment_resources_dir, "server_config_with_credentials.json") - - -@pytest.fixture(scope="session") -def no_credentials(environment_resources_dir): - return os.path.join(environment_resources_dir, "server_config_no_credentials.json") - - -@pytest.fixture(scope="session") -def partial_credentials(environment_resources_dir): - return os.path.join(environment_resources_dir, "server_config_partial_credentials.json") - - -@pytest.fixture(scope="session") -def standard_with_credentials(environment_resources_dir): - return os.path.join(environment_resources_dir, "server_config_standard_with_credentials.json") - - -@pytest.fixture(scope="session") -def server_config_resources_dir(resources_dir): - return os.path.join(resources_dir, "server_configs") - - -@pytest.fixture(scope="session") -def test_server_config(server_config_resources_dir): - return os.path.join(server_config_resources_dir, "test_server_config.json") +def mocked_data_dir(pytestconfig): + return os.path.join(pytestconfig.rootdir, "monkey", "tests", "mocked_data") @pytest.fixture diff --git a/monkey/tests/resources/logger_config.json b/monkey/tests/mocked_data/logger_config.json similarity index 100% rename from monkey/tests/resources/logger_config.json rename to monkey/tests/mocked_data/logger_config.json diff --git a/monkey/tests/resources/mongo_key.bin b/monkey/tests/mocked_data/mongo_key.bin similarity index 100% rename from monkey/tests/resources/mongo_key.bin rename to monkey/tests/mocked_data/mongo_key.bin diff --git a/monkey/tests/resources/environment/server_config_no_credentials.json b/monkey/tests/mocked_data/server_configs/server_config_no_credentials.json similarity index 65% rename from monkey/tests/resources/environment/server_config_no_credentials.json rename to monkey/tests/mocked_data/server_configs/server_config_no_credentials.json index 0b7de96ef..31da48aa4 100644 --- a/monkey/tests/resources/environment/server_config_no_credentials.json +++ b/monkey/tests/mocked_data/server_configs/server_config_no_credentials.json @@ -2,5 +2,8 @@ "environment" : { "server_config": "password", "deployment": "develop" + }, + "mongodb": { + "start_mongodb": true } } diff --git a/monkey/tests/resources/environment/server_config_partial_credentials.json b/monkey/tests/mocked_data/server_configs/server_config_partial_credentials.json similarity index 68% rename from monkey/tests/resources/environment/server_config_partial_credentials.json rename to monkey/tests/mocked_data/server_configs/server_config_partial_credentials.json index 6158c4f30..e29d514cd 100644 --- a/monkey/tests/resources/environment/server_config_partial_credentials.json +++ b/monkey/tests/mocked_data/server_configs/server_config_partial_credentials.json @@ -3,5 +3,8 @@ "server_config": "password", "deployment": "develop", "user": "test" + }, + "mongodb": { + "start_mongodb": true } } diff --git a/monkey/tests/resources/environment/server_config_standard_env.json b/monkey/tests/mocked_data/server_configs/server_config_standard_env.json similarity index 64% rename from monkey/tests/resources/environment/server_config_standard_env.json rename to monkey/tests/mocked_data/server_configs/server_config_standard_env.json index 3d5e0b8a0..9c3a9899f 100644 --- a/monkey/tests/resources/environment/server_config_standard_env.json +++ b/monkey/tests/mocked_data/server_configs/server_config_standard_env.json @@ -2,5 +2,8 @@ "environment" : { "server_config": "standard", "deployment": "develop" + }, + "mongodb": { + "start_mongodb": true } } diff --git a/monkey/tests/resources/environment/server_config_standard_with_credentials.json b/monkey/tests/mocked_data/server_configs/server_config_standard_with_credentials.json similarity index 76% rename from monkey/tests/resources/environment/server_config_standard_with_credentials.json rename to monkey/tests/mocked_data/server_configs/server_config_standard_with_credentials.json index b8cdf5258..28d8653c8 100644 --- a/monkey/tests/resources/environment/server_config_standard_with_credentials.json +++ b/monkey/tests/mocked_data/server_configs/server_config_standard_with_credentials.json @@ -5,5 +5,8 @@ "deployment": "develop", "user": "test", "password_hash": "abcdef" + }, + "mongodb": { + "start_mongodb": true } } diff --git a/monkey/tests/resources/environment/server_config_with_credentials.json b/monkey/tests/mocked_data/server_configs/server_config_with_credentials.json similarity index 73% rename from monkey/tests/resources/environment/server_config_with_credentials.json rename to monkey/tests/mocked_data/server_configs/server_config_with_credentials.json index 73cd6bbc3..2f75c48fb 100644 --- a/monkey/tests/resources/environment/server_config_with_credentials.json +++ b/monkey/tests/mocked_data/server_configs/server_config_with_credentials.json @@ -4,5 +4,8 @@ "deployment": "develop", "user": "test", "password_hash": "abcdef" + }, + "mongodb": { + "start_mongodb": true } } diff --git a/monkey/tests/resources/server_configs/test_server_config.json b/monkey/tests/mocked_data/server_configs/test_server_config.json similarity index 100% rename from monkey/tests/resources/server_configs/test_server_config.json rename to monkey/tests/mocked_data/server_configs/test_server_config.json diff --git a/monkey/tests/monkey_island/cc/environment/conftest.py b/monkey/tests/monkey_island/cc/environment/conftest.py new file mode 100644 index 000000000..0450437a1 --- /dev/null +++ b/monkey/tests/monkey_island/cc/environment/conftest.py @@ -0,0 +1,23 @@ +import os + +import pytest + + +@pytest.fixture(scope="module") +def with_credentials(mocked_server_configs_dir): + return os.path.join(mocked_server_configs_dir, "server_config_with_credentials.json") + + +@pytest.fixture(scope="module") +def no_credentials(mocked_server_configs_dir): + return os.path.join(mocked_server_configs_dir, "server_config_no_credentials.json") + + +@pytest.fixture(scope="module") +def partial_credentials(mocked_server_configs_dir): + return os.path.join(mocked_server_configs_dir, "server_config_partial_credentials.json") + + +@pytest.fixture(scope="module") +def standard_with_credentials(mocked_server_configs_dir): + return os.path.join(mocked_server_configs_dir, "server_config_standard_with_credentials.json") diff --git a/monkey/tests/monkey_island/cc/environment/test_environment.py b/monkey/tests/monkey_island/cc/environment/test_environment.py index 4c32cf137..3b0c2c713 100644 --- a/monkey/tests/monkey_island/cc/environment/test_environment.py +++ b/monkey/tests/monkey_island/cc/environment/test_environment.py @@ -27,7 +27,7 @@ FULL_USER_CREDENTIALS = UserCreds(username="test", password_hash="1231234") # This fixture is a dirty hack that can be removed once these tests are converted from # unittest to pytest. Instead, the appropriate fixtures from conftest.py can be used. @pytest.fixture(scope="module", autouse=True) -def configure_resources(environment_resources_dir): +def configure_resources(mocked_server_configs_dir): global WITH_CREDENTIALS global NO_CREDENTIALS global PARTIAL_CREDENTIALS @@ -35,16 +35,16 @@ def configure_resources(environment_resources_dir): global STANDARD_ENV WITH_CREDENTIALS = os.path.join( - environment_resources_dir, "server_config_with_credentials.json" + mocked_server_configs_dir, "server_config_with_credentials.json" ) - NO_CREDENTIALS = os.path.join(environment_resources_dir, "server_config_no_credentials.json") + NO_CREDENTIALS = os.path.join(mocked_server_configs_dir, "server_config_no_credentials.json") PARTIAL_CREDENTIALS = os.path.join( - environment_resources_dir, "server_config_partial_credentials.json" + mocked_server_configs_dir, "server_config_partial_credentials.json" ) STANDARD_WITH_CREDENTIALS = os.path.join( - environment_resources_dir, "server_config_standard_with_credentials.json" + mocked_server_configs_dir, "server_config_standard_with_credentials.json" ) - STANDARD_ENV = os.path.join(environment_resources_dir, "server_config_standard_env.json") + STANDARD_ENV = os.path.join(mocked_server_configs_dir, "server_config_standard_env.json") def get_tmp_file(): diff --git a/monkey/tests/monkey_island/cc/test_encryptor.py b/monkey/tests/monkey_island/cc/test_encryptor.py index a7ad9e6b6..b92b6cf11 100644 --- a/monkey/tests/monkey_island/cc/test_encryptor.py +++ b/monkey/tests/monkey_island/cc/test_encryptor.py @@ -8,20 +8,20 @@ PLAINTEXT = "Hello, Monkey!" CYPHERTEXT = "vKgvD6SjRyIh1dh2AM/rnTa0NI/vjfwnbZLbMocWtE4e42WJmSUz2ordtbQrH1Fq" -def test_aes_cbc_encryption(resources_dir): - initialize_encryptor(resources_dir) +def test_aes_cbc_encryption(mocked_data_dir): + initialize_encryptor(mocked_data_dir) assert get_encryptor().enc(PLAINTEXT) != PLAINTEXT -def test_aes_cbc_decryption(resources_dir): - initialize_encryptor(resources_dir) +def test_aes_cbc_decryption(mocked_data_dir): + initialize_encryptor(mocked_data_dir) assert get_encryptor().dec(CYPHERTEXT) == PLAINTEXT -def test_aes_cbc_enc_dec(resources_dir): - initialize_encryptor(resources_dir) +def test_aes_cbc_enc_dec(mocked_data_dir): + initialize_encryptor(mocked_data_dir) assert get_encryptor().dec(get_encryptor().enc(PLAINTEXT)) == PLAINTEXT diff --git a/monkey/tests/monkey_island/conftest.py b/monkey/tests/monkey_island/conftest.py new file mode 100644 index 000000000..589f3f20c --- /dev/null +++ b/monkey/tests/monkey_island/conftest.py @@ -0,0 +1,13 @@ +import os + +import pytest + + +@pytest.fixture(scope="module") +def mocked_server_configs_dir(mocked_data_dir): + return os.path.join(mocked_data_dir, "server_configs") + + +@pytest.fixture(scope="module") +def test_server_config(mocked_server_configs_dir): + return os.path.join(mocked_server_configs_dir, "test_server_config.json")