mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 10:21:14 -05:00
Merge pull request #665 from alphagov/fix-test-key-external-number-issue
Fix test key external number issue
This commit is contained in:
@@ -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)
|
||||||
)
|
)
|
||||||
@@ -163,7 +164,7 @@ def send_email(self, service_id,
|
|||||||
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 +204,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(
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ def test_post_sms_contract(client, mocker, sample_template):
|
|||||||
|
|
||||||
|
|
||||||
def test_post_email_contract(client, mocker, sample_email_template):
|
def test_post_email_contract(client, mocker, sample_email_template):
|
||||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
|
|||||||
Reference in New Issue
Block a user