Skip to content

Commit

Permalink
feat: update engine custom form before engine sent
Browse files Browse the repository at this point in the history
  • Loading branch information
johanseto committed Oct 1, 2024
1 parent 7a2ca46 commit e7f53fa
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
6 changes: 3 additions & 3 deletions eox_nelp/pearson_vue/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
ExamAuthorizationDataImport,
RealTimeImport,
)
from eox_nelp.pearson_vue.utils import update_user_engine_custom_form
from eox_nelp.pearson_vue.utils import update_user_engines

User = get_user_model()

Expand Down Expand Up @@ -130,9 +130,9 @@ def real_time_import_task_v2(user_id, exam_id=None, action_name="rti", **kwargs)
@audit_method(action="Pearson Engine Action")
@rename_function(name=action_key)
def audit_pearson_engine_action(user_id, exam_id, action_key, **kwargs):
action = getattr(PearsonEngineApiClient(), action_key)
user = User.objects.get(id=user_id)
update_user_engine_custom_form(user, action_name, exam_id)
update_user_engines(user, action_name, exam_id)
action = getattr(PearsonEngineApiClient(), action_key)
response = action(
user=user,
exam_id=exam_id,
Expand Down
49 changes: 35 additions & 14 deletions eox_nelp/pearson_vue/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,76 +118,95 @@ def setUp(self):
self.exam_id = "exam123"
self.kwargs = {"extra_info": "test"}

@patch("eox_nelp.pearson_vue.tasks.update_user_engines")
@patch("eox_nelp.pearson_vue.tasks.PearsonEngineApiClient")
def test_real_time_import_rti(self, mock_api_client):
def test_real_time_import_rti(self, mock_api_client, update_user_engines_mock):
"""Test real-time import action using the Pearson Engine API.
Expected behavior:
- update_user_engines is called with correct parameters.
- 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})
action_name = "rti"
real_time_import_task_v2(self.user.id, action_name=action_name, **self.kwargs)

real_time_import_task_v2(self.user.id, action_name="rti", **self.kwargs)

update_user_engines_mock.assert_called_once_with(self.user, action_name, None)
mock_action.assert_called_once_with(user=self.user, exam_id=None, **self.kwargs)

@patch("eox_nelp.pearson_vue.tasks.update_user_engines")
@patch("eox_nelp.pearson_vue.tasks.PearsonEngineApiClient")
def test_real_time_import_cdd(self, mock_api_client):
def test_real_time_import_cdd(self, mock_api_client, update_user_engines_mock):
"""Test candidate demographics import action using the Pearson Engine API.
Expected behavior:
- update_user_engines is called with correct parameters.
- 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})
action_name = "cdd"
real_time_import_task_v2(self.user.id, action_name=action_name, **self.kwargs)

real_time_import_task_v2(self.user.id, action_name="cdd", **self.kwargs)

update_user_engines_mock.assert_called_once_with(self.user, action_name, None)
mock_action.assert_called_once_with(user=self.user, exam_id=None, **self.kwargs)

@patch("eox_nelp.pearson_vue.tasks.update_user_engines")
@patch("eox_nelp.pearson_vue.tasks.PearsonEngineApiClient")
def test_real_time_import_ead(self, mock_api_client):
def test_real_time_import_ead(self, mock_api_client, update_user_engines_mock):
"""Test exam authorization import action using the Pearson Engine API.
Expected behavior:
- update_user_engines is called with correct parameters.
- 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})
action_name = "ead"
real_time_import_task_v2(self.user.id, exam_id=self.exam_id, action_name=action_name, **self.kwargs)

real_time_import_task_v2(self.user.id, exam_id=self.exam_id, action_name="ead", **self.kwargs)

update_user_engines_mock.assert_called_once_with(self.user, action_name, self.exam_id,)
mock_action.assert_called_once_with(user=self.user, exam_id=self.exam_id, **self.kwargs)

def test_real_time_import_invalid_action(self):
@patch("eox_nelp.pearson_vue.tasks.update_user_engines")
def test_real_time_import_invalid_action(self, update_user_engines_mock):
"""Test that a KeyError is raised for an invalid action name.
Expected behavior:
- KeyError is raised when an invalid action name is provided.
- update_user_engines is not called
"""
with self.assertRaises(KeyError):
real_time_import_task_v2(self.user.id, action_name="invalid_action")
update_user_engines_mock.assert_not_called()

@patch("eox_nelp.pearson_vue.tasks.update_user_engines")
@patch('eox_nelp.pearson_vue.tasks.PearsonEngineApiClient')
def test_real_time_import_user_not_found(self, mock_api_client): # pylint: disable=unused-argument
def test_real_time_import_user_not_found(self, mock_api_client, update_user_engines_mock):
"""Test that a DoesNotExist is raised for an invalid user id.
Expected behavior:
- DoesNotExist is raised when an invalid usser id is provided.
- update_user_engines is not called
- PearsonEngineApiClient is not called
- DoesNotExist is raised when an invalid user id is provided.
"""
with self.assertRaises(User.DoesNotExist):
real_time_import_task_v2(12345678, action_name="rti")
mock_api_client.assert_not_called()
update_user_engines_mock.assert_not_called()

@patch("eox_nelp.pearson_vue.tasks.update_user_engines")
@patch("eox_nelp.pearson_vue.tasks.PearsonEngineApiClient")
def test_raise_exception_on_error_response(self, mock_api_client):
def test_raise_exception_on_error_response(self, mock_api_client, update_user_engines_mock):
"""Test that an exception is raised when the API response contains an Error.
Expected behavior:
- The exception is raised.
- update_user_engines is called with correct parameters.
- The action method is called with the correct parameters.
- Exception contains the expected message.
"""
Expand All @@ -197,10 +216,12 @@ def test_raise_exception_on_error_response(self, mock_api_client):
"error": True,
"message": expected_message,
}
action_name = "rti"
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)
real_time_import_task_v2(self.user.id, action_name=action_name, **self.kwargs)

update_user_engines_mock.assert_called_once_with(self.user, action_name, None)
mock_action.assert_called_once_with(user=self.user, exam_id=None, **self.kwargs)
self.assertEqual(expected_message, str(context.exception))
1 change: 0 additions & 1 deletion eox_nelp/pearson_vue/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"""
import re

from custom_reg_form.models import PearsonEngine
import xmltodict
from pydantic.v1.utils import deep_update

Expand Down

0 comments on commit e7f53fa

Please sign in to comment.