Ensures that both the CSV processing and the API both use the new deliver_email and deliver_sms tasks not the old ones with the redundant parameter.

This commit is contained in:
Martyn Inglis
2016-09-28 15:05:50 +01:00
parent 9233ffa3d4
commit c13ead77e4
5 changed files with 106 additions and 115 deletions

View File

@@ -16,7 +16,7 @@ from app import (
encryption
)
from app.aws import s3
from app.celery.provider_tasks import send_sms_to_provider, send_email_to_provider
from app.celery import provider_tasks
from app.dao.jobs_dao import (
dao_update_job,
dao_get_job_by_id
@@ -131,7 +131,7 @@ def send_sms(self,
created_at, notification, notification_id, service.id, SMS_TYPE, api_key_id, key_type
)
)
send_sms_to_provider.apply_async((service_id, notification_id), queue='send-sms')
provider_tasks.deliver_sms.apply_async((notification_id), queue='send-sms')
current_app.logger.info(
"SMS {} created at {}".format(notification_id, created_at)
@@ -170,7 +170,7 @@ def send_email(self, service_id,
)
)
send_email_to_provider.apply_async((service_id, notification_id), queue='send-email')
provider_tasks.deliver_email.apply_async((notification_id), queue='send-email')
current_app.logger.info("Email {} created at {}".format(notification_id, created_at))
except SQLAlchemyError as e:

View File

