Skip to content

Commit

Permalink
feat: add audit for succesfull imports
Browse files Browse the repository at this point in the history
  • Loading branch information
johanseto committed Jul 8, 2024
1 parent 115e025 commit 342bb01
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
2 changes: 2 additions & 0 deletions eox_nelp/api_clients/pearson_rti.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def import_candidate_demographics(self, payload):
"message": xml_response.find("message").text,
"candidate_id": xml_response.find("cdd:cddResponse").attrs.get("candidateID"),
"client_candidate_id": xml_response.find("cdd:cddResponse").attrs.get("clientCandidateID"),
"import_type": "cdd",
}

LOGGER.error(
Expand Down Expand Up @@ -131,6 +132,7 @@ def import_exam_authorization(self, payload):
"status": status.text.lower(),
"message": xml_response.find("message").text if xml_response.find("message") else "",
"client_candidate_id": xml_response.find("clientCandidateID").text,
"import_type": "ead",
}

LOGGER.error(
Expand Down
2 changes: 2 additions & 0 deletions eox_nelp/api_clients/tests/test_pearson_rti.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def test_accepted_status_cdd(self, post_mock):
"message": "ok",
"candidate_id": "12345",
"client_candidate_id": "CC1234",
"import_type": "cdd",
}
post_mock.return_value = """
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"
Expand Down Expand Up @@ -200,6 +201,7 @@ def test_accepted_status_ead(self, post_mock):
"status": "accepted",
"message": "ok",
"client_candidate_id": "CC1234",
"import_type": "ead",
}
post_mock.return_value = """
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"
Expand Down
34 changes: 32 additions & 2 deletions eox_nelp/pearson_vue/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ def import_candidate_demographics(cdd_request, **kwargs): # pylint: disable=unu
response = api_client.import_candidate_demographics(payload)

if response.get("status", "error") == "accepted":
return response
audit_sucessfull_import(import_response=response, cdd_request=cdd_request)
return {"import_response": response}

raise PearsonImportError(
exception_reason=f"Import candidate demographics pipeline has failed with the following response: {response}",
Expand Down Expand Up @@ -276,7 +277,8 @@ def import_exam_authorization(ead_request, **kwargs): # pylint: disable=unused-
response = api_client.import_exam_authorization(payload)

if response.get("status", "error") == "accepted":
return response
audit_sucessfull_import(import_response=response, ead_request=ead_request)
return {"import_response": response}

raise PearsonImportError(
exception_reason=f"Import exam authorization pipeline has failed with the following response: {response}",
Expand Down Expand Up @@ -613,3 +615,31 @@ def get_enrollment_from_id(enrollment_id, enrollment=None, **kwargs): # pylint:
pass

return {}


def audit_sucessfull_import(import_response, cdd_request=None, ead_request=None):
"""This is not a pipe step, could be but its no the intention because inside a pipeline could
be more than one import. so this is a helper for pipelines.
Args:
import_response (dict): response from client api with import_type key
cdd_request (dict, optional): Defaults to None.
ead_request (dict, optional): Defaults to None.
"""
import_type = import_response.get("import_type", "")

@audit_method(action=f"Pearson Vue Succesfull Import: {import_type.upper}")
@rename_function(name=f"audit_accepted_import_{import_type}")
def audit_accepted_import(import_response, cdd_request, ead_request):
logger.info(
"Succesfull import: %s.\n cdd_request: %s. ead_request:%s",
import_response,
cdd_request,
ead_request,
)

audit_accepted_import(
import_response=import_response,
cdd_request=cdd_request,
ead_request=ead_request,
)
57 changes: 57 additions & 0 deletions eox_nelp/pearson_vue/tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
)
from eox_nelp.pearson_vue.pipeline import (
audit_pearson_error,
audit_sucessfull_import,
build_cdd_request,
build_ead_request,
check_service_availability,
Expand Down Expand Up @@ -1231,3 +1232,59 @@ def test_get_enrollment_with_provided_enrollment(self):

# Verify mock calls
CourseEnrollment.objects.get.assert_not_called()


class TestAuditSuccesfullImport(unittest.TestCase):
"""
Unit tests for the audit_sucessfull_import method.
"""

def test_audit_succesfull_cdd(self):
"""Test correct behaviour calling audit_sucessfull_import.
Expected behavior:
- The result is the expected value(None).
- Expected log info.
"""
import_response = {
"status": "accepted",
"message": "ok",
"client_candidate_id": "CC1234",
"import_type": "cdd",
}
cdd_request = {}
log_info = [
f"INFO:{pipeline.__name__}:"
f"Succesfull import: {import_response}.\n cdd_request: {cdd_request}. ead_request:{None}"

]

with self.assertLogs(pipeline.__name__, level="INFO") as logs:
self.assertIsNone(audit_sucessfull_import(import_response=import_response, cdd_request=cdd_request))

self.assertListEqual(log_info, logs.output)

def test_audit_succesfull_ead(self):
"""Test correct behaviour calling audit_sucessfull_import.
Expected behavior:
- The result is the expected value(None).
- Expected log info.
"""
import_response = {
"status": "accepted",
"message": "ok",
"client_candidate_id": "CC1234",
"import_type": "ead",
}
ead_request = {}
log_info = [
f"INFO:{pipeline.__name__}:"
f"Succesfull import: {import_response}.\n cdd_request: {None}. ead_request:{ead_request}"

]

with self.assertLogs(pipeline.__name__, level="INFO") as logs:
self.assertIsNone(audit_sucessfull_import(import_response=import_response, ead_request=ead_request))

self.assertListEqual(log_info, logs.output)

0 comments on commit 342bb01

Please sign in to comment.