Merge pull request #1177 from guardicore/unit_test_fixture_refactoring

Unit test fixture refactoring
This commit is contained in:
VakarisZ 2021-05-19 09:38:17 +03:00 committed by GitHub
commit 832453fdb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 66 additions and 61 deletions

View File

@ -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 data_for_tests_dir(pytestconfig):
return os.path.join(pytestconfig.rootdir, "monkey", "tests", "data_for_tests")
@pytest.fixture

View File

@ -2,5 +2,8 @@
"environment" : {
"server_config": "password",
"deployment": "develop"
},
"mongodb": {
"start_mongodb": true
}
}

View File

@ -3,5 +3,8 @@
"server_config": "password",
"deployment": "develop",
"user": "test"
},
"mongodb": {
"start_mongodb": true
}
}

View File

@ -2,5 +2,8 @@
"environment" : {
"server_config": "standard",
"deployment": "develop"
},
"mongodb": {
"start_mongodb": true
}
}

View File

@ -5,5 +5,8 @@
"deployment": "develop",
"user": "test",
"password_hash": "abcdef"
},
"mongodb": {
"start_mongodb": true
}
}

View File

@ -4,5 +4,8 @@
"deployment": "develop",
"user": "test",
"password_hash": "abcdef"
},
"mongodb": {
"start_mongodb": true
}
}

View File

@ -0,0 +1,23 @@
import os
import pytest
@pytest.fixture(scope="module")
def with_credentials(server_configs_dir):
return os.path.join(server_configs_dir, "server_config_with_credentials.json")
@pytest.fixture(scope="module")
def no_credentials(server_configs_dir):
return os.path.join(server_configs_dir, "server_config_no_credentials.json")
@pytest.fixture(scope="module")
def partial_credentials(server_configs_dir):
return os.path.join(server_configs_dir, "server_config_partial_credentials.json")
@pytest.fixture(scope="module")
def standard_with_credentials(server_configs_dir):
return os.path.join(server_configs_dir, "server_config_standard_with_credentials.json")

View File

@ -27,24 +27,20 @@ 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(server_configs_dir):
global WITH_CREDENTIALS
global NO_CREDENTIALS
global PARTIAL_CREDENTIALS
global STANDARD_WITH_CREDENTIALS
global STANDARD_ENV
WITH_CREDENTIALS = os.path.join(
environment_resources_dir, "server_config_with_credentials.json"
)
NO_CREDENTIALS = os.path.join(environment_resources_dir, "server_config_no_credentials.json")
PARTIAL_CREDENTIALS = os.path.join(
environment_resources_dir, "server_config_partial_credentials.json"
)
WITH_CREDENTIALS = os.path.join(server_configs_dir, "server_config_with_credentials.json")
NO_CREDENTIALS = os.path.join(server_configs_dir, "server_config_no_credentials.json")
PARTIAL_CREDENTIALS = os.path.join(server_configs_dir, "server_config_partial_credentials.json")
STANDARD_WITH_CREDENTIALS = os.path.join(
environment_resources_dir, "server_config_standard_with_credentials.json"
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(server_configs_dir, "server_config_standard_env.json")
def get_tmp_file():

View File

@ -50,13 +50,7 @@ def test_save_to_file(config_file, standard_with_credentials):
with open(config_file, "r") as f:
from_file = json.load(f)
assert len(from_file.keys()) == 2
assert len(from_file["environment"].keys()) == 5
assert from_file["environment"]["server_config"] == "standard"
assert from_file["environment"]["deployment"] == "develop"
assert from_file["environment"]["user"] == "test"
assert from_file["environment"]["password_hash"] == "abcdef"
assert from_file["environment"]["aws"] == "test_aws"
assert environment_config.to_dict() == from_file["environment"]
def test_save_to_file_preserve_log_level(config_file, standard_with_credentials):
@ -69,7 +63,6 @@ def test_save_to_file_preserve_log_level(config_file, standard_with_credentials)
with open(config_file, "r") as f:
from_file = json.load(f)
assert len(from_file.keys()) == 2
assert "log_level" in from_file
assert from_file["log_level"] == "NOTICE"

View File

@ -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(data_for_tests_dir):
initialize_encryptor(data_for_tests_dir)
assert get_encryptor().enc(PLAINTEXT) != PLAINTEXT
def test_aes_cbc_decryption(resources_dir):
initialize_encryptor(resources_dir)
def test_aes_cbc_decryption(data_for_tests_dir):
initialize_encryptor(data_for_tests_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(data_for_tests_dir):
initialize_encryptor(data_for_tests_dir)
assert get_encryptor().dec(get_encryptor().enc(PLAINTEXT)) == PLAINTEXT

View File

@ -0,0 +1,13 @@
import os
import pytest
@pytest.fixture(scope="module")
def server_configs_dir(data_for_tests_dir):
return os.path.join(data_for_tests_dir, "server_configs")
@pytest.fixture(scope="module")
def test_server_config(server_configs_dir):
return os.path.join(server_configs_dir, "test_server_config.json")