Skip to content

Commit

Permalink
add type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
Limmen committed Aug 9, 2023
1 parent ad24fed commit f5345b6
Showing 1 changed file with 25 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -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:
"""
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit f5345b6

Please sign in to comment.