UT: Add typehints to test_pba_file_upload.py

This commit is contained in:
Mike Salvatore 2022-07-13 11:38:52 -04:00
parent 9c7bf6c8b5
commit e48e2cb9af
1 changed files with 20 additions and 10 deletions

View File

@ -1,9 +1,13 @@
from typing import Callable
import pytest import pytest
from flask.testing import FlaskClient
from tests.common import StubDIContainer from tests.common import StubDIContainer
from tests.monkey_island import InMemoryAgentConfigurationRepository, SingleFileRepository from tests.monkey_island import InMemoryAgentConfigurationRepository, SingleFileRepository
from tests.unit_tests.monkey_island.conftest import get_url_for_resource from tests.unit_tests.monkey_island.conftest import get_url_for_resource
from tests.utils import raise_ from tests.utils import raise_
from common import DIContainer
from monkey_island.cc.repository import IAgentConfigurationRepository, IFileRepository from monkey_island.cc.repository import IAgentConfigurationRepository, IFileRepository
from monkey_island.cc.resources.pba_file_upload import LINUX_PBA_TYPE, WINDOWS_PBA_TYPE, FileUpload from monkey_island.cc.resources.pba_file_upload import LINUX_PBA_TYPE, WINDOWS_PBA_TYPE, FileUpload
@ -25,17 +29,21 @@ Content-Type: text/x-python
@pytest.fixture @pytest.fixture
def file_repository(): def file_repository() -> IFileRepository:
return SingleFileRepository() return SingleFileRepository()
@pytest.fixture @pytest.fixture
def agent_configuration_repository(): def agent_configuration_repository() -> IAgentConfigurationRepository:
return InMemoryAgentConfigurationRepository() return InMemoryAgentConfigurationRepository()
@pytest.fixture @pytest.fixture
def flask_client(build_flask_client, file_repository, agent_configuration_repository): def flask_client(
build_flask_client: Callable[[DIContainer], FlaskClient],
file_repository: IFileRepository,
agent_configuration_repository: IAgentConfigurationRepository,
) -> FlaskClient:
container = StubDIContainer() container = StubDIContainer()
container.register_instance(IFileRepository, file_repository) container.register_instance(IFileRepository, file_repository)
container.register_instance(IAgentConfigurationRepository, agent_configuration_repository) container.register_instance(IAgentConfigurationRepository, agent_configuration_repository)
@ -45,7 +53,7 @@ def flask_client(build_flask_client, file_repository, agent_configuration_reposi
@pytest.mark.parametrize("pba_os", [LINUX_PBA_TYPE, WINDOWS_PBA_TYPE]) @pytest.mark.parametrize("pba_os", [LINUX_PBA_TYPE, WINDOWS_PBA_TYPE])
def test_pba_file_upload_post(flask_client, pba_os): def test_pba_file_upload_post(flask_client: FlaskClient, pba_os: str):
url = get_url_for_resource(FileUpload, target_os=pba_os) url = get_url_for_resource(FileUpload, target_os=pba_os)
resp = flask_client.post( resp = flask_client.post(
url, url,
@ -56,7 +64,7 @@ def test_pba_file_upload_post(flask_client, pba_os):
assert resp.status_code == 200 assert resp.status_code == 200
def test_pba_file_upload_post__invalid(flask_client): def test_pba_file_upload_post__invalid(flask_client: FlaskClient):
url = get_url_for_resource(FileUpload, target_os="bogus") url = get_url_for_resource(FileUpload, target_os="bogus")
resp = flask_client.post( resp = flask_client.post(
url, url,
@ -68,7 +76,9 @@ def test_pba_file_upload_post__invalid(flask_client):
@pytest.mark.parametrize("pba_os", [LINUX_PBA_TYPE, WINDOWS_PBA_TYPE]) @pytest.mark.parametrize("pba_os", [LINUX_PBA_TYPE, WINDOWS_PBA_TYPE])
def test_pba_file_upload_post__internal_server_error(flask_client, pba_os, file_repository): def test_pba_file_upload_post__internal_server_error(
flask_client: FlaskClient, pba_os: str, file_repository: IFileRepository
):
file_repository.save_file = lambda x, y: raise_(Exception()) file_repository.save_file = lambda x, y: raise_(Exception())
url = get_url_for_resource(FileUpload, target_os=pba_os) url = get_url_for_resource(FileUpload, target_os=pba_os)
@ -82,14 +92,14 @@ def test_pba_file_upload_post__internal_server_error(flask_client, pba_os, file_
@pytest.mark.parametrize("pba_os", [LINUX_PBA_TYPE, WINDOWS_PBA_TYPE]) @pytest.mark.parametrize("pba_os", [LINUX_PBA_TYPE, WINDOWS_PBA_TYPE])
def test_pba_file_upload_get__file_not_found(flask_client, pba_os): def test_pba_file_upload_get__file_not_found(flask_client: FlaskClient, pba_os: str):
url = get_url_for_resource(FileUpload, target_os=pba_os, filename="bobug_mogus.py") url = get_url_for_resource(FileUpload, target_os=pba_os, filename="bobug_mogus.py")
resp = flask_client.get(url) resp = flask_client.get(url)
assert resp.status_code == 404 assert resp.status_code == 404
@pytest.mark.parametrize("pba_os", [LINUX_PBA_TYPE, WINDOWS_PBA_TYPE]) @pytest.mark.parametrize("pba_os", [LINUX_PBA_TYPE, WINDOWS_PBA_TYPE])
def test_file_download_endpoint_500(open_error_flask_client, pba_os): def test_file_download_endpoint_500(open_error_flask_client, pba_os: str):
url = get_url_for_resource(FileUpload, target_os=pba_os, filename="bobug_mogus.py") url = get_url_for_resource(FileUpload, target_os=pba_os, filename="bobug_mogus.py")
resp = open_error_flask_client.get(url) resp = open_error_flask_client.get(url)
@ -98,7 +108,7 @@ def test_file_download_endpoint_500(open_error_flask_client, pba_os):
@pytest.mark.parametrize("pba_os", [LINUX_PBA_TYPE, WINDOWS_PBA_TYPE]) @pytest.mark.parametrize("pba_os", [LINUX_PBA_TYPE, WINDOWS_PBA_TYPE])
def test_pba_file_upload_endpoint(flask_client, pba_os): def test_pba_file_upload_endpoint(flask_client: FlaskClient, pba_os: str):
url_with_os = get_url_for_resource(FileUpload, target_os=pba_os) url_with_os = get_url_for_resource(FileUpload, target_os=pba_os)
resp_post = flask_client.post( resp_post = flask_client.post(
@ -123,7 +133,7 @@ def test_pba_file_upload_endpoint(flask_client, pba_os):
assert resp_get_del.status_code == 404 assert resp_get_del.status_code == 404
def test_pba_file_upload_endpoint__invalid(flask_client): def test_pba_file_upload_endpoint__invalid(flask_client: FlaskClient):
url_with_os = get_url_for_resource(FileUpload, target_os="bogus") url_with_os = get_url_for_resource(FileUpload, target_os="bogus")
resp_post = flask_client.post( resp_post = flask_client.post(