Skip to content

Commit

Permalink
Add the rate limit check to the bulk endpoint (#2434)
Browse files Browse the repository at this point in the history
This PR adds the rate limit check to the v2/notifications/bulk endpoint.
  • Loading branch information
smcmurtry authored Jan 31, 2025
1 parent 83573c5 commit c50c9f4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/v2/notifications/post_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ def post_bulk():
raise BadRequestError(message="You should specify either rows or csv", status_code=400)
template = validate_template_exists(form["template_id"], authenticated_service)
check_service_has_permission(template.template_type, authenticated_service.permissions)
check_rate_limiting(authenticated_service, api_user)

if template.template_type == SMS_TYPE:
fragments_sent = fetch_todays_requested_sms_count(authenticated_service.id)
Expand Down
38 changes: 38 additions & 0 deletions tests/app/v2/notifications/test_post_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -2829,3 +2829,41 @@ def test_seed_bounce_rate(self, notify_api, sample_email_template, mocker, epoch

if result:
seed_bounce_rate_in_redis.apply_async.assert_called_once_with(service_id)


@pytest.mark.parametrize(
"notification_type",
[
"email",
"sms",
],
)
def test_post_bulk_returns_429_if_over_rate_limit(
notify_db_session,
mocker,
client,
sample_email_template,
notify_user,
notify_api,
notification_type,
):
rows = [["email address"], ["[email protected]"]]
data = {"name": "job_name", "template_id": sample_email_template.id, "rows": rows}

job_id = str(uuid.uuid4())
create_api_key(service=sample_email_template.service)
mocker.patch("app.v2.notifications.post_notifications.upload_job_to_s3", return_value=job_id)
mocker.patch("app.v2.notifications.post_notifications.process_job.apply_async")
mocker.patch(
"app.v2.notifications.post_notifications.check_rate_limiting",
side_effect=RateLimitError("LIMIT", "INTERVAL", "TYPE"),
)

auth_header = create_authorization_header(service_id=sample_email_template.service.id)

response = client.post(
path="/v2/notifications/bulk",
data=json.dumps(data),
headers=[("Content-Type", "application/json"), auth_header],
)
assert response.status_code == 429

0 comments on commit c50c9f4

Please sign in to comment.