From ba724db969258fc1e77a1eb209abd0b403a4a9e2 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 12 Sep 2022 13:41:51 -0400 Subject: [PATCH] Island: Publish IslandEventTopic.CLEAR_SIMULATION_DATA from resource --- .../cc/resources/clear_simulation_data.py | 8 ++--- .../resources/test_clear_simulation_data.py | 29 +++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 monkey/tests/unit_tests/monkey_island/cc/resources/test_clear_simulation_data.py diff --git a/monkey/monkey_island/cc/resources/clear_simulation_data.py b/monkey/monkey_island/cc/resources/clear_simulation_data.py index c5a37ec96..e582dbd11 100644 --- a/monkey/monkey_island/cc/resources/clear_simulation_data.py +++ b/monkey/monkey_island/cc/resources/clear_simulation_data.py @@ -2,16 +2,16 @@ from http import HTTPStatus from flask import make_response +from monkey_island.cc.event_queue import IIslandEventQueue, IslandEventTopic from monkey_island.cc.resources.AbstractResource import AbstractResource from monkey_island.cc.resources.request_authentication import jwt_required -from monkey_island.cc.services import RepositoryService class ClearSimulationData(AbstractResource): urls = ["/api/clear-simulation-data"] - def __init__(self, repository_service: RepositoryService): - self._repository_service = repository_service + def __init__(self, island_event_queue: IIslandEventQueue): + self._island_event_queue = island_event_queue @jwt_required def post(self): @@ -19,5 +19,5 @@ class ClearSimulationData(AbstractResource): Clear all data collected during the simulation """ - self._repository_service.clear_simulation_data() + self._island_event_queue.publish(IslandEventTopic.CLEAR_SIMULATION_DATA) return make_response({}, HTTPStatus.NO_CONTENT) diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/test_clear_simulation_data.py b/monkey/tests/unit_tests/monkey_island/cc/resources/test_clear_simulation_data.py new file mode 100644 index 000000000..3ae081fcb --- /dev/null +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/test_clear_simulation_data.py @@ -0,0 +1,29 @@ +from http import HTTPStatus +from unittest.mock import MagicMock + +import pytest +from tests.common import StubDIContainer + +from monkey_island.cc.event_queue import IIslandEventQueue, IslandEventTopic +from monkey_island.cc.resources import ClearSimulationData + + +@pytest.fixture +def mock_island_event_queue() -> IIslandEventQueue: + return MagicMock(spec=IIslandEventQueue) + + +@pytest.fixture +def flask_client(build_flask_client, mock_island_event_queue): + container = StubDIContainer() + container.register_instance(IIslandEventQueue, mock_island_event_queue) + + with build_flask_client(container) as flask_client: + yield flask_client + + +def test_clear_simulation_data(flask_client, mock_island_event_queue): + resp = flask_client.post(ClearSimulationData.urls[0], follow_redirects=True) + + assert resp.status_code == HTTPStatus.NO_CONTENT + mock_island_event_queue.publish.assert_called_once_with(IslandEventTopic.CLEAR_SIMULATION_DATA)