User cache for service in send_to_provider methods.

This will remove a call to the db if the service exists in the cache.
This commit is contained in:
Rebecca Law
2021-02-11 16:39:25 +00:00
parent 200f8aad81
commit 61af203ad6
4 changed files with 29 additions and 14 deletions

View File

@@ -430,6 +430,7 @@ class Development(Config):
class Test(Development): class Test(Development):
SQLALCHEMY_ECHO = False
NOTIFY_EMAIL_DOMAIN = 'test.notify.com' NOTIFY_EMAIL_DOMAIN = 'test.notify.com'
FROM_NUMBER = 'testing' FROM_NUMBER = 'testing'
NOTIFY_ENVIRONMENT = 'test' NOTIFY_ENVIRONMENT = 'test'

View File

@@ -28,11 +28,11 @@ from app.models import (
NOTIFICATION_SENDING, NOTIFICATION_SENDING,
NOTIFICATION_STATUS_TYPES_COMPLETED NOTIFICATION_STATUS_TYPES_COMPLETED
) )
from app.serialised_models import SerialisedTemplate from app.serialised_models import SerialisedTemplate, SerialisedService
def send_sms_to_provider(notification): def send_sms_to_provider(notification):
service = notification.service service = SerialisedService.from_id(notification.service_id)
service_id = service.id service_id = service.id
if not service.active: if not service.active:
technical_failure(notification=notification) technical_failure(notification=notification)
@@ -42,7 +42,7 @@ def send_sms_to_provider(notification):
provider = provider_to_use(SMS_TYPE, notification.international) provider = provider_to_use(SMS_TYPE, notification.international)
template_dict = SerialisedTemplate.get_dict(template_id=notification.template_id, template_dict = SerialisedTemplate.get_dict(template_id=notification.template_id,
service_id=service_id, service_id=service.id,
version=notification.template_version)['data'] version=notification.template_version)['data']
template = SMSMessageTemplate( template = SMSMessageTemplate(
@@ -87,7 +87,7 @@ def send_sms_to_provider(notification):
def send_email_to_provider(notification): def send_email_to_provider(notification):
service = notification.service service = SerialisedService.from_id(notification.service_id)
service_id = service.id service_id = service.id
if not service.active: if not service.active:
technical_failure(notification=notification) technical_failure(notification=notification)

View File

@@ -75,6 +75,7 @@ class SerialisedTemplate(SerialisedModel):
class SerialisedService(SerialisedModel): class SerialisedService(SerialisedModel):
ALLOWED_PROPERTIES = { ALLOWED_PROPERTIES = {
'id', 'id',
'name',
'active', 'active',
'contact_link', 'contact_link',
'email_from', 'email_from',
@@ -83,6 +84,8 @@ class SerialisedService(SerialisedModel):
'rate_limit', 'rate_limit',
'research_mode', 'research_mode',
'restricted', 'restricted',
'prefix_sms',
'email_branding'
} }
@classmethod @classmethod

View File

@@ -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( def test_send_sms_to_provider_should_return_template_if_found_in_redis(
mocker, client, sample_template mocker, client, sample_template
): ):
from app.schemas import service_schema, template_schema
from app.schemas import template_schema service_dict = service_schema.dump(sample_template.service).data
template_dict = template_schema.dump(sample_template).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( mocker.patch(
'app.redis_store.get', 'app.redis_store.get',
side_effect=[ side_effect=[
json.dumps({'data': service_dict}).encode('utf-8'),
json.dumps({'data': template_dict}).encode('utf-8'), json.dumps({'data': template_dict}).encode('utf-8'),
], ],
) )
mock_get_template = mocker.patch( mock_get_template = mocker.patch(
'app.dao.templates_dao.dao_get_template_by_id_and_service_id' '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.mmg_client.send_sms')
# mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async') # 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) send_to_providers.send_sms_to_provider(notification)
assert mock_get_template.called is False 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( def test_send_email_to_provider_should_return_template_if_found_in_redis(
mocker, client, sample_email_template 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 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( mocker.patch(
'app.redis_store.get', 'app.redis_store.get',
side_effect=[ side_effect=[
json.dumps({'data': service_dict}).encode('utf-8'),
json.dumps({'data': template_dict}).encode('utf-8'), json.dumps({'data': template_dict}).encode('utf-8'),
], ],
) )
mock_get_template = mocker.patch( mock_get_template = mocker.patch(
'app.dao.templates_dao.dao_get_template_by_id_and_service_id' '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') 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) send_to_providers.send_email_to_provider(notification)
assert mock_get_template.called is False assert mock_get_template.called is False
assert mock_get_service.called is False