diff --git a/application/fhir_logging_client/service.py b/application/fhir_logging_client/service.py index c5e4ea0..6001e11 100644 --- a/application/fhir_logging_client/service.py +++ b/application/fhir_logging_client/service.py @@ -41,8 +41,8 @@ def register_idp_interaction(entity_what_reference: str, requesting_client_id: s def _get_audit_event(entity_what_reference: str, requesting_client_id: str, trace_headers: dict): entity_type = entity_what_reference.split("/")[0] - if entity_type != "Patient" and entity_type != "Practitioner": - raise Exception(f"Cannot log IDP interaction - Entity type must be Patient or Practitioner. Got [{entity_type}] instead.") + if entity_type != "Patient" and entity_type != "Practitioner" and entity_type != "RelatedPerson": + raise Exception(f"Cannot log IDP interaction - Entity type must be Patient, Practitioner or RelatedPerson. Got [{entity_type}] instead.") extension_ = [] if 'X-Request-Id' in trace_headers: @@ -113,8 +113,8 @@ def _get_audit_event(entity_what_reference: str, requesting_client_id: str, trac }, "role": { "system": "http://terminology.hl7.org/CodeSystem/object-role", - "code": f"{'1' if entity_type == 'Patient' else '15'}", - "display": entity_type + "code": f"{'1' if entity_type == 'Patient' else '6' if entity_type == 'RelatedPerson' else '15'}", + "display":f"{'User' if entity_type == 'RelatedPerson' else entity_type }", } } ] diff --git a/test/test_fhir_logging_service.py b/test/test_fhir_logging_service.py index f8ff283..e84921e 100644 --- a/test/test_fhir_logging_service.py +++ b/test/test_fhir_logging_service.py @@ -77,6 +77,7 @@ def test_happy(mock1, testing_app: FlaskClient): assert 'Authorization' in resp.json()['headers'] assert 'X-Request-Id' in resp.json()['headers'] + @mock.patch('requests.post', side_effect=_test_fhir_logging_happy_post) def test_happy_headers(mock1, testing_app: FlaskClient): @@ -104,3 +105,31 @@ def test_happy_headers(mock1, testing_app: FlaskClient): assert trace_headers['X-Request-Id'] == resp.json()['headers']['X-Correlation-Id'] # Trace ID should remain the same assert trace_headers['X-Trace-Id'] == resp.json()['headers']['X-Trace-Id'] + + +@mock.patch('requests.post', side_effect=_test_fhir_logging_happy_post) +def test_happy_related_person(mock1, testing_app: FlaskClient): + """Test logging with a RelatedPerson entity""" + + testing_app.get("test") + resp = fhir_logging_service.register_idp_interaction("RelatedPerson/123", "456", {}) + + json_content = resp.json()['json'] + resp_audit_event = AuditEvent(**json_content) + + assert resp_audit_event.entity[0].what.reference == "RelatedPerson/123" + assert resp_audit_event.entity[0].role.code == "6" # Ensure role code is '6' for RelatedPerson + assert resp_audit_event.entity[0].role.display == "User" # Ensure role display is 'User' + assert resp_audit_event.agent[0].who.reference == "Device/456" + assert resp_audit_event.source.observer.reference == "Device/my-unit-test-auth-server-device-id" + assert resp_audit_event.outcome == "0" + assert 'Authorization' in resp.json()['headers'] + assert 'X-Request-Id' in resp.json()['headers'] + + +@mock.patch('requests.post', side_effect=_test_fhir_logging_happy_post) +def test_invalid_entity_type(mock1, testing_app: FlaskClient): + """Test logging with an invalid entity type, should raise an Exception""" + + with pytest.raises(Exception, match=r"Cannot log IDP interaction - Entity type must be Patient, Practitioner or RelatedPerson. Got \[InvalidType\] instead."): + fhir_logging_service.register_idp_interaction("InvalidType/123", "456", {})