From 91fcd75a8645bf59c2b8a331b93838ea0b3d3b92 Mon Sep 17 00:00:00 2001 From: nforsg Date: Tue, 8 Aug 2023 17:39:15 +0200 Subject: [PATCH 1/3] work in progress --- .../csle_common/metastore/metastore_facade.py | 2 +- .../resources/emulation_statistics/routes.py | 11 +- .../test_resources_emulation_statistics.py | 225 ++++++++++++++++++ 3 files changed, 234 insertions(+), 4 deletions(-) create mode 100644 simulation-system/libs/csle-rest-api/tests/test_resources_emulation_statistics.py diff --git a/simulation-system/libs/csle-common/src/csle_common/metastore/metastore_facade.py b/simulation-system/libs/csle-common/src/csle_common/metastore/metastore_facade.py index a1ca29364..8939ea0fb 100644 --- a/simulation-system/libs/csle-common/src/csle_common/metastore/metastore_facade.py +++ b/simulation-system/libs/csle-common/src/csle_common/metastore/metastore_facade.py @@ -476,7 +476,7 @@ def list_emulation_statistics() -> List[EmulationStatistics]: return records @staticmethod - def list_emulation_statistics_ids() -> List[Dict]: + def list_emulation_statistics_ids() -> List[Tuple]: """ :return: A list of emulation statistics ids in the metastore """ diff --git a/simulation-system/libs/csle-rest-api/src/csle_rest_api/resources/emulation_statistics/routes.py b/simulation-system/libs/csle-rest-api/src/csle_rest_api/resources/emulation_statistics/routes.py index 2abfd415a..0e1567e76 100644 --- a/simulation-system/libs/csle-rest-api/src/csle_rest_api/resources/emulation_statistics/routes.py +++ b/simulation-system/libs/csle-rest-api/src/csle_rest_api/resources/emulation_statistics/routes.py @@ -1,13 +1,18 @@ """ Routes and sub-resources for the /emulation-statistics resource """ +import logging from typing import Tuple -from flask import Blueprint, jsonify, request, Response + import csle_common.constants.constants as constants -import csle_rest_api.constants.constants as api_constants from csle_common.metastore.metastore_facade import MetastoreFacade +from flask import Blueprint, Response, jsonify, request + +import csle_rest_api.constants.constants as api_constants import csle_rest_api.util.rest_api_util as rest_api_util +logger = logging.getLogger() + # Creates a blueprint "sub application" of the main REST app emulation_statistics_bp = Blueprint( api_constants.MGMT_WEBAPP.EMULATION_STATISTICS_RESOURCE, __name__, @@ -26,9 +31,9 @@ def emulation_statistics() -> Tuple[Response, int]: if request.method == api_constants.MGMT_WEBAPP.HTTP_REST_DELETE: requires_admin = True authorized = rest_api_util.check_if_user_is_authorized(request=request, requires_admin=requires_admin) + if authorized is not None: return authorized - if request.method == api_constants.MGMT_WEBAPP.HTTP_REST_GET: # Check if ids query parameter is True, then only return the ids and not the whole list of emulation statistics ids = request.args.get(api_constants.MGMT_WEBAPP.IDS_QUERY_PARAM) 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 new file mode 100644 index 000000000..2aa632944 --- /dev/null +++ b/simulation-system/libs/csle-rest-api/tests/test_resources_emulation_statistics.py @@ -0,0 +1,225 @@ +import json +import logging +from typing import Dict, List, Tuple + +import csle_common.constants.constants as constants +import pytest +import pytest_mock +from csle_common.dao.system_identification.emulation_statistics import ( + EmulationStatistics, +) + +import csle_rest_api.constants.constants as api_constants +from csle_rest_api.rest_api import create_app + +logger = logging.getLogger() + + +class TestResourcesEmulationsStatisticsSuite: + """ + Test suite for /emulations-statistics resource + """ + + @pytest.fixture + def flask_app(self): + """ + :return: the flask app fixture representing the webserver + """ + return create_app(static_folder="../../../../../management-system/csle-mgmt-webapp/build") + + @pytest.fixture + def em_stat_ids(self, mocker: pytest_mock.MockFixture): + """ + Pytest fixture for mocking the list_emulation_statistics_ids method + + :param mocker: the pytest mocker object + :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 + + @pytest.fixture + def remove(self, mocker: pytest_mock.MockFixture): + """ + Pytest fixture for mocking the remove_emulation_statistic method + + :param mocker: the pytest mocker object + :return: the mocked function + """ + def remove_emulation_statistic(stat: EmulationStatistics) -> None: + return None + remove_emulation_statistic_mocker = mocker.MagicMock(side_effect=remove_emulation_statistic) + return remove_emulation_statistic_mocker + + @pytest.fixture + def em_stat(self, mocker: pytest_mock.MockFixture): + """ + Pytest fixture for mocking the list_emulation_statistics function + + :param mocker: the pytest mocker object + :return: the mocked function + """ + def list_emulation_statistics() -> List[EmulationStatistics]: + em_stat = TestResourcesEmulationsStatisticsSuite.get_ex_em_stat() + return [em_stat] + list_emulation_statistics_mocker = mocker.MagicMock(side_effect=list_emulation_statistics) + return list_emulation_statistics_mocker + + @pytest.fixture + def get_em_stat(self, mocker): + """ + Pytest fixture for mocking the get_emulation_statistic method + + :param mocker: the pytest mocker object + :return: the mocked function + """ + def get_emulation_statistic(id: int) -> EmulationStatistics: + em_stat = TestResourcesEmulationsStatisticsSuite.get_ex_em_stat() + return em_stat + get_emulation_statistic_mocker = mocker.MagicMock(side_effect=get_emulation_statistic) + return get_emulation_statistic_mocker + + @pytest.fixture + 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 + """ + def get_emulation_statistic(id: int) -> None: + return None + get_emulation_statistic_mocker = mocker.MagicMock(side_effect=get_emulation_statistic) + return get_emulation_statistic_mocker + + @staticmethod + def get_ex_em_stat() -> EmulationStatistics: + """ + Static help method for returning an Emulationstatistic object + :return: an EmulationsStistics 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): + """ + Testing the GET HTTPS method for the /emulation-statistics resource + + :param mocker: the pytest mocker object + :param flask_app: the flask_app fixture + :param em_stat_ids: the em_stat_ids fixture + :param em_stat: the em_stat fixture + """ + mocker.patch("csle_common.metastore.metastore_facade.MetastoreFacade.list_emulation_statistics_ids", + 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) + 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) + 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") + response_data_list = json.loads(response_data) + response_data_dict = response_data_list[0] + assert response.status_code == constants.HTTPS.OK_STATUS_CODE + assert response_data_dict[api_constants.MGMT_WEBAPP.ID_PROPERTY] == 1 + assert response_data_dict[api_constants.MGMT_WEBAPP.EMULATION_PROPERTY] == 1 + response = flask_app.test_client().get(f"{api_constants.MGMT_WEBAPP.EMULATION_STATISTICS_RESOURCE}") + response_data = response.data.decode("utf-8") + response_data_list = json.loads(response_data) + response_data_dict = response_data_list[0] + ex_em_stat = TestResourcesEmulationsStatisticsSuite.get_ex_em_stat() + 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) + 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") + response_data_list = json.loads(response_data) + response_data_dict = response_data_list[0] + assert response.status_code == constants.HTTPS.OK_STATUS_CODE + assert response_data_dict[api_constants.MGMT_WEBAPP.ID_PROPERTY] == 1 + assert response_data_dict[api_constants.MGMT_WEBAPP.EMULATION_PROPERTY] == 1 + response = flask_app.test_client().get(f"{api_constants.MGMT_WEBAPP.EMULATION_STATISTICS_RESOURCE}") + response_data = response.data.decode("utf-8") + response_data_list = json.loads(response_data) + response_data_dict = response_data_list[0] + ex_em_stat = TestResourcesEmulationsStatisticsSuite.get_ex_em_stat() + 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] + + def test_emulation_statistics_delete(self, mocker, flask_app, not_logged_in, logged_in, + logged_in_as_admin, em_stat, remove): + """ + Testing the DELETE HTTPS method for the /emulation-statistics resource + + :param mocker: the pytest mocker object + :param flask_app: the flask_app fixture + :param em_stat_ids: the em_stat_ids fixture + :param em_stat: the em_stat fixture + :param remove: the remove fixture + """ + mocker.patch("csle_common.metastore.metastore_facade.MetastoreFacade.list_emulation_statistics", + 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) + 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) + 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) + 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): + """ + Testing the GET HTTPS method for the /emulation-statistics/id resource + + :param mocker: the pytest mocker object + :param flask_app: the flask_app fixture + :param em_stat_ids: the em_stat_ids fixture + :param em_stat: the em_stat fixture + """ + 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) + 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) From ad24fedc559673435aa1a5da40aceffc6cb818d9 Mon Sep 17 00:00:00 2001 From: nforsg Date: Wed, 9 Aug 2023 08:57:22 +0200 Subject: [PATCH 2/3] testing of emulation_statistics finished --- .../test_resources_emulation_statistics.py | 74 ++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) 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 2aa632944..4f254d7fa 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,6 +1,6 @@ import json import logging -from typing import Dict, List, Tuple +from typing import List, Tuple import csle_common.constants.constants as constants import pytest @@ -223,3 +223,75 @@ def test_emulation_statistics_ids_get(self, mocker, flask_app, not_logged_in, lo 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) + 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) + 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) + ex_em_stat = TestResourcesEmulationsStatisticsSuite.get_ex_em_stat() + 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_common.metastore.metastore_facade.MetastoreFacade.get_emulation_statistic", + side_effect=get_em_stat_none) + 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) + 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) + 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) + ex_em_stat = TestResourcesEmulationsStatisticsSuite.get_ex_em_stat() + 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_common.metastore.metastore_facade.MetastoreFacade.get_emulation_statistic", + side_effect=get_em_stat_none) + 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) + 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): + """ + Testing the DELETE HTTPS method for the /emulation-statistics/id resource + + :param mocker: the pytest mocker object + :param flask_app: the flask_app fixture + :param em_stat_ids: the em_stat_ids fixture + :param em_stat: the em_stat fixture + :param remove: the remove fixture + """ + mocker.patch("csle_common.metastore.metastore_facade.MetastoreFacade.get_emulation_statistic", + 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) + 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) + 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) + 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.OK_STATUS_CODE + assert response_data_dict == {} From f5345b6db1c9108abb4f5243b02cb38092f1204e Mon Sep 17 00:00:00 2001 From: Kim Hammar Date: Wed, 9 Aug 2023 10:19:00 +0200 Subject: [PATCH 3/3] 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)