From 61af203ad666955b8aeee468ecea1f92d981c9ac Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Thu, 11 Feb 2021 16:39:25 +0000 Subject: [PATCH] User cache for service in send_to_provider methods. This will remove a call to the db if the service exists in the cache. --- app/config.py | 1 + app/delivery/send_to_providers.py | 8 ++--- app/serialised_models.py | 3 ++ tests/app/delivery/test_send_to_providers.py | 31 +++++++++++++------- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/app/config.py b/app/config.py index 372334cab..0e6aab7a4 100644 --- a/app/config.py +++ b/app/config.py @@ -430,6 +430,7 @@ class Development(Config): class Test(Development): + SQLALCHEMY_ECHO = False NOTIFY_EMAIL_DOMAIN = 'test.notify.com' FROM_NUMBER = 'testing' NOTIFY_ENVIRONMENT = 'test' diff --git a/app/delivery/send_to_providers.py b/app/delivery/send_to_providers.py index 6fdf31d86..44a8fc736 100644 --- a/app/delivery/send_to_providers.py +++ b/app/delivery/send_to_providers.py @@ -28,11 +28,11 @@ from app.models import ( NOTIFICATION_SENDING, NOTIFICATION_STATUS_TYPES_COMPLETED ) -from app.serialised_models import SerialisedTemplate +from app.serialised_models import SerialisedTemplate, SerialisedService def send_sms_to_provider(notification): - service = notification.service + service = SerialisedService.from_id(notification.service_id) service_id = service.id if not service.active: technical_failure(notification=notification) @@ -42,7 +42,7 @@ def send_sms_to_provider(notification): provider = provider_to_use(SMS_TYPE, notification.international) template_dict = SerialisedTemplate.get_dict(template_id=notification.template_id, - service_id=service_id, + service_id=service.id, version=notification.template_version)['data'] template = SMSMessageTemplate( @@ -87,7 +87,7 @@ def send_sms_to_provider(notification): def send_email_to_provider(notification): - service = notification.service + service = SerialisedService.from_id(notification.service_id) service_id = service.id if not service.active: technical_failure(notification=notification) diff --git a/app/serialised_models.py b/app/serialised_models.py index 31870a3d5..8f9c93d69 100644 --- a/app/serialised_models.py +++ b/app/serialised_models.py @@ -75,6 +75,7 @@ class SerialisedTemplate(SerialisedModel): class SerialisedService(SerialisedModel): ALLOWED_PROPERTIES = { 'id', + 'name', 'active', 'contact_link', 'email_from', @@ -83,6 +84,8 @@ class SerialisedService(SerialisedModel): 'rate_limit', 'research_mode', 'restricted', + 'prefix_sms', + 'email_branding' } @classmethod diff --git a/tests/app/delivery/test_send_to_providers.py b/tests/app/delivery/test_send_to_providers.py index ad8e98f0c..e41eb43cb 100644 --- a/tests/app/delivery/test_send_to_providers.py +++ b/tests/app/delivery/test_send_to_providers.py @@ -736,48 +736,59 @@ def test_send_email_to_provider_uses_reply_to_from_notification( def test_send_sms_to_provider_should_return_template_if_found_in_redis( mocker, client, sample_template ): - - from app.schemas import template_schema + from app.schemas import service_schema, template_schema + service_dict = service_schema.dump(sample_template.service).data template_dict = template_schema.dump(sample_template).data - notification = create_notification(template=sample_template, - to_field= '+447700900855', - normalised_to=validate_and_format_phone_number('+447700900855')) mocker.patch( 'app.redis_store.get', side_effect=[ + json.dumps({'data': service_dict}).encode('utf-8'), json.dumps({'data': template_dict}).encode('utf-8'), ], ) mock_get_template = mocker.patch( 'app.dao.templates_dao.dao_get_template_by_id_and_service_id' ) + mock_get_service = mocker.patch( + 'app.dao.services_dao.dao_fetch_service_by_id' + ) + mocker.patch('app.mmg_client.send_sms') # mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async') - + notification = create_notification(template=sample_template, + to_field='+447700900855', + normalised_to=validate_and_format_phone_number('+447700900855')) send_to_providers.send_sms_to_provider(notification) assert mock_get_template.called is False + assert mock_get_service.called is False def test_send_email_to_provider_should_return_template_if_found_in_redis( mocker, client, sample_email_template ): - from app.schemas import template_schema + from app.schemas import service_schema, template_schema + service_dict = service_schema.dump(sample_email_template.service).data template_dict = template_schema.dump(sample_email_template).data - notification = create_notification(template=sample_email_template, - to_field='test@example.com', - normalised_to='test@example.com') mocker.patch( 'app.redis_store.get', side_effect=[ + json.dumps({'data': service_dict}).encode('utf-8'), json.dumps({'data': template_dict}).encode('utf-8'), ], ) mock_get_template = mocker.patch( 'app.dao.templates_dao.dao_get_template_by_id_and_service_id' ) + mock_get_service = mocker.patch( + 'app.dao.services_dao.dao_fetch_service_by_id' + ) mocker.patch('app.aws_ses_client.send_email', return_value='reference') + notification = create_notification(template=sample_email_template, + to_field='test@example.com', + normalised_to='test@example.com') send_to_providers.send_email_to_provider(notification) assert mock_get_template.called is False + assert mock_get_service.called is False