diff --git a/app/notifications/rest.py b/app/notifications/rest.py index 99be6b0b2..982e3128d 100644 --- a/app/notifications/rest.py +++ b/app/notifications/rest.py @@ -217,7 +217,6 @@ def send_notification(notification_type): template_id=notification['template'], service_id=service_id ) - errors = unarchived_template_schema.validate({'archived': template.archived}) if errors: raise InvalidRequest(errors, status_code=400) @@ -270,15 +269,17 @@ def send_notification(notification_type): notification_id = create_uuid() notification.update({"template_version": template.version}) - persist_notification( - service, - notification_id, - notification, - datetime.utcnow().strftime(DATETIME_FORMAT), - notification_type, - str(api_user.id), - api_user.key_type - ) + + if not _simulated_recipient(notification['to'], notification_type): + persist_notification( + service, + notification_id, + notification, + datetime.utcnow().strftime(DATETIME_FORMAT), + notification_type, + str(api_user.id), + api_user.key_type + ) return jsonify( data=get_notification_return_data( @@ -311,6 +312,12 @@ def get_notification_statistics_for_day(): return jsonify(data=data), 200 +def _simulated_recipient(to_address, notification_type): + return (to_address in current_app.config['SIMULATED_SMS_NUMBERS'] + if notification_type == SMS_TYPE + else to_address in current_app.config['SIMULATED_EMAIL_ADDRESSES']) + + def persist_notification( service, notification_id, diff --git a/config.py b/config.py index 6b2be3721..fed24ce27 100644 --- a/config.py +++ b/config.py @@ -142,6 +142,13 @@ class Config(object): SENDING_NOTIFICATIONS_TIMEOUT_PERIOD = 259200 + SIMULATED_EMAIL_ADDRESSES = ('simulate-delivered@notifications.service.gov.uk', + 'simulate-permanent-failure@notifications.service.gov.uk', + 'simulate-temporary-failure@notifications.service.gov.uk', + ) + + SIMULATED_SMS_NUMBERS = ('+447700900000', '+447700900111', '+447700900222') + ###################### # Config overrides ### diff --git a/tests/app/notifications/rest/test_send_notification.py b/tests/app/notifications/rest/test_send_notification.py index 9dd29e1b5..0b5e5eaf0 100644 --- a/tests/app/notifications/rest/test_send_notification.py +++ b/tests/app/notifications/rest/test_send_notification.py @@ -674,14 +674,11 @@ def test_should_not_return_html_in_body(notify_api, notify_db, notify_db_session def test_should_not_send_email_if_team_api_key_and_not_a_service_user(notify_api, sample_email_template, mocker): with notify_api.test_request_context(), notify_api.test_client() as client: mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async') - data = { 'to': "not-someone-we-trust@email-address.com", 'template': str(sample_email_template.id), } - # import pdb - # pdb.set_trace() auth_header = create_authorization_header(service_id=sample_email_template.service_id, key_type=KEY_TYPE_TEAM) response = client.post( @@ -944,3 +941,61 @@ def test_should_delete_sms_notification_and_return_error_if_sqs_fails(notify_api assert response.status_code == 500 assert not notifications_dao.get_notification_by_id(fake_uuid) assert not NotificationHistory.query.get(fake_uuid) + + +@pytest.mark.parametrize('to_email', [ + 'simulate-delivered@notifications.service.gov.uk', + 'simulate-permanent-failure@notifications.service.gov.uk', + 'simulate-temporary-failure@notifications.service.gov.uk' +]) +def test_should_not_persist_notification_or_send_email_if_simulated_email( + client, + to_email, + sample_email_template, + mocker): + apply_async = mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async') + + data = { + 'to': to_email, + 'template': sample_email_template.id + } + + auth_header = create_authorization_header(service_id=sample_email_template.service_id) + + response = client.post( + path='/notifications/email', + data=json.dumps(data), + headers=[('Content-Type', 'application/json'), auth_header]) + + assert response.status_code == 201 + apply_async.assert_not_called() + assert Notification.query.count() == 0 + + +@pytest.mark.parametrize('to_sms', [ + '07700 900000', + '07700 900111', + '07700 900222' +]) +def test_should_not_persist_notification_or_send_sms_if_simulated_number( + client, + to_sms, + sample_template, + mocker): + apply_async = mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async') + + data = { + 'to': to_sms, + 'template': sample_template.id + } + + auth_header = create_authorization_header(service_id=sample_template.service_id) + + response = client.post( + path='/notifications/sms', + data=json.dumps(data), + headers=[('Content-Type', 'application/json'), auth_header]) + + assert response.status_code == 201 + apply_async.assert_not_called() + assert Notification.query.count() == 0