From a27edfa94b0e82a3acd71e174e7be01f41564ca9 Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Wed, 6 Jul 2022 13:04:01 +0200 Subject: [PATCH] UT: Add test for GET propagation credentials --- monkey/tests/monkey_island/__init__.py | 1 + .../propagation_credentials_repository.py | 62 +++++++++++++++++++ .../resources/test_propagation_credentials.py | 28 +++++++++ 3 files changed, 91 insertions(+) create mode 100644 monkey/tests/monkey_island/propagation_credentials_repository.py create mode 100644 monkey/tests/unit_tests/monkey_island/cc/resources/test_propagation_credentials.py diff --git a/monkey/tests/monkey_island/__init__.py b/monkey/tests/monkey_island/__init__.py index 935752c37..1f3305665 100644 --- a/monkey/tests/monkey_island/__init__.py +++ b/monkey/tests/monkey_island/__init__.py @@ -3,3 +3,4 @@ from .mock_file_repository import MockFileRepository, FILE_CONTENTS, FILE_NAME from .open_error_file_repository import OpenErrorFileRepository from .in_memory_agent_configuration_repository import InMemoryAgentConfigurationRepository from .in_memory_simulation_configuration import InMemorySimulationRepository +from .propagation_credentials_repository import PropagationCredentialsRepository diff --git a/monkey/tests/monkey_island/propagation_credentials_repository.py b/monkey/tests/monkey_island/propagation_credentials_repository.py new file mode 100644 index 000000000..4ac1350fc --- /dev/null +++ b/monkey/tests/monkey_island/propagation_credentials_repository.py @@ -0,0 +1,62 @@ +from typing import Sequence + +from monkey_island.cc.repository import ICredentialsRepository +from monkey_island.cc.services.telemetry.processing.credentials import Credentials + +fake_username = "m0nk3y_user" +fake_special_username = "m0nk3y.user" +fake_nt_hash = "c1c58f96cdf212b50837bc11a00be47c" +fake_lm_hash = "299BD128C1101FD6" +fake_password_1 = "trytostealthis" +fake_password_2 = "password" +fake_password_3 = "12345678" +propagation_credentials_1 = { + "identities": [{"username": fake_username, "credential_type": "USERNAME"}], + "secrets": [ + {"nt_hash": fake_nt_hash, "credential_type": "NT_HASH"}, + {"lm_hash": fake_lm_hash, "credential_type": "LM_HASH"}, + {"password": fake_password_1, "credential_type": "PASSWORD"}, + ], +} + +propagation_credentials_2 = { + "identities": [ + {"username": fake_username, "credential_type": "USERNAME"}, + {"username": fake_special_username, "credential_type": "USERNAME"}, + ], + "secrets": [ + {"password": fake_password_1, "credential_type": "PASSWORD"}, + {"password": fake_password_2, "credential_type": "PASSWORD"}, + {"password": fake_password_3, "credential_type": "PASSWORD"}, + ], +} + + +class PropagationCredentialsRepository(ICredentialsRepository): + def get_configured_credentials(self) -> Sequence[Credentials]: + pass + + def get_stolen_credentials(self) -> Sequence[Credentials]: + pass + + def get_all_credentials(self) -> Sequence[Credentials]: + + return [ + Credentials.from_mapping(propagation_credentials_1, monkey_guid="some_guid"), + Credentials.from_mapping(propagation_credentials_2, monkey_guid="second_guid"), + ] + + def save_configured_credentials(self, credentials: Credentials): + pass + + def save_stolen_credentials(self, credentials: Credentials): + pass + + def remove_configured_credentials(self): + pass + + def remove_stolen_credentials(self): + pass + + def remove_all_credentials(self): + pass diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/test_propagation_credentials.py b/monkey/tests/unit_tests/monkey_island/cc/resources/test_propagation_credentials.py new file mode 100644 index 000000000..374a6db9c --- /dev/null +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/test_propagation_credentials.py @@ -0,0 +1,28 @@ +import json + +import pytest +from tests.common import StubDIContainer +from tests.monkey_island import PropagationCredentialsRepository +from tests.unit_tests.monkey_island.conftest import get_url_for_resource + +from monkey_island.cc.repository import ICredentialsRepository +from monkey_island.cc.resources.propagation_credentials import PropagationCredentials + + +@pytest.fixture +def flask_client(build_flask_client): + container = StubDIContainer() + + container.register(ICredentialsRepository, PropagationCredentialsRepository) + + with build_flask_client(container) as flask_client: + yield flask_client + + +def test_propagation_credentials_endpoint_get(flask_client): + propagation_credentials_url = get_url_for_resource(PropagationCredentials) + + resp = flask_client.get(propagation_credentials_url) + + assert resp.status_code == 200 + assert len(json.loads(resp.data)["propagation_credentials"]) == 2