Skip to content

Commit

Permalink
Test coverage for integrations with empty endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
marianobrc committed Dec 27, 2024
1 parent d3723b9 commit 15c94c2
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
63 changes: 63 additions & 0 deletions app/actions/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import asyncio

import httpx
import pytest
from erclient import ERClientException
from gundi_core.schemas.v2 import Integration
Expand Down Expand Up @@ -55,6 +57,51 @@ def er_integration_v2():
)


@pytest.fixture
def er_integration_v2_with_empty_url():
return Integration.parse_obj(
{'id': '779ff3ab-5589-4f4c-9e0a-ae8d6c9edff0', 'name': 'Gundi ER', 'base_url': 'https:///',
'enabled': True,
'type': {'id': '50229e21-a9fe-4caa-862c-8592dfb2479b', 'name': 'EarthRanger', 'value': 'earth_ranger',
'description': 'Integration type for Earth Ranger Sites', 'actions': [
{'id': '80448d1c-4696-4b32-a59f-f3494fc949ac', 'type': 'auth', 'name': 'Authenticate', 'value': 'auth',
'description': 'Authenticate against Earth Ranger',
'schema': {'type': 'object', 'required': ['token'], 'properties': {'token': {'type': 'string'}}}},
{'id': '4b721b37-f4ca-4f20-b07c-2caadb095ecb', 'type': 'pull', 'name': 'Pull Events',
'value': 'pull_events', 'description': 'Extract events from EarthRanger sites',
'schema': {'type': 'object', 'title': 'PullObservationsConfig', 'required': ['start_datetime'],
'properties': {'start_datetime': {'type': 'string', 'title': 'Start Datetime'}}}},
{'id': '75b3040f-ab1f-42e7-b39f-8965c088b154', 'type': 'pull', 'name': 'Pull Observations',
'value': 'pull_observations', 'description': 'Extract observations from an EarthRanger Site',
'schema': {'type': 'object', 'title': 'PullObservationsConfig', 'required': ['start_datetime'],
'properties': {'start_datetime': {'type': 'string', 'title': 'Start Datetime'}}}},
{'id': '425a2e2f-ae71-44fb-9314-bc0116638e4f', 'type': 'push', 'name': 'Push Event Attachments',
'value': 'push_event_attachments',
'description': 'EarthRanger sites support adding attachments to events', 'schema': {}},
{'id': '8e101f31-e693-404c-b6ee-20fde6019f16', 'type': 'push', 'name': 'Push Events',
'value': 'push_events', 'description': 'EarthRanger sites support sending Events (a.k.a Reports)',
'schema': {}}]},
'owner': {'id': 'a91b400b-482a-4546-8fcb-ee42b01deeb6', 'name': 'Test Org', 'description': ''},
'configurations': [
{'id': '5577c323-b961-4277-9047-b1f27fd6a1b7', 'integration': '779ff3ab-5589-4f4c-9e0a-ae8d6c9edff0',
'action': {'id': '75b3040f-ab1f-42e7-b39f-8965c088b154', 'type': 'pull', 'name': 'Pull Observations',
'value': 'pull_observations'},
'data': {'end_datetime': '2023-11-10T06:00:00-00:00', 'start_datetime': '2023-11-10T05:30:00-00:00',
'force_run_since_start': False}},
{'id': '431af42b-c431-40af-8b57-a349253e15df', 'integration': '779ff3ab-5589-4f4c-9e0a-ae8d6c9edff0',
'action': {'id': '4b721b37-f4ca-4f20-b07c-2caadb095ecb', 'type': 'pull', 'name': 'Pull Events',
'value': 'pull_events'}, 'data': {'start_datetime': '2023-11-16T00:00:00-03:00'}},
{'id': '30f8878c-4a98-4c95-88eb-79f73c40fb2f', 'integration': '779ff3ab-5589-4f4c-9e0a-ae8d6c9edff0',
'action': {'id': '80448d1c-4696-4b32-a59f-f3494fc949ac', 'type': 'auth', 'name': 'Authenticate',
'value': 'auth'}, 'data': {'token': 'testtoken2a97022f21732461ee103a08fac8a35'}}],
'additional': {'topic': 'gundi-er-dispatcher-iD9M1ON-topic', 'broker': 'gcp_pubsub'},
'default_route': {'id': '5abf3845-7c9f-478a-bc0f-b24d87038c4b', 'name': 'Gundi ER Provider - Default Route'},
'status': 'healthy',
'status_details': ''
}
)


@pytest.fixture
def mock_gundi_client_v2(
mocker,
Expand Down Expand Up @@ -128,20 +175,36 @@ def er_generic_exception():
)


@pytest.fixture
def er_connect_error():
return httpx.ConnectError("[Errno -3] Temporary failure in name resolution")


@pytest.fixture
def er_read_timeout_error():
return httpx.ReadTimeout("Read timeout")


@pytest.fixture
def mock_erclient_class_with_error(
request,
mocker,
er_401_exception,
er_500_exception,
er_generic_exception,
er_connect_error,
er_read_timeout_error,
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
elif request.param == "er_connect_error":
er_error = er_connect_error
elif request.param == "er_read_timeout_error":
er_error = er_read_timeout_error
else:
er_error = er_generic_exception
mocked_erclient_class = mocker.MagicMock()
Expand Down
28 changes: 28 additions & 0 deletions app/actions/tests/test_actions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pytest

from app.conftest import async_return
from app.services.action_runner import execute_action


Expand Down Expand Up @@ -28,6 +30,8 @@ async def test_execute_auth_action_with_valid_credentials(
"er_401_exception",
"er_500_exception",
"er_generic_exception",
"er_connect_error",
"er_read_timeout_error",
],
indirect=["mock_erclient_class_with_error"])
@pytest.mark.asyncio
Expand Down Expand Up @@ -103,3 +107,27 @@ async def test_execute_pull_observations_action(
assert mock_erclient_class.return_value.get_observations.called
assert mock_gundi_sensors_client_class.return_value.post_observations.call_count == 2
assert response == {"observations_extracted": len(observations_batch_one) + len(observations_batch_one)}


@pytest.mark.asyncio
async def test_execute_auth_action_with_invalid_url(
mocker, mock_gundi_client_v2, mock_erclient_class,
er_integration_v2_with_empty_url, mock_publish_event
):
mock_gundi_client_v2.get_integration_details.return_value = async_return(
er_integration_v2_with_empty_url
)
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)

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

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

0 comments on commit 15c94c2

Please sign in to comment.