Skip to content

Commit

Permalink
PRMDR - Disable all upload journey lambdas when feature flag is disab…
Browse files Browse the repository at this point in the history
…led (#381)

* [PRMDR-390] adding feature flag to all upload lambdas
  • Loading branch information
NogaNHS authored Jun 19, 2024
1 parent 210c3f5 commit c03ce37
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 31 deletions.
13 changes: 12 additions & 1 deletion lambdas/handlers/update_upload_state_handler.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import json
from json import JSONDecodeError

from enums.feature_flags import FeatureFlags
from enums.lambda_error import LambdaError
from enums.logging_app_interaction import LoggingAppInteraction
from services.feature_flags_service import FeatureFlagService
from services.update_upload_state_service import UpdateUploadStateService
from utils.audit_logging_setup import LoggingService
from utils.decorators.ensure_env_var import ensure_environment_variables
from utils.decorators.handle_lambda_exceptions import handle_lambda_exceptions
from utils.decorators.override_error_check import override_error_check
from utils.decorators.set_audit_arg import set_request_context_for_logging
from utils.lambda_exceptions import UpdateUploadStateException
from utils.lambda_exceptions import FeatureFlagsException, UpdateUploadStateException
from utils.lambda_response import ApiGatewayResponse
from utils.request_context import request_context

Expand All @@ -32,6 +34,15 @@ def lambda_handler(event, context):
request_context.app_interaction = LoggingAppInteraction.UPDATE_UPLOAD_STATE.value
failed_message = "Update upload state failed"
logger.info("Update upload state handler triggered")
feature_flag_service = FeatureFlagService()
upload_flag_name = FeatureFlags.UPLOAD_LAMBDA_ENABLED.value
upload_lambda_enabled_flag_object = feature_flag_service.get_feature_flags_by_flag(
upload_flag_name
)

if not upload_lambda_enabled_flag_object[upload_flag_name]:
logger.info("Feature flag not enabled, event will not be processed")
raise FeatureFlagsException(500, LambdaError.FeatureFlagDisabled)
try:
event_body = json.loads(event["body"])
logger.info("Using update upload service...")
Expand Down
12 changes: 11 additions & 1 deletion lambdas/handlers/upload_confirm_result_handler.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import json
from json import JSONDecodeError

from enums.feature_flags import FeatureFlags
from enums.lambda_error import LambdaError
from enums.logging_app_interaction import LoggingAppInteraction
from services.feature_flags_service import FeatureFlagService
from services.upload_confirm_result_service import UploadConfirmResultService
from utils.audit_logging_setup import LoggingService
from utils.decorators.ensure_env_var import ensure_environment_variables
from utils.decorators.handle_lambda_exceptions import handle_lambda_exceptions
from utils.decorators.override_error_check import override_error_check
from utils.decorators.set_audit_arg import set_request_context_for_logging
from utils.exceptions import InvalidResourceIdException
from utils.lambda_exceptions import UploadConfirmResultException
from utils.lambda_exceptions import FeatureFlagsException, UploadConfirmResultException
from utils.lambda_response import ApiGatewayResponse
from utils.request_context import request_context
from utils.utilities import validate_nhs_number
Expand All @@ -37,7 +39,15 @@ def lambda_handler(event, context):
request_context.app_interaction = LoggingAppInteraction.UPLOAD_CONFIRMATION.value

logger.info("Upload confirm result handler triggered")
feature_flag_service = FeatureFlagService()
upload_flag_name = FeatureFlags.UPLOAD_LAMBDA_ENABLED.value
upload_lambda_enabled_flag_object = feature_flag_service.get_feature_flags_by_flag(
upload_flag_name
)

if not upload_lambda_enabled_flag_object[upload_flag_name]:
logger.info("Feature flag not enabled, event will not be processed")
raise FeatureFlagsException(500, LambdaError.FeatureFlagDisabled)
nhs_number, documents = processing_event_details(event)
request_context.patient_nhs_no = nhs_number
upload_confirm_result_service = UploadConfirmResultService(nhs_number)
Expand Down
13 changes: 12 additions & 1 deletion lambdas/handlers/virus_scan_result_handler.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import json
from json import JSONDecodeError

from enums.feature_flags import FeatureFlags
from enums.lambda_error import LambdaError
from enums.logging_app_interaction import LoggingAppInteraction
from enums.supported_document_types import SupportedDocumentTypes
from services.feature_flags_service import FeatureFlagService
from services.virus_scan_result_service import VirusScanService
from utils.audit_logging_setup import LoggingService
from utils.decorators.ensure_env_var import ensure_environment_variables
from utils.decorators.handle_lambda_exceptions import handle_lambda_exceptions
from utils.decorators.override_error_check import override_error_check
from utils.decorators.set_audit_arg import set_request_context_for_logging
from utils.lambda_exceptions import VirusScanResultException
from utils.lambda_exceptions import FeatureFlagsException, VirusScanResultException
from utils.lambda_response import ApiGatewayResponse
from utils.request_context import request_context

Expand All @@ -32,6 +34,15 @@
@handle_lambda_exceptions
def lambda_handler(event, context):
request_context.app_interaction = LoggingAppInteraction.VIRUS_SCAN.value
feature_flag_service = FeatureFlagService()
upload_flag_name = FeatureFlags.UPLOAD_LAMBDA_ENABLED.value
upload_lambda_enabled_flag_object = feature_flag_service.get_feature_flags_by_flag(
upload_flag_name
)

if not upload_lambda_enabled_flag_object[upload_flag_name]:
logger.info("Feature flag not enabled, event will not be processed")
raise FeatureFlagsException(500, LambdaError.FeatureFlagDisabled)
try:
event_body = json.loads(event["body"])
if not event_body:
Expand Down
19 changes: 19 additions & 0 deletions lambdas/tests/unit/handlers/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from services.feature_flags_service import FeatureFlagService


@pytest.fixture
Expand Down Expand Up @@ -95,3 +96,21 @@ def missing_id_event():
"queryStringParameters": {"invalid": ""},
}
return api_gateway_proxy_event


