Fix issue where test key cannot send messages to external numbers

This commit is contained in:
Imdad Ahad
2016-09-07 09:57:20 +01:00
parent 727c00af21
commit 840b99b277
2 changed files with 69 additions and 6 deletions
+8 -5
View File
@@ -28,7 +28,8 @@ from app.models import (
Notification, Notification,
EMAIL_TYPE, EMAIL_TYPE,
SMS_TYPE, SMS_TYPE,
KEY_TYPE_NORMAL KEY_TYPE_NORMAL,
KEY_TYPE_TEST
) )
from app.statsd_decorators import statsd from app.statsd_decorators import statsd
@@ -126,7 +127,7 @@ def send_sms(self,
notification = encryption.decrypt(encrypted_notification) notification = encryption.decrypt(encrypted_notification)
service = dao_fetch_service_by_id(service_id) service = dao_fetch_service_by_id(service_id)
if not service_allowed_to_send_to(notification['to'], service): if not service_allowed_to_send_to(notification['to'], service, key_type):
current_app.logger.info( current_app.logger.info(
"SMS {} failed as restricted service".format(notification_id) "SMS {} failed as restricted service".format(notification_id)
) )
@@ -160,10 +161,12 @@ def send_email(self, service_id,
created_at, created_at,
api_key_id=None, api_key_id=None,
key_type=KEY_TYPE_NORMAL): key_type=KEY_TYPE_NORMAL):
notification = encryption.decrypt(encrypted_notification) notification = encryption.decrypt(encrypted_notification)
service = dao_fetch_service_by_id(service_id) service = dao_fetch_service_by_id(service_id)
if not service_allowed_to_send_to(notification['to'], service): if not service_allowed_to_send_to(notification['to'], service, key_type):
current_app.logger.info("Email {} failed as restricted service".format(notification_id)) current_app.logger.info("Email {} failed as restricted service".format(notification_id))
return return
@@ -203,8 +206,8 @@ def _save_notification(created_at, notification, notification_id, service_id, no
dao_create_notification(notification_db_object) dao_create_notification(notification_db_object)
def service_allowed_to_send_to(recipient, service): def service_allowed_to_send_to(recipient, service, key_type):
if not service.restricted: if not service.restricted or key_type == KEY_TYPE_TEST:
return True return True
return allowed_to_send_to( return allowed_to_send_to(
+61 -1
View File
@@ -17,7 +17,7 @@ from app.celery.tasks import (
send_email send_email
) )
from app.dao import jobs_dao from app.dao import jobs_dao
from app.models import Notification, KEY_TYPE_TEAM from app.models import Notification, KEY_TYPE_TEAM, KEY_TYPE_TEST
from tests.app import load_example_csv from tests.app import load_example_csv
from tests.app.conftest import ( from tests.app.conftest import (
sample_service, sample_service,
@@ -350,6 +350,66 @@ def test_should_send_sms_if_restricted_service_and_valid_number(notify_db, notif
assert persisted_notification.notification_type == 'sms' assert persisted_notification.notification_type == 'sms'
def test_should_not_send_sms_if_restricted_service_and_invalid_number_with_test_key(notify_db,
notify_db_session,
mocker):
user = sample_user(notify_db, notify_db_session, mobile_numnber="07700 900205")
service = sample_service(notify_db, notify_db_session, user=user, restricted=True)
template = sample_template(notify_db, notify_db_session, service=service)
notification = _notification_json(template, "07700 900849")
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
notification_id = uuid.uuid4()
send_sms(
service.id,
notification_id,
encryption.encrypt(notification),
datetime.utcnow().strftime(DATETIME_FORMAT),
key_type=KEY_TYPE_TEST
)
provider_tasks.send_sms_to_provider.apply_async.assert_called_once_with(
(service.id,
notification_id),
queue="send-sms"
)
persisted_notification = Notification.query.filter_by(id=notification_id).one()
assert persisted_notification.id == notification_id
def test_should_not_send_email_if_restricted_service_and_invalid_email_address_with_test_key(notify_db,
notify_db_session,
mocker):
user = sample_user(notify_db, notify_db_session)
service = sample_service(notify_db, notify_db_session, user=user, restricted=True)
template = sample_template(
notify_db, notify_db_session, service=service, template_type='email', subject_line='Hello'
)
notification = _notification_json(template, to="test@example.com")
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
notification_id = uuid.uuid4()
send_email(
service.id,
notification_id,
encryption.encrypt(notification),
datetime.utcnow().strftime(DATETIME_FORMAT),
key_type=KEY_TYPE_TEST
)
provider_tasks.send_email_to_provider.apply_async.assert_called_once_with(
(service.id,
notification_id),
queue="send-email"
)
persisted_notification = Notification.query.filter_by(id=notification_id).one()
assert persisted_notification.id == notification_id
def test_should_not_send_sms_if_restricted_service_and_invalid_number(notify_db, notify_db_session, mocker): def test_should_not_send_sms_if_restricted_service_and_invalid_number(notify_db, notify_db_session, mocker):
user = sample_user(notify_db, notify_db_session, mobile_numnber="07700 900205") user = sample_user(notify_db, notify_db_session, mobile_numnber="07700 900205")
service = sample_service(notify_db, notify_db_session, user=user, restricted=True) service = sample_service(notify_db, notify_db_session, user=user, restricted=True)