Skip to content

Commit

Permalink
cc: Move config files and other testing resources to tests/ dir
Browse files Browse the repository at this point in the history
  • Loading branch information
mssalvatore committed Apr 20, 2021
1 parent 88a3ab5 commit 753c058
Show file tree
Hide file tree
Showing 16 changed files with 113 additions and 52 deletions.
46 changes: 46 additions & 0 deletions monkey/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,51 @@
import os
import sys
from pathlib import Path

import pytest

MONKEY_BASE_PATH = str(Path(__file__).parent.parent)
sys.path.insert(0, MONKEY_BASE_PATH)


print("imported")


@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 with_data_dir(environment_resources_dir):
return os.path.join(environment_resources_dir, "server_config_with_data_dir.json")


@pytest.fixture(scope="session")
def with_data_dir_home(environment_resources_dir):
return os.path.join(environment_resources_dir, "server_config_with_data_dir_home.json")
40 changes: 30 additions & 10 deletions monkey/tests/monkey_island/cc/environment/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,44 @@
from unittest import TestCase
from unittest.mock import MagicMock, patch

import pytest

from common.utils.exceptions import (
AlreadyRegisteredError,
CredentialsNotRequiredError,
InvalidRegistrationCredentialsError,
RegistrationNotNeededError,
)
from monkey_island.cc.environment import Environment, EnvironmentConfig, UserCreds
from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH

TEST_RESOURCES_DIR = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", "testing", "environment")

WITH_CREDENTIALS = os.path.join(TEST_RESOURCES_DIR, "server_config_with_credentials.json")
NO_CREDENTIALS = os.path.join(TEST_RESOURCES_DIR, "server_config_no_credentials.json")
PARTIAL_CREDENTIALS = os.path.join(TEST_RESOURCES_DIR, "server_config_partial_credentials.json")
STANDARD_WITH_CREDENTIALS = os.path.join(
TEST_RESOURCES_DIR, "server_config_standard_with_credentials.json"
)
STANDARD_ENV = os.path.join(TEST_RESOURCES_DIR, "server_config_standard_env.json")
WITH_CREDENTIALS = None
NO_CREDENTIALS = None
PARTIAL_CREDENTIALS = None
STANDARD_WITH_CREDENTIALS = None
STANDARD_ENV = None


# 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):
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"
)
STANDARD_WITH_CREDENTIALS = os.path.join(
environment_resources_dir, "server_config_standard_with_credentials.json"
)
STANDARD_ENV = os.path.join(environment_resources_dir, "server_config_standard_env.json")


def get_tmp_file():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,16 @@

from monkey_island.cc.environment.environment_config import EnvironmentConfig
from monkey_island.cc.environment.user_creds import UserCreds
from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR, MONKEY_ISLAND_ABS_PATH

TEST_RESOURCES_DIR = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", "testing", "environment")

WITH_CREDENTIALS = os.path.join(TEST_RESOURCES_DIR, "server_config_with_credentials.json")
NO_CREDENTIALS = os.path.join(TEST_RESOURCES_DIR, "server_config_no_credentials.json")
PARTIAL_CREDENTIALS = os.path.join(TEST_RESOURCES_DIR, "server_config_partial_credentials.json")
STANDARD_WITH_CREDENTIALS = os.path.join(
TEST_RESOURCES_DIR, "server_config_standard_with_credentials.json"
)
WITH_DATA_DIR = os.path.join(TEST_RESOURCES_DIR, "server_config_with_data_dir.json")
WITH_DATA_DIR_HOME = os.path.join(TEST_RESOURCES_DIR, "server_config_with_data_dir_home.json")
from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR


@pytest.fixture
def config_file(tmpdir):
return os.path.join(tmpdir, "test_config.json")


def test_get_with_credentials():
config_dict = EnvironmentConfig(WITH_CREDENTIALS).to_dict()
def test_get_with_credentials(with_credentials):
config_dict = EnvironmentConfig(with_credentials).to_dict()

assert len(config_dict.keys()) == 5
assert config_dict["server_config"] == "password"
Expand All @@ -36,17 +25,17 @@ def test_get_with_credentials():
assert config_dict["data_dir"] == DEFAULT_DATA_DIR


def test_get_with_no_credentials():
config_dict = EnvironmentConfig(NO_CREDENTIALS).to_dict()
def test_get_with_no_credentials(no_credentials):
config_dict = EnvironmentConfig(no_credentials).to_dict()

assert len(config_dict.keys()) == 3
assert config_dict["server_config"] == "password"
assert config_dict["deployment"] == "develop"
assert config_dict["data_dir"] == DEFAULT_DATA_DIR


def test_get_with_partial_credentials():
config_dict = EnvironmentConfig(PARTIAL_CREDENTIALS).to_dict()
def test_get_with_partial_credentials(partial_credentials):
config_dict = EnvironmentConfig(partial_credentials).to_dict()

assert len(config_dict.keys()) == 4
assert config_dict["server_config"] == "password"
Expand All @@ -55,8 +44,8 @@ def test_get_with_partial_credentials():
assert config_dict["data_dir"] == DEFAULT_DATA_DIR


