From 8304bc00031a20e9a62bf6c94dbdbcae965860c2 Mon Sep 17 00:00:00 2001 From: wbanks Date: Thu, 1 Feb 2024 12:10:36 -0400 Subject: [PATCH] Fix tests, squashed a bug --- app/notifications/rest.py | 8 +++++++- .../app/notifications/rest/test_send_notification.py | 11 ++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/app/notifications/rest.py b/app/notifications/rest.py index 3a4f80c9d7..70b09228f8 100644 --- a/app/notifications/rest.py +++ b/app/notifications/rest.py @@ -10,6 +10,7 @@ EMAIL_TYPE, INTERNATIONAL_SMS_TYPE, KEY_TYPE_TEAM, + KEY_TYPE_TEST, LETTER_TYPE, SMS_TYPE, NotificationType, @@ -21,6 +22,7 @@ simulated_recipient, ) from app.notifications.validators import ( + check_email_daily_limit, check_rate_limiting, check_template_is_active, check_template_is_for_notification_type, @@ -102,12 +104,17 @@ def send_notification(notification_type: NotificationType): raise InvalidRequest(errors, status_code=400) current_app.logger.info(f"POST to V1 API: send_notification, service_id: {authenticated_service.id}") + check_rate_limiting(authenticated_service, api_user) template = templates_dao.dao_get_template_by_id_and_service_id( template_id=notification_form["template"], service_id=authenticated_service.id ) + simulated = simulated_recipient(notification_form["to"], notification_type) + if not simulated != api_user.key_type == KEY_TYPE_TEST: + check_email_daily_limit(authenticated_service, 1) + check_template_is_for_notification_type(notification_type, template.template_type) check_template_is_active(template) @@ -124,7 +131,6 @@ def send_notification(notification_type: NotificationType): _service_can_send_internationally(authenticated_service, notification_form["to"]) # Do not persist or send notification to the queue if it is a simulated recipient - simulated = simulated_recipient(notification_form["to"], notification_type) notification_model = persist_notification( template_id=template.id, template_version=template.version, diff --git a/tests/app/notifications/rest/test_send_notification.py b/tests/app/notifications/rest/test_send_notification.py index 9cf45dd9ae..71f8c63ec6 100644 --- a/tests/app/notifications/rest/test_send_notification.py +++ b/tests/app/notifications/rest/test_send_notification.py @@ -28,7 +28,11 @@ Template, ) from app.utils import get_document_url -from app.v2.errors import RateLimitError, TooManyRequestsError +from app.v2.errors import ( + RateLimitError, + TooManyRequestsError, + TrialServiceTooManyEmailRequestsError, +) from tests import create_authorization_header from tests.app.conftest import ( create_sample_api_key, @@ -417,13 +421,14 @@ def test_should_block_api_call_if_over_day_limit_for_live_service(notify_db, not def test_should_block_api_call_if_over_day_limit_for_restricted_service(notify_db, notify_db_session, notify_api, mocker): with notify_api.test_request_context(): with notify_api.test_client() as client: - mocker.patch("app.celery.provider_tasks.deliver_sms.apply_async") + mocker.patch("app.celery.provider_tasks.deliver_email.apply_async") mocker.patch( "app.notifications.validators.check_email_daily_limit", - side_effect=TooManyRequestsError(1), + side_effect=TrialServiceTooManyEmailRequestsError(1), ) service = create_sample_service(notify_db, notify_db_session, limit=1, restricted=True) + create_sample_service_safelist(notify_db, notify_db_session, service=service, email_address="ok@ok.com") email_template = create_sample_email_template(notify_db, notify_db_session, service=service) create_sample_notification( notify_db,