@pytest.fixture
def mock_upload_lambda_enabled(mocker):
mock_function = mocker.patch.object(FeatureFlagService, "get_feature_flags_by_flag")
mock_upload_lambda_feature_flag = mock_function.return_value = {
"uploadLambdaEnabled": True
}
yield mock_upload_lambda_feature_flag


@pytest.fixture
def mock_upload_lambda_disabled(mocker):
mock_function = mocker.patch.object(FeatureFlagService, "get_feature_flags_by_flag")
mock_upload_lambda_feature_flag = mock_function.return_value = {
"uploadLambdaEnabled": False
}
yield mock_upload_lambda_feature_flag
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
lambda_handler,
processing_event_details,
)
from services.feature_flags_service import FeatureFlagService
from tests.unit.conftest import MOCK_LG_STAGING_STORE_BUCKET, TEST_NHS_NUMBER, TEST_UUID
from tests.unit.helpers.data.create_document_reference import (
ARF_FILE_LIST,
Expand Down Expand Up @@ -54,24 +53,6 @@ def mock_processing_event_details(mocker):
)


@pytest.fixture
def mock_upload_lambda_enabled(mocker):
mock_function = mocker.patch.object(FeatureFlagService, "get_feature_flags_by_flag")
mock_upload_lambda_feature_flag = mock_function.return_value = {
"uploadLambdaEnabled": True
}
yield mock_upload_lambda_feature_flag


@pytest.fixture
def mock_upload_lambda_disabled(mocker):
mock_function = mocker.patch.object(FeatureFlagService, "get_feature_flags_by_flag")
mock_upload_lambda_feature_flag = mock_function.return_value = {
"uploadLambdaEnabled": False
}
yield mock_upload_lambda_feature_flag


def test_create_document_reference_valid_both_lg_and_arf_type_returns_200(
set_env, both_type_event, context, mocker, mock_upload_lambda_enabled
):
Expand Down
36 changes: 32 additions & 4 deletions lambdas/tests/unit/handlers/test_update_upload_state_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class MockError(Enum):


