From d7329ea83942587c2cb49b04b3796c1318c2802f Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Thu, 23 Jun 2022 14:31:51 +0200 Subject: [PATCH] UT: Add tests for AgentConfiguration resource --- .../cc/resources/test_agent_configuration.py | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 monkey/tests/unit_tests/monkey_island/cc/resources/test_agent_configuration.py diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/test_agent_configuration.py b/monkey/tests/unit_tests/monkey_island/cc/resources/test_agent_configuration.py new file mode 100644 index 000000000..5cac629e0 --- /dev/null +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/test_agent_configuration.py @@ -0,0 +1,50 @@ +import json + +import pytest +from tests.common import StubDIContainer +from tests.common.example_agent_configuration import AGENT_CONFIGURATION +from tests.monkey_island import InMemoryFileAgentConfigurationRepository +from tests.unit_tests.monkey_island.conftest import get_url_for_resource + +from monkey_island.cc.repository import IAgentConfigurationRepository +from monkey_island.cc.resources.agent_configuration import AgentConfiguration + + +@pytest.fixture +def flask_client(build_flask_client): + container = StubDIContainer() + + container.register(IAgentConfigurationRepository, InMemoryFileAgentConfigurationRepository) + + with build_flask_client(container) as flask_client: + yield flask_client + + +def test_agent_configuration_endpoint(flask_client): + agent_configuration_url = get_url_for_resource(AgentConfiguration) + + flask_client.post( + agent_configuration_url, data=json.dumps(AGENT_CONFIGURATION), follow_redirects=True + ) + resp = flask_client.get(agent_configuration_url) + + assert resp.status_code == 200 + assert json.loads(resp.data) == AGENT_CONFIGURATION + + +def test_agent_configuration_invalid_config(flask_client): + agent_configuration_url = get_url_for_resource(AgentConfiguration) + + resp = flask_client.post( + agent_configuration_url, data=json.dumps({"invalid_config": "invalid_stuff"}) + ) + + assert resp.status_code == 400 + + +def test_agent_configuration_invalid_json(flask_client): + agent_configuration_url = get_url_for_resource(AgentConfiguration) + + resp = flask_client.post(agent_configuration_url, data="InvalidJson!") + + assert resp.status_code == 400