From 066f106882cd95186791f58d4d4561beb655bb87 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 23 Sep 2022 18:01:12 +0530 Subject: [PATCH] UT: Move relevant tests from test_agent_signals.py to test_terminate_all_agents.py --- .../cc/resources/test_agent_signals.py | 36 ----------- .../cc/resources/test_terminate_all_agents.py | 59 +++++++++++++++++++ 2 files changed, 59 insertions(+), 36 deletions(-) create mode 100644 monkey/tests/unit_tests/monkey_island/cc/resources/test_terminate_all_agents.py diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/test_agent_signals.py b/monkey/tests/unit_tests/monkey_island/cc/resources/test_agent_signals.py index b4a6a4d7f..3f644e051 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/resources/test_agent_signals.py +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/test_agent_signals.py @@ -5,10 +5,8 @@ from uuid import UUID import pytest from tests.common import StubDIContainer -from monkey_island.cc.event_queue import IIslandEventQueue from monkey_island.cc.models import AgentSignals as Signals from monkey_island.cc.repository import RetrievalError, StorageError -from monkey_island.cc.resources import AgentSignals from monkey_island.cc.services import AgentSignalsService TIMESTAMP_1 = 123456789 @@ -30,11 +28,6 @@ def mock_agent_signals_service(): def flask_client_builder(build_flask_client, mock_agent_signals_service): def inner(side_effect=None): container = StubDIContainer() - - mock_island_event_queue = MagicMock(spec=IIslandEventQueue) - mock_island_event_queue.publish.side_effect = side_effect - container.register_instance(IIslandEventQueue, mock_island_event_queue) - container.register_instance(AgentSignalsService, mock_agent_signals_service) with build_flask_client(container) as flask_client: @@ -48,35 +41,6 @@ def flask_client(flask_client_builder): return flask_client_builder() -def test_agent_signals_terminate_all_post(flask_client): - resp = flask_client.post( - AgentSignals.urls[0], - json={"terminate_time": TIMESTAMP_1}, - follow_redirects=True, - ) - assert resp.status_code == HTTPStatus.NO_CONTENT - - -@pytest.mark.parametrize( - "bad_data", - [ - "bad timestamp", - {}, - {"wrong_key": TIMESTAMP_1}, - TIMESTAMP_1, - {"terminate_time": 0}, - {"terminate_time": -1}, - ], -) -def test_agent_signals_terminate_all_post__invalid_timestamp(flask_client, bad_data): - resp = flask_client.post( - AgentSignals.urls[0], - json=bad_data, - follow_redirects=True, - ) - assert resp.status_code == HTTPStatus.BAD_REQUEST - - @pytest.mark.parametrize( "url, signals", [ diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/test_terminate_all_agents.py b/monkey/tests/unit_tests/monkey_island/cc/resources/test_terminate_all_agents.py new file mode 100644 index 000000000..305293f26 --- /dev/null +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/test_terminate_all_agents.py @@ -0,0 +1,59 @@ +from http import HTTPStatus +from unittest.mock import MagicMock + +import pytest +from tests.common import StubDIContainer + +from monkey_island.cc.event_queue import IIslandEventQueue +from monkey_island.cc.resources import TerminateAllAgents + +TIMESTAMP = 123456789 + + +@pytest.fixture +def flask_client_builder(build_flask_client): + def inner(side_effect=None): + container = StubDIContainer() + + mock_island_event_queue = MagicMock(spec=IIslandEventQueue) + mock_island_event_queue.publish.side_effect = side_effect + container.register_instance(IIslandEventQueue, mock_island_event_queue) + + with build_flask_client(container) as flask_client: + return flask_client + + return inner + + +@pytest.fixture +def flask_client(flask_client_builder): + return flask_client_builder() + + +def test_terminate_all_agents_post(flask_client): + resp = flask_client.post( + TerminateAllAgents.urls[0], + json={"terminate_time": TIMESTAMP}, + follow_redirects=True, + ) + assert resp.status_code == HTTPStatus.NO_CONTENT + + +@pytest.mark.parametrize( + "bad_data", + [ + "bad timestamp", + {}, + {"wrong_key": TIMESTAMP}, + TIMESTAMP, + {"terminate_time": 0}, + {"terminate_time": -1}, + ], +) +def test_terminate_all_agents_post__invalid_timestamp(flask_client, bad_data): + resp = flask_client.post( + TerminateAllAgents.urls[0], + json=bad_data, + follow_redirects=True, + ) + assert resp.status_code == HTTPStatus.BAD_REQUEST