From e0baf5c2225967b39a322d207955c9c4752560c7 Mon Sep 17 00:00:00 2001 From: wbanks Date: Mon, 23 Dec 2024 12:06:25 -0400 Subject: [PATCH] -m --- app/notifications/validators.py | 4 ++-- app/v2/notifications/post_notifications.py | 7 ++++++- tests/app/notifications/test_validators.py | 4 ++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/app/notifications/validators.py b/app/notifications/validators.py index 58a2386786..06c6bce85f 100644 --- a/app/notifications/validators.py +++ b/app/notifications/validators.py @@ -766,12 +766,12 @@ def decode_personalisation_files(json_personalisation): return json_personalisation, errors -def validate_notification_does_not_exceed_sqs_limit(notification): +def validate_notification_does_not_exceed_sqs_limit(notification, caller_payload): # SQS max payload size is 256KB if len(str(notification)) >= current_app.config["MAX_SQS_PAYLOAD_SIZE"]: # find the largest value in the payload for logging and return message max_key, max_length = max( - ((key, len(str(value))) for key, value in flatten_dct(dict(notification)).items()), key=lambda x: x[1] + ((key, len(str(value))) for key, value in flatten_dct(dict(caller_payload)).items()), key=lambda x: x[1] ) current_app.logger.debug( f"Unable to send notification {notification['id']}. Payload size exceeds SQS limit of 262144 bytes. Largest key: {max_key} is {max_length} bytes." diff --git a/app/v2/notifications/post_notifications.py b/app/v2/notifications/post_notifications.py index 6e5a8c5f48..5fa8e2de4c 100644 --- a/app/v2/notifications/post_notifications.py +++ b/app/v2/notifications/post_notifications.py @@ -429,6 +429,11 @@ def process_sms_or_email_notification( service=service, notification_type=notification_type, ) + caller_payload = { + "template_id": template.id, + "email_address" if notification_type == EMAIL_TYPE else "phone_number": form_send_to, + "personalisation": form.get("personalisation"), + } # Do not persist or send notification to the queue if it is a simulated recipient simulated = simulated_recipient(send_to, notification_type) @@ -449,7 +454,7 @@ def process_sms_or_email_notification( "reply_to_text": reply_to_text, } - validate_notification_does_not_exceed_sqs_limit(_notification) + validate_notification_does_not_exceed_sqs_limit(_notification, caller_payload) signed_notification_data = signer_notification.sign(_notification) notification = {**_notification} diff --git a/tests/app/notifications/test_validators.py b/tests/app/notifications/test_validators.py index 9db6e1d798..cfeb15cd3a 100644 --- a/tests/app/notifications/test_validators.py +++ b/tests/app/notifications/test_validators.py @@ -719,7 +719,7 @@ def test_validate_notification_does_not_exceed_sqs_limit_exceeds_limit(mocker): logger = mocker.patch("app.notifications.validators.current_app.logger.debug") with pytest.raises(BadRequestError) as e: - validate_notification_does_not_exceed_sqs_limit(notification) + validate_notification_does_not_exceed_sqs_limit(notification, notification) # We flatten the dict here so the key is composite assert e.value.message == "Notification size cannot exceed 256Kb. Consider reducing the size of: payload.key2." @@ -738,7 +738,7 @@ def test_validate_notification_does_not_exceed_sqs_limit_within_limit(mocker): "key3": "value3", }, } - assert validate_notification_does_not_exceed_sqs_limit(notification) is None + assert validate_notification_does_not_exceed_sqs_limit(notification, notification) is None class TestAnnualLimitValidators: