Skip to content

Commit

Permalink
PRMDR 247 subtask (#81)
Browse files Browse the repository at this point in the history
* prmdr-247 added pdf validater and tests
* prmdr-247 add validator to create doc ref lambda
  • Loading branch information
NogaNHS authored Oct 9, 2023
1 parent b380038 commit c367ae2
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lambdas/handlers/create_document_reference_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 17 additions & 1 deletion lambdas/models/nhs_document_reference.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
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):
fileName: str
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__(
Expand Down
7 changes: 7 additions & 0 deletions lambdas/services/lloyd_george_validator.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
):
Expand Down
27 changes: 27 additions & 0 deletions lambdas/tests/unit/helpers/data/create_document_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}},
Expand Down
15 changes: 15 additions & 0 deletions lambdas/tests/unit/services/test_lloyd_george_validator.py
Original file line number Diff line number Diff line change
@@ -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.'

0 comments on commit c367ae2

Please sign in to comment.