Skip to content
Open
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
4 changes: 2 additions & 2 deletions pathology-api/src/pathology_api/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def _fetch_requesting_organisation(
organisation_identifiers = [
identifier
for identifier in requesting_organisation.identifier
if isinstance(identifier, OrganizationIdentifier)
if isinstance(identifier, OrganizationIdentifier) and identifier.value
]

if not organisation_identifiers:
Expand Down Expand Up @@ -141,7 +141,7 @@ def handle_request(bundle: Bundle) -> PdmResponse:
_logger.debug("Requesting organization: %s", requesting_organisation)

subject = composition.subject
if subject is None:
if subject is None or not subject.identifier.value:
raise ValidationError("Composition does not define a valid subject identifier")

pdm_response = post_document(bundle)
Expand Down
41 changes: 40 additions & 1 deletion pathology-api/src/pathology_api/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,49 @@ def test_handle_request(
bundle_id=result_bundle.id,
)

def test_handle_request_with_empty_subject(
self, build_valid_test_result: Callable[[str, str], Bundle]
) -> None:
bundle = build_valid_test_result("", "ods_code")

with pytest.raises(
ValidationError,
match="Composition does not define a valid subject identifier",
):
handle_request(bundle)

def test_handle_request_with_empty_ods_code(
self, build_valid_test_result: Callable[[str, str], Bundle]
) -> None:
bundle = build_valid_test_result("nhs_number_1", "")

if (
created_practitioner_role := bundle.get_resource(
url="practitioner_role", t=PractitionerRole
)
) is None:
raise ValueError(
"Test setup error: PractitionerRole resource not found in bundle"
)

if (
expected_organisation_reference := created_practitioner_role.organization
) is None:
raise ValueError(
"Test setup error: PractitionerRole resource does not define an "
"organization reference"
)

with pytest.raises(
ValidationError,
match=rf"Organization \({expected_organisation_reference.reference}\) does "
"not define a supported identifier. Supported system 'https://fhir.nhs.uk/Id/ods-organization-code'",
):
handle_request(bundle)

def test_handle_request_raises_error_when_create_event_fails(
self, build_valid_test_result: Callable[[str, str], Bundle]
) -> None:
# Arrange
bundle = build_valid_test_result("nhs_number_1", "ods_code")

expected_error_message = "Failed to create bundle"
Expand Down
36 changes: 36 additions & 0 deletions pathology-api/tests/integration/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,27 @@ def test_invalid_payload_returns_error(
"Composition does not define a valid subject identifier",
id="composition with no subject",
),
pytest.param(
lambda service_request_reference: {
"resourceType": "Composition",
"extension": [
{
"url": "http://hl7.eu/fhir/StructureDefinition/composition-basedOn-order-or-requisition",
"valueReference": {
"reference": service_request_reference,
},
}
],
"subject": {
"identifier": {
"system": "https://fhir.nhs.uk/Id/nhs-number",
"value": "",
}
},
},
"Composition does not define a valid subject identifier",
id="composition with subject with empty identifier value",
),
pytest.param(
lambda _: {
"resourceType": "Composition",
Expand Down Expand Up @@ -819,6 +840,21 @@ def test_invalid_practitioner_role_resource(
"Supported system 'https://fhir.nhs.uk/Id/ods-organization-code'",
id="organization with unknown identifier system",
),
pytest.param(
{
"resourceType": "Organization",
"identifier": [
{
"system": "https://fhir.nhs.uk/Id/ods-organization-code",
"value": "",
}
],
},
"Organization (organization) does not define a "
"supported identifier. "
r"Supported system 'https://fhir.nhs.uk/Id/ods-organization-code'",
id="organization with identifier with empty value",
),
],
)
def test_invalid_organization_resource(
Expand Down
Loading