Check the service is active before sending the notification to the provider.

This commit is contained in:
Rebecca Law
2017-01-31 13:53:13 +00:00
parent ef0f708da0
commit fde3216f9e
5 changed files with 42 additions and 15 deletions

View File

@@ -203,7 +203,7 @@ class Test(Config):
Queue('send-email', Exchange('default'), routing_key='send-email'),
Queue('research-mode', Exchange('default'), routing_key='research-mode')
]
REDIS_ENABLED = False
REDIS_ENABLED = True
API_HOST_NAME = "http://localhost:6011"

View File

@@ -13,19 +13,24 @@ from app.dao.provider_details_dao import (
get_provider_details_by_notification_type,
dao_toggle_sms_provider
)
from app.dao.services_dao import dao_fetch_service_by_id
from app.celery.research_mode_tasks import send_sms_response, send_email_response
from app.dao.templates_dao import dao_get_template_by_id
from app.models import SMS_TYPE, KEY_TYPE_TEST, BRANDING_ORG, EMAIL_TYPE
def send_sms_to_provider(notification):
service = dao_fetch_service_by_id(notification.service_id)
provider = provider_to_use(SMS_TYPE, notification.id)
current_app.logger.info(
"Starting sending SMS {} to provider at {}".format(notification.id, datetime.utcnow())
)
service = notification.service
if not notification.service.active:
current_app.logger.warn(
"Send sms for notification id {} to provider is not allowed: service {} is inactive".format(notification.id,
service.id))
return
if notification.status == 'created':
provider = provider_to_use(SMS_TYPE, notification.id)
current_app.logger.info(
"Starting sending SMS {} to provider at {}".format(notification.id, datetime.utcnow())
)
template_model = dao_get_template_by_id(notification.template_id, notification.template_version)
template = SMSMessageTemplate(
template_model.__dict__,
@@ -61,12 +66,18 @@ def send_sms_to_provider(notification):
def send_email_to_provider(notification):
service = dao_fetch_service_by_id(notification.service_id)
provider = provider_to_use(EMAIL_TYPE, notification.id)
current_app.logger.info(
"Starting sending EMAIL {} to provider at {}".format(notification.id, datetime.utcnow())
)
service = notification.service
if not notification.service.active:
current_app.logger.warn(
"Send email for notification id {} to provider is not allowed: service {} is inactive".format(
notification.id,
service.id))
return
if notification.status == 'created':
provider = provider_to_use(EMAIL_TYPE, notification.id)
current_app.logger.info(
"Starting sending EMAIL {} to provider at {}".format(notification.id, datetime.utcnow())
)
template_dict = dao_get_template_by_id(notification.template_id, notification.template_version).__dict__
html_email = HTMLEmailTemplate(

View File

@@ -112,7 +112,7 @@ def get_jobs_by_service(service_id):
def create_job(service_id):
service = dao_fetch_service_by_id(service_id)
if not service.active:
raise InvalidRequest("Unable to create job to inactive service", 400)
raise InvalidRequest("Create job is not allowed: service is inactive ", 403)
data = request.get_json()

View File

@@ -117,6 +117,19 @@ def test_should_send_personalised_template_to_correct_email_provider_and_persist
assert notification.personalisation == {"name": "Jo"}
@pytest.mark.parametrize("client_send",
["app.aws_ses_client.send_email",
"app.mmg_client.send_sms",
"app.firetext_client.send_sms"])
def test_should_not_send_message_when_service_is_inactive(sample_service, sample_notification, mocker,
client_send):
sample_service.active = False
send_to_providers.send_email_to_provider(sample_notification)
send_mock = mocker.patch(client_send, return_value='reference')
send_mock.assert_not_called()
def test_send_sms_should_use_template_version_from_notification_not_latest(
notify_db,
notify_db_session,

View File

@@ -170,7 +170,7 @@ def test_create_scheduled_job(notify_api, sample_template, mocker, fake_uuid):
assert resp_json['data']['original_file_name'] == 'thisisatest.csv'
def test_create_job_returns_400_if_service_is_not_active(client, fake_uuid, sample_service, mocker):
def test_create_job_returns_403_if_service_is_not_active(client, fake_uuid, sample_service, mocker):
sample_service.active = False
mock_job_dao = mocker.patch("app.dao.jobs_dao.dao_create_job")
auth_header = create_authorization_header()
@@ -178,7 +178,10 @@ def test_create_job_returns_400_if_service_is_not_active(client, fake_uuid, samp
data="",
headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 400
assert response.status_code == 403
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json['result'] == 'error'
assert resp_json['message'] == "Create job is not allowed: service is inactive "
mock_job_dao.assert_not_called()