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
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_data():


def fetch_with_retry_mtls(
session, url, headers, condition_func=None, max_retries=5, delay=10
session, url, headers, condition_func=None, max_retries=5, delay=10,
):
retries = 0
while retries < max_retries:
Expand All @@ -47,7 +47,7 @@ def fetch_with_retry_mtls(


def create_mtls_session(
client_cert_path=CLIENT_CERT_PATH, client_key_path=CLIENT_KEY_PATH
client_cert_path=CLIENT_CERT_PATH, client_key_path=CLIENT_KEY_PATH,
):
session = requests.Session()
session.cert = (client_cert_path, client_key_path)
Expand Down Expand Up @@ -86,49 +86,6 @@ def temp_cert_and_key():
shutil.rmtree(temp_dir)


def get_pdm_document_reference(
record_id="",
client_cert_path=None,
client_key_path=None,
resource_type="DocumentReference",
pdm_snomed=PDM_SNOMED,
endpoint_override=None,
):
if not endpoint_override:
url = f"https://{MTLS_ENDPOINT}/{resource_type}/{pdm_snomed}~{record_id}"
else:
url = f"https://{MTLS_ENDPOINT}/{resource_type}/{endpoint_override}"
headers = {
"X-Correlation-Id": "1234",
}

# Call with invalid or unauthorised certs
if client_cert_path and client_key_path:
session = create_mtls_session(client_cert_path, client_key_path)
else:
# Call with default valid certs
session = create_mtls_session()

response = session.get(url, headers=headers)
return response


def delete_document_reference(endpoint, client_cert_path=None, client_key_path=None):
"""Helper to perform a DELETE by NHS number."""
url = f"https://{MTLS_ENDPOINT}/DocumentReference{endpoint}"
headers = {
"X-Correlation-Id": "1234",
}

# Use provided certs if available, else defaults
if client_cert_path and client_key_path:
session = create_mtls_session(client_cert_path, client_key_path)
else:
session = create_mtls_session()

return session.delete(url=url, headers=headers)


def create_and_store_pdm_record(
test_data,
nhs_number: str = "9912003071",
Expand All @@ -138,23 +95,14 @@ def create_and_store_pdm_record(
):
"""Helper to create metadata and resource for a record."""
record = pdm_data_helper.build_record(
nhs_number=nhs_number, doc_status=doc_status, size=size
nhs_number=nhs_number, doc_status=doc_status, size=size,
)
test_data.append(record)
pdm_data_helper.create_metadata(record, **dynamo_kwargs)
pdm_data_helper.create_resource(record)
return record


def upload_document(payload, resource_type="DocumentReference"):
"""Helper to upload DocumentReference."""
url = f"https://{MTLS_ENDPOINT}/{resource_type}"
headers = {
"X-Correlation-Id": "1234",
}
session = create_mtls_session()
return session.post(url, headers=headers, data=payload)


def retrieve_document_with_retry(doc_id, condition):
"""Poll until condition is met on DocumentReference retrieval."""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import uuid
from tests.e2e.api.fhir.conftest import (
delete_document_reference,
)

from tests.e2e.helpers.data_helper import PdmDataHelper
from tests.e2e.helpers.rest_helper import delete_document_reference

pdm_data_helper = PdmDataHelper()


def test_no_documents_found(test_data):
response = delete_document_reference(
f"?subject:identifier=https://fhir.nhs.uk/Id/nhs-number|9912003071&_id={uuid.uuid4()}"
f"?subject:identifier=https://fhir.nhs.uk/Id/nhs-number|9912003071&_id={uuid.uuid4()}",
)
assert response.status_code == 404
response_json = response.json()
Expand All @@ -19,7 +18,7 @@ def test_no_documents_found(test_data):

def test_malformatted_nhs_id(test_data):
response = delete_document_reference(
f"?subject:identifier=https://fhir.nhs.uk/Id/nhs-number|991200&_id={uuid.uuid4()}"
f"?subject:identifier=https://fhir.nhs.uk/Id/nhs-number|991200&_id={uuid.uuid4()}",
)
assert response.status_code == 400
response_json = response.json()
Expand All @@ -31,7 +30,7 @@ def test_malformatted_nhs_id(test_data):

def test_malformatted_document_id(test_data):
response = delete_document_reference(
"?subject:identifier=https://fhir.nhs.uk/Id/nhs-number|9912003071&_id=1234"
"?subject:identifier=https://fhir.nhs.uk/Id/nhs-number|9912003071&_id=1234",
)
assert response.status_code == 400
response_json = response.json()
Expand All @@ -51,7 +50,7 @@ def test_no_query_params(test_data):

def test_incorrect_query_params(test_data):
response = delete_document_reference(
"?foo=https://fhir.nhs.uk/Id/nhs-number|9912003071"
"?foo=https://fhir.nhs.uk/Id/nhs-number|9912003071",
)
assert response.status_code == 400
response_json = response.json()
Expand All @@ -63,7 +62,7 @@ def test_incorrect_query_params(test_data):

def test_correct_query_params_with_incorrect_params(test_data):
response = delete_document_reference(
f"?subject:identifier=https://fhir.nhs.uk/Id/nhs-number|9912003071&_id={uuid.uuid4()}&foo=1234"
f"?subject:identifier=https://fhir.nhs.uk/Id/nhs-number|9912003071&_id={uuid.uuid4()}&foo=1234",
)
assert response.status_code == 404
response_json = response.json()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from tests.e2e.api.fhir.conftest import (
create_and_store_pdm_record,
get_pdm_document_reference,
delete_document_reference,
)
from tests.e2e.helpers.data_helper import PdmDataHelper
from tests.e2e.helpers.rest_helper import (
delete_document_reference,
get_pdm_document_reference,
)

pdm_data_helper = PdmDataHelper()

Expand All @@ -16,7 +18,7 @@ def test_delete_record_by_patient_details_and_doc_id(test_data):
assert get_response_1.status_code == 200

response = delete_document_reference(
f"?subject:identifier=https://fhir.nhs.uk/Id/nhs-number|9912003071&_id={expected_record_id}"
f"?subject:identifier=https://fhir.nhs.uk/Id/nhs-number|9912003071&_id={expected_record_id}",
)
assert response.status_code == 204

Expand All @@ -38,7 +40,7 @@ def test_delete_only_one_record_by_patient_details_and_doc_id(test_data):
assert get_response_2.status_code == 200

response = delete_document_reference(
f"?subject:identifier=https://fhir.nhs.uk/Id/nhs-number|9912003071&_id={expected_record_id_1}"
f"?subject:identifier=https://fhir.nhs.uk/Id/nhs-number|9912003071&_id={expected_record_id_1}",
)
assert response.status_code == 204

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import uuid

from tests.e2e.api.fhir.conftest import get_pdm_document_reference
from tests.e2e.helpers.rest_helper import get_pdm_document_reference

UNAUTHORISED_CLIENT_CERT_PATH = os.environ.get("UNAUTHORISED_CLIENT_CERT_PATH")
UNAUTHORISED_CLIENT_KEY_PATH = os.environ.get("UNAUTHORISED_CLIENT_KEY_PATH")
Expand All @@ -10,7 +10,7 @@
def test_mtls_invalid_common_name():
record_id = str(uuid.uuid4())
response = get_pdm_document_reference(
record_id, UNAUTHORISED_CLIENT_CERT_PATH, UNAUTHORISED_CLIENT_KEY_PATH
record_id, UNAUTHORISED_CLIENT_CERT_PATH, UNAUTHORISED_CLIENT_KEY_PATH,
)
assert response.status_code == 400

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from enums.document_retention import DocumentRetentionDays
from tests.e2e.api.fhir.conftest import (
create_and_store_pdm_record,
get_pdm_document_reference,
)
from tests.e2e.helpers.data_helper import PdmDataHelper
from tests.e2e.helpers.rest_helper import get_pdm_document_reference

pdm_data_helper = PdmDataHelper()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import pytest
from tests.e2e.api.fhir.conftest import (
PDM_S3_BUCKET,
create_and_store_pdm_record,
get_pdm_document_reference,
PDM_SNOMED,
create_and_store_pdm_record,
)
from tests.e2e.helpers.data_helper import PdmDataHelper
from tests.e2e.helpers.rest_helper import get_pdm_document_reference

pdm_data_helper = PdmDataHelper()

Expand All @@ -34,7 +34,7 @@ def assert_returned_document_reference(pdm_record, response):
],
)
def test_successful_retrieval_of_document_reference(
test_data, doc_status, response_status
test_data, doc_status, response_status,
):
pdm_record = create_and_store_pdm_record(test_data, doc_status=doc_status)

