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

refactor: use PearsonImportError for imports #185

Merged
merged 1 commit into from
Jul 3, 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
7 changes: 7 additions & 0 deletions eox_nelp/pearson_vue/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,10 @@ class PearsonValidationError(PearsonBaseError):
"""Pearson Validation error class
"""
exception_type = "validation-error"


class PearsonImportError(PearsonBaseError):
"""Pearson Import error class.
Error related with a failure response from Pearson Vue site.
"""
exception_type = "import-error"
101 changes: 45 additions & 56 deletions eox_nelp/pearson_vue/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from eox_nelp.pearson_vue.exceptions import (
PearsonAttributeError,
PearsonBaseError,
PearsonImportError,
PearsonKeyError,
PearsonValidationError,
)
Expand Down Expand Up @@ -200,41 +201,35 @@ def import_candidate_demographics(cdd_request, **kwargs): # pylint: disable=unu
Raises:
Exception: If the Pearson VUE RTI service does not accept the import request.
"""
@audit_method(action="Import Candidate Demographics")
def import_candidate_demographics_request(cdd_request):
"""This is a wrapper that allows to make audit-able the import_candidate_demographics method."""
api_client = PearsonRTIApiClient()
payload = {
"soapenv:Envelope": {
"soapenv:Header": {
"wsse:Security": {
"wsse:UsernameToken": {
"wsse:Username": getattr(settings, "PEARSON_RTI_WSDL_USERNAME"),
"wsse:Password": {
"#text": getattr(settings, "PEARSON_RTI_WSDL_PASSWORD")
},
api_client = PearsonRTIApiClient()
payload = {
"soapenv:Envelope": {
"soapenv:Header": {
"wsse:Security": {
"wsse:UsernameToken": {
"wsse:Username": getattr(settings, "PEARSON_RTI_WSDL_USERNAME"),
"wsse:Password": {
"#text": getattr(settings, "PEARSON_RTI_WSDL_PASSWORD")
},
},
},
"soapenv:Body": {
"sch:cddRequest": cdd_request
},
},
}
payload = update_xml_with_dict(PAYLOAD_CDD, payload)

response = api_client.import_candidate_demographics(payload)
"soapenv:Body": {
"sch:cddRequest": cdd_request
},
},
}
payload = update_xml_with_dict(PAYLOAD_CDD, payload)

if response.get("status", "error") == "accepted":
return response
response = api_client.import_candidate_demographics(payload)

logger.info("Import candidate demographics pipeline has failed with the following response: %s", response)
# pylint: disable=broad-exception-raised
raise Exception(
response.get("message", "Error trying to process import candidate demographics request.")
)
if response.get("status", "error") == "accepted":
return response

import_candidate_demographics_request(cdd_request)
raise PearsonImportError(
exception_reason=f"Import candidate demographics pipeline has failed with the following response: {response}",
pipe_frame=inspect.currentframe()
)


def import_exam_authorization(ead_request, **kwargs): # pylint: disable=unused-argument
Expand All @@ -256,40 +251,34 @@ def import_exam_authorization(ead_request, **kwargs): # pylint: disable=unused-
Raises:
Exception: If the Pearson VUE RTI service does not accept the import request.
"""
@audit_method(action="Import Exam Authorization")
def import_exam_authorization_request(ead_request):
"""This is a wrapper that allows to make audit-able the import_exam_authorization method."""
api_client = PearsonRTIApiClient()
payload = {
"soapenv:Envelope": {
"soapenv:Header": {
"wsse:Security": {
"wsse:UsernameToken": {
"wsse:Username": getattr(settings, "PEARSON_RTI_WSDL_USERNAME"),
"wsse:Password": {
"#text": getattr(settings, "PEARSON_RTI_WSDL_PASSWORD")
},
api_client = PearsonRTIApiClient()
payload = {
"soapenv:Envelope": {
"soapenv:Header": {
"wsse:Security": {
"wsse:UsernameToken": {
"wsse:Username": getattr(settings, "PEARSON_RTI_WSDL_USERNAME"),
"wsse:Password": {
"#text": getattr(settings, "PEARSON_RTI_WSDL_PASSWORD")
},
},
},
"soapenv:Body": {
"sch:eadRequest": ead_request
},
},
}
payload = update_xml_with_dict(PAYLOAD_EAD, payload)
response = api_client.import_exam_authorization(payload)

if response.get("status", "error") == "accepted":
return response
"soapenv:Body": {
"sch:eadRequest": ead_request
},
},
}
payload = update_xml_with_dict(PAYLOAD_EAD, payload)
response = api_client.import_exam_authorization(payload)

logger.info("Import exam authorization pipeline has failed with the following response: %s", response)
# pylint: disable=broad-exception-raised
raise Exception(
response.get("message", "Error trying to process import exam authorization request.")
)
if response.get("status", "error") == "accepted":
return response

import_exam_authorization_request(ead_request)
raise PearsonImportError(
exception_reason=f"Import exam authorization pipeline has failed with the following response: {response}",
pipe_frame=inspect.currentframe(),
)


def get_exam_data(user_id, course_id, **kwargs): # pylint: disable=unused-argument
Expand Down
31 changes: 22 additions & 9 deletions eox_nelp/pearson_vue/tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
from eox_nelp.edxapp_wrapper.student import CourseEnrollment, anonymous_id_for_user
from eox_nelp.pearson_vue import pipeline
from eox_nelp.pearson_vue.constants import PAYLOAD_CDD, PAYLOAD_EAD, PAYLOAD_PING_DATABASE
from eox_nelp.pearson_vue.exceptions import PearsonAttributeError, PearsonKeyError, PearsonValidationError
from eox_nelp.pearson_vue.exceptions import (
PearsonAttributeError,
PearsonImportError,
PearsonKeyError,
PearsonValidationError,
)
from eox_nelp.pearson_vue.pipeline import (
audit_pearson_error,
build_cdd_request,
Expand Down Expand Up @@ -417,13 +422,14 @@ def test_import_candidate_demographics_failure(self, mock_update_xml_with_dict,
when the import request is not accepted.
Expected behavior:
- Function raises an exception.
- Function raises a PearsonImportError exception.
- Exception message is the expected.
- update_xml_with_dict was called with the right parameters.
- import_candidate_demographics method was called with the result of update_xml_with_dict.
"""
mock_update_xml_with_dict.return_value = 'updated_payload'
mock_api_client.return_value.import_candidate_demographics.return_value = {"status": "error"}
response_return = {"status": "error"}
mock_api_client.return_value.import_candidate_demographics.return_value = response_return
input_data = {
"cdd_request": self.cdd_request
}
Expand All @@ -445,10 +451,13 @@ def test_import_candidate_demographics_failure(self, mock_update_xml_with_dict,
},
}

with self.assertRaises(Exception) as context:
with self.assertRaises(PearsonImportError) as context:
import_candidate_demographics(**input_data)

self.assertEqual(str(context.exception), "Error trying to process import candidate demographics request.")
self.assertEqual(
context.exception.exception_reason,
f"Import candidate demographics pipeline has failed with the following response: {response_return}"
)
mock_update_xml_with_dict.assert_called_once_with(PAYLOAD_CDD, expected_payload)
mock_api_client.return_value.import_candidate_demographics.assert_called_once_with(mock_update_xml_with_dict())

Expand Down Expand Up @@ -509,13 +518,14 @@ def test_import_exam_authorization_failure(self, mock_update_xml_with_dict, mock
Test that the import_exam_authorization function raises an exception when the import request is not accepted.
Expected behavior:
- Function raises an exception.
- Function raises a PearsonImportError exception.
- Exception message is the expected.
- update_xml_with_dict was called with the right parameters.
- import_exam_authorization method was called with the result of update_xml_with_dict.
"""
mock_update_xml_with_dict.return_value = 'updated_payload'
mock_api_client.return_value.import_exam_authorization.return_value = {"status": "error"}
response_return = {"status": "error"}
mock_api_client.return_value.import_exam_authorization.return_value = response_return
input_data = {
"ead_request": self.ead_request
}
Expand All @@ -537,10 +547,13 @@ def test_import_exam_authorization_failure(self, mock_update_xml_with_dict, mock
},
}

with self.assertRaises(Exception) as context:
with self.assertRaises(PearsonImportError) as context:
import_exam_authorization(**input_data)

self.assertEqual(str(context.exception), "Error trying to process import exam authorization request.")
self.assertEqual(
context.exception.exception_reason,
f"Import exam authorization pipeline has failed with the following response: {response_return}",
)
mock_update_xml_with_dict.assert_called_once_with(PAYLOAD_EAD, expected_payload)
mock_api_client.return_value.import_exam_authorization.assert_called_once_with(mock_update_xml_with_dict())

Expand Down
Loading