|
| 1 | +from http import HTTPStatus |
| 2 | + |
| 3 | +import pytest |
| 4 | +from tests.common import StubDIContainer |
| 5 | +from tests.monkey_island import InMemoryAgentPluginRepository |
| 6 | +from tests.unit_tests.common.agent_plugins.test_agent_plugin_manifest import FAKE_TYPE |
| 7 | +from tests.unit_tests.monkey_island.cc.fake_agent_plugin_data import FAKE_AGENT_PLUGIN_1 |
| 8 | +from tests.unit_tests.monkey_island.conftest import get_url_for_resource |
| 9 | + |
| 10 | +from monkey_island.cc.repositories import IAgentPluginRepository, RetrievalError |
| 11 | +from monkey_island.cc.resources import AgentPluginsManifest |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def agent_plugin_repository(): |
| 16 | + return InMemoryAgentPluginRepository() |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def flask_client(build_flask_client, agent_plugin_repository): |
| 21 | + container = StubDIContainer() |
| 22 | + container.register_instance(IAgentPluginRepository, agent_plugin_repository) |
| 23 | + |
| 24 | + with build_flask_client(container) as flask_client: |
| 25 | + yield flask_client |
| 26 | + |
| 27 | + |
| 28 | +def test_get_plugin_manifest(flask_client, agent_plugin_repository): |
| 29 | + agent_plugin_repository.save_plugin(FAKE_AGENT_PLUGIN_1) |
| 30 | + |
| 31 | + expected_response = { |
| 32 | + "description": None, |
| 33 | + "link_to_documentation": "www.beefface.com", |
| 34 | + "name": "rdp_exploiter", |
| 35 | + "plugin_type": "Exploiter", |
| 36 | + "safe": False, |
| 37 | + "supported_operating_systems": ["linux"], |
| 38 | + "title": "Remote Desktop Protocol exploiter", |
| 39 | + } |
| 40 | + resp = flask_client.get( |
| 41 | + get_url_for_resource( |
| 42 | + AgentPluginsManifest, |
| 43 | + plugin_type=FAKE_TYPE, |
| 44 | + name=FAKE_AGENT_PLUGIN_1.plugin_manifest.name, |
| 45 | + ) |
| 46 | + ) |
| 47 | + |
| 48 | + assert resp.status_code == HTTPStatus.OK |
| 49 | + assert resp.json == expected_response |
| 50 | + |
| 51 | + |
| 52 | +def test_get_plugins_manifest__not_found_if_name_does_not_exist(flask_client): |
| 53 | + resp = flask_client.get( |
| 54 | + get_url_for_resource(AgentPluginsManifest, plugin_type="Payload", name="name") |
| 55 | + ) |
| 56 | + |
| 57 | + assert resp.status_code == HTTPStatus.NOT_FOUND |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize( |
| 61 | + "type_", |
| 62 | + ["DummyType", "ExploiteR"], |
| 63 | +) |
| 64 | +def test_get_plugins_manifest__not_found_if_type_is_invalid(flask_client, type_): |
| 65 | + resp = flask_client.get( |
| 66 | + get_url_for_resource(AgentPluginsManifest, plugin_type=type_, name="name") |
| 67 | + ) |
| 68 | + |
| 69 | + assert resp.status_code == HTTPStatus.NOT_FOUND |
| 70 | + |
| 71 | + |
| 72 | +def test_get_plugins_manifest__server_error(flask_client, agent_plugin_repository): |
| 73 | + def raise_retrieval_error(plugin_type, name): |
| 74 | + raise RetrievalError |
| 75 | + |
| 76 | + agent_plugin_repository.get_plugin = raise_retrieval_error |
| 77 | + |
| 78 | + resp = flask_client.get( |
| 79 | + get_url_for_resource(AgentPluginsManifest, plugin_type="Payload", name="name") |
| 80 | + ) |
| 81 | + |
| 82 | + assert resp.status_code == HTTPStatus.INTERNAL_SERVER_ERROR |
0 commit comments