forked from p15670423/monkey
UT: Remove all authentication test code from enviroment and
enviromentconfig
This commit is contained in:
parent
3ed5a5619f
commit
59d78d5e30
|
@ -1,9 +0,0 @@
|
|||
{
|
||||
"environment" : {
|
||||
"server_config": "password",
|
||||
"user": "test"
|
||||
},
|
||||
"mongodb": {
|
||||
"start_mongodb": true
|
||||
}
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
{
|
||||
"log_level": "NOTICE",
|
||||
"environment" : {
|
||||
"server_config": "password",
|
||||
"user": "test",
|
||||
"password_hash": "abcdef"
|
||||
},
|
||||
"mongodb": {
|
||||
"start_mongodb": true
|
||||
}
|
||||
}
|
|
@ -3,16 +3,6 @@ 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")
|
||||
|
|
|
@ -1,91 +1,10 @@
|
|||
import os
|
||||
import tempfile
|
||||
from typing import Dict
|
||||
from unittest import TestCase
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from common.utils.exceptions import AlreadyRegisteredError, InvalidRegistrationCredentialsError
|
||||
from monkey_island.cc.environment import Environment, EnvironmentConfig, UserCreds
|
||||
|
||||
WITH_CREDENTIALS = None
|
||||
NO_CREDENTIALS = None
|
||||
PARTIAL_CREDENTIALS = None
|
||||
|
||||
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(server_configs_dir):
|
||||
global WITH_CREDENTIALS
|
||||
global NO_CREDENTIALS
|
||||
global PARTIAL_CREDENTIALS
|
||||
|
||||
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")
|
||||
|
||||
|
||||
def get_tmp_file():
|
||||
with tempfile.NamedTemporaryFile(delete=False) as f:
|
||||
return f.name
|
||||
|
||||
|
||||
class StubEnvironmentConfig(EnvironmentConfig):
|
||||
def __init__(self, server_config, deployment, user_creds):
|
||||
self.server_config = server_config
|
||||
self.deployment = deployment
|
||||
self.user_creds = user_creds
|
||||
self.server_config_path = get_tmp_file()
|
||||
|
||||
def __del__(self):
|
||||
os.remove(self.server_config_path)
|
||||
from monkey_island.cc.environment import Environment, EnvironmentConfig
|
||||
|
||||
|
||||
class TestEnvironment(TestCase):
|
||||
class EnvironmentCredentialsRequired(Environment):
|
||||
def __init__(self):
|
||||
config = StubEnvironmentConfig("test", "test", None)
|
||||
super().__init__(config)
|
||||
|
||||
class EnvironmentAlreadyRegistered(Environment):
|
||||
def __init__(self):
|
||||
config = StubEnvironmentConfig("test", "test", UserCreds("test_user", "test_secret"))
|
||||
super().__init__(config)
|
||||
|
||||
@patch.object(target=EnvironmentConfig, attribute="save_to_file", new=MagicMock())
|
||||
def test_try_add_user(self):
|
||||
env = TestEnvironment.EnvironmentCredentialsRequired()
|
||||
credentials = USER_CREDENTIALS
|
||||
env.try_add_user(credentials)
|
||||
|
||||
credentials = UserCreds(username="test", password_hash="")
|
||||
with self.assertRaises(InvalidRegistrationCredentialsError):
|
||||
env.try_add_user(credentials)
|
||||
|
||||
def test_try_needs_registration(self):
|
||||
env = TestEnvironment.EnvironmentAlreadyRegistered()
|
||||
with self.assertRaises(AlreadyRegisteredError):
|
||||
env._try_needs_registration()
|
||||
|
||||
env = TestEnvironment.EnvironmentCredentialsRequired()
|
||||
self.assertTrue(env._try_needs_registration())
|
||||
|
||||
def test_needs_registration(self):
|
||||
env = TestEnvironment.EnvironmentCredentialsRequired()
|
||||
self._test_bool_env_method("needs_registration", env, WITH_CREDENTIALS, False)
|
||||
self._test_bool_env_method("needs_registration", env, NO_CREDENTIALS, True)
|
||||
self._test_bool_env_method("needs_registration", env, PARTIAL_CREDENTIALS, True)
|
||||
|
||||
def test_is_registered(self):
|
||||
env = TestEnvironment.EnvironmentCredentialsRequired()
|
||||
self._test_bool_env_method("_is_registered", env, WITH_CREDENTIALS, True)
|
||||
self._test_bool_env_method("_is_registered", env, NO_CREDENTIALS, False)
|
||||
self._test_bool_env_method("_is_registered", env, PARTIAL_CREDENTIALS, False)
|
||||
|
||||
def _test_bool_env_method(
|
||||
self, method_name: str, env: Environment, config: Dict, expected_result: bool
|
||||
):
|
||||
|
|
|
@ -5,7 +5,6 @@ import shutil
|
|||
import pytest
|
||||
|
||||
from monkey_island.cc.environment.environment_config import EnvironmentConfig
|
||||
from monkey_island.cc.environment.user_creds import UserCreds
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
@ -13,15 +12,6 @@ def config_file(tmpdir):
|
|||
return os.path.join(tmpdir, "test_config.json")
|
||||
|
||||
|
||||
def test_get_with_credentials(with_credentials):
|
||||
config_dict = EnvironmentConfig(with_credentials).to_dict()
|
||||
|
||||
assert len(config_dict.keys()) == 3
|
||||
assert config_dict["server_config"] == "password"
|
||||
assert config_dict["user"] == "test"
|
||||
assert config_dict["password_hash"] == "abcdef"
|
||||
|
||||
|
||||
def test_get_with_no_credentials(no_credentials):
|
||||
config_dict = EnvironmentConfig(no_credentials).to_dict()
|
||||
|
||||
|
@ -29,16 +19,8 @@ def test_get_with_no_credentials(no_credentials):
|
|||
assert config_dict["server_config"] == "password"
|
||||
|
||||
|
||||
def test_get_with_partial_credentials(partial_credentials):
|
||||
config_dict = EnvironmentConfig(partial_credentials).to_dict()
|
||||
|
||||
assert len(config_dict.keys()) == 2
|
||||
assert config_dict["server_config"] == "password"
|
||||
assert config_dict["user"] == "test"
|
||||
|
||||
|
||||
def test_save_to_file(config_file, with_credentials):
|
||||
shutil.copyfile(with_credentials, config_file)
|
||||
def test_save_to_file(config_file, no_credentials):
|
||||
shutil.copyfile(no_credentials, config_file)
|
||||
|
||||
environment_config = EnvironmentConfig(config_file)
|
||||
environment_config.aws = "test_aws"
|
||||
|
@ -48,43 +30,3 @@ def test_save_to_file(config_file, with_credentials):
|
|||
from_file = json.load(f)
|
||||
|
||||
assert environment_config.to_dict() == from_file["environment"]
|
||||
|
||||
|
||||
def test_save_to_file_preserve_log_level(config_file, with_credentials):
|
||||
shutil.copyfile(with_credentials, config_file)
|
||||
|
||||
environment_config = EnvironmentConfig(config_file)
|
||||
environment_config.aws = "test_aws"
|
||||
environment_config.save_to_file()
|
||||
|
||||
with open(config_file, "r") as f:
|
||||
from_file = json.load(f)
|
||||
|
||||
assert "log_level" in from_file
|
||||
assert from_file["log_level"] == "NOTICE"
|
||||
|
||||
|
||||
def test_add_user(config_file, with_credentials):
|
||||
new_user = "new_user"
|
||||
new_password_hash = "fedcba"
|
||||
new_user_creds = UserCreds(new_user, new_password_hash)
|
||||
|
||||
shutil.copyfile(with_credentials, config_file)
|
||||
|
||||
environment_config = EnvironmentConfig(config_file)
|
||||
environment_config.add_user(new_user_creds)
|
||||
|
||||
with open(config_file, "r") as f:
|
||||
from_file = json.load(f)
|
||||
|
||||
assert len(from_file["environment"].keys()) == 3
|
||||
assert from_file["environment"]["user"] == new_user
|
||||
assert from_file["environment"]["password_hash"] == new_password_hash
|
||||
|
||||
|
||||
def test_user(with_credentials):
|
||||
environment_config = EnvironmentConfig(with_credentials)
|
||||
user = environment_config.user_creds
|
||||
|
||||
assert user.username == "test"
|
||||
assert user.password_hash == "abcdef"
|
||||
|
|
Loading…
Reference in New Issue