Skip to content

Commit

Permalink
PRMDR-699 use nems enabled feature flag in nems message lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
RachelHowellNHS authored Feb 23, 2024
1 parent d763338 commit 449839a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
8 changes: 8 additions & 0 deletions lambdas/enums/feature_flags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from enum import Enum


class FeatureFlags(Enum):
NEMS_ENABLED = "nemsEnabled"
UPLOAD_LLOYD_GEORGE_WORKFLOW_ENABLED = "uploadLloydGeorgeWorkflowEnabled"
UPLOAD_LAMBDA_ENABLED = "uploadLambdaEnabled"
UPLOAD_ARF_WORKFLOW_ENABLED = "uploadArfWorkflowEnabled"
22 changes: 21 additions & 1 deletion lambdas/handlers/nems_message_handler.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from enums.feature_flags import FeatureFlags
from services.feature_flags_service import FeatureFlagService
from services.process_nems_message_service import ProcessNemsMessageService
from utils.audit_logging_setup import LoggingService
from utils.decorators.ensure_env_var import ensure_environment_variables
Expand All @@ -9,10 +11,28 @@


@set_request_context_for_logging
@ensure_environment_variables(
names=[
"APPCONFIG_APPLICATION",
"APPCONFIG_CONFIGURATION",
"APPCONFIG_ENVIRONMENT",
"LLOYD_GEORGE_DYNAMODB_NAME",
]
)
@override_error_check
@ensure_environment_variables(["LLOYD_GEORGE_DYNAMODB_NAME"])
def lambda_handler(event, context):
logger.info(f"Received event: {event}")

feature_flag_service = FeatureFlagService()
nems_flag_name = FeatureFlags.NEMS_ENABLED.value
nems_enabled_flag_object = feature_flag_service.get_feature_flags_by_flag(
nems_flag_name
)

if not nems_enabled_flag_object[nems_flag_name]:
logger.info("Feature flag not enabled, event will not be processed")
return

if "Records" not in event or len(event["Records"]) < 1:
http_status_code = 400
response_body = (
Expand Down
2 changes: 1 addition & 1 deletion lambdas/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ botocore==1.33.11
certifi==2023.7.22
cffi==1.15.1
charset-normalizer==3.2.0
cryptography==42.0.2
cryptography==42.0.4
email-validator==2.1.0.post1
fhir.resources[xml]==7.1.0
idna==3.4
Expand Down
33 changes: 29 additions & 4 deletions lambdas/tests/unit/handlers/test_process_nems_message.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from handlers.nems_message_handler import lambda_handler
from services.feature_flags_service import FeatureFlagService
from services.process_nems_message_service import ProcessNemsMessageService
from tests.unit.helpers.data.bulk_upload.test_data import (
TEST_EVENT_WITH_NO_SQS_MESSAGES,
Expand All @@ -14,7 +15,23 @@ def mock_service(mocker):
yield mocker.patch.object(ProcessNemsMessageService, "process_messages_from_event")


def test_can_process_event_with_one_message_no_error(mock_service, context, set_env):
@pytest.fixture
def mock_nems_enabled(mocker):
mock_function = mocker.patch.object(FeatureFlagService, "get_feature_flags_by_flag")
mock_nems_feature_flag = mock_function.return_value = {"nemsEnabled": True}
yield mock_nems_feature_flag


@pytest.fixture
def mock_nems_disabled(mocker):
mock_function = mocker.patch.object(FeatureFlagService, "get_feature_flags_by_flag")
mock_nems_feature_flag = mock_function.return_value = {"nemsEnabled": False}
yield mock_nems_feature_flag


def test_can_process_event_with_one_message_no_error(
mock_service, context, set_env, mock_nems_enabled
):
expected = {"batchItemFailures": []}
mock_service.return_value = []

Expand All @@ -25,7 +42,7 @@ def test_can_process_event_with_one_message_no_error(mock_service, context, set_


def test_can_process_event_with_several_messages_no_error(
mock_service, context, set_env
mock_service, context, set_env, mock_nems_enabled
):
expected = {"batchItemFailures": []}
mock_service.return_value = []
Expand All @@ -37,7 +54,7 @@ def test_can_process_event_with_several_messages_no_error(


def test_can_process_event_with_several_messages_with_fail(
mock_service, context, set_env
mock_service, context, set_env, mock_nems_enabled
):
expected = {"batchItemFailures": ["test_response"]}
mock_service.return_value = ["test_response"]
Expand All @@ -49,7 +66,7 @@ def test_can_process_event_with_several_messages_with_fail(


def test_receive_correct_response_when_no_records_in_event(
mock_service, context, set_env
mock_service, context, set_env, mock_nems_enabled
):
expected = ApiGatewayResponse(
400,
Expand All @@ -59,3 +76,11 @@ def test_receive_correct_response_when_no_records_in_event(
actual = lambda_handler(TEST_EVENT_WITH_NO_SQS_MESSAGES, context)

assert expected == actual


def test_no_event_processing_when_nems_flag_not_enabled(
mock_service, context, set_env, mock_nems_disabled
):
lambda_handler(TEST_EVENT_WITH_SQS_MESSAGES, context)

mock_service.assert_not_called()

0 comments on commit 449839a

Please sign in to comment.