Skip to content

Commit

Permalink
feat: update user engines
Browse files Browse the repository at this point in the history
* feat: update user engine custom form data

feat: update engine custom form before engine sent

* chore: apply suggestions from code review
  • Loading branch information
johanseto authored Oct 28, 2024
1 parent ddf0d26 commit 74b58da
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
6 changes: 4 additions & 2 deletions eox_nelp/pearson_vue/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
ExamAuthorizationDataImport,
RealTimeImport,
)
from eox_nelp.pearson_vue.utils import update_user_engines

User = get_user_model()

Expand Down Expand Up @@ -129,10 +130,11 @@ 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):
user = User.objects.get(id=user_id)
update_user_engines(user, action_name, exam_id)
action = getattr(PearsonEngineApiClient(), action_key)

response = action(
user=User.objects.get(id=user_id),
user=user,
exam_id=exam_id,
**kwargs
)
Expand Down
46 changes: 35 additions & 11 deletions eox_nelp/pearson_vue/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,76 +118,98 @@ 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="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)

@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="cdd", **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)

@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="ead", **self.kwargs)
real_time_import_task_v2(self.user.id, exam_id=self.exam_id, action_name=action_name, **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 +219,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))

0 comments on commit 74b58da

Please sign in to comment.