From f5345b6db1c9108abb4f5243b02cb38092f1204e Mon Sep 17 00:00:00 2001 From: Kim Hammar Date: Wed, 9 Aug 2023 10:19:00 +0200 Subject: [PATCH] add type hints --- .../test_resources_emulation_statistics.py | 69 +++++++------------ 1 file changed, 25 insertions(+), 44 deletions(-) diff --git a/simulation-system/libs/csle-rest-api/tests/test_resources_emulation_statistics.py b/simulation-system/libs/csle-rest-api/tests/test_resources_emulation_statistics.py index 4f254d7fa..a3de8f352 100644 --- a/simulation-system/libs/csle-rest-api/tests/test_resources_emulation_statistics.py +++ b/simulation-system/libs/csle-rest-api/tests/test_resources_emulation_statistics.py @@ -1,19 +1,13 @@ import json import logging from typing import List, Tuple - -import csle_common.constants.constants as constants import pytest import pytest_mock -from csle_common.dao.system_identification.emulation_statistics import ( - EmulationStatistics, -) - +from csle_common.dao.system_identification.emulation_statistics import EmulationStatistics +import csle_common.constants.constants as constants import csle_rest_api.constants.constants as api_constants from csle_rest_api.rest_api import create_app -logger = logging.getLogger() - class TestResourcesEmulationsStatisticsSuite: """ @@ -36,7 +30,6 @@ def em_stat_ids(self, mocker: pytest_mock.MockFixture): :return: the mocked function """ def list_emulation_statistics_ids() -> List[Tuple[int, int]]: - logger.info("kommer jag hit eller ") return [(1, 1)] list_emulation_statistics_ids_mocker = mocker.MagicMock(side_effect=list_emulation_statistics_ids) return list_emulation_statistics_ids_mocker @@ -69,7 +62,7 @@ def list_emulation_statistics() -> List[EmulationStatistics]: return list_emulation_statistics_mocker @pytest.fixture - def get_em_stat(self, mocker): + def get_em_stat(self, mocker: pytest_mock.MockFixture): """ Pytest fixture for mocking the get_emulation_statistic method @@ -88,7 +81,7 @@ def get_em_stat_none(self, mocker: pytest_mock.MockFixture): Pytest fixture for mocking the get_emulation_statistic method :param mocker: the pytest mocker object - :return: th emocked function + :return: the mocked function """ def get_emulation_statistic(id: int) -> None: return None @@ -99,13 +92,14 @@ def get_emulation_statistic(id: int) -> None: def get_ex_em_stat() -> EmulationStatistics: """ Static help method for returning an Emulationstatistic object - :return: an EmulationsStistics object + + :return: an EmulationsSatistics object """ em_stat = EmulationStatistics(emulation_name="JohnDoe", descr="null") return em_stat - def test_emulation_statistics_get(self, mocker, flask_app, not_logged_in, logged_in, - logged_in_as_admin, em_stat_ids, em_stat): + def test_emulation_statistics_get(self, mocker: pytest_mock.MockFixture, flask_app, not_logged_in, logged_in, + logged_in_as_admin, em_stat_ids, em_stat) -> None: """ Testing the GET HTTPS method for the /emulation-statistics resource @@ -118,22 +112,19 @@ def test_emulation_statistics_get(self, mocker, flask_app, not_logged_in, logged side_effect=em_stat_ids) mocker.patch("csle_common.metastore.metastore_facade.MetastoreFacade.list_emulation_statistics", side_effect=em_stat) - mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized", - side_effect=not_logged_in) + mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized", side_effect=not_logged_in) response = flask_app.test_client().get(f"{api_constants.MGMT_WEBAPP.EMULATION_STATISTICS_RESOURCE}" f"?{api_constants.MGMT_WEBAPP.IDS_QUERY_PARAM}") response_data = response.data.decode("utf-8") response_data_dict = json.loads(response_data) assert response.status_code == constants.HTTPS.UNAUTHORIZED_STATUS_CODE assert response_data_dict == {} - response = flask_app.test_client().get(f"{api_constants.MGMT_WEBAPP.EMULATION_STATISTICS_RESOURCE}") response_data = response.data.decode("utf-8") response_data_dict = json.loads(response_data) assert response.status_code == constants.HTTPS.UNAUTHORIZED_STATUS_CODE assert response_data_dict == {} - mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized", - side_effect=logged_in) + mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized", side_effect=logged_in) response = flask_app.test_client().get(f"{api_constants.MGMT_WEBAPP.EMULATION_STATISTICS_RESOURCE}" f"?{api_constants.MGMT_WEBAPP.IDS_QUERY_PARAM}=true") response_data = response.data.decode("utf-8") @@ -150,8 +141,7 @@ def test_emulation_statistics_get(self, mocker, flask_app, not_logged_in, logged ex_em_stat_dict = ex_em_stat.to_dict() for k in response_data_dict: assert response_data_dict[k] == ex_em_stat_dict[k] - mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized", - side_effect=logged_in_as_admin) + mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized", side_effect=logged_in_as_admin) response = flask_app.test_client().get(f"{api_constants.MGMT_WEBAPP.EMULATION_STATISTICS_RESOURCE}" f"?{api_constants.MGMT_WEBAPP.IDS_QUERY_PARAM}=true") response_data = response.data.decode("utf-8") @@ -169,8 +159,8 @@ def test_emulation_statistics_get(self, mocker, flask_app, not_logged_in, logged for k in response_data_dict: assert response_data_dict[k] == ex_em_stat_dict[k] - def test_emulation_statistics_delete(self, mocker, flask_app, not_logged_in, logged_in, - logged_in_as_admin, em_stat, remove): + def test_emulation_statistics_delete(self, mocker: pytest_mock.MockFixture, flask_app, not_logged_in, logged_in, + logged_in_as_admin, em_stat, remove) -> None: """ Testing the DELETE HTTPS method for the /emulation-statistics resource @@ -184,30 +174,27 @@ def test_emulation_statistics_delete(self, mocker, flask_app, not_logged_in, log side_effect=em_stat) mocker.patch("csle_common.metastore.metastore_facade.MetastoreFacade.remove_emulation_statistic", side_effect=remove) - mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized", - side_effect=not_logged_in) + mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized", side_effect=not_logged_in) response = flask_app.test_client().delete(f"{api_constants.MGMT_WEBAPP.EMULATION_STATISTICS_RESOURCE}") response_data = response.data.decode("utf-8") response_data_dict = json.loads(response_data) assert response.status_code == constants.HTTPS.UNAUTHORIZED_STATUS_CODE assert response_data_dict == {} - mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized", - side_effect=logged_in) + mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized", side_effect=logged_in) response = flask_app.test_client().delete(f"{api_constants.MGMT_WEBAPP.EMULATION_STATISTICS_RESOURCE}") response_data = response.data.decode("utf-8") response_data_dict = json.loads(response_data) assert response.status_code == constants.HTTPS.UNAUTHORIZED_STATUS_CODE assert response_data_dict == {} - mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized", - side_effect=logged_in_as_admin) + mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized", side_effect=logged_in_as_admin) response = flask_app.test_client().delete(f"{api_constants.MGMT_WEBAPP.EMULATION_STATISTICS_RESOURCE}") response_data = response.data.decode("utf-8") response_data_dict = json.loads(response_data) assert response.status_code == constants.HTTPS.OK_STATUS_CODE assert response_data_dict == {} - def test_emulation_statistics_ids_get(self, mocker, flask_app, not_logged_in, logged_in, - logged_in_as_admin, get_em_stat, get_em_stat_none): + def test_emulation_statistics_ids_get(self, mocker: pytest_mock.MockFixture, flask_app, not_logged_in, logged_in, + logged_in_as_admin, get_em_stat, get_em_stat_none) -> None: """ Testing the GET HTTPS method for the /emulation-statistics/id resource @@ -218,8 +205,7 @@ def test_emulation_statistics_ids_get(self, mocker, flask_app, not_logged_in, lo """ mocker.patch("csle_common.metastore.metastore_facade.MetastoreFacade.get_emulation_statistic", side_effect=get_em_stat) - mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized", - side_effect=not_logged_in) + mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized", side_effect=not_logged_in) response = flask_app.test_client().get(f"{api_constants.MGMT_WEBAPP.EMULATION_STATISTICS_RESOURCE}/10") response_data = response.data.decode("utf-8") response_data_dict = json.loads(response_data) @@ -241,8 +227,7 @@ def test_emulation_statistics_ids_get(self, mocker, flask_app, not_logged_in, lo response_data_dict = json.loads(response_data) assert response_data_dict == {} assert response.status_code == constants.HTTPS.OK_STATUS_CODE - mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized", - side_effect=logged_in_as_admin) + mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized", side_effect=logged_in_as_admin) response = flask_app.test_client().get(f"{api_constants.MGMT_WEBAPP.EMULATION_STATISTICS_RESOURCE}/10") response_data = response.data.decode("utf-8") response_data_dict = json.loads(response_data) @@ -258,9 +243,8 @@ def test_emulation_statistics_ids_get(self, mocker, flask_app, not_logged_in, lo assert response_data_dict == {} assert response.status_code == constants.HTTPS.OK_STATUS_CODE - def test_emulation_statistics_ids_delete(self, mocker, flask_app, not_logged_in, logged_in, - logged_in_as_admin, get_em_stat, get_em_stat_none, - remove): + def test_emulation_statistics_ids_delete(self, mocker: pytest_mock.MockFixture, flask_app, not_logged_in, logged_in, + logged_in_as_admin, get_em_stat, get_em_stat_none, remove) -> None: """ Testing the DELETE HTTPS method for the /emulation-statistics/id resource @@ -274,22 +258,19 @@ def test_emulation_statistics_ids_delete(self, mocker, flask_app, not_logged_in, side_effect=get_em_stat) mocker.patch("csle_common.metastore.metastore_facade.MetastoreFacade.remove_emulation_statistic", side_effect=remove) - mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized", - side_effect=not_logged_in) + mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized", side_effect=not_logged_in) response = flask_app.test_client().delete(f"{api_constants.MGMT_WEBAPP.EMULATION_STATISTICS_RESOURCE}/10") response_data = response.data.decode("utf-8") response_data_dict = json.loads(response_data) assert response.status_code == constants.HTTPS.UNAUTHORIZED_STATUS_CODE assert response_data_dict == {} - mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized", - side_effect=logged_in) + mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized", side_effect=logged_in) response = flask_app.test_client().delete(f"{api_constants.MGMT_WEBAPP.EMULATION_STATISTICS_RESOURCE}/10") response_data = response.data.decode("utf-8") response_data_dict = json.loads(response_data) assert response.status_code == constants.HTTPS.UNAUTHORIZED_STATUS_CODE assert response_data_dict == {} - mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized", - side_effect=logged_in_as_admin) + mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized", side_effect=logged_in_as_admin) response = flask_app.test_client().delete(f"{api_constants.MGMT_WEBAPP.EMULATION_STATISTICS_RESOURCE}/10") response_data = response.data.decode("utf-8") response_data_dict = json.loads(response_data)