From c367ae2e9719c4348987b26189c2529209dcfbb3 Mon Sep 17 00:00:00 2001 From: NogaNHS <127490765+NogaNHS@users.noreply.github.com> Date: Mon, 9 Oct 2023 16:09:29 +0100 Subject: [PATCH] PRMDR 247 subtask (#81) * prmdr-247 added pdf validater and tests * prmdr-247 add validator to create doc ref lambda --- .../create_document_reference_handler.py | 2 +- lambdas/models/nhs_document_reference.py | 18 ++++++++++++- lambdas/services/lloyd_george_validator.py | 7 +++++ .../test_create_document_reference_handler.py | 11 +++++++- .../helpers/data/create_document_reference.py | 27 +++++++++++++++++++ .../services/test_lloyd_george_validator.py | 15 +++++++++++ 6 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 lambdas/services/lloyd_george_validator.py create mode 100644 lambdas/tests/unit/services/test_lloyd_george_validator.py diff --git a/lambdas/handlers/create_document_reference_handler.py b/lambdas/handlers/create_document_reference_handler.py index 80a4c7baa..2cb032d8e 100644 --- a/lambdas/handlers/create_document_reference_handler.py +++ b/lambdas/handlers/create_document_reference_handler.py @@ -62,7 +62,7 @@ def lambda_handler(event, context): except ValidationError as e: logger.error(e) return ApiGatewayResponse( - 400, f"Failed to parse document upload request data: {str(e)}", "GET" + 400, f"Failed to parse document upload request data", "GET" ).create_api_gateway_response() except JSONDecodeError as e: logger.error(e) diff --git a/lambdas/models/nhs_document_reference.py b/lambdas/models/nhs_document_reference.py index fe62df64a..9153be2df 100644 --- a/lambdas/models/nhs_document_reference.py +++ b/lambdas/models/nhs_document_reference.py @@ -1,7 +1,10 @@ from datetime import datetime, timezone +from typing import Any from enums.metadata_field_names import DocumentReferenceMetadataFields -from pydantic import BaseModel +from pydantic import BaseModel, model_validator + +from services.lloyd_george_validator import validate_lg_file_type class UploadRequestDocument(BaseModel): @@ -9,6 +12,19 @@ class UploadRequestDocument(BaseModel): contentType: str docType: str + @model_validator(mode='before') + @classmethod + def check_file_type_for_lg(cls, data: Any) -> Any: + if isinstance(data, dict): + doc_type = data.get('docType') + content_type = data.get('contentType') + elif isinstance(data, UploadRequestDocument): + doc_type = data.docType + content_type = data.contentType + if doc_type == 'LG': + validate_lg_file_type(content_type) + return data + class NHSDocumentReference: def __init__( diff --git a/lambdas/services/lloyd_george_validator.py b/lambdas/services/lloyd_george_validator.py new file mode 100644 index 000000000..6d3c43801 --- /dev/null +++ b/lambdas/services/lloyd_george_validator.py @@ -0,0 +1,7 @@ +class LGFileTypeException(ValueError): + """One or more of the files do not match the required file type.""" + pass + +def validate_lg_file_type(file_type): + if file_type != 'application/pdf': + raise LGFileTypeException diff --git a/lambdas/tests/unit/handlers/test_create_document_reference_handler.py b/lambdas/tests/unit/handlers/test_create_document_reference_handler.py index 85255490e..80c2a30bd 100644 --- a/lambdas/tests/unit/handlers/test_create_document_reference_handler.py +++ b/lambdas/tests/unit/handlers/test_create_document_reference_handler.py @@ -10,7 +10,7 @@ TEST_NHS_NUMBER, TEST_OBJECT_KEY) from tests.unit.helpers.data.create_document_reference import ( ARF_MOCK_EVENT_BODY, ARF_MOCK_RESPONSE, LG_AND_ARF_MOCK_RESPONSE, - LG_MOCK_EVENT_BODY, LG_MOCK_RESPONSE, MOCK_EVENT_BODY) + LG_MOCK_EVENT_BODY, LG_MOCK_RESPONSE, MOCK_EVENT_BODY, LG_MOCK_BAD_EVENT_BODY) from tests.unit.services.test_s3_service import MOCK_PRESIGNED_POST_RESPONSE from utils.lambda_response import ApiGatewayResponse @@ -266,6 +266,15 @@ def test_create_document_reference_arf_type_s3_ClientError_returns_500( assert actual == expected +def test_invalid_file_type_for_lg_return_400(set_env, context): + expected = ApiGatewayResponse( + 400, + "Failed to parse document upload request data", + "GET", + ).create_api_gateway_response() + actual = lambda_handler({"body": json.dumps(LG_MOCK_BAD_EVENT_BODY)}, context) + assert actual == expected + def test_create_document_reference_unknown_document_type_returns_400( set_env, arf_type_event, context, mocker ): diff --git a/lambdas/tests/unit/helpers/data/create_document_reference.py b/lambdas/tests/unit/helpers/data/create_document_reference.py index 512116d6b..4cba8720d 100644 --- a/lambdas/tests/unit/helpers/data/create_document_reference.py +++ b/lambdas/tests/unit/helpers/data/create_document_reference.py @@ -69,6 +69,33 @@ "created": "2023-10-02T15:55:30.650Z", } +LG_MOCK_BAD_EVENT_BODY = { + "resourceType": "DocumentReference", + "subject": {"identifier": {"value": TEST_NHS_NUMBER}}, + "content": [ + { + "attachment": [ + { + "fileName": f"1of3_Lloyd_George_Record_[Joe Bloggs]_[{TEST_NHS_NUMBER}]_[25-12-2019].pdf", + "contentType": "text/plain", + "docType": "LG", + }, + { + "fileName": f"2of3_Lloyd_George_Record_[Joe Bloggs]_[{TEST_NHS_NUMBER}]_[25-12-2019].pdf", + "contentType": "application/pdf", + "docType": "LG", + }, + { + "fileName": f"3of3_Lloyd_George_Record_[Joe Bloggs]_[{TEST_NHS_NUMBER}]_[25-12-2019].pdf", + "contentType": "application/pdf", + "docType": "LG", + }, + ] + } + ], + "created": "2023-10-02T15:55:30.650Z", +} + ARF_MOCK_EVENT_BODY = { "resourceType": "DocumentReference", "subject": {"identifier": {"value": TEST_NHS_NUMBER}}, diff --git a/lambdas/tests/unit/services/test_lloyd_george_validator.py b/lambdas/tests/unit/services/test_lloyd_george_validator.py new file mode 100644 index 000000000..1e0dfdf66 --- /dev/null +++ b/lambdas/tests/unit/services/test_lloyd_george_validator.py @@ -0,0 +1,15 @@ +import pytest + +from services.lloyd_george_validator import validate_lg_file_type, LGFileTypeException + +def test_catching_error_when_file_type_not_pdf(): + with pytest.raises(LGFileTypeException): + file_type = 'image/png' + validate_lg_file_type(file_type) + +def test_valid_file_type(): + try: + file_type = 'application/pdf' + validate_lg_file_type(file_type) + except LGFileTypeException: + assert False, 'One or more of the files do not match the required file type.'