diff --git a/app/celery/tasks.py b/app/celery/tasks.py index cf6c9dcdc..f9540fa9d 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -219,13 +219,11 @@ def send_sms(service_id, notification_id, encrypted_notification, created_at): provider = provider_to_use('sms', notification_id) - restricted = False - if not service_allowed_to_send_to(notification['to'], service): current_app.logger.info( "SMS {} failed as restricted service".format(notification_id) ) - restricted = True + return try: @@ -244,7 +242,7 @@ def send_sms(service_id, notification_id, encrypted_notification, created_at): service_id=service_id, job_id=notification.get('job', None), job_row_number=notification.get('row_number', None), - status='failed' if restricted else 'sending', + status='sending', created_at=datetime.strptime(created_at, DATETIME_FORMAT), sent_at=sent_at, sent_by=provider.get_name(), @@ -257,9 +255,6 @@ def send_sms(service_id, notification_id, encrypted_notification, created_at): ) dao_create_notification(notification_db_object, TEMPLATE_TYPE_SMS, provider.get_name()) - if restricted: - return - try: provider.send_sms( to=validate_and_format_phone_number(notification['to']), @@ -292,13 +287,11 @@ def send_email(service_id, notification_id, from_address, encrypted_notification provider = provider_to_use('email', notification_id) - restricted = False - if not service_allowed_to_send_to(notification['to'], service): current_app.logger.info( "Email {} failed as restricted service".format(notification_id) ) - restricted = True + return try: sent_at = datetime.utcnow() @@ -310,7 +303,7 @@ def send_email(service_id, notification_id, from_address, encrypted_notification service_id=service_id, job_id=notification.get('job', None), job_row_number=notification.get('row_number', None), - status='failed' if restricted else 'sending', + status='sending', created_at=datetime.strptime(created_at, DATETIME_FORMAT), sent_at=sent_at, sent_by=provider.get_name() @@ -323,9 +316,6 @@ def send_email(service_id, notification_id, from_address, encrypted_notification datetime.strptime(created_at, DATETIME_FORMAT) ) - if restricted: - return - try: template = Template( dao_get_template_by_id(notification['template'], notification['template_version']).__dict__, diff --git a/tests/app/celery/test_tasks.py b/tests/app/celery/test_tasks.py index d6aa09d9a..b0d6e70b2 100644 --- a/tests/app/celery/test_tasks.py +++ b/tests/app/celery/test_tasks.py @@ -456,8 +456,9 @@ def test_should_not_send_sms_if_restricted_service_and_invalid_number(notify_db, "encrypted-in-reality", now.strftime(DATETIME_FORMAT) ) - mmg_client.send_sms.assert_not_called() + with pytest.raises(NoResultFound): + notifications_dao.get_notification(service.id, notification_id) def test_send_sms_should_use_template_version_from_job_not_latest(sample_template, mocker): @@ -557,6 +558,8 @@ def test_should_not_send_email_if_restricted_service_and_invalid_email_address(n ) aws_ses_client.send_email.assert_not_called() + with pytest.raises(NoResultFound): + notifications_dao.get_notification(service.id, notification_id) def test_should_send_template_to_correct_sms_provider_and_persist_with_job_id(sample_job, mocker):