@pytest.fixture
def mock_update_upload_state_service(mocker):
def mock_update_upload_state_service(mocker, mock_upload_lambda_enabled):
mocked_class = mocker.patch(
"handlers.update_upload_state_handler.UpdateUploadStateService"
)
Expand Down Expand Up @@ -51,7 +51,9 @@ def test_update_upload_state_handler_success_arf(
assert expected == actual


def test_update_upload_state_handler_both_doc_types_raise_error(set_env, context):
def test_update_upload_state_handler_both_doc_types_raise_error(
set_env, context, mock_upload_lambda_enabled
):
expected_body = {
"message": "Missing fields",
"err_code": "US_4002",
Expand Down Expand Up @@ -98,7 +100,11 @@ def test_lambda_handler_missing_environment_variables_returns_500(
assert expected == actual


def test_lambda_handler_invalid_body_raises_exception(set_env, context):
def test_lambda_handler_invalid_body_raises_exception(
set_env,
context,
mock_upload_lambda_enabled,
):
expected_body = {
"message": "Invalid request body",
"err_code": "US_4005",
Expand All @@ -113,7 +119,9 @@ def test_lambda_handler_invalid_body_raises_exception(set_env, context):
assert expected == actual


def test_lambda_handler_missing_body_raises_exception(set_env, context):
def test_lambda_handler_missing_body_raises_exception(
set_env, context, mock_upload_lambda_enabled
):
expected_body = {
"message": "Missing request body",
"err_code": "US_4001",
Expand All @@ -126,3 +134,23 @@ def test_lambda_handler_missing_body_raises_exception(set_env, context):
).create_api_gateway_response()
actual = lambda_handler(MOCK_NO_BODY_EVENT, context)
assert expected == actual


def test_no_event_processing_when_upload_lambda_flag_not_enabled(
set_env, context, mock_upload_lambda_disabled
):

expected_body = json.dumps(
{
"message": "Feature is not enabled",
"err_code": "FFL_5003",
"interaction_id": "88888888-4444-4444-4444-121212121212",
}
)
expected = ApiGatewayResponse(
500, expected_body, "POST"
).create_api_gateway_response()

actual = lambda_handler(MOCK_VALID_ARF_EVENT, context)

assert actual == expected
26 changes: 22 additions & 4 deletions lambdas/tests/unit/handlers/test_upload_confirm_result_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class MockError(Enum):


@pytest.fixture
def mock_upload_confirm_result_service(mocker):
def mock_upload_confirm_result_service(mocker, mock_upload_lambda_enabled):
mocked_class = mocker.patch(
"handlers.upload_confirm_result_handler.UploadConfirmResultService"
)
Expand Down Expand Up @@ -180,9 +180,7 @@ def test_processing_event_details_returns_nhs_number_and_documents():


def test_lambda_handler_processing_event_details_raises_error(
context,
set_env,
mock_processing_event_details,
context, set_env, mock_processing_event_details, mock_upload_lambda_enabled
):
mock_processing_event_details.side_effect = UploadConfirmResultException(
400, MockError.Error
Expand Down Expand Up @@ -217,3 +215,23 @@ def test_lambda_handler_service_raises_error(
MOCK_ARF_DOCUMENTS
)
mock_processing_event_details.assert_called_with(MOCK_VALID_ARF_EVENT)


def test_no_event_processing_when_upload_lambda_flag_not_enabled(
set_env, context, mock_upload_lambda_disabled
):

expected_body = json.dumps(
{
"message": "Feature is not enabled",
"err_code": "FFL_5003",
"interaction_id": "88888888-4444-4444-4444-121212121212",
}
)
expected = ApiGatewayResponse(
500, expected_body, "POST"
).create_api_gateway_response()

actual = lambda_handler(MOCK_VALID_ARF_EVENT, context)

assert actual == expected
29 changes: 28 additions & 1 deletion lambdas/tests/unit/handlers/test_virus_scan_result_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@


@pytest.fixture
def mock_virus_scan_service(mocker):
def mock_virus_scan_service(
mocker,
mock_upload_lambda_enabled,
):
mocked_class = mocker.patch("handlers.virus_scan_result_handler.VirusScanService")
mocked_service = mocked_class.return_value
yield mocked_service
Expand Down Expand Up @@ -135,3 +138,27 @@ def test_lambda_handler_responds_with_400_when_no_doc_type_in_document_reference

assert actual == expected
mock_virus_scan_service.scan_file.assert_not_called()


def test_no_event_processing_when_upload_lambda_flag_not_enabled(
set_env, context, mock_upload_lambda_disabled
):

valid_event = {
"httpMethod": "POST",
"body": json.dumps(VALID_DOCUMENT_REFERENCE_LOWERCASE),
}
expected_body = json.dumps(
{
"message": "Feature is not enabled",
"err_code": "FFL_5003",
"interaction_id": "88888888-4444-4444-4444-121212121212",
}
)
expected = ApiGatewayResponse(
500, expected_body, "POST"
).create_api_gateway_response()

actual = lambda_handler(valid_event, context)

assert actual == expected

0 comments on commit c03ce37

Please sign in to comment.