Skip to content

Commit

Permalink
Test coverage for test credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
marianobrc committed Dec 24, 2024
1 parent 4ca8371 commit 4976d17
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 2 deletions.
101 changes: 101 additions & 0 deletions app/actions/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import pytest
from erclient import ERClientException
from gundi_core.schemas.v2 import Integration


Expand Down Expand Up @@ -105,6 +106,106 @@ def mock_erclient_class(
return mocked_erclient_class



@pytest.fixture
def er_401_exception():
return ERClientException(
'Failed to GET to ER web service. provider_key: None, service: https://gundi-dev.staging.pamdas.org/api/v1.0, path: user/me,\n\t 401 from ER. Message: Authentication credentials were not provided. {"status":{"code":401,"message":"Unauthorized","detail":"Authentication credentials were not provided."}}'
)


@pytest.fixture
def er_500_exception():
return ERClientException(
'Failed to GET to ER web service. provider_key: None, service: https://gundi-dev.staging.pamdas.org/api/v1.0, path: user/me,\n\t 500 from ER. Message: duplicate key value violates unique constraint "observations_observation_tenant_source_at_unique"'
)


@pytest.fixture
def er_generic_exception():
return ERClientException(
'Failed to GET to ER web service. provider_key: None, service: https://gundi-dev.staging.pamdas.org/api/v1.0, path: user/me,\n\t Error from ER. Message: Something went wrong'
)


@pytest.fixture
def mock_erclient_class_with_error(
request,
mocker,
er_401_exception,
er_500_exception,
er_generic_exception,
er_client_close_response
):

if request.param == "er_401_exception":
er_error = er_401_exception
elif request.param == "er_500_exception":
er_error = er_500_exception
else:
er_error = er_generic_exception
mocked_erclient_class = mocker.MagicMock()
erclient_mock = mocker.MagicMock()
erclient_mock.get_me.side_effect = er_error
erclient_mock.auth_headers.side_effect = er_error
erclient_mock.get_events.side_effect = er_error
erclient_mock.get_observations.side_effect = er_error
erclient_mock.close.return_value = async_return(
er_client_close_response
)
erclient_mock.__aenter__.return_value = erclient_mock
erclient_mock.__aexit__.return_value = er_client_close_response
mocked_erclient_class.return_value = erclient_mock
return mocked_erclient_class



@pytest.fixture
def mock_erclient_class_with_auth_401(
mocker,
auth_headers_response,
er_401_exception,

):
mocked_erclient_class = mocker.MagicMock()
erclient_mock = mocker.MagicMock()
erclient_mock.get_me.side_effect = er_401_exception
erclient_mock.auth_headers.side_effect = er_401_exception
erclient_mock.get_events.side_effect = er_401_exception
erclient_mock.get_observations.side_effect = er_401_exception
erclient_mock.close.return_value = async_return(
er_client_close_response
)
erclient_mock.__aenter__.return_value = erclient_mock
erclient_mock.__aexit__.return_value = er_client_close_response
mocked_erclient_class.return_value = erclient_mock
return mocked_erclient_class


@pytest.fixture
def mock_erclient_class_with_auth_500(
mocker,
auth_headers_response,
er_500_exception,
get_events_response,
get_observations_response,
er_client_close_response
):
mocked_erclient_class = mocker.MagicMock()
erclient_mock = mocker.MagicMock()
erclient_mock.get_me.side_effect = er_500_exception
erclient_mock.auth_headers.side_effect = er_500_exception
erclient_mock.get_events.side_effect = er_500_exception
erclient_mock.get_observations.side_effect = er_500_exception
erclient_mock.close.return_value = async_return(
er_client_close_response
)
erclient_mock.__aenter__.return_value = erclient_mock
erclient_mock.__aexit__.return_value = er_client_close_response
mocked_erclient_class.return_value = erclient_mock
return mocked_erclient_class


@pytest.fixture
def mock_gundi_sensors_client_class(mocker, events_created_response, observations_created_response):
mock_gundi_sensors_client_class = mocker.MagicMock()
Expand Down
33 changes: 31 additions & 2 deletions app/actions/tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


@pytest.mark.asyncio
async def test_execute_auth_action(
async def test_execute_auth_action_with_valid_credentials(
mocker, mock_gundi_client_v2, mock_erclient_class, er_integration_v2,
mock_publish_event
):
Expand All @@ -19,7 +19,36 @@ async def test_execute_auth_action(

assert mock_gundi_client_v2.get_integration_details.called
assert mock_erclient_class.return_value.get_me.called
assert response == {"valid_credentials": True}
assert response.get("valid_credentials") == True


@pytest.mark.parametrize(
"mock_erclient_class_with_error",
[
"er_401_exception",
"er_500_exception",
"er_generic_exception",
],
indirect=["mock_erclient_class_with_error"])
@pytest.mark.asyncio
async def test_execute_auth_action_with_invalid_credentials(
mocker, mock_gundi_client_v2, er_integration_v2,
mock_publish_event, mock_erclient_class_with_error
):
mocker.patch("app.services.action_runner._portal", mock_gundi_client_v2)
mocker.patch("app.services.activity_logger.publish_event", mock_publish_event)
mocker.patch("app.services.action_runner.publish_event", mock_publish_event)
mocker.patch("app.actions.handlers.AsyncERClient", mock_erclient_class_with_error)

response = await execute_action(
integration_id=str(er_integration_v2.id),
action_id="auth"
)

assert mock_gundi_client_v2.get_integration_details.called
assert mock_erclient_class_with_error.return_value.get_me.called
assert response.get("valid_credentials") == False
assert "error" in response


@pytest.mark.asyncio
Expand Down

0 comments on commit 4976d17

Please sign in to comment.