diff --git a/app/delivery/send_to_providers.py b/app/delivery/send_to_providers.py index 7ff67b50e..6634daa32 100644 --- a/app/delivery/send_to_providers.py +++ b/app/delivery/send_to_providers.py @@ -121,21 +121,17 @@ def update_notification(notification, provider): def provider_to_use(notification_type, notification_id, international=False): - provider = None - # Default to MMG for international SMS - if notification_type == SMS_TYPE: - provider_id = 'mmg' if international else get_current_provider('sms') - provider = get_provider_details_by_identifier('mmg') - elif notification_type == EMAIL_TYPE: - provider = get_current_provider(notification_type) + active_providers_in_order = [ + p for p in get_provider_details_by_notification_type(notification_type, international) if p.active + ] - if not provider: + if not active_providers_in_order: current_app.logger.error( "{} {} failed as no active providers".format(notification_type, notification_id) ) raise Exception("No active {} providers".format(notification_type)) - return clients.get_client_by_name_and_type(provider.identifier, notification_type) + return clients.get_client_by_name_and_type(active_providers_in_order[0].identifier, notification_type) def get_logo_url(base_url, branding_path, logo_file): @@ -151,11 +147,8 @@ def get_logo_url(base_url, branding_path, logo_file): base_url = parse.urlparse(base_url) netloc = base_url.netloc - if ( - base_url.netloc.startswith('localhost') or - # covers both preview and staging - 'notify.works' in base_url.netloc - ): + # covers both preview and staging + if base_url.netloc.startswith('localhost') or 'notify.works' in base_url.netloc: path = '/static' + branding_path + logo_file else: if base_url.netloc.startswith('www'): diff --git a/tests/app/delivery/test_send_to_providers.py b/tests/app/delivery/test_send_to_providers.py index 2894d75e9..cbd384844 100644 --- a/tests/app/delivery/test_send_to_providers.py +++ b/tests/app/delivery/test_send_to_providers.py @@ -7,8 +7,9 @@ import pytest from notifications_utils.recipients import validate_phone_number, format_phone_number import app -from app import mmg_client +from app import mmg_client, firetext_client from app.dao import (provider_details_dao, notifications_dao) +from app.dao.provider_details_dao import dao_switch_sms_provider_to_provider_with_identifier from app.delivery import send_to_providers from app.models import ( Notification, @@ -18,7 +19,7 @@ from app.models import ( KEY_TYPE_TEAM, BRANDING_ORG, BRANDING_BOTH, -) + ProviderDetails) from tests.app.db import create_service, create_template, create_notification @@ -471,3 +472,61 @@ def test_should_update_billable_units_according_to_research_mode_and_key_type(no ) assert sample_notification.billable_units == billable_units + + +def test_should_send_sms_to_international_providers( + restore_provider_details, + sample_sms_template_with_html, + sample_user, + mocker +): + mocker.patch('app.provider_details.switch_providers.get_user_by_id', return_value=sample_user) + + dao_switch_sms_provider_to_provider_with_identifier('firetext') + + db_notification_uk = create_notification( + template=sample_sms_template_with_html, + to_field="+447234123999", + personalisation={"name": "Jo"}, + status='created', + international=False) + + db_notification_international = create_notification( + template=sample_sms_template_with_html, + to_field="+447234123111", + personalisation={"name": "Jo"}, + status='created', + international=True) + + mocker.patch('app.mmg_client.send_sms') + mocker.patch('app.firetext_client.send_sms') + + send_to_providers.send_sms_to_provider( + db_notification_uk + ) + + firetext_client.send_sms.assert_called_once_with( + to=format_phone_number(validate_phone_number("+447234123999")), + content=ANY, + reference=str(db_notification_uk.id), + sender=None + ) + + send_to_providers.send_sms_to_provider( + db_notification_international + ) + + mmg_client.send_sms.assert_called_once_with( + to=format_phone_number(validate_phone_number("+447234123111")), + content=ANY, + reference=str(db_notification_international.id), + sender=None + ) + + notification_uk = Notification.query.filter_by(id=db_notification_uk.id).one() + notification_int = Notification.query.filter_by(id=db_notification_international.id).one() + + assert notification_uk.status == 'sending' + assert notification_uk.sent_by == 'firetext' + assert notification_int.status == 'sending' + assert notification_int.sent_by == 'mmg'