def test_save_to_file(config_file):
shutil.copyfile(STANDARD_WITH_CREDENTIALS, config_file)
def test_save_to_file(config_file, standard_with_credentials):
shutil.copyfile(standard_with_credentials, config_file)

environment_config = EnvironmentConfig(config_file)
environment_config.aws = "test_aws"
Expand All @@ -74,12 +63,12 @@ def test_save_to_file(config_file):
assert from_file["data_dir"] == DEFAULT_DATA_DIR


def test_add_user(config_file):
def test_add_user(config_file, standard_with_credentials):
new_user = "new_user"
new_password_hash = "fedcba"
new_user_creds = UserCreds(new_user, new_password_hash)

shutil.copyfile(STANDARD_WITH_CREDENTIALS, config_file)
shutil.copyfile(standard_with_credentials, config_file)

environment_config = EnvironmentConfig(config_file)
environment_config.add_user(new_user_creds)
Expand All @@ -92,8 +81,8 @@ def test_add_user(config_file):
assert from_file["password_hash"] == new_password_hash


def test_get_users():
environment_config = EnvironmentConfig(STANDARD_WITH_CREDENTIALS)
def test_get_users(standard_with_credentials):
environment_config = EnvironmentConfig(standard_with_credentials)
users = environment_config.get_users()

assert len(users) == 1
Expand All @@ -115,17 +104,17 @@ def test_generate_default_file(config_file):
assert environment_config.data_dir == DEFAULT_DATA_DIR


def test_data_dir():
environment_config = EnvironmentConfig(WITH_DATA_DIR)
def test_data_dir(with_data_dir):
environment_config = EnvironmentConfig(with_data_dir)
assert environment_config.data_dir == "/test/data/dir"


def set_home_env(monkeypatch, tmpdir):
monkeypatch.setenv("HOME", str(tmpdir))


def test_data_dir_abs_path_from_file(monkeypatch, tmpdir):
def test_data_dir_abs_path_from_file(monkeypatch, tmpdir, with_data_dir_home):
set_home_env(monkeypatch, tmpdir)

config = EnvironmentConfig(WITH_DATA_DIR_HOME)
config = EnvironmentConfig(with_data_dir_home)
assert config.data_dir_abs_path == os.path.join(tmpdir, "data_dir")
12 changes: 6 additions & 6 deletions monkey/tests/monkey_island/cc/server_utils/test_island_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

import pytest

from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH
from monkey_island.cc.server_utils.island_logger import json_setup_logging

TEST_LOGGER_CONFIG_PATH = os.path.join(
MONKEY_ISLAND_ABS_PATH, "cc", "testing", "logger_config.json"
)

@pytest.fixture()
def test_logger_config_path(resources_dir):
return os.path.join(resources_dir, "logger_config.json")


# TODO move into monkey/monkey_island/cc/test_common/fixtures after rebase/backmerge
Expand All @@ -17,11 +17,11 @@ def mock_home_env(monkeypatch, tmpdir):
monkeypatch.setenv("HOME", str(tmpdir))


def test_expanduser_filename(mock_home_env, tmpdir):
def test_expanduser_filename(mock_home_env, tmpdir, test_logger_config_path):
INFO_LOG = os.path.join(tmpdir, "info.log")
TEST_STRING = "Hello, Monkey!"

json_setup_logging(TEST_LOGGER_CONFIG_PATH)
json_setup_logging(test_logger_config_path)

logger = logging.getLogger("TestLogger")
logger.info(TEST_STRING)
Expand Down
14 changes: 6 additions & 8 deletions monkey/tests/monkey_island/cc/test_encryptor.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import os

from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH
from monkey_island.cc.server_utils.encryptor import get_encryptor, initialize_encryptor

TEST_DATA_DIR = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", "testing")
PASSWORD_FILENAME = "mongo_key.bin"

PLAINTEXT = "Hello, Monkey!"
CYPHERTEXT = "vKgvD6SjRyIh1dh2AM/rnTa0NI/vjfwnbZLbMocWtE4e42WJmSUz2ordtbQrH1Fq"


def test_aes_cbc_encryption():
initialize_encryptor(TEST_DATA_DIR)
def test_aes_cbc_encryption(resources_dir):
initialize_encryptor(resources_dir)

assert get_encryptor().enc(PLAINTEXT) != PLAINTEXT


def test_aes_cbc_decryption():
initialize_encryptor(TEST_DATA_DIR)
def test_aes_cbc_decryption(resources_dir):
initialize_encryptor(resources_dir)

assert get_encryptor().dec(CYPHERTEXT) == PLAINTEXT


def test_aes_cbc_enc_dec():
initialize_encryptor(TEST_DATA_DIR)
def test_aes_cbc_enc_dec(resources_dir):
initialize_encryptor(resources_dir)

assert get_encryptor().dec(get_encryptor().enc(PLAINTEXT)) == PLAINTEXT

Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions monkey/tests/resources/server_config_no_credentials.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"server_config": "password",
"deployment": "develop"
}
4 changes: 4 additions & 0 deletions monkey/tests/resources/server_config_with_credentials.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"server_config": "password",
"deployment": "develop"
}

0 comments on commit 753c058

Please sign in to comment.