Expand All @@ -50,7 +50,7 @@ def test_successful_retrieval_of_document_reference(
def test_file_retrieval(test_data, file_size):
"""Test retrieval for small and large files."""
pdm_record = create_and_store_pdm_record(
test_data, size=file_size if file_size else None
test_data, size=file_size if file_size else None,
)
response = get_pdm_document_reference(pdm_record["id"])
assert response.status_code == 200
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,15 @@
import pytest
from enums.document_retention import DocumentRetentionDays
from tests.e2e.api.fhir.conftest import (
MTLS_ENDPOINT,
PDM_SNOMED,
create_and_store_pdm_record,
create_mtls_session,
)
from tests.e2e.conftest import APIM_ENDPOINT
from tests.e2e.helpers.data_helper import PdmDataHelper
from tests.e2e.helpers.rest_helper import search_document_reference

pdm_data_helper = PdmDataHelper()


def search_document_reference(
nhs_number,
client_cert_path=None,
client_key_path=None,
resource_type="DocumentReference",
):
"""Helper to perform search by NHS number with optional mTLS certs."""
url = f"https://{MTLS_ENDPOINT}/{resource_type}?subject:identifier=https://fhir.nhs.uk/Id/nhs-number|{nhs_number}"
headers = {
"X-Correlation-Id": "1234",
}

# Use provided certs if available, else defaults
if client_cert_path and client_key_path:
session = create_mtls_session(client_cert_path, client_key_path)
else:
session = create_mtls_session()

return session.get(url, headers=headers)


def test_search_nonexistent_document_references_for_patient_details():
response = search_document_reference("9449305943")
assert response.status_code == 200
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from tests.e2e.api.fhir.conftest import (
MTLS_ENDPOINT,
retrieve_document_with_retry,
upload_document,
)
from tests.e2e.helpers.data_helper import PdmDataHelper
from tests.e2e.helpers.rest_helper import upload_document_reference

pdm_data_helper = PdmDataHelper()

Expand All @@ -26,7 +26,7 @@ def test_create_document_presign_fails():
record["data"] = base64.b64encode(f.read()).decode("utf-8")
payload = pdm_data_helper.create_upload_payload(record)

upload_response = upload_document(payload)
upload_response = upload_document_reference(payload)
assert upload_response.status_code == 413
assert upload_response.text == "HTTP content length exceeded 10485760 bytes."

Expand All @@ -44,7 +44,7 @@ def test_create_document_virus(test_data):
record["data"] = base64.b64encode(eicar_string.encode()).decode()
payload = pdm_data_helper.create_upload_payload(record)

raw_upload_response = upload_document(payload)
raw_upload_response = upload_document_reference(payload)
assert raw_upload_response.status_code == 201
upload_response = raw_upload_response.json()
record["id"] = upload_response["id"].split("~")[1]
Expand All @@ -59,7 +59,7 @@ def condition(response_json):
)

