Skip to content

Commit

Permalink
KOP-835: fhir_logging_client now also work with RelatedPersons
Browse files Browse the repository at this point in the history
  • Loading branch information
JorisHeadease committed Aug 9, 2024
1 parent f70dd78 commit 8304125
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
8 changes: 4 additions & 4 deletions application/fhir_logging_client/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 }",
}
}
]
Expand Down
29 changes: 29 additions & 0 deletions test/test_fhir_logging_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down Expand Up @@ -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", {})

0 comments on commit 8304125

Please sign in to comment.