Merge branch 'master' into revert-queue-config

This commit is contained in:
Rebecca Law
2016-03-02 13:20:55 +00:00
17 changed files with 283 additions and 124 deletions

View File

@@ -85,7 +85,40 @@ def test_should_process_all_sms_job(sample_job, mocker):
assert job.status == 'finished'
def test_should_send_template_to_correct_sms_provider_and_persist(sample_template, mocker):
def test_should_send_template_to_correct_sms_provider_and_persist(sample_template_with_placeholders, mocker):
notification = {
"template": sample_template_with_placeholders.id,
"to": "+441234123123",
"personalisation": {"name": "Jo"}
}
mocker.patch('app.encryption.decrypt', return_value=notification)
mocker.patch('app.firetext_client.send_sms')
mocker.patch('app.firetext_client.get_name', return_value="firetext")
notification_id = uuid.uuid4()
now = datetime.utcnow()
send_sms(
sample_template_with_placeholders.service_id,
notification_id,
"encrypted-in-reality",
now
)
firetext_client.send_sms.assert_called_once_with("+441234123123", "Sample service: Hello Jo")
persisted_notification = notifications_dao.get_notification(
sample_template_with_placeholders.service_id, notification_id
)
assert persisted_notification.id == notification_id
assert persisted_notification.to == '+441234123123'
assert persisted_notification.template_id == sample_template_with_placeholders.id
assert persisted_notification.status == 'sent'
assert persisted_notification.created_at == now
assert persisted_notification.sent_at > now
assert persisted_notification.sent_by == 'firetext'
assert not persisted_notification.job_id
def test_should_send_sms_without_personalisation(sample_template, mocker):
notification = {
"template": sample_template.id,
"to": "+441234123123"
@@ -103,16 +136,7 @@ def test_should_send_template_to_correct_sms_provider_and_persist(sample_templat
now
)
firetext_client.send_sms.assert_called_once_with("+441234123123", sample_template.content)
persisted_notification = notifications_dao.get_notification(sample_template.service_id, notification_id)
assert persisted_notification.id == notification_id
assert persisted_notification.to == '+441234123123'
assert persisted_notification.template_id == sample_template.id
assert persisted_notification.status == 'sent'
assert persisted_notification.created_at == now
assert persisted_notification.sent_at > now
assert persisted_notification.sent_by == 'firetext'
assert not persisted_notification.job_id
firetext_client.send_sms.assert_called_once_with("+441234123123", "Sample service: This is a template")
def test_should_send_template_to_correct_sms_provider_and_persist_with_job_id(sample_job, mocker):
@@ -133,7 +157,7 @@ def test_should_send_template_to_correct_sms_provider_and_persist_with_job_id(sa
"encrypted-in-reality",
now)
firetext_client.send_sms.assert_called_once_with("+441234123123", sample_job.template.content)
firetext_client.send_sms.assert_called_once_with("+441234123123", "Sample service: This is a template")
persisted_notification = notifications_dao.get_notification(sample_job.template.service_id, notification_id)
assert persisted_notification.id == notification_id
assert persisted_notification.to == '+441234123123'
@@ -145,15 +169,54 @@ def test_should_send_template_to_correct_sms_provider_and_persist_with_job_id(sa
assert persisted_notification.sent_by == 'firetext'
def test_should_use_email_template_and_persist(sample_email_template, mocker):
def test_should_use_email_template_and_persist(sample_email_template_with_placeholders, mocker):
notification = {
"template": sample_email_template.id,
"to": "my_email@my_email.com"
"template": sample_email_template_with_placeholders.id,
"to": "my_email@my_email.com",
"personalisation": {"name": "Jo"}
}
mocker.patch('app.encryption.decrypt', return_value=notification)
mocker.patch('app.aws_ses_client.send_email')
mocker.patch('app.aws_ses_client.get_name', return_value='ses')
notification_id = uuid.uuid4()
now = datetime.utcnow()
send_email(
sample_email_template_with_placeholders.service_id,
notification_id,
'subject',
'email_from',
"encrypted-in-reality",
now)
aws_ses_client.send_email.assert_called_once_with(
"email_from",
"my_email@my_email.com",
"subject",
"Hello Jo"
)
persisted_notification = notifications_dao.get_notification(
sample_email_template_with_placeholders.service_id, notification_id
)
assert persisted_notification.id == notification_id
assert persisted_notification.to == 'my_email@my_email.com'
assert persisted_notification.template_id == sample_email_template_with_placeholders.id
assert persisted_notification.created_at == now
assert persisted_notification.sent_at > now
assert persisted_notification.status == 'sent'
assert persisted_notification.sent_by == 'ses'
def test_should_use_email_template_and_persist_without_personalisation(
sample_email_template, mocker
):
mocker.patch('app.encryption.decrypt', return_value={
"template": sample_email_template.id,
"to": "my_email@my_email.com",
})
mocker.patch('app.aws_ses_client.send_email')
mocker.patch('app.aws_ses_client.get_name', return_value='ses')
notification_id = uuid.uuid4()
now = datetime.utcnow()
send_email(
@@ -168,16 +231,8 @@ def test_should_use_email_template_and_persist(sample_email_template, mocker):
"email_from",
"my_email@my_email.com",
"subject",
sample_email_template.content
"This is a template"
)
persisted_notification = notifications_dao.get_notification(sample_email_template.service_id, notification_id)
assert persisted_notification.id == notification_id
assert persisted_notification.to == 'my_email@my_email.com'
assert persisted_notification.template_id == sample_email_template.id
assert persisted_notification.created_at == now
assert persisted_notification.sent_at > now
assert persisted_notification.status == 'sent'
assert persisted_notification.sent_by == 'ses'
def test_should_persist_notification_as_failed_if_sms_client_fails(sample_template, mocker):
@@ -198,7 +253,7 @@ def test_should_persist_notification_as_failed_if_sms_client_fails(sample_templa
"encrypted-in-reality",
now)
firetext_client.send_sms.assert_called_once_with("+441234123123", sample_template.content)
firetext_client.send_sms.assert_called_once_with("+441234123123", "Sample service: This is a template")
persisted_notification = notifications_dao.get_notification(sample_template.service_id, notification_id)
assert persisted_notification.id == notification_id
assert persisted_notification.to == '+441234123123'

View File

@@ -142,6 +142,11 @@ def sample_template(notify_db,
return template
@pytest.fixture(scope='function')
def sample_template_with_placeholders(notify_db, notify_db_session):
return sample_template(notify_db, notify_db_session, content="Hello ((name))")
@pytest.fixture(scope='function')
def sample_email_template(
notify_db,
@@ -169,6 +174,11 @@ def sample_email_template(
return template
@pytest.fixture(scope='function')
def sample_email_template_with_placeholders(notify_db, notify_db_session):
return sample_email_template(notify_db, notify_db_session, content="Hello ((name))")
@pytest.fixture(scope='function')
def sample_api_key(notify_db,
notify_db_session,

View File

@@ -329,6 +329,106 @@ def test_send_notification_invalid_template_id(notify_api, sample_template, mock
assert test_string in json_resp['message']['template']
@freeze_time("2016-01-01 11:09:00.061258")
def test_send_notification_with_placeholders_replaced(notify_api, sample_template_with_placeholders, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.tasks.send_sms.apply_async')
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
data = {
'to': '+441234123123',
'template': sample_template_with_placeholders.id,
'personalisation': {
'name': 'Jo'
}
}
auth_header = create_authorization_header(
service_id=sample_template_with_placeholders.service.id,
request_body=json.dumps(data),
path='/notifications/sms',
method='POST')
response = client.post(
path='/notifications/sms',
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
notification_id = json.loads(response.data)['notification_id']
app.celery.tasks.send_sms.apply_async.assert_called_once_with(
(str(sample_template_with_placeholders.service.id),
notification_id,
"something_encrypted",
"2016-01-01 11:09:00.061258"),
queue="sms"
)
assert response.status_code == 201
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.tasks.send_sms.apply_async')
data = {
'to': '+441234123123',
'template': sample_template_with_placeholders.id,
'personalisation': {
'foo': 'bar'
}
}
auth_header = create_authorization_header(
service_id=sample_template_with_placeholders.service.id,
request_body=json.dumps(data),
path='/notifications/sms',
method='POST')
response = client.post(
path='/notifications/sms',
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
json_resp = json.loads(response.get_data(as_text=True))
app.celery.tasks.send_sms.apply_async.assert_not_called()
assert response.status_code == 400
assert 'Missing personalisation: name' in json_resp['message']['template']
def test_send_notification_with_too_much_personalisation_data(
notify_api, sample_template_with_placeholders, mocker
):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.tasks.send_sms.apply_async')
data = {
'to': '+441234123123',
'template': sample_template_with_placeholders.id,
'personalisation': {
'name': 'Jo', 'foo': 'bar'
}
}
auth_header = create_authorization_header(
service_id=sample_template_with_placeholders.service.id,
request_body=json.dumps(data),
path='/notifications/sms',
method='POST')
response = client.post(
path='/notifications/sms',
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
json_resp = json.loads(response.get_data(as_text=True))
app.celery.tasks.send_sms.apply_async.assert_not_called()
assert response.status_code == 400
assert 'Personalisation not needed for template: foo' in json_resp['message']['template']
def test_prevents_sending_to_any_mobile_on_restricted_service(notify_api, sample_template, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:

View File

@@ -1,28 +0,0 @@
from app.csv import get_recipient_from_csv
from tests.app import load_example_csv
def test_should_process_single_phone_number_file():
sms_file = load_example_csv('sms')
len(get_recipient_from_csv(sms_file)) == 1
assert get_recipient_from_csv(sms_file)[0] == '+441234123123'
def test_should_process_multple_phone_number_file_in_order():
sms_file = load_example_csv('multiple_sms')
len(get_recipient_from_csv(sms_file)) == 10
assert get_recipient_from_csv(sms_file)[0] == '+441234123121'
assert get_recipient_from_csv(sms_file)[9] == '+441234123120'
def test_should_process_single_email_file():
sms_file = load_example_csv('email')
len(get_recipient_from_csv(sms_file)) == 1
assert get_recipient_from_csv(sms_file)[0] == 'test@test.com'
def test_should_process_multple_email_file_in_order():
sms_file = load_example_csv('multiple_email')
len(get_recipient_from_csv(sms_file)) == 10
assert get_recipient_from_csv(sms_file)[0] == 'test1@test.com'
assert get_recipient_from_csv(sms_file)[9] == 'test0@test.com'