From d0293b4edc899ab29ae5aed76b90d217d347f6e9 Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Wed, 28 Sep 2022 16:36:38 +0200 Subject: [PATCH] UT: Move and rename StubAgentLogRepository to InMemoryAgentLogRepository --- monkey/tests/monkey_island/__init__.py | 1 + .../in_memory_agent_log_repository.py | 19 +++++++++++++++ .../cc/resources/test_agent_logs.py | 23 +++---------------- 3 files changed, 23 insertions(+), 20 deletions(-) create mode 100644 monkey/tests/monkey_island/in_memory_agent_log_repository.py diff --git a/monkey/tests/monkey_island/__init__.py b/monkey/tests/monkey_island/__init__.py index f7033842d..4b7f0f7c7 100644 --- a/monkey/tests/monkey_island/__init__.py +++ b/monkey/tests/monkey_island/__init__.py @@ -4,3 +4,4 @@ from .open_error_file_repository import OpenErrorFileRepository from .in_memory_agent_configuration_repository import InMemoryAgentConfigurationRepository from .in_memory_simulation_configuration import InMemorySimulationRepository from .in_memory_credentials_repository import InMemoryCredentialsRepository +from .in_memory_agent_log_repository import InMemoryAgentLogRepository diff --git a/monkey/tests/monkey_island/in_memory_agent_log_repository.py b/monkey/tests/monkey_island/in_memory_agent_log_repository.py new file mode 100644 index 000000000..ccd1d18cf --- /dev/null +++ b/monkey/tests/monkey_island/in_memory_agent_log_repository.py @@ -0,0 +1,19 @@ +from common.types import AgentID +from monkey_island.cc.repository import IAgentLogRepository, UnknownRecordError + + +class InMemoryAgentLogRepository(IAgentLogRepository): + def __init__(self): + self._agent_logs = {} + + def upsert_agent_log(self, agent_id: AgentID, log_contents: str): + if agent_id not in self._agent_logs.keys(): + self._agent_logs[agent_id] = log_contents + + def get_agent_log(self, agent_id: AgentID) -> str: + if agent_id not in self._agent_logs: + raise UnknownRecordError("Error occured while getting agent") + return self._agent_logs[agent_id] + + def reset(self): + self._agent_logs = {} diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/test_agent_logs.py b/monkey/tests/unit_tests/monkey_island/cc/resources/test_agent_logs.py index f6eb81e65..0f7e86b53 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/resources/test_agent_logs.py +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/test_agent_logs.py @@ -3,9 +3,9 @@ from uuid import UUID import pytest from tests.common import StubDIContainer +from tests.monkey_island import InMemoryAgentLogRepository -from common.types import AgentID -from monkey_island.cc.repository import IAgentLogRepository, UnknownRecordError +from monkey_island.cc.repository import IAgentLogRepository AGENT_ID_1 = UUID("c0dd10b3-e21a-4da9-9d96-a99c19ebd7c5") AGENT_ID_2 = UUID("f811ad00-5a68-4437-bd51-7b5cc1768ad5") @@ -14,27 +14,10 @@ AGENT_LOGS_URL_1 = f"/api/agent-logs/{AGENT_ID_1}" AGENT_LOGS_URL_2 = f"/api/agent-logs/{AGENT_ID_2}" -class StubAgentLogRepository(IAgentLogRepository): - def __init__(self): - self._agent_logs = {} - - def upsert_agent_log(self, agent_id: AgentID, log_contents: str): - if agent_id not in self._agent_logs.keys(): - self._agent_logs[agent_id] = log_contents - - def get_agent_log(self, agent_id: AgentID) -> str: - if agent_id not in self._agent_logs: - raise UnknownRecordError("Error occured while getting agent") - return self._agent_logs[agent_id] - - def reset(self): - self._agent_logs = {} - - @pytest.fixture def flask_client(build_flask_client): container = StubDIContainer() - container.register_instance(IAgentLogRepository, StubAgentLogRepository()) + container.register_instance(IAgentLogRepository, InMemoryAgentLogRepository()) with build_flask_client(container) as flask_client: yield flask_client