@@ -46,7 +46,7 @@ from app.errors import (
)
register_errors(notifications)
from app.celery.provider_tasks import send_sms_to_provider, send_email_to_provider
from app.celery import provider_tasks
@notifications.route('/notifications/email/ses', methods=['POST'])
@@ -342,9 +342,9 @@ def persist_notification(
try:
if notification_type == SMS_TYPE:
send_sms_to_provider.apply_async((str(service.id), str(notification_id)), queue='send-sms')
provider_tasks.deliver_sms.apply_async((str(notification_id)), queue='send-sms')
if notification_type == EMAIL_TYPE:
send_email_to_provider.apply_async((str(service.id), str(notification_id)), queue='send-email')
provider_tasks.deliver_email.apply_async((str(notification_id)), queue='send-email')
except Exception as e:
current_app.logger.exception("Failed to send to SQS exception", e)
dao_delete_notifications_and_history_by_id(notification_id)

View File

@@ -8,8 +8,8 @@ import app
def test_should_have_decorated_tasks_functions():
assert send_sms_to_provider.__wrapped__.__name__ == 'send_sms_to_provider'
assert send_email_to_provider.__wrapped__.__name__ == 'send_email_to_provider'
assert deliver_sms.__wrapped__.__name__ == 'deliver_sms'
assert deliver_email.__wrapped__.__name__ == 'deliver_email'
def test_should_by_10_second_delay_as_default():

View File

@@ -326,7 +326,7 @@ def test_should_send_template_to_correct_sms_task_and_persist(sample_template_wi
notification = _notification_json(sample_template_with_placeholders,
to="+447234123123", personalisation={"name": "Jo"})
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
notification_id = uuid.uuid4()
@@ -337,9 +337,8 @@ def test_should_send_template_to_correct_sms_task_and_persist(sample_template_wi
datetime.utcnow().strftime(DATETIME_FORMAT)
)
provider_tasks.send_sms_to_provider.apply_async.assert_called_once_with(
(sample_template_with_placeholders.service_id,
notification_id),
provider_tasks.deliver_sms.apply_async.assert_called_once_with(
(notification_id),
queue="send-sms"
)
@@ -364,7 +363,7 @@ def test_should_send_sms_if_restricted_service_and_valid_number(notify_db, notif
template = sample_template(notify_db, notify_db_session, service=service)
notification = _notification_json(template, "+447700900890") # The users own number, but in a different format
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
notification_id = uuid.uuid4()
encrypt_notification = encryption.encrypt(notification)
@@ -375,9 +374,8 @@ def test_should_send_sms_if_restricted_service_and_valid_number(notify_db, notif
datetime.utcnow().strftime(DATETIME_FORMAT)
)
provider_tasks.send_sms_to_provider.apply_async.assert_called_once_with(
(service.id,
notification_id),
provider_tasks.deliver_sms.apply_async.assert_called_once_with(
(notification_id),
queue="send-sms"
)
@@ -403,7 +401,7 @@ def test_should_not_send_sms_if_restricted_service_and_invalid_number_with_test_
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')
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
notification_id = uuid.uuid4()
send_sms(
@@ -414,9 +412,8 @@ def test_should_not_send_sms_if_restricted_service_and_invalid_number_with_test_
key_type=KEY_TYPE_TEST
)
provider_tasks.send_sms_to_provider.apply_async.assert_called_once_with(
(service.id,
notification_id),
provider_tasks.deliver_sms.apply_async.assert_called_once_with(
(notification_id),
queue="send-sms"
)
@@ -434,7 +431,7 @@ def test_should_not_send_email_if_restricted_service_and_invalid_email_address_w
)
notification = _notification_json(template, to="test@example.com")
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
notification_id = uuid.uuid4()
send_email(
@@ -445,9 +442,8 @@ def test_should_not_send_email_if_restricted_service_and_invalid_email_address_w
key_type=KEY_TYPE_TEST
)
provider_tasks.send_email_to_provider.apply_async.assert_called_once_with(
(service.id,
notification_id),
provider_tasks.deliver_email.apply_async.assert_called_once_with(
(notification_id),
queue="send-email"
)
@@ -461,7 +457,7 @@ def test_should_not_send_sms_if_restricted_service_and_invalid_number(notify_db,
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')
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
notification_id = uuid.uuid4()
send_sms(
@@ -470,7 +466,7 @@ def test_should_not_send_sms_if_restricted_service_and_invalid_number(notify_db,
encryption.encrypt(notification),
datetime.utcnow().strftime(DATETIME_FORMAT)
)
provider_tasks.send_sms_to_provider.apply_async.assert_not_called()
provider_tasks.deliver_sms.apply_async.assert_not_called()
with pytest.raises(NoResultFound):
Notification.query.filter_by(id=notification_id).one()
@@ -501,7 +497,7 @@ def test_should_send_sms_template_to_and_persist_with_job_id(sample_job, sample_
to="+447234123123",
job_id=sample_job.id,
row_number=2)
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
notification_id = uuid.uuid4()
send_sms(
@@ -512,9 +508,8 @@ def test_should_send_sms_template_to_and_persist_with_job_id(sample_job, sample_
api_key_id=str(sample_api_key.id),
key_type=KEY_TYPE_TEAM
)
provider_tasks.send_sms_to_provider.apply_async.assert_called_once_with(
(sample_job.service.id,
notification_id),
provider_tasks.deliver_sms.apply_async.assert_called_once_with(
(notification_id),
queue="send-sms"
)
persisted_notification = Notification.query.filter_by(id=notification_id).one()
@@ -538,7 +533,7 @@ def test_should_use_email_template_and_persist(sample_email_template_with_placeh
"my_email@my_email.com",
{"name": "Jo"},
row_number=1)
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
notification_id = uuid.uuid4()
@@ -556,8 +551,8 @@ def test_should_use_email_template_and_persist(sample_email_template_with_placeh
)
persisted_notification = Notification.query.filter_by(id=notification_id).one()
provider_tasks.send_email_to_provider.apply_async.assert_called_once_with(
(sample_email_template_with_placeholders.service_id, notification_id), queue='send-email')
provider_tasks.deliver_email.apply_async.assert_called_once_with(
(notification_id), queue='send-email')
assert persisted_notification.id == notification_id
assert persisted_notification.to == 'my_email@my_email.com'
@@ -581,7 +576,7 @@ def test_send_email_should_use_template_version_from_job_not_latest(sample_email
# Change the template
from app.dao.templates_dao import dao_update_template, dao_get_template_by_id
sample_email_template.content = sample_email_template.content + " another version of the template"
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
dao_update_template(sample_email_template)
t = dao_get_template_by_id(sample_email_template.id)
assert t.version > version_on_notification
@@ -594,8 +589,7 @@ def test_send_email_should_use_template_version_from_job_not_latest(sample_email
now.strftime(DATETIME_FORMAT)
)
provider_tasks.send_email_to_provider.apply_async.assert_called_once_with((sample_email_template.service_id,
notification_id), queue='send-email')
provider_tasks.deliver_email.apply_async.assert_called_once_with((notification_id), queue='send-email')
persisted_notification = Notification.query.filter_by(id=notification_id).one()
assert persisted_notification.id == notification_id
@@ -612,7 +606,7 @@ def test_send_email_should_use_template_version_from_job_not_latest(sample_email
def test_should_use_email_template_subject_placeholders(sample_email_template_with_placeholders, mocker):
notification = _notification_json(sample_email_template_with_placeholders,
"my_email@my_email.com", {"name": "Jo"})
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
notification_id = uuid.uuid4()
now = datetime.utcnow()
@@ -622,8 +616,8 @@ def test_should_use_email_template_subject_placeholders(sample_email_template_wi
encryption.encrypt(notification),
now.strftime(DATETIME_FORMAT)
)
provider_tasks.send_email_to_provider.apply_async.assert_called_once_with(
(sample_email_template_with_placeholders.service_id, notification_id, ), queue='send-email'
provider_tasks.deliver_email.apply_async.assert_called_once_with(
(notification_id), queue='send-email'
)
persisted_notification = Notification.query.filter_by(id=notification_id).one()
assert persisted_notification.id == notification_id
@@ -638,7 +632,7 @@ def test_should_use_email_template_subject_placeholders(sample_email_template_wi
def test_should_use_email_template_and_persist_without_personalisation(sample_email_template, mocker):
notification = _notification_json(sample_email_template, "my_email@my_email.com")
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
notification_id = uuid.uuid4()
@@ -649,8 +643,7 @@ def test_should_use_email_template_and_persist_without_personalisation(sample_em
encryption.encrypt(notification),
now.strftime(DATETIME_FORMAT)
)
provider_tasks.send_email_to_provider.apply_async.assert_called_once_with((sample_email_template.service_id,
notification_id), queue='send-email')
provider_tasks.deliver_email.apply_async.assert_called_once_with((notification_id), queue='send-email')
persisted_notification = Notification.query.filter_by(id=notification_id).one()
assert persisted_notification.id == notification_id
@@ -670,7 +663,7 @@ def test_send_sms_should_go_to_retry_queue_if_database_errors(sample_template, m
expected_exception = SQLAlchemyError()
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
mocker.patch('app.celery.tasks.send_sms.retry', side_effect=Exception())
mocker.patch('app.celery.tasks.dao_create_notification', side_effect=expected_exception)
now = datetime.utcnow()
@@ -684,7 +677,7 @@ def test_send_sms_should_go_to_retry_queue_if_database_errors(sample_template, m
encryption.encrypt(notification),
now.strftime(DATETIME_FORMAT)
)
provider_tasks.send_sms_to_provider.apply_async.assert_not_called()
provider_tasks.deliver_sms.apply_async.assert_not_called()
tasks.send_sms.retry.assert_called_with(exc=expected_exception, queue='retry')
with pytest.raises(NoResultFound) as e:
@@ -697,7 +690,7 @@ def test_send_email_should_go_to_retry_queue_if_database_errors(sample_email_tem
expected_exception = SQLAlchemyError()
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
mocker.patch('app.celery.tasks.send_email.retry', side_effect=Exception())
mocker.patch('app.celery.tasks.dao_create_notification', side_effect=expected_exception)
now = datetime.utcnow()
@@ -711,7 +704,7 @@ def test_send_email_should_go_to_retry_queue_if_database_errors(sample_email_tem
encryption.encrypt(notification),
now.strftime(DATETIME_FORMAT)
)
provider_tasks.send_email_to_provider.apply_async.assert_not_called()
provider_tasks.deliver_email.apply_async.assert_not_called()
tasks.send_email.retry.assert_called_with(exc=expected_exception, queue='retry')
with pytest.raises(NoResultFound) as e:

View File

@@ -25,7 +25,7 @@ from tests.app.conftest import (
def test_create_sms_should_reject_if_missing_required_fields(notify_api, sample_api_key, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
data = {}
auth_header = create_authorization_header(service_id=sample_api_key.service_id)
@@ -45,7 +45,7 @@ def test_create_sms_should_reject_if_missing_required_fields(notify_api, sample_
def test_should_reject_bad_phone_numbers(notify_api, sample_template, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
data = {
'to': 'invalid',
@@ -59,7 +59,7 @@ def test_should_reject_bad_phone_numbers(notify_api, sample_template, mocker):
headers=[('Content-Type', 'application/json'), auth_header])
json_resp = json.loads(response.get_data(as_text=True))
app.celery.provider_tasks.send_sms_to_provider.apply_async.assert_not_called()
app.celery.provider_tasks.deliver_sms.apply_async.assert_not_called()
assert json_resp['result'] == 'error'
assert len(json_resp['message'].keys()) == 1
assert 'Invalid phone number: Must not contain letters or symbols' in json_resp['message']['to']
@@ -69,7 +69,7 @@ def test_should_reject_bad_phone_numbers(notify_api, sample_template, mocker):
def test_send_notification_invalid_template_id(notify_api, sample_template, mocker, fake_uuid):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
data = {
'to': '+447700900855',
@@ -83,7 +83,7 @@ def test_send_notification_invalid_template_id(notify_api, sample_template, mock
headers=[('Content-Type', 'application/json'), auth_header])
json_resp = json.loads(response.get_data(as_text=True))
app.celery.provider_tasks.send_sms_to_provider.apply_async.assert_not_called()
app.celery.provider_tasks.deliver_sms.apply_async.assert_not_called()
assert response.status_code == 404
test_string = 'No result found'
@@ -94,7 +94,7 @@ def test_send_notification_invalid_template_id(notify_api, sample_template, mock
def test_send_notification_with_placeholders_replaced(notify_api, sample_email_template_with_placeholders, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.tasks.send_email_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
data = {
'to': 'ok@ok.com',
@@ -114,9 +114,8 @@ def test_send_notification_with_placeholders_replaced(notify_api, sample_email_t
notification_id = response_data['notification']['id']
data.update({"template_version": sample_email_template_with_placeholders.version})
app.celery.provider_tasks.send_email_to_provider.apply_async.assert_called_once_with(
(str(sample_email_template_with_placeholders.service.id),
str(notification_id)),
app.celery.provider_tasks.deliver_email.apply_async.assert_called_once_with(
(str(notification_id)),
queue="send-email"
)
assert response.status_code == 201
@@ -147,7 +146,7 @@ def test_should_not_send_notification_for_archived_template(notify_api, sample_t
def test_send_notification_with_missing_personalisation(notify_api, sample_template_with_placeholders, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
data = {
'to': '+447700900855',
@@ -164,7 +163,7 @@ def test_send_notification_with_missing_personalisation(notify_api, sample_templ
headers=[('Content-Type', 'application/json'), auth_header])
json_resp = json.loads(response.get_data(as_text=True))
app.celery.provider_tasks.send_sms_to_provider.apply_async.assert_not_called()
app.celery.provider_tasks.deliver_sms.apply_async.assert_not_called()
assert response.status_code == 400
assert 'Missing personalisation: name' in json_resp['message']['template']
@@ -175,7 +174,7 @@ def test_send_notification_with_too_much_personalisation_data(
):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
data = {
'to': '+447700900855',
@@ -192,7 +191,7 @@ def test_send_notification_with_too_much_personalisation_data(
headers=[('Content-Type', 'application/json'), auth_header])
json_resp = json.loads(response.get_data(as_text=True))
app.celery.provider_tasks.send_sms_to_provider.apply_async.assert_not_called()
app.celery.provider_tasks.deliver_sms.apply_async.assert_not_called()
assert response.status_code == 400
assert 'Personalisation not needed for template: foo' in json_resp['message']['template']
@@ -201,7 +200,7 @@ def test_send_notification_with_too_much_personalisation_data(
def test_should_not_send_sms_if_restricted_and_not_a_service_user(notify_api, sample_template, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
sample_template.service.restricted = True
dao_update_service(sample_template.service)
@@ -219,7 +218,7 @@ def test_should_not_send_sms_if_restricted_and_not_a_service_user(notify_api, sa
headers=[('Content-Type', 'application/json'), auth_header])
json_resp = json.loads(response.get_data(as_text=True))
app.celery.provider_tasks.send_sms_to_provider.apply_async.assert_not_called()
app.celery.provider_tasks.deliver_sms.apply_async.assert_not_called()
assert response.status_code == 400
assert [(
@@ -231,7 +230,7 @@ def test_should_not_send_sms_if_restricted_and_not_a_service_user(notify_api, sa
def test_should_send_sms_if_restricted_and_a_service_user(notify_api, sample_template, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
sample_template.service.restricted = True
dao_update_service(sample_template.service)
@@ -247,14 +246,14 @@ def test_should_send_sms_if_restricted_and_a_service_user(notify_api, sample_tem
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
assert app.celery.provider_tasks.send_sms_to_provider.apply_async.called
assert app.celery.provider_tasks.deliver_sms.apply_async.called
assert response.status_code == 201
def test_should_send_email_if_restricted_and_a_service_user(notify_api, sample_email_template, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
sample_email_template.service.restricted = True
dao_update_service(sample_email_template.service)
@@ -270,14 +269,14 @@ def test_should_send_email_if_restricted_and_a_service_user(notify_api, sample_e
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
assert app.celery.provider_tasks.send_email_to_provider.apply_async.called
assert app.celery.provider_tasks.deliver_email.apply_async.called
assert response.status_code == 201
def test_should_not_allow_template_from_another_service(notify_api, service_factory, sample_user, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
service_1 = service_factory.get('service 1', user=sample_user, email_from='service.1')
service_2 = service_factory.get('service 2', user=sample_user, email_from='service.2')
@@ -296,7 +295,7 @@ def test_should_not_allow_template_from_another_service(notify_api, service_fact
headers=[('Content-Type', 'application/json'), auth_header])
json_resp = json.loads(response.get_data(as_text=True))
app.celery.provider_tasks.send_sms_to_provider.apply_async.assert_not_called()
app.celery.provider_tasks.deliver_sms.apply_async.assert_not_called()
assert response.status_code == 404
test_string = 'No result found'
@@ -320,7 +319,7 @@ def test_should_not_allow_template_content_too_large(
):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
template = create_sample_template(
notify_db,
notify_db_session,
@@ -357,7 +356,7 @@ def test_should_not_allow_template_content_too_large(
def test_should_allow_valid_sms_notification(notify_api, sample_template, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
data = {
@@ -375,8 +374,8 @@ def test_should_allow_valid_sms_notification(notify_api, sample_template, mocker
response_data = json.loads(response.data)['data']
notification_id = response_data['notification']['id']
app.celery.provider_tasks.send_sms_to_provider.apply_async.assert_called_once_with(
(str(sample_template.service_id), notification_id), queue='send-sms')
app.celery.provider_tasks.deliver_sms.apply_async.assert_called_once_with(
(notification_id), queue='send-sms')
assert response.status_code == 201
assert notification_id
assert 'subject' not in response_data
@@ -387,7 +386,7 @@ def test_should_allow_valid_sms_notification(notify_api, sample_template, mocker
def test_create_email_should_reject_if_missing_required_fields(notify_api, sample_api_key, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
data = {}
auth_header = create_authorization_header(service_id=sample_api_key.service_id)
@@ -398,7 +397,7 @@ def test_create_email_should_reject_if_missing_required_fields(notify_api, sampl
headers=[('Content-Type', 'application/json'), auth_header])
json_resp = json.loads(response.get_data(as_text=True))
app.celery.provider_tasks.send_email_to_provider.apply_async.assert_not_called()
app.celery.provider_tasks.deliver_email.apply_async.assert_not_called()
assert json_resp['result'] == 'error'
assert 'Missing data for required field.' in json_resp['message']['to'][0]
assert 'Missing data for required field.' in json_resp['message']['template'][0]
@@ -408,7 +407,7 @@ def test_create_email_should_reject_if_missing_required_fields(notify_api, sampl
def test_should_reject_email_notification_with_bad_email(notify_api, sample_email_template, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
to_address = "bad-email"
data = {
'to': to_address,
@@ -422,7 +421,7 @@ def test_should_reject_email_notification_with_bad_email(notify_api, sample_emai
headers=[('Content-Type', 'application/json'), auth_header])
data = json.loads(response.get_data(as_text=True))
app.celery.provider_tasks.send_email_to_provider.apply_async.assert_not_called()
app.celery.provider_tasks.deliver_email.apply_async.assert_not_called()
assert response.status_code == 400
assert data['result'] == 'error'
assert data['message']['to'][0] == 'Not a valid email address'
@@ -432,7 +431,7 @@ def test_should_reject_email_notification_with_template_id_that_cant_be_found(
notify_api, sample_email_template, mocker, fake_uuid):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
data = {
'to': 'ok@ok.com',
'template': fake_uuid
@@ -445,7 +444,7 @@ def test_should_reject_email_notification_with_template_id_that_cant_be_found(
headers=[('Content-Type', 'application/json'), auth_header])
data = json.loads(response.get_data(as_text=True))
app.celery.provider_tasks.send_email_to_provider.apply_async.assert_not_called()
app.celery.provider_tasks.deliver_email.apply_async.assert_not_called()
assert response.status_code == 404
assert data['result'] == 'error'
test_string = 'No result found'
@@ -455,7 +454,7 @@ def test_should_reject_email_notification_with_template_id_that_cant_be_found(
def test_should_not_allow_email_template_from_another_service(notify_api, service_factory, sample_user, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
service_1 = service_factory.get('service 1', template_type='email', user=sample_user,
email_from='service.1')
@@ -477,7 +476,7 @@ def test_should_not_allow_email_template_from_another_service(notify_api, servic
headers=[('Content-Type', 'application/json'), auth_header])
json_resp = json.loads(response.get_data(as_text=True))
app.celery.provider_tasks.send_email_to_provider.apply_async.assert_not_called()
app.celery.provider_tasks.deliver_email.apply_async.assert_not_called()
assert response.status_code == 404
test_string = 'No result found'
@@ -487,7 +486,7 @@ def test_should_not_allow_email_template_from_another_service(notify_api, servic
def test_should_not_send_email_if_restricted_and_not_a_service_user(notify_api, sample_email_template, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
sample_email_template.service.restricted = True
dao_update_service(sample_email_template.service)
@@ -505,7 +504,7 @@ def test_should_not_send_email_if_restricted_and_not_a_service_user(notify_api,
headers=[('Content-Type', 'application/json'), auth_header])
json_resp = json.loads(response.get_data(as_text=True))
app.celery.provider_tasks.send_email_to_provider.apply_async.assert_not_called()
app.celery.provider_tasks.deliver_email.apply_async.assert_not_called()
assert response.status_code == 400
assert [(
@@ -518,7 +517,7 @@ def test_should_not_send_email_if_restricted_and_not_a_service_user(notify_api,
def test_should_allow_valid_email_notification(notify_api, sample_email_template, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
data = {
@@ -535,8 +534,8 @@ def test_should_allow_valid_email_notification(notify_api, sample_email_template
assert response.status_code == 201
response_data = json.loads(response.get_data(as_text=True))['data']
notification_id = response_data['notification']['id']
app.celery.provider_tasks.send_email_to_provider.apply_async.assert_called_once_with(
(str(sample_email_template.service_id), notification_id),
app.celery.provider_tasks.deliver_email.apply_async.assert_called_once_with(
(notification_id),
queue="send-email"
)
@@ -555,7 +554,7 @@ def test_should_not_block_api_call_if_over_day_limit_for_live_service(
mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
service = create_sample_service(notify_db, notify_db_session, limit=1, restricted=False)
@@ -588,7 +587,7 @@ def test_should_block_api_call_if_over_day_limit_for_restricted_service(
mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
service = create_sample_service(notify_db, notify_db_session, limit=1, restricted=True)
@@ -624,7 +623,7 @@ def test_should_allow_api_call_if_under_day_limit_regardless_of_type(
restricted):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
service = create_sample_service(notify_db, notify_db_session, limit=2, restricted=restricted)
@@ -650,7 +649,7 @@ def test_should_allow_api_call_if_under_day_limit_regardless_of_type(
def test_should_not_return_html_in_body(notify_api, notify_db, notify_db_session, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
email_template = create_sample_email_template(notify_db, notify_db.session, content='hello\nthere')
data = {
@@ -670,7 +669,7 @@ def test_should_not_return_html_in_body(notify_api, notify_db, notify_db_session
def test_should_not_send_email_if_team_api_key_and_not_a_service_user(notify_api, sample_email_template, mocker):
with notify_api.test_request_context(), notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
data = {
'to': "not-someone-we-trust@email-address.com",
'template': str(sample_email_template.id),
@@ -685,7 +684,7 @@ def test_should_not_send_email_if_team_api_key_and_not_a_service_user(notify_api
json_resp = json.loads(response.get_data(as_text=True))
app.celery.provider_tasks.send_email_to_provider.apply_async.assert_not_called()
app.celery.provider_tasks.deliver_email.apply_async.assert_not_called()
assert response.status_code == 400
assert [
@@ -695,7 +694,7 @@ def test_should_not_send_email_if_team_api_key_and_not_a_service_user(notify_api
def test_should_not_send_sms_if_team_api_key_and_not_a_service_user(notify_api, sample_template, mocker):
with notify_api.test_request_context(), notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
data = {
'to': '07123123123',
@@ -710,7 +709,7 @@ def test_should_not_send_sms_if_team_api_key_and_not_a_service_user(notify_api,
headers=[('Content-Type', 'application/json'), auth_header])
json_resp = json.loads(response.get_data(as_text=True))
app.celery.provider_tasks.send_sms_to_provider.apply_async.assert_not_called()
app.celery.provider_tasks.deliver_sms.apply_async.assert_not_called()
assert response.status_code == 400
assert [
@@ -720,7 +719,7 @@ def test_should_not_send_sms_if_team_api_key_and_not_a_service_user(notify_api,
def test_should_send_email_if_team_api_key_and_a_service_user(notify_api, sample_email_template, fake_uuid, mocker):
with notify_api.test_request_context(), notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
mocker.patch('app.notifications.rest.create_uuid', return_value=fake_uuid)
data = {
@@ -739,8 +738,8 @@ def test_should_send_email_if_team_api_key_and_a_service_user(notify_api, sample
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))])
app.celery.provider_tasks.send_email_to_provider.apply_async.assert_called_once_with(
(str(sample_email_template.service.id), fake_uuid),
app.celery.provider_tasks.deliver_email.apply_async.assert_called_once_with(
(fake_uuid),
queue='send-email')
assert response.status_code == 201
@@ -751,7 +750,7 @@ def test_should_send_email_to_anyone_with_test_key(
notify_api, sample_email_template, mocker, restricted, limit, fake_uuid
):
with notify_api.test_request_context(), notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
mocker.patch('app.notifications.rest.create_uuid', return_value=fake_uuid)
data = {
@@ -775,14 +774,14 @@ def test_should_send_email_to_anyone_with_test_key(
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))]
)
app.celery.provider_tasks.send_email_to_provider.apply_async.assert_called_once_with(
(str(sample_email_template.service.id), fake_uuid), queue='send-email')
app.celery.provider_tasks.deliver_email.apply_async.assert_called_once_with(
(fake_uuid), queue='send-email')
assert response.status_code == 201
def test_should_send_sms_if_team_api_key_and_a_service_user(notify_api, sample_template, fake_uuid, mocker):
with notify_api.test_request_context(), notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
mocker.patch('app.notifications.rest.create_uuid', return_value=fake_uuid)
data = {
@@ -801,14 +800,14 @@ def test_should_send_sms_if_team_api_key_and_a_service_user(notify_api, sample_t
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))])
app.celery.provider_tasks.send_sms_to_provider.apply_async.assert_called_once_with(
(str(sample_template.service.id), fake_uuid), queue='send-sms')
app.celery.provider_tasks.deliver_sms.apply_async.assert_called_once_with(
(fake_uuid), queue='send-sms')
assert response.status_code == 201
def test_should_persist_sms_notification(notify_api, sample_template, fake_uuid, mocker):
with notify_api.test_request_context(), notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
mocker.patch('app.notifications.rest.create_uuid', return_value=fake_uuid)
data = {
@@ -828,8 +827,8 @@ def test_should_persist_sms_notification(notify_api, sample_template, fake_uuid,
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))])
app.celery.provider_tasks.send_sms_to_provider.apply_async.assert_called_once_with(
(str(sample_template.service.id), fake_uuid), queue='send-sms')
app.celery.provider_tasks.deliver_sms.apply_async.assert_called_once_with(
(fake_uuid), queue='send-sms')
assert response.status_code == 201
notification = notifications_dao.get_notification_by_id(fake_uuid)
@@ -840,7 +839,7 @@ def test_should_persist_sms_notification(notify_api, sample_template, fake_uuid,
def test_should_persist_email_notification(notify_api, sample_email_template, fake_uuid, mocker):
with notify_api.test_request_context(), notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
mocker.patch('app.notifications.rest.create_uuid', return_value=fake_uuid)
data = {
@@ -860,8 +859,8 @@ def test_should_persist_email_notification(notify_api, sample_email_template, fa
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))])
app.celery.provider_tasks.send_email_to_provider.apply_async.assert_called_once_with(
(str(sample_email_template.service.id), fake_uuid), queue='send-email')
app.celery.provider_tasks.deliver_email.apply_async.assert_called_once_with(
(fake_uuid), queue='send-email')
assert response.status_code == 201
notification = notifications_dao.get_notification_by_id(fake_uuid)
@@ -877,7 +876,7 @@ def test_should_delete_email_notification_and_return_error_if_sqs_fails(
mocker):
with notify_api.test_request_context(), notify_api.test_client() as client:
mocker.patch(
'app.celery.provider_tasks.send_email_to_provider.apply_async',
'app.celery.provider_tasks.deliver_email.apply_async',
side_effect=Exception("failed to talk to SQS")
)
mocker.patch('app.notifications.rest.create_uuid', return_value=fake_uuid)
@@ -899,8 +898,8 @@ def test_should_delete_email_notification_and_return_error_if_sqs_fails(
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))])
app.celery.provider_tasks.send_email_to_provider.apply_async.assert_called_once_with(
(str(sample_email_template.service.id), fake_uuid), queue='send-email')
app.celery.provider_tasks.deliver_email.apply_async.assert_called_once_with(
(fake_uuid), queue='send-email')
assert response.status_code == 500
assert not notifications_dao.get_notification_by_id(fake_uuid)
@@ -910,7 +909,7 @@ def test_should_delete_email_notification_and_return_error_if_sqs_fails(
def test_should_delete_sms_notification_and_return_error_if_sqs_fails(notify_api, sample_template, fake_uuid, mocker):
with notify_api.test_request_context(), notify_api.test_client() as client:
mocker.patch(
'app.celery.provider_tasks.send_sms_to_provider.apply_async',
'app.celery.provider_tasks.deliver_sms.apply_async',
side_effect=Exception("failed to talk to SQS")
)
mocker.patch('app.notifications.rest.create_uuid', return_value=fake_uuid)
@@ -932,8 +931,7 @@ def test_should_delete_sms_notification_and_return_error_if_sqs_fails(notify_api
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))])
app.celery.provider_tasks.send_sms_to_provider.apply_async.assert_called_once_with(
(str(sample_template.service.id), fake_uuid), queue='send-sms')
app.celery.provider_tasks.deliver_sms.apply_async.assert_called_once_with((fake_uuid), queue='send-sms')
assert response.status_code == 500
assert not notifications_dao.get_notification_by_id(fake_uuid)
@@ -950,7 +948,7 @@ def test_should_not_persist_notification_or_send_email_if_simulated_email(
to_email,
sample_email_template,
mocker):
apply_async = mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
apply_async = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
data = {
'to': to_email,
@@ -979,7 +977,7 @@ def test_should_not_persist_notification_or_send_sms_if_simulated_number(
to_sms,
sample_template,
mocker):
apply_async = mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
apply_async = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
data = {
'to': to_sms,