diff --git a/lambdas/tests/unit/handlers/conftest.py b/lambdas/tests/unit/handlers/conftest.py index 4244f489c..2c3f9fa42 100755 --- a/lambdas/tests/unit/handlers/conftest.py +++ b/lambdas/tests/unit/handlers/conftest.py @@ -21,6 +21,22 @@ def valid_id_and_both_doctype_event(): return api_gateway_proxy_event +@pytest.fixture +def valid_id_and_arf_doctype_event(): + api_gateway_proxy_event = { + "queryStringParameters": {"patientId": "9000000009", "docType": "ARF"}, + } + return api_gateway_proxy_event + + +@pytest.fixture +def valid_id_and_lg_doctype_event(): + api_gateway_proxy_event = { + "queryStringParameters": {"patientId": "9000000009", "docType": "LG"}, + } + return api_gateway_proxy_event + + @pytest.fixture def valid_id_and_invalid_doctype_event(): api_gateway_proxy_event = { diff --git a/lambdas/tests/unit/handlers/test_create_document_manifest_by_nhs_number_handler.py b/lambdas/tests/unit/handlers/test_create_document_manifest_by_nhs_number_handler.py index 59170b4d3..a29a6e6d9 100644 --- a/lambdas/tests/unit/handlers/test_create_document_manifest_by_nhs_number_handler.py +++ b/lambdas/tests/unit/handlers/test_create_document_manifest_by_nhs_number_handler.py @@ -1,4 +1,7 @@ +from unittest.mock import call + from enums.metadata_field_names import DocumentReferenceMetadataFields +from enums.supported_document_types import SupportedDocumentTypes from handlers.document_manifest_by_nhs_number_handler import lambda_handler from tests.unit.helpers.data.test_documents import TEST_DS_DOCS, TEST_LG_DOCS from utils.lambda_response import ApiGatewayResponse @@ -11,7 +14,7 @@ def test_lambda_handler_returns_204_when_no_documents_returned_from_dynamo_response( - mocker, set_env, valid_id_and_both_doctype_event, context + mocker, set_env, valid_id_and_arf_doctype_event, context ): mock_document_query = mocker.patch( "services.manifest_dynamo_service.ManifestDynamoService.discover_uploaded_documents" @@ -22,7 +25,7 @@ def test_lambda_handler_returns_204_when_no_documents_returned_from_dynamo_respo 204, "No documents found for given NHS number and document type", "GET" ).create_api_gateway_response() - actual = lambda_handler(valid_id_and_both_doctype_event, context) + actual = lambda_handler(valid_id_and_arf_doctype_event, context) assert expected == actual @@ -44,7 +47,63 @@ def test_lambda_handler_returns_400_when_doc_type_invalid_response( assert expected == actual -def test_lambda_handler_valid_parameters_returns_200( +def manifest_service_side_effect(nhs_number, doc_type): + if doc_type == SupportedDocumentTypes.ARF: + return [TEST_DS_DOCS] + if doc_type == SupportedDocumentTypes.LG: + return [TEST_LG_DOCS] + return None + + +def test_lambda_handler_valid_parameters_arf_doc_type_request_returns_200( + mocker, set_env, valid_id_and_arf_doctype_event, context +): + expected_url = "test-url" + + mock_dynamo = mocker.patch( + "services.manifest_dynamo_service.ManifestDynamoService.discover_uploaded_documents" + ) + mock_dynamo.side_effect = manifest_service_side_effect + + mock_doc_manifest_url = mocker.patch( + "services.document_manifest_service.DocumentManifestService.create_document_manifest_presigned_url" + ) + mock_doc_manifest_url.return_value = expected_url + + expected = ApiGatewayResponse( + 200, expected_url, "GET" + ).create_api_gateway_response() + + actual = lambda_handler(valid_id_and_arf_doctype_event, context) + mock_dynamo.assert_called_once_with("9000000009", SupportedDocumentTypes.ARF) + assert expected == actual + + +def test_lambda_handler_valid_parameters_lg_doc_type_request_returns_200( + mocker, set_env, valid_id_and_lg_doctype_event, context +): + expected_url = "test-url" + + mock_dynamo = mocker.patch( + "services.manifest_dynamo_service.ManifestDynamoService.discover_uploaded_documents" + ) + mock_dynamo.side_effect = manifest_service_side_effect + + mock_doc_manifest_url = mocker.patch( + "services.document_manifest_service.DocumentManifestService.create_document_manifest_presigned_url" + ) + mock_doc_manifest_url.return_value = expected_url + + expected = ApiGatewayResponse( + 200, expected_url, "GET" + ).create_api_gateway_response() + + actual = lambda_handler(valid_id_and_lg_doctype_event, context) + mock_dynamo.assert_called_once_with("9000000009", SupportedDocumentTypes.LG) + assert expected == actual + + +def test_lambda_handler_valid_parameters_both_doc_type_request_returns_200( mocker, set_env, valid_id_and_both_doctype_event, context ): expected_url = "test-url" @@ -52,7 +111,7 @@ def test_lambda_handler_valid_parameters_returns_200( mock_dynamo = mocker.patch( "services.manifest_dynamo_service.ManifestDynamoService.discover_uploaded_documents" ) - mock_dynamo.side_effect = [TEST_DS_DOCS, TEST_LG_DOCS] + mock_dynamo.side_effect = manifest_service_side_effect mock_doc_manifest_url = mocker.patch( "services.document_manifest_service.DocumentManifestService.create_document_manifest_presigned_url" @@ -64,12 +123,15 @@ def test_lambda_handler_valid_parameters_returns_200( ).create_api_gateway_response() actual = lambda_handler(valid_id_and_both_doctype_event, context) - + mock_dynamo.assert_has_calls([ + call("9000000009", SupportedDocumentTypes.ARF), + call("9000000009", SupportedDocumentTypes.LG) + ]) assert expected == actual def test_lambda_handler_missing_environment_variables_returns_500( - set_env, monkeypatch, valid_id_and_both_doctype_event, context + set_env, monkeypatch, valid_id_and_arf_doctype_event, context ): monkeypatch.delenv("DOCUMENT_STORE_DYNAMODB_NAME") expected = ApiGatewayResponse( @@ -77,7 +139,7 @@ def test_lambda_handler_missing_environment_variables_returns_500( "An error occurred due to missing key: 'DOCUMENT_STORE_DYNAMODB_NAME'", "GET", ).create_api_gateway_response() - actual = lambda_handler(valid_id_and_both_doctype_event, context) + actual = lambda_handler(valid_id_and_arf_doctype_event, context) assert expected == actual @@ -107,7 +169,3 @@ def test_lambda_handler_returns_400_when_doc_type_not_supplied( ).create_api_gateway_response() actual = lambda_handler(valid_id_event, context) assert expected == actual - -# only gets LG docs -# only gets ARF docs -# gets all docs