Skip to content

Commit

Permalink
-m
Browse files Browse the repository at this point in the history
  • Loading branch information
whabanks committed Dec 23, 2024
1 parent 2d82814 commit e0baf5c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/notifications/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
7 changes: 6 additions & 1 deletion app/v2/notifications/post_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}
Expand Down
4 changes: 2 additions & 2 deletions tests/app/notifications/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -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:
Expand Down

0 comments on commit e0baf5c

Please sign in to comment.