Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: raise exception on api response error #215

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions eox_nelp/pearson_vue/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
Functions:
real_time_import_task(data: dict) -> None: Performs an asynchronous call to the RTI service.
"""

from celery import shared_task
from django.contrib.auth import get_user_model
from requests import exceptions

from eox_nelp.api_clients.pearson_engine import PearsonEngineApiClient
from eox_nelp.pearson_vue.constants import ALLOWED_RTI_ACTIONS
Expand Down Expand Up @@ -101,7 +101,7 @@ def rti_error_handler_task(self, pipeline_index=0, **kwargs):
self.retry(exc=exc, kwargs=error_rti.backend_data)


@shared_task
@shared_task(autoretry_for=(exceptions.Timeout, exceptions.ConnectionError), retry_backoff=5)
def real_time_import_task_v2(user_id, exam_id=None, action_name="rti", **kwargs):
"""
Asynchronous task to perform a real-time import action using the Pearson Engine API.
Expand Down Expand Up @@ -131,10 +131,13 @@ def real_time_import_task_v2(user_id, exam_id=None, action_name="rti", **kwargs)
def audit_pearson_engine_action(user_id, exam_id, action_key, **kwargs):
action = getattr(PearsonEngineApiClient(), action_key)

action(
response = action(
user=User.objects.get(id=user_id),
exam_id=exam_id,
**kwargs
)

if response.get("error"):
raise Exception(response.get("message", "Unknown error")) # pylint: disable=broad-exception-raised

audit_pearson_engine_action(user_id, exam_id, action_key, **kwargs)
26 changes: 26 additions & 0 deletions eox_nelp/pearson_vue/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def test_real_time_import_rti(self, mock_api_client):
- The real_time_import method is called with the correct parameters.
"""
mock_action = MagicMock()
mock_action.return_value = {"error": False}
mock_api_client.return_value = MagicMock(**{"real_time_import": mock_action})

real_time_import_task_v2(self.user.id, action_name="rti", **self.kwargs)
Expand All @@ -140,6 +141,7 @@ def test_real_time_import_cdd(self, mock_api_client):
- The import_candidate_demographics method is called with the correct parameters.
"""
mock_action = MagicMock()
mock_action.return_value = {"error": False}
mock_api_client.return_value = MagicMock(**{"import_candidate_demographics": mock_action})

real_time_import_task_v2(self.user.id, action_name="cdd", **self.kwargs)
Expand All @@ -154,6 +156,7 @@ def test_real_time_import_ead(self, mock_api_client):
- The import_exam_authorization method is called with the correct parameters.
"""
mock_action = MagicMock()
mock_action.return_value = {"error": False}
mock_api_client.return_value = MagicMock(**{"import_exam_authorization": mock_action})

real_time_import_task_v2(self.user.id, exam_id=self.exam_id, action_name="ead", **self.kwargs)
Expand All @@ -178,3 +181,26 @@ def test_real_time_import_user_not_found(self, mock_api_client): # pylint: disa
"""
with self.assertRaises(User.DoesNotExist):
real_time_import_task_v2(12345678, action_name="rti")

@patch("eox_nelp.pearson_vue.tasks.PearsonEngineApiClient")
def test_raise_exception_on_error_response(self, mock_api_client):
"""Test that an exception is raised when the API response contains an Error.

Expected behavior:
- The exception is raised.
- The action method is called with the correct parameters.
- Exception contains the expected message.
"""
mock_action = MagicMock()
expected_message = "Timeout error"
mock_action.return_value = {
"error": True,
"message": expected_message,
}
mock_api_client.return_value = MagicMock(**{"real_time_import": mock_action})

with self.assertRaises(Exception) as context:
real_time_import_task_v2(self.user.id, action_name="rti", **self.kwargs)

mock_action.assert_called_once_with(user=self.user, exam_id=None, **self.kwargs)
self.assertEqual(expected_message, str(context.exception))
Loading