From 323b2ff537fad79e5c8d3375cb593a8af8953d7b Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Wed, 6 Apr 2016 15:56:34 +0100 Subject: [PATCH] Use MMG client for send-sms --- app/celery/tasks.py | 5 +-- app/clients/sms/mmg.py | 2 +- tests/app/celery/test_tasks.py | 57 ++++++++++++++++------------------ 3 files changed, 31 insertions(+), 33 deletions(-) diff --git a/app/celery/tasks.py b/app/celery/tasks.py index 999888357..720bed74d 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -6,6 +6,7 @@ from sqlalchemy.exc import SQLAlchemyError from app.clients.email.aws_ses import AwsSesClientException from app.clients.sms.firetext import FiretextClientException +from app.clients.sms.mmg import MMGClientException from app.dao.services_dao import dao_fetch_service_by_id from app.dao.templates_dao import dao_get_template_by_id @@ -198,7 +199,7 @@ def remove_job(job_id): def send_sms(service_id, notification_id, encrypted_notification, created_at): notification = encryption.decrypt(encrypted_notification) service = dao_fetch_service_by_id(service_id) - client = firetext_client + client = mmg_client restricted = False @@ -239,7 +240,7 @@ def send_sms(service_id, notification_id, encrypted_notification, created_at): content=template.replaced, reference=str(notification_id) ) - except FiretextClientException as e: + except MMGClientException as e: current_app.logger.error( "SMS notification {} failed".format(notification_id) ) diff --git a/app/clients/sms/mmg.py b/app/clients/sms/mmg.py index 14e2ef526..9efffee88 100644 --- a/app/clients/sms/mmg.py +++ b/app/clients/sms/mmg.py @@ -42,7 +42,7 @@ class MMGClient(SmsClient): super(SmsClient, self).__init__(*args, *kwargs) self.api_key = config.get('MMG_API_KEY') self.from_number = config.get('NOTIFY_FROM_NUMBER') - self.name = 'mmg' + self.name = 'MMG' def get_name(self): return self.name diff --git a/tests/app/celery/test_tasks.py b/tests/app/celery/test_tasks.py index 5d87719d2..5fe212664 100644 --- a/tests/app/celery/test_tasks.py +++ b/tests/app/celery/test_tasks.py @@ -16,9 +16,8 @@ from app.celery.tasks import ( delete_failed_notifications, delete_successful_notifications ) -from app import (firetext_client, aws_ses_client, encryption, DATETIME_FORMAT, mmg_client) +from app import (aws_ses_client, encryption, DATETIME_FORMAT, mmg_client) from app.clients.email.aws_ses import AwsSesClientException -from app.clients.sms.firetext import FiretextClientException from app.clients.sms.mmg import MMGClientException from app.dao import notifications_dao, jobs_dao from sqlalchemy.exc import SQLAlchemyError @@ -42,10 +41,6 @@ class AnyStringWith(str): def __eq__(self, other): return self in other - -def firetext_error(): - return {'code': 0, 'description': 'error'} - mmg_error = {'Error': '40', 'Description': 'error'} @@ -278,8 +273,8 @@ def test_should_send_template_to_correct_sms_provider_and_persist(sample_templat "personalisation": {"name": "Jo"} } mocker.patch('app.encryption.decrypt', return_value=notification) - mocker.patch('app.firetext_client.send_sms') - mocker.patch('app.firetext_client.get_name', return_value="firetext") + mocker.patch('app.mmg_client.send_sms') + mocker.patch('app.mmg_client.get_name', return_value="MMG") notification_id = uuid.uuid4() now = datetime.utcnow() @@ -290,7 +285,7 @@ def test_should_send_template_to_correct_sms_provider_and_persist(sample_templat now.strftime(DATETIME_FORMAT) ) - firetext_client.send_sms.assert_called_once_with( + mmg_client.send_sms.assert_called_once_with( to=format_phone_number(validate_phone_number("+447234123123")), content="Sample service: Hello Jo", reference=str(notification_id) @@ -304,7 +299,7 @@ def test_should_send_template_to_correct_sms_provider_and_persist(sample_templat assert persisted_notification.status == 'sent' assert persisted_notification.created_at == now assert persisted_notification.sent_at > now - assert persisted_notification.sent_by == 'firetext' + assert persisted_notification.sent_by == 'MMG' assert not persisted_notification.job_id @@ -314,8 +309,8 @@ def test_should_send_sms_without_personalisation(sample_template, mocker): "to": "+447234123123" } mocker.patch('app.encryption.decrypt', return_value=notification) - mocker.patch('app.firetext_client.send_sms') - mocker.patch('app.firetext_client.get_name', return_value="firetext") + mocker.patch('app.mmg_client.send_sms') + mocker.patch('app.mmg_client.get_name', return_value="MMG") notification_id = uuid.uuid4() now = datetime.utcnow() @@ -326,7 +321,7 @@ def test_should_send_sms_without_personalisation(sample_template, mocker): now.strftime(DATETIME_FORMAT) ) - firetext_client.send_sms.assert_called_once_with( + mmg_client.send_sms.assert_called_once_with( to=format_phone_number(validate_phone_number("+447234123123")), content="Sample service: This is a template", reference=str(notification_id) @@ -343,8 +338,8 @@ def test_should_send_sms_if_restricted_service_and_valid_number(notify_db, notif "to": "+447700900890" # The user’s own number, but in a different format } mocker.patch('app.encryption.decrypt', return_value=notification) - mocker.patch('app.firetext_client.send_sms') - mocker.patch('app.firetext_client.get_name', return_value="firetext") + mocker.patch('app.mmg_client.send_sms') + mocker.patch('app.mmg_client.get_name', return_value="MMG") notification_id = uuid.uuid4() now = datetime.utcnow() @@ -355,7 +350,7 @@ def test_should_send_sms_if_restricted_service_and_valid_number(notify_db, notif now.strftime(DATETIME_FORMAT) ) - firetext_client.send_sms.assert_called_once_with( + mmg_client.send_sms.assert_called_once_with( to=format_phone_number(validate_phone_number("+447700900890")), content="Sample service: This is a template", reference=str(notification_id) @@ -372,8 +367,8 @@ def test_should_not_send_sms_if_restricted_service_and_invalid_number(notify_db, "to": "07700 900849" } mocker.patch('app.encryption.decrypt', return_value=notification) - mocker.patch('app.firetext_client.send_sms') - mocker.patch('app.firetext_client.get_name', return_value="firetext") + mocker.patch('app.mmg_client.send_sms') + mocker.patch('app.mmg_client.get_name', return_value="MMG") notification_id = uuid.uuid4() now = datetime.utcnow() @@ -384,7 +379,7 @@ def test_should_not_send_sms_if_restricted_service_and_invalid_number(notify_db, now.strftime(DATETIME_FORMAT) ) - firetext_client.send_sms.assert_not_called() + mmg_client.send_sms.assert_not_called() def test_should_send_email_if_restricted_service_and_valid_email(notify_db, notify_db_session, mocker): @@ -435,9 +430,11 @@ def test_should_not_send_email_if_restricted_service_and_invalid_email_address(n notification_id = uuid.uuid4() now = datetime.utcnow() - send_sms( + send_email( service.id, notification_id, + 'subject', + 'email_from', "encrypted-in-reality", now.strftime(DATETIME_FORMAT) ) @@ -452,8 +449,8 @@ def test_should_send_template_to_correct_sms_provider_and_persist_with_job_id(sa "to": "+447234123123" } mocker.patch('app.encryption.decrypt', return_value=notification) - mocker.patch('app.firetext_client.send_sms') - mocker.patch('app.firetext_client.get_name', return_value="firetext") + mocker.patch('app.mmg_client.send_sms') + mocker.patch('app.mmg_client.get_name', return_value="MMG") notification_id = uuid.uuid4() now = datetime.utcnow() @@ -463,7 +460,7 @@ def test_should_send_template_to_correct_sms_provider_and_persist_with_job_id(sa "encrypted-in-reality", now.strftime(DATETIME_FORMAT) ) - firetext_client.send_sms.assert_called_once_with( + mmg_client.send_sms.assert_called_once_with( to=format_phone_number(validate_phone_number("+447234123123")), content="Sample service: This is a template", reference=str(notification_id) @@ -476,7 +473,7 @@ def test_should_send_template_to_correct_sms_provider_and_persist_with_job_id(sa assert persisted_notification.status == 'sent' assert persisted_notification.sent_at > now assert persisted_notification.created_at == now - assert persisted_notification.sent_by == 'firetext' + assert persisted_notification.sent_by == 'MMG' def test_should_use_email_template_and_persist(sample_email_template_with_placeholders, mocker): @@ -578,8 +575,8 @@ def test_should_persist_notification_as_failed_if_sms_client_fails(sample_templa "to": "+447234123123" } mocker.patch('app.encryption.decrypt', return_value=notification) - mocker.patch('app.firetext_client.send_sms', side_effect=FiretextClientException(firetext_error())) - mocker.patch('app.firetext_client.get_name', return_value="firetext") + mocker.patch('app.mmg_client.send_sms', side_effect=MMGClientException(mmg_error)) + mocker.patch('app.mmg_client.get_name', return_value="MMG") now = datetime.utcnow() notification_id = uuid.uuid4() @@ -590,7 +587,7 @@ def test_should_persist_notification_as_failed_if_sms_client_fails(sample_templa "encrypted-in-reality", now.strftime(DATETIME_FORMAT) ) - firetext_client.send_sms.assert_called_once_with( + mmg_client.send_sms.assert_called_once_with( to=format_phone_number(validate_phone_number("+447234123123")), content="Sample service: This is a template", reference=str(notification_id) @@ -602,7 +599,7 @@ def test_should_persist_notification_as_failed_if_sms_client_fails(sample_templa assert persisted_notification.status == 'failed' assert persisted_notification.created_at == now assert persisted_notification.sent_at > now - assert persisted_notification.sent_by == 'firetext' + assert persisted_notification.sent_by == 'MMG' def test_should_persist_notification_as_failed_if_email_client_fails(sample_email_template, mocker): @@ -649,7 +646,7 @@ def test_should_not_send_sms_if_db_peristance_failed(sample_template, mocker): "to": "+447234123123" } mocker.patch('app.encryption.decrypt', return_value=notification) - mocker.patch('app.firetext_client.send_sms') + mocker.patch('app.mmg_client.send_sms') mocker.patch('app.db.session.add', side_effect=SQLAlchemyError()) now = datetime.utcnow() @@ -661,7 +658,7 @@ def test_should_not_send_sms_if_db_peristance_failed(sample_template, mocker): "encrypted-in-reality", now.strftime(DATETIME_FORMAT) ) - firetext_client.send_sms.assert_not_called() + mmg_client.send_sms.assert_not_called() with pytest.raises(NoResultFound) as e: notifications_dao.get_notification(sample_template.service_id, notification_id) assert 'No row was found for one' in str(e.value)