raw_retrieve_response = retrieve_document_with_retry(
upload_response["id"], condition
upload_response["id"], condition,
)
retrieve_response = raw_retrieve_response.json()

Expand All @@ -84,7 +84,7 @@ def condition(response_json):
],
)
def test_search_edge_cases(
nhs_number, expected_status, expected_code, expected_diagnostics
nhs_number, expected_status, expected_code, expected_diagnostics,
):
record = {
"ods": "H81109",
Expand All @@ -95,7 +95,7 @@ def test_search_edge_cases(
record["data"] = base64.b64encode(sample_pdf_bytes).decode("utf-8")

payload = pdm_data_helper.create_upload_payload(record)
response = upload_document(payload)
response = upload_document_reference(payload)
assert response.status_code == expected_status

body = response.json()
Expand Down Expand Up @@ -123,7 +123,7 @@ def test_forbidden_with_invalid_cert(temp_cert_and_key):
headers = {"Authorization": "Bearer 123", "X-Correlation-Id": "1234"}

response = requests.post(
url, headers=headers, cert=(cert_path, key_path), data=payload
url, headers=headers, cert=(cert_path, key_path), data=payload,
)
body = response.json()
assert response.status_code == 403
Expand All @@ -147,7 +147,7 @@ def test_create_document_with_invalid_author_returns_error(test_data, author_pay
payload["author"][0] = author_payload
payload = json.dumps(payload)

raw_upload_response = upload_document(payload)
raw_upload_response = upload_document_reference(payload)
response_json = raw_upload_response.json()
assert raw_upload_response.status_code == 400
assert response_json["resourceType"] == "OperationOutcome"
Expand All @@ -168,7 +168,7 @@ def test_upload_invalid_resource_type(test_data):
payload = pdm_data_helper.create_upload_payload(record=record, return_json=True)
payload = json.dumps(payload)

raw_upload_response = upload_document(payload, resource_type="FooBar")
raw_upload_response = upload_document_reference(payload, resource_type="FooBar")
assert raw_upload_response.status_code == 403

response_json = raw_upload_response.json()
Expand Down
Loading
Loading