diff --git a/app/models.py b/app/models.py index fb530ea3b..b39de532d 100644 --- a/app/models.py +++ b/app/models.py @@ -144,6 +144,7 @@ EMAIL_TYPE = 'email' WHITELIST_RECIPIENT_TYPE = [MOBILE_TYPE, EMAIL_TYPE] whitelist_recipient_types = db.Enum(*WHITELIST_RECIPIENT_TYPE, name='recipient_type') + class ServiceWhitelist(db.Model): __tablename__ = 'service_whitelist' diff --git a/app/notifications/rest.py b/app/notifications/rest.py index b5dad7427..269da9bef 100644 --- a/app/notifications/rest.py +++ b/app/notifications/rest.py @@ -254,7 +254,8 @@ def send_notification(notification_type): notification['to'], itertools.chain( itertools.chain.from_iterable([user.mobile_number, user.email_address] for user in service.users), - ([member.recipient for member in service.whitelist]) if api_user.key_type == KEY_TYPE_NORMAL else iter([]) + ([member.recipient for member in service.whitelist]) + if api_user.key_type == KEY_TYPE_NORMAL else iter([]) ) ) )): diff --git a/tests/app/notifications/rest/test_send_notification.py b/tests/app/notifications/rest/test_send_notification.py index f6a0145de..56fcfad33 100644 --- a/tests/app/notifications/rest/test_send_notification.py +++ b/tests/app/notifications/rest/test_send_notification.py @@ -1004,12 +1004,11 @@ def test_should_not_persist_notification_or_send_sms_if_simulated_number( @pytest.mark.parametrize('to_sms', ['07827992635']) -def test_should_not_send_sms_to_non_whitelist_recipient_in_trial_mode_with_live_key( - client, - notify_db, - notify_db_session, - to_sms, - mocker): +def test_should_not_send_sms_to_non_whitelist_recipient_in_trial_mode_with_live_key(client, + notify_db, + notify_db_session, + to_sms, + mocker): apply_async = mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async') service = create_sample_service(notify_db, notify_db_session, limit=2, restricted=True) service_whitelist = create_sample_service_whitelist(notify_db, notify_db_session, service=service) @@ -1044,13 +1043,13 @@ def test_should_not_send_sms_to_non_whitelist_recipient_in_trial_mode_with_live_ assert expected_response_message in json_resp['message']['to'] apply_async.assert_not_called() + @pytest.mark.parametrize('to_email', ['non_whitelist_recipient@mail.com']) -def test_should_not_send_email_to_non_whitelist_recipient_in_trial_mode_with_live_key( - client, - notify_db, - notify_db_session, - to_email, - mocker): +def test_should_not_send_email_to_non_whitelist_recipient_in_trial_mode_with_live_key(client, + notify_db, + notify_db_session, + to_email, + mocker): apply_async = mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async') service = create_sample_service(notify_db, notify_db_session, limit=2, restricted=True) service_whitelist = create_sample_service_whitelist(notify_db, notify_db_session, service=service) @@ -1087,15 +1086,15 @@ def test_should_not_send_email_to_non_whitelist_recipient_in_trial_mode_with_liv @pytest.mark.parametrize('to_sms', ['07827992635']) -def test_should_not_send_sms_to_whitelist_recipient_in_trial_mode_with_team_key( - client, - notify_db, - notify_db_session, - to_sms, - mocker): +def test_should_not_send_sms_to_whitelist_recipient_in_trial_mode_with_team_key(client, + notify_db, + notify_db_session, + to_sms, + mocker): apply_async = mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async') service = create_sample_service(notify_db, notify_db_session, limit=2, restricted=True) - service_whitelist = create_sample_service_whitelist(notify_db, notify_db_session, service=service, mobile_number=to_sms) + service_whitelist = create_sample_service_whitelist(notify_db, notify_db_session, + service=service, mobile_number=to_sms) sms_template = create_sample_template(notify_db, notify_db_session, service=service) assert service_whitelist.service_id == service.id @@ -1124,15 +1123,15 @@ def test_should_not_send_sms_to_whitelist_recipient_in_trial_mode_with_team_key( @pytest.mark.parametrize('to_email', ['non_whitelist_recipient@mail.com']) -def test_should_not_send_email_to_whitelist_recipient_in_trial_mode_with_team_key( - client, - notify_db, - notify_db_session, - to_email, - mocker): +def test_should_not_send_email_to_whitelist_recipient_in_trial_mode_with_team_key(client, + notify_db, + notify_db_session, + to_email, + mocker): apply_async = mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async') service = create_sample_service(notify_db, notify_db_session, limit=2, restricted=True) - service_whitelist = create_sample_service_whitelist(notify_db, notify_db_session, service=service, email_address=to_email) + service_whitelist = create_sample_service_whitelist(notify_db, notify_db_session, + service=service, email_address=to_email) email_template = create_sample_email_template(notify_db, notify_db_session, service=service) assert service_whitelist.service_id == service.id @@ -1161,16 +1160,16 @@ def test_should_not_send_email_to_whitelist_recipient_in_trial_mode_with_team_ke @pytest.mark.parametrize('to_sms', ['07123123123']) -def test_should_send_sms_to_whitelist_recipient_in_trial_mode_with_live_key( - client, - notify_db, - notify_db_session, - to_sms, - mocker): +def test_should_send_sms_to_whitelist_recipient_in_trial_mode_with_live_key(client, + notify_db, + notify_db_session, + to_sms, + mocker): apply_async = mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async') service = create_sample_service(notify_db, notify_db_session, limit=2, restricted=True) - service_whitelist = create_sample_service_whitelist(notify_db, notify_db_session, service=service, mobile_number=to_sms) + service_whitelist = create_sample_service_whitelist(notify_db, notify_db_session, + service=service, mobile_number=to_sms) sms_template = create_sample_template(notify_db, notify_db_session, service=service) assert service_whitelist.service_id == service.id @@ -1200,16 +1199,16 @@ def test_should_send_sms_to_whitelist_recipient_in_trial_mode_with_live_key( @pytest.mark.parametrize('to_email', ['whitelist_recipient@mail.com']) -def test_should_send_email_to_whitelist_recipient_in_trial_mode_with_live_key( - client, - notify_db, - notify_db_session, - to_email, - mocker): +def test_should_send_email_to_whitelist_recipient_in_trial_mode_with_live_key(client, + notify_db, + notify_db_session, + to_email, + mocker): apply_async = mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async') service = create_sample_service(notify_db, notify_db_session, limit=2, restricted=True) - service_whitelist = create_sample_service_whitelist(notify_db, notify_db_session, service=service, email_address=to_email) + service_whitelist = create_sample_service_whitelist(notify_db, notify_db_session, + service=service, email_address=to_email) email_template = create_sample_email_template(notify_db, notify_db_session, service=service) assert service_whitelist.service_id == service.id diff --git a/tests/app/service/test_service_whitelist.py b/tests/app/service/test_service_whitelist.py index 1eba735fa..23f6a3aa2 100644 --- a/tests/app/service/test_service_whitelist.py +++ b/tests/app/service/test_service_whitelist.py @@ -20,6 +20,7 @@ def test_get_whitelist_returns_data(client, sample_service_whitelist): 'phone_numbers': [] } + def test_get_whitelist_separates_emails_and_phones(client, sample_service): dao_add_and_commit_whitelisted_contacts([ ServiceWhitelist.from_string(sample_service.id, EMAIL_TYPE, 'service@example.com'), @@ -71,6 +72,7 @@ def test_update_whitelist_replaces_old_whitelist(client, sample_service_whitelis assert whitelist[0].recipient == '07123456789' assert whitelist[1].recipient == 'foo@bar.com' + def test_update_whitelist_doesnt_remove_old_whitelist_if_error(client, sample_service_whitelist): data = {