Skip to content

Commit

Permalink
Fix tests, squashed a bug
Browse files Browse the repository at this point in the history
  • Loading branch information
whabanks committed Feb 1, 2024
1 parent be024bd commit 8304bc0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 7 additions & 1 deletion app/notifications/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
EMAIL_TYPE,
INTERNATIONAL_SMS_TYPE,
KEY_TYPE_TEAM,
KEY_TYPE_TEST,
LETTER_TYPE,
SMS_TYPE,
NotificationType,
Expand All @@ -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,
Expand Down Expand Up @@ -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)

Expand All @@ -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,
Expand Down
11 changes: 8 additions & 3 deletions tests/app/notifications/rest/test_send_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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="[email protected]")
email_template = create_sample_email_template(notify_db, notify_db_session, service=service)
create_sample_notification(
notify_db,
Expand Down

0 comments on commit 8304bc0

Please sign in to comment.