forked from p15670423/monkey
UT: Move relevant tests from test_agent_signals.py to test_terminate_all_agents.py
This commit is contained in:
parent
645e03e46f
commit
066f106882
|
@ -5,10 +5,8 @@ from uuid import UUID
|
||||||
import pytest
|
import pytest
|
||||||
from tests.common import StubDIContainer
|
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.models import AgentSignals as Signals
|
||||||
from monkey_island.cc.repository import RetrievalError, StorageError
|
from monkey_island.cc.repository import RetrievalError, StorageError
|
||||||
from monkey_island.cc.resources import AgentSignals
|
|
||||||
from monkey_island.cc.services import AgentSignalsService
|
from monkey_island.cc.services import AgentSignalsService
|
||||||
|
|
||||||
TIMESTAMP_1 = 123456789
|
TIMESTAMP_1 = 123456789
|
||||||
|
@ -30,11 +28,6 @@ def mock_agent_signals_service():
|
||||||
def flask_client_builder(build_flask_client, mock_agent_signals_service):
|
def flask_client_builder(build_flask_client, mock_agent_signals_service):
|
||||||
def inner(side_effect=None):
|
def inner(side_effect=None):
|
||||||
container = StubDIContainer()
|
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)
|
container.register_instance(AgentSignalsService, mock_agent_signals_service)
|
||||||
|
|
||||||
with build_flask_client(container) as flask_client:
|
with build_flask_client(container) as flask_client:
|
||||||
|
@ -48,35 +41,6 @@ def flask_client(flask_client_builder):
|
||||||
return 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(
|
@pytest.mark.parametrize(
|
||||||
"url, signals",
|
"url, signals",
|
||||||
[
|
[
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue