2016-06-15 12:27:57 +01:00
|
|
|
|
import random
|
|
|
|
|
|
import string
|
2017-03-30 10:38:51 +01:00
|
|
|
|
import pytest
|
2016-09-28 17:03:17 +01:00
|
|
|
|
from datetime import datetime
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
2016-06-15 12:11:48 +01:00
|
|
|
|
from flask import (json, current_app)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
from freezegun import freeze_time
|
2016-06-30 17:32:49 +01:00
|
|
|
|
from notifications_python_client.authentication import create_jwt_token
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
import app
|
2016-09-07 13:47:22 +01:00
|
|
|
|
from app.dao import notifications_dao
|
2016-09-28 17:03:17 +01:00
|
|
|
|
from app.models import ApiKey, KEY_TYPE_NORMAL, KEY_TYPE_TEAM, KEY_TYPE_TEST, Notification, NotificationHistory
|
2016-06-15 12:27:57 +01:00
|
|
|
|
from app.dao.templates_dao import dao_get_all_templates_for_service, dao_update_template
|
|
|
|
|
|
from app.dao.services_dao import dao_update_service
|
2016-06-30 17:32:49 +01:00
|
|
|
|
from app.dao.api_key_dao import save_model_api_key
|
2017-04-24 14:15:08 +01:00
|
|
|
|
from app.v2.errors import RateLimitError
|
2016-06-15 12:27:57 +01:00
|
|
|
|
from tests import create_authorization_header
|
|
|
|
|
|
from tests.app.conftest import (
|
|
|
|
|
|
sample_notification as create_sample_notification,
|
|
|
|
|
|
sample_service as create_sample_service,
|
|
|
|
|
|
sample_email_template as create_sample_email_template,
|
2016-09-27 13:48:51 +01:00
|
|
|
|
sample_template as create_sample_template,
|
|
|
|
|
|
sample_service_whitelist as create_sample_service_whitelist,
|
2017-04-26 15:56:45 +01:00
|
|
|
|
sample_api_key as create_sample_api_key,
|
|
|
|
|
|
sample_service)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
2016-10-03 10:57:10 +01:00
|
|
|
|
from app.models import Template
|
|
|
|
|
|
from app.errors import InvalidRequest
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
2016-10-03 10:57:10 +01:00
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('template_type',
|
|
|
|
|
|
['sms', 'email'])
|
|
|
|
|
|
def test_create_notification_should_reject_if_missing_required_fields(notify_api,
|
|
|
|
|
|
sample_api_key, mocker, template_type):
|
2016-06-15 12:27:57 +01:00
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
|
with notify_api.test_client() as client:
|
2016-10-03 14:26:36 +01:00
|
|
|
|
mocked = mocker.patch('app.celery.provider_tasks.deliver_{}.apply_async'.format(template_type))
|
2016-06-15 12:27:57 +01:00
|
|
|
|
data = {}
|
|
|
|
|
|
auth_header = create_authorization_header(service_id=sample_api_key.service_id)
|
|
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2016-10-03 10:57:10 +01:00
|
|
|
|
path='/notifications/{}'.format(template_type),
|
2016-06-15 12:27:57 +01:00
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
2016-10-03 14:26:36 +01:00
|
|
|
|
mocked.assert_not_called()
|
2016-06-15 12:27:57 +01:00
|
|
|
|
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]
|
|
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
2016-10-03 14:26:36 +01:00
|
|
|
|
mocked = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'to': 'invalid',
|
|
|
|
|
|
'template': sample_template.id
|
|
|
|
|
|
}
|
2016-06-30 13:21:35 +01:00
|
|
|
|
auth_header = create_authorization_header(service_id=sample_template.service_id)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
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))
|
2016-10-03 14:26:36 +01:00
|
|
|
|
mocked.assert_not_called()
|
2016-06-15 12:27:57 +01:00
|
|
|
|
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']
|
|
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-10-03 10:57:10 +01:00
|
|
|
|
@pytest.mark.parametrize('template_type, to',
|
|
|
|
|
|
[('sms', '+447700900855'),
|
|
|
|
|
|
('email', 'ok@ok.com')])
|
|
|
|
|
|
def test_send_notification_invalid_template_id(notify_api, sample_template, mocker, fake_uuid, template_type, to):
|
2016-06-15 12:27:57 +01:00
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
|
with notify_api.test_client() as client:
|
2016-10-03 14:26:36 +01:00
|
|
|
|
mocked = mocker.patch('app.celery.provider_tasks.deliver_{}.apply_async'.format(template_type))
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
data = {
|
2016-10-03 10:57:10 +01:00
|
|
|
|
'to': to,
|
2016-06-15 12:27:57 +01:00
|
|
|
|
'template': fake_uuid
|
|
|
|
|
|
}
|
2016-06-30 13:21:35 +01:00
|
|
|
|
auth_header = create_authorization_header(service_id=sample_template.service_id)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2016-10-03 10:57:10 +01:00
|
|
|
|
path='/notifications/{}'.format(template_type),
|
2016-06-15 12:27:57 +01:00
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
2016-10-03 14:26:36 +01:00
|
|
|
|
mocked.assert_not_called()
|
2016-06-15 12:27:57 +01:00
|
|
|
|
assert response.status_code == 404
|
|
|
|
|
|
test_string = 'No result found'
|
|
|
|
|
|
assert test_string in json_resp['message']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@freeze_time("2016-01-01 11:09:00.061258")
|
2016-06-21 15:03:33 +01:00
|
|
|
|
def test_send_notification_with_placeholders_replaced(notify_api, sample_email_template_with_placeholders, mocker):
|
2016-06-15 12:27:57 +01:00
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
|
with notify_api.test_client() as client:
|
2017-03-30 10:46:23 +01:00
|
|
|
|
mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
data = {
|
2016-06-21 15:03:33 +01:00
|
|
|
|
'to': 'ok@ok.com',
|
|
|
|
|
|
'template': str(sample_email_template_with_placeholders.id),
|
2016-06-15 12:27:57 +01:00
|
|
|
|
'personalisation': {
|
|
|
|
|
|
'name': 'Jo'
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-06-21 15:03:33 +01:00
|
|
|
|
auth_header = create_authorization_header(service_id=sample_email_template_with_placeholders.service.id)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2016-06-21 15:03:33 +01:00
|
|
|
|
path='/notifications/email',
|
2016-06-15 12:27:57 +01:00
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
|
2016-06-15 12:11:48 +01:00
|
|
|
|
response_data = json.loads(response.data)['data']
|
|
|
|
|
|
notification_id = response_data['notification']['id']
|
2016-06-21 15:03:33 +01:00
|
|
|
|
data.update({"template_version": sample_email_template_with_placeholders.version})
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
2016-10-03 14:26:36 +01:00
|
|
|
|
mocked.assert_called_once_with(
|
2017-03-30 10:46:23 +01:00
|
|
|
|
[notification_id],
|
2017-05-25 10:51:49 +01:00
|
|
|
|
queue="send-tasks"
|
2016-06-15 12:27:57 +01:00
|
|
|
|
)
|
|
|
|
|
|
assert response.status_code == 201
|
2016-12-09 15:56:25 +00:00
|
|
|
|
assert response_data['body'] == u'Hello Jo\nThis is an email from GOV.\u200BUK'
|
2016-06-21 15:03:33 +01:00
|
|
|
|
assert response_data['subject'] == 'Jo'
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-04-05 09:28:17 +01:00
|
|
|
|
@pytest.mark.parametrize('personalisation, expected_body, expected_subject', [
|
|
|
|
|
|
(
|
|
|
|
|
|
['Jo', 'John', 'Josephine'],
|
|
|
|
|
|
(
|
|
|
|
|
|
'Hello \n\n'
|
|
|
|
|
|
'* Jo\n'
|
|
|
|
|
|
'* John\n'
|
|
|
|
|
|
'* Josephine\n'
|
|
|
|
|
|
'This is an email from GOV.\u200BUK'
|
|
|
|
|
|
),
|
|
|
|
|
|
'Jo, John and Josephine',
|
|
|
|
|
|
),
|
|
|
|
|
|
(
|
|
|
|
|
|
6,
|
|
|
|
|
|
(
|
|
|
|
|
|
'Hello 6\n'
|
|
|
|
|
|
'This is an email from GOV.\u200BUK'
|
|
|
|
|
|
),
|
|
|
|
|
|
'6',
|
|
|
|
|
|
),
|
|
|
|
|
|
pytest.mark.xfail((
|
|
|
|
|
|
None,
|
|
|
|
|
|
('we consider None equivalent to missing personalisation'),
|
|
|
|
|
|
'',
|
|
|
|
|
|
)),
|
|
|
|
|
|
])
|
|
|
|
|
|
def test_send_notification_with_placeholders_replaced_with_unusual_types(
|
2017-04-05 09:18:58 +01:00
|
|
|
|
client,
|
|
|
|
|
|
sample_email_template_with_placeholders,
|
2017-04-05 09:28:17 +01:00
|
|
|
|
mocker,
|
|
|
|
|
|
personalisation,
|
|
|
|
|
|
expected_body,
|
|
|
|
|
|
expected_subject,
|
2017-04-05 09:18:58 +01:00
|
|
|
|
):
|
|
|
|
|
|
mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
|
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
|
path='/notifications/email',
|
|
|
|
|
|
data=json.dumps(
|
|
|
|
|
|
{
|
|
|
|
|
|
'to': 'ok@ok.com',
|
|
|
|
|
|
'template': str(sample_email_template_with_placeholders.id),
|
|
|
|
|
|
'personalisation': {
|
2017-04-05 09:28:17 +01:00
|
|
|
|
'name': personalisation
|
2017-04-05 09:18:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
),
|
|
|
|
|
|
headers=[
|
|
|
|
|
|
('Content-Type', 'application/json'),
|
|
|
|
|
|
create_authorization_header(service_id=sample_email_template_with_placeholders.service.id)
|
|
|
|
|
|
]
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 201
|
2017-04-05 09:28:17 +01:00
|
|
|
|
response_data = json.loads(response.data)['data']
|
|
|
|
|
|
assert response_data['body'] == expected_body
|
|
|
|
|
|
assert response_data['subject'] == expected_subject
|
2017-04-05 09:18:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-06-15 12:11:48 +01:00
|
|
|
|
def test_should_not_send_notification_for_archived_template(notify_api, sample_template):
|
2016-06-15 12:27:57 +01:00
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
|
with notify_api.test_client() as client:
|
|
|
|
|
|
sample_template.archived = True
|
|
|
|
|
|
dao_update_template(sample_template)
|
|
|
|
|
|
json_data = json.dumps({
|
|
|
|
|
|
'to': '+447700900855',
|
|
|
|
|
|
'template': sample_template.id
|
|
|
|
|
|
})
|
2016-06-30 13:21:35 +01:00
|
|
|
|
auth_header = create_authorization_header(service_id=sample_template.service_id)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
resp = client.post(
|
2016-06-15 12:11:48 +01:00
|
|
|
|
path='/notifications/sms',
|
2016-06-15 12:27:57 +01:00
|
|
|
|
data=json_data,
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
assert resp.status_code == 400
|
|
|
|
|
|
json_resp = json.loads(resp.get_data(as_text=True))
|
2016-10-27 11:46:37 +01:00
|
|
|
|
assert 'Template has been deleted' in json_resp['message']
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-10-03 10:57:10 +01:00
|
|
|
|
@pytest.mark.parametrize('template_type, to',
|
|
|
|
|
|
[('sms', '+447700900855'),
|
|
|
|
|
|
('email', 'not-someone-we-trust@email-address.com')])
|
|
|
|
|
|
def test_should_not_send_notification_if_restricted_and_not_a_service_user(notify_api,
|
|
|
|
|
|
sample_template,
|
|
|
|
|
|
sample_email_template,
|
|
|
|
|
|
mocker,
|
|
|
|
|
|
template_type,
|
|
|
|
|
|
to):
|
2016-06-15 12:27:57 +01:00
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
|
with notify_api.test_client() as client:
|
2016-10-03 14:26:36 +01:00
|
|
|
|
mocked = mocker.patch('app.celery.provider_tasks.deliver_{}.apply_async'.format(template_type))
|
2016-10-03 10:57:10 +01:00
|
|
|
|
template = sample_template if template_type == 'sms' else sample_email_template
|
|
|
|
|
|
template.service.restricted = True
|
|
|
|
|
|
dao_update_service(template.service)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
data = {
|
2016-10-03 10:57:10 +01:00
|
|
|
|
'to': to,
|
|
|
|
|
|
'template': template.id
|
2016-06-15 12:27:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-10-03 10:57:10 +01:00
|
|
|
|
auth_header = create_authorization_header(service_id=template.service_id)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2016-10-03 10:57:10 +01:00
|
|
|
|
path='/notifications/{}'.format(template_type),
|
2016-06-15 12:27:57 +01:00
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
2016-10-03 14:26:36 +01:00
|
|
|
|
mocked.assert_not_called()
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 400
|
2016-08-10 15:46:12 +01:00
|
|
|
|
assert [(
|
|
|
|
|
|
'Can’t send to this recipient when service is in trial mode '
|
|
|
|
|
|
'– see https://www.notifications.service.gov.uk/trial-mode'
|
|
|
|
|
|
)] == json_resp['message']['to']
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-10-03 10:57:10 +01:00
|
|
|
|
@pytest.mark.parametrize('template_type',
|
|
|
|
|
|
['sms', 'email'])
|
|
|
|
|
|
def test_should_send_notification_if_restricted_and_a_service_user(notify_api,
|
|
|
|
|
|
sample_template,
|
|
|
|
|
|
sample_email_template,
|
|
|
|
|
|
template_type,
|
|
|
|
|
|
mocker):
|
2016-06-30 13:21:35 +01:00
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
|
with notify_api.test_client() as client:
|
2017-03-30 10:46:23 +01:00
|
|
|
|
mocked = mocker.patch('app.celery.provider_tasks.deliver_{}.apply_async'.format(template_type))
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
2016-10-03 10:57:10 +01:00
|
|
|
|
template = sample_template if template_type == 'sms' else sample_email_template
|
|
|
|
|
|
to = template.service.created_by.mobile_number if template_type == 'sms' \
|
|
|
|
|
|
else template.service.created_by.email_address
|
|
|
|
|
|
template.service.restricted = True
|
|
|
|
|
|
dao_update_service(template.service)
|
2016-06-30 13:21:35 +01:00
|
|
|
|
data = {
|
2016-10-03 10:57:10 +01:00
|
|
|
|
'to': to,
|
|
|
|
|
|
'template': template.id
|
2016-06-30 13:21:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-10-03 10:57:10 +01:00
|
|
|
|
auth_header = create_authorization_header(service_id=template.service_id)
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2016-10-03 10:57:10 +01:00
|
|
|
|
path='/notifications/{}'.format(template_type),
|
2016-06-30 13:21:35 +01:00
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
|
2016-10-03 14:26:36 +01:00
|
|
|
|
assert mocked.called == 1
|
2016-06-30 13:21:35 +01:00
|
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-10-03 10:57:10 +01:00
|
|
|
|
@pytest.mark.parametrize('template_type',
|
|
|
|
|
|
['sms', 'email'])
|
|
|
|
|
|
def test_should_not_allow_template_from_another_service(notify_api,
|
|
|
|
|
|
service_factory,
|
|
|
|
|
|
sample_user,
|
|
|
|
|
|
mocker,
|
|
|
|
|
|
template_type):
|
2016-06-15 12:27:57 +01:00
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
|
with notify_api.test_client() as client:
|
2016-10-03 14:26:36 +01:00
|
|
|
|
mocked = mocker.patch('app.celery.provider_tasks.deliver_{}.apply_async'.format(template_type))
|
2016-06-15 12:27:57 +01:00
|
|
|
|
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')
|
|
|
|
|
|
|
|
|
|
|
|
service_2_templates = dao_get_all_templates_for_service(service_id=service_2.id)
|
2016-10-03 10:57:10 +01:00
|
|
|
|
to = sample_user.mobile_number if template_type == 'sms' else sample_user.email_address
|
2016-06-15 12:27:57 +01:00
|
|
|
|
data = {
|
2016-10-03 10:57:10 +01:00
|
|
|
|
'to': to,
|
2016-06-15 12:27:57 +01:00
|
|
|
|
'template': service_2_templates[0].id
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(service_id=service_1.id)
|
|
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2016-10-03 10:57:10 +01:00
|
|
|
|
path='/notifications/{}'.format(template_type),
|
2016-06-15 12:27:57 +01:00
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
2016-10-03 14:26:36 +01:00
|
|
|
|
mocked.assert_not_called()
|
2016-06-15 12:27:57 +01:00
|
|
|
|
assert response.status_code == 404
|
|
|
|
|
|
test_string = 'No result found'
|
|
|
|
|
|
assert test_string in json_resp['message']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@freeze_time("2016-01-01 11:09:00.061258")
|
|
|
|
|
|
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:
|
2017-03-30 10:46:23 +01:00
|
|
|
|
mocked = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'to': '07700 900 855',
|
|
|
|
|
|
'template': str(sample_template.id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(service_id=sample_template.service_id)
|
|
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
|
path='/notifications/sms',
|
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
|
2016-06-15 12:11:48 +01:00
|
|
|
|
response_data = json.loads(response.data)['data']
|
|
|
|
|
|
notification_id = response_data['notification']['id']
|
2016-09-07 13:47:22 +01:00
|
|
|
|
|
2017-05-25 10:51:49 +01:00
|
|
|
|
mocked.assert_called_once_with([notification_id], queue='send-tasks')
|
2016-06-15 12:27:57 +01:00
|
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
assert notification_id
|
2016-06-15 12:11:48 +01:00
|
|
|
|
assert 'subject' not in response_data
|
|
|
|
|
|
assert response_data['body'] == sample_template.content
|
|
|
|
|
|
assert response_data['template_version'] == sample_template.version
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
2016-10-03 14:26:36 +01:00
|
|
|
|
mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
2016-06-15 12:27:57 +01:00
|
|
|
|
to_address = "bad-email"
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'to': to_address,
|
2016-06-30 13:21:35 +01:00
|
|
|
|
'template': str(sample_email_template.service_id)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
}
|
2016-06-30 13:21:35 +01:00
|
|
|
|
auth_header = create_authorization_header(service_id=sample_email_template.service_id)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
|
path='/notifications/email',
|
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
|
|
|
|
|
|
data = json.loads(response.get_data(as_text=True))
|
2016-10-03 14:26:36 +01:00
|
|
|
|
mocked.apply_async.assert_not_called()
|
2016-06-15 12:27:57 +01:00
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
assert data['result'] == 'error'
|
2017-01-09 16:22:27 +00:00
|
|
|
|
assert data['message']['to'][0] == 'Not a valid email address'
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-03-30 10:46:23 +01:00
|
|
|
|
@freeze_time("2016-01-01 11:09:00.061258")
|
2016-06-15 12:27:57 +01:00
|
|
|
|
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:
|
2017-03-30 10:46:23 +01:00
|
|
|
|
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'to': 'ok@ok.com',
|
|
|
|
|
|
'template': str(sample_email_template.id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(service_id=sample_email_template.service_id)
|
|
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
|
path='/notifications/email',
|
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
assert response.status_code == 201
|
2016-06-15 12:11:48 +01:00
|
|
|
|
response_data = json.loads(response.get_data(as_text=True))['data']
|
|
|
|
|
|
notification_id = response_data['notification']['id']
|
2017-03-30 10:46:23 +01:00
|
|
|
|
app.celery.provider_tasks.deliver_email.apply_async.assert_called_once_with(
|
|
|
|
|
|
[notification_id],
|
2017-05-25 10:51:49 +01:00
|
|
|
|
queue="send-tasks"
|
2016-06-15 12:27:57 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
assert notification_id
|
2016-06-15 12:11:48 +01:00
|
|
|
|
assert response_data['subject'] == 'Email Subject'
|
|
|
|
|
|
assert response_data['body'] == sample_email_template.content
|
|
|
|
|
|
assert response_data['template_version'] == sample_email_template.version
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@freeze_time("2016-01-01 12:00:00.061258")
|
2016-11-11 16:47:52 +00:00
|
|
|
|
def test_should_block_api_call_if_over_day_limit_for_live_service(
|
2016-09-06 14:21:36 +01:00
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
notify_api,
|
|
|
|
|
|
mocker):
|
2016-06-15 12:27:57 +01:00
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
|
with notify_api.test_client() as client:
|
2016-09-28 15:05:50 +01:00
|
|
|
|
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
2016-09-06 14:21:36 +01:00
|
|
|
|
service = create_sample_service(notify_db, notify_db_session, limit=1, restricted=False)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
email_template = create_sample_email_template(notify_db, notify_db_session, service=service)
|
|
|
|
|
|
create_sample_notification(
|
|
|
|
|
|
notify_db, notify_db_session, template=email_template, service=service, created_at=datetime.utcnow()
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'to': 'ok@ok.com',
|
|
|
|
|
|
'template': str(email_template.id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(service_id=service.id)
|
|
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
|
path='/notifications/email',
|
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
2016-09-06 14:21:36 +01:00
|
|
|
|
json.loads(response.get_data(as_text=True))
|
2016-11-11 16:47:52 +00:00
|
|
|
|
assert response.status_code == 429
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@freeze_time("2016-01-01 12:00:00.061258")
|
2016-09-06 14:21:36 +01:00
|
|
|
|
def test_should_block_api_call_if_over_day_limit_for_restricted_service(
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
notify_api,
|
|
|
|
|
|
mocker):
|
2016-06-15 12:27:57 +01:00
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
|
with notify_api.test_client() as client:
|
2016-09-28 15:05:50 +01:00
|
|
|
|
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
service = create_sample_service(notify_db, notify_db_session, limit=1, restricted=True)
|
|
|
|
|
|
email_template = create_sample_email_template(notify_db, notify_db_session, service=service)
|
|
|
|
|
|
create_sample_notification(
|
|
|
|
|
|
notify_db, notify_db_session, template=email_template, service=service, created_at=datetime.utcnow()
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
data = {
|
2016-09-06 14:21:36 +01:00
|
|
|
|
'to': 'ok@ok.com',
|
|
|
|
|
|
'template': str(email_template.id)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(service_id=service.id)
|
|
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2016-09-06 14:21:36 +01:00
|
|
|
|
path='/notifications/email',
|
2016-06-15 12:27:57 +01:00
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
2016-09-06 14:21:36 +01:00
|
|
|
|
json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
|
2016-06-15 12:27:57 +01:00
|
|
|
|
assert response.status_code == 429
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-09-06 14:21:36 +01:00
|
|
|
|
@pytest.mark.parametrize('restricted', [True, False])
|
2016-06-15 12:27:57 +01:00
|
|
|
|
@freeze_time("2016-01-01 12:00:00.061258")
|
2016-09-06 14:21:36 +01:00
|
|
|
|
def test_should_allow_api_call_if_under_day_limit_regardless_of_type(
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
notify_api,
|
|
|
|
|
|
sample_user,
|
|
|
|
|
|
mocker,
|
|
|
|
|
|
restricted):
|
2016-06-15 12:27:57 +01:00
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
|
with notify_api.test_client() as client:
|
2017-03-30 10:46:23 +01:00
|
|
|
|
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
2016-09-06 14:21:36 +01:00
|
|
|
|
service = create_sample_service(notify_db, notify_db_session, limit=2, restricted=restricted)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
email_template = create_sample_email_template(notify_db, notify_db_session, service=service)
|
|
|
|
|
|
sms_template = create_sample_template(notify_db, notify_db_session, service=service)
|
|
|
|
|
|
create_sample_notification(notify_db, notify_db_session, template=email_template, service=service)
|
|
|
|
|
|
|
|
|
|
|
|
data = {
|
2016-09-06 14:21:36 +01:00
|
|
|
|
'to': sample_user.mobile_number,
|
2016-06-15 12:27:57 +01:00
|
|
|
|
'template': str(sms_template.id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(service_id=service.id)
|
|
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
|
path='/notifications/sms',
|
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-06-30 13:21:35 +01:00
|
|
|
|
def test_should_not_return_html_in_body(notify_api, notify_db, notify_db_session, mocker):
|
2016-06-15 12:11:48 +01:00
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
|
with notify_api.test_client() as client:
|
2017-03-30 10:46:23 +01:00
|
|
|
|
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
2016-12-28 12:30:26 +00:00
|
|
|
|
email_template = create_sample_email_template(notify_db, notify_db_session, content='hello\nthere')
|
2016-06-15 12:11:48 +01:00
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'to': 'ok@ok.com',
|
|
|
|
|
|
'template': str(email_template.id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(service_id=email_template.service_id)
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
|
path='/notifications/email',
|
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
assert json.loads(response.get_data(as_text=True))['data']['body'] == 'hello\nthere'
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
2017-03-30 10:46:23 +01:00
|
|
|
|
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
2016-06-30 13:21:35 +01:00
|
|
|
|
data = {
|
|
|
|
|
|
'to': "not-someone-we-trust@email-address.com",
|
|
|
|
|
|
'template': str(sample_email_template.id),
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(service_id=sample_email_template.service_id, key_type=KEY_TYPE_TEAM)
|
|
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
|
path='/notifications/email',
|
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
|
2017-03-30 10:46:23 +01:00
|
|
|
|
app.celery.provider_tasks.deliver_email.apply_async.assert_not_called()
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 400
|
2017-03-30 10:46:23 +01:00
|
|
|
|
assert [
|
|
|
|
|
|
'Can’t send to this recipient using a team-only API key'
|
|
|
|
|
|
] == json_resp['message']['to']
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
2017-03-30 10:46:23 +01:00
|
|
|
|
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'to': '07123123123',
|
|
|
|
|
|
'template': str(sample_template.id),
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(service_id=sample_template.service_id, key_type=KEY_TYPE_TEAM)
|
|
|
|
|
|
|
|
|
|
|
|
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))
|
2017-03-30 10:46:23 +01:00
|
|
|
|
app.celery.provider_tasks.deliver_sms.apply_async.assert_not_called()
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 400
|
2017-03-30 10:46:23 +01:00
|
|
|
|
assert [
|
|
|
|
|
|
'Can’t send to this recipient using a team-only API key'
|
|
|
|
|
|
] == json_resp['message']['to']
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-09-07 13:47:22 +01:00
|
|
|
|
def test_should_send_email_if_team_api_key_and_a_service_user(notify_api, sample_email_template, fake_uuid, mocker):
|
2016-06-30 13:21:35 +01:00
|
|
|
|
with notify_api.test_request_context(), notify_api.test_client() as client:
|
2017-03-30 10:46:23 +01:00
|
|
|
|
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
2016-10-28 17:10:00 +01:00
|
|
|
|
mocker.patch('app.dao.notifications_dao.create_uuid', return_value=fake_uuid)
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'to': sample_email_template.service.created_by.email_address,
|
|
|
|
|
|
'template': sample_email_template.id
|
|
|
|
|
|
}
|
2016-06-30 17:32:49 +01:00
|
|
|
|
api_key = ApiKey(service=sample_email_template.service,
|
|
|
|
|
|
name='team_key',
|
|
|
|
|
|
created_by=sample_email_template.created_by,
|
|
|
|
|
|
key_type=KEY_TYPE_TEAM)
|
|
|
|
|
|
save_model_api_key(api_key)
|
2017-06-19 14:32:22 +01:00
|
|
|
|
auth_header = create_jwt_token(secret=api_key.secret, client_id=str(api_key.service_id))
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
|
path='/notifications/email',
|
|
|
|
|
|
data=json.dumps(data),
|
2016-06-30 17:32:49 +01:00
|
|
|
|
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))])
|
2017-03-30 10:46:23 +01:00
|
|
|
|
|
2017-05-25 10:51:49 +01:00
|
|
|
|
app.celery.provider_tasks.deliver_email.apply_async.assert_called_once_with([fake_uuid], queue='send-tasks')
|
2016-06-30 13:21:35 +01:00
|
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-09-28 15:48:12 +01:00
|
|
|
|
@pytest.mark.parametrize('restricted', [True, False])
|
|
|
|
|
|
@pytest.mark.parametrize('limit', [0, 1])
|
|
|
|
|
|
def test_should_send_sms_to_anyone_with_test_key(
|
2017-03-30 10:46:23 +01:00
|
|
|
|
notify_api, sample_template, mocker, restricted, limit, fake_uuid
|
2016-09-28 15:48:12 +01:00
|
|
|
|
):
|
|
|
|
|
|
with notify_api.test_request_context(), notify_api.test_client() as client:
|
2017-03-30 10:46:23 +01:00
|
|
|
|
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
2016-10-28 17:10:00 +01:00
|
|
|
|
mocker.patch('app.dao.notifications_dao.create_uuid', return_value=fake_uuid)
|
2016-09-28 15:48:12 +01:00
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'to': '07811111111',
|
|
|
|
|
|
'template': sample_template.id
|
|
|
|
|
|
}
|
|
|
|
|
|
sample_template.service.restricted = restricted
|
|
|
|
|
|
sample_template.service.message_limit = limit
|
|
|
|
|
|
api_key = ApiKey(
|
|
|
|
|
|
service=sample_template.service,
|
|
|
|
|
|
name='test_key',
|
|
|
|
|
|
created_by=sample_template.created_by,
|
|
|
|
|
|
key_type=KEY_TYPE_TEST
|
|
|
|
|
|
)
|
|
|
|
|
|
save_model_api_key(api_key)
|
2017-06-19 14:32:22 +01:00
|
|
|
|
auth_header = create_jwt_token(secret=api_key.secret, client_id=str(api_key.service_id))
|
2016-09-28 15:48:12 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
|
path='/notifications/sms',
|
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))]
|
|
|
|
|
|
)
|
2017-05-25 10:51:49 +01:00
|
|
|
|
app.celery.provider_tasks.deliver_sms.apply_async.assert_called_once_with(
|
|
|
|
|
|
[fake_uuid], queue='research-mode-tasks'
|
|
|
|
|
|
)
|
2016-06-30 13:21:35 +01:00
|
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-30 11:00:22 +01:00
|
|
|
|
@pytest.mark.parametrize('restricted', [True, False])
|
|
|
|
|
|
@pytest.mark.parametrize('limit', [0, 1])
|
|
|
|
|
|
def test_should_send_email_to_anyone_with_test_key(
|
2017-03-30 10:46:23 +01:00
|
|
|
|
notify_api, sample_email_template, mocker, restricted, limit, fake_uuid
|
2016-08-30 11:00:22 +01:00
|
|
|
|
):
|
2016-08-30 10:47:27 +01:00
|
|
|
|
with notify_api.test_request_context(), notify_api.test_client() as client:
|
2017-03-30 10:46:23 +01:00
|
|
|
|
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
|
|
|
|
|
mocker.patch('app.dao.notifications_dao.create_uuid', return_value=fake_uuid)
|
2016-08-30 10:47:27 +01:00
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'to': 'anyone123@example.com',
|
|
|
|
|
|
'template': sample_email_template.id
|
|
|
|
|
|
}
|
2016-08-30 11:00:22 +01:00
|
|
|
|
sample_email_template.service.restricted = restricted
|
|
|
|
|
|
sample_email_template.service.message_limit = limit
|
2016-08-30 10:47:27 +01:00
|
|
|
|
api_key = ApiKey(
|
|
|
|
|
|
service=sample_email_template.service,
|
|
|
|
|
|
name='test_key',
|
|
|
|
|
|
created_by=sample_email_template.created_by,
|
|
|
|
|
|
key_type=KEY_TYPE_TEST
|
|
|
|
|
|
)
|
|
|
|
|
|
save_model_api_key(api_key)
|
2017-06-19 14:32:22 +01:00
|
|
|
|
auth_header = create_jwt_token(secret=api_key.secret, client_id=str(api_key.service_id))
|
2016-08-30 10:47:27 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
|
path='/notifications/email',
|
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))]
|
|
|
|
|
|
)
|
2017-03-30 10:46:23 +01:00
|
|
|
|
|
2017-05-25 10:51:49 +01:00
|
|
|
|
app.celery.provider_tasks.deliver_email.apply_async.assert_called_once_with(
|
|
|
|
|
|
[fake_uuid], queue='research-mode-tasks'
|
|
|
|
|
|
)
|
2016-08-30 10:47:27 +01:00
|
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-09-07 13:47:22 +01:00
|
|
|
|
def test_should_send_sms_if_team_api_key_and_a_service_user(notify_api, sample_template, fake_uuid, mocker):
|
2016-06-30 13:21:35 +01:00
|
|
|
|
with notify_api.test_request_context(), notify_api.test_client() as client:
|
2017-03-30 10:46:23 +01:00
|
|
|
|
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
2016-10-28 17:10:00 +01:00
|
|
|
|
mocker.patch('app.dao.notifications_dao.create_uuid', return_value=fake_uuid)
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'to': sample_template.service.created_by.mobile_number,
|
|
|
|
|
|
'template': sample_template.id
|
|
|
|
|
|
}
|
2016-06-30 17:32:49 +01:00
|
|
|
|
api_key = ApiKey(service=sample_template.service,
|
|
|
|
|
|
name='team_key',
|
|
|
|
|
|
created_by=sample_template.created_by,
|
|
|
|
|
|
key_type=KEY_TYPE_TEAM)
|
|
|
|
|
|
save_model_api_key(api_key)
|
2017-06-19 14:32:22 +01:00
|
|
|
|
auth_header = create_jwt_token(secret=api_key.secret, client_id=str(api_key.service_id))
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
|
path='/notifications/sms',
|
|
|
|
|
|
data=json.dumps(data),
|
2016-06-30 17:32:49 +01:00
|
|
|
|
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))])
|
|
|
|
|
|
|
2017-05-25 10:51:49 +01:00
|
|
|
|
app.celery.provider_tasks.deliver_sms.apply_async.assert_called_once_with([fake_uuid], queue='send-tasks')
|
2016-09-07 13:47:22 +01:00
|
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-10-03 10:57:10 +01:00
|
|
|
|
@pytest.mark.parametrize('template_type',
|
|
|
|
|
|
['sms', 'email'])
|
|
|
|
|
|
def test_should_persist_notification(notify_api, sample_template,
|
|
|
|
|
|
sample_email_template,
|
|
|
|
|
|
template_type,
|
|
|
|
|
|
fake_uuid, mocker):
|
2016-09-07 13:47:22 +01:00
|
|
|
|
with notify_api.test_request_context(), notify_api.test_client() as client:
|
2017-03-30 10:46:23 +01:00
|
|
|
|
mocked = mocker.patch('app.celery.provider_tasks.deliver_{}.apply_async'.format(template_type))
|
2016-10-28 17:10:00 +01:00
|
|
|
|
mocker.patch('app.dao.notifications_dao.create_uuid', return_value=fake_uuid)
|
2016-10-03 10:57:10 +01:00
|
|
|
|
template = sample_template if template_type == 'sms' else sample_email_template
|
|
|
|
|
|
to = sample_template.service.created_by.mobile_number if template_type == 'sms' \
|
|
|
|
|
|
else sample_email_template.service.created_by.email_address
|
2016-09-07 13:47:22 +01:00
|
|
|
|
data = {
|
2016-10-03 10:57:10 +01:00
|
|
|
|
'to': to,
|
|
|
|
|
|
'template': template.id
|
2016-09-07 13:47:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
api_key = ApiKey(
|
2016-10-03 10:57:10 +01:00
|
|
|
|
service=template.service,
|
2016-09-07 13:47:22 +01:00
|
|
|
|
name='team_key',
|
2016-10-03 10:57:10 +01:00
|
|
|
|
created_by=template.created_by,
|
2016-09-07 13:47:22 +01:00
|
|
|
|
key_type=KEY_TYPE_TEAM)
|
|
|
|
|
|
save_model_api_key(api_key)
|
2017-06-19 14:32:22 +01:00
|
|
|
|
auth_header = create_jwt_token(secret=api_key.secret, client_id=str(api_key.service_id))
|
2016-09-07 13:47:22 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2016-10-03 10:57:10 +01:00
|
|
|
|
path='/notifications/{}'.format(template_type),
|
2016-09-07 13:47:22 +01:00
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))])
|
|
|
|
|
|
|
2017-05-25 10:51:49 +01:00
|
|
|
|
mocked.assert_called_once_with([fake_uuid], queue='send-tasks')
|
2016-09-07 13:47:22 +01:00
|
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
|
2017-03-30 10:46:23 +01:00
|
|
|
|
notification = notifications_dao.get_notification_by_id(fake_uuid)
|
|
|
|
|
|
assert notification.to == to
|
|
|
|
|
|
assert notification.template_id == template.id
|
|
|
|
|
|
assert notification.notification_type == template_type
|
2016-09-08 16:00:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-10-03 10:57:10 +01:00
|
|
|
|
@pytest.mark.parametrize('template_type',
|
|
|
|
|
|
['sms', 'email'])
|
|
|
|
|
|
def test_should_delete_notification_and_return_error_if_sqs_fails(
|
2017-02-13 18:47:29 +00:00
|
|
|
|
client,
|
2016-09-08 16:00:18 +01:00
|
|
|
|
sample_email_template,
|
2016-10-03 10:57:10 +01:00
|
|
|
|
sample_template,
|
2016-09-08 16:00:18 +01:00
|
|
|
|
fake_uuid,
|
2016-10-03 10:57:10 +01:00
|
|
|
|
mocker,
|
|
|
|
|
|
template_type):
|
2017-02-13 18:47:29 +00:00
|
|
|
|
mocked = mocker.patch(
|
|
|
|
|
|
'app.celery.provider_tasks.deliver_{}.apply_async'.format(template_type),
|
|
|
|
|
|
side_effect=Exception("failed to talk to SQS")
|
|
|
|
|
|
)
|
|
|
|
|
|
mocker.patch('app.dao.notifications_dao.create_uuid', return_value=fake_uuid)
|
|
|
|
|
|
template = sample_template if template_type == 'sms' else sample_email_template
|
|
|
|
|
|
to = sample_template.service.created_by.mobile_number if template_type == 'sms' \
|
|
|
|
|
|
else sample_email_template.service.created_by.email_address
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'to': to,
|
|
|
|
|
|
'template': template.id
|
|
|
|
|
|
}
|
|
|
|
|
|
api_key = ApiKey(
|
|
|
|
|
|
service=template.service,
|
|
|
|
|
|
name='team_key',
|
|
|
|
|
|
created_by=template.created_by,
|
|
|
|
|
|
key_type=KEY_TYPE_TEAM)
|
|
|
|
|
|
save_model_api_key(api_key)
|
2017-06-19 14:32:22 +01:00
|
|
|
|
auth_header = create_jwt_token(secret=api_key.secret, client_id=str(api_key.service_id))
|
2016-09-08 16:00:18 +01:00
|
|
|
|
|
2017-02-13 18:47:29 +00:00
|
|
|
|
response = client.post(
|
|
|
|
|
|
path='/notifications/{}'.format(template_type),
|
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))])
|
2016-09-08 16:00:18 +01:00
|
|
|
|
|
2017-05-25 10:51:49 +01:00
|
|
|
|
mocked.assert_called_once_with([fake_uuid], queue='send-tasks')
|
2017-02-13 18:47:29 +00:00
|
|
|
|
assert response.status_code == 500
|
|
|
|
|
|
assert not notifications_dao.get_notification_by_id(fake_uuid)
|
|
|
|
|
|
assert not NotificationHistory.query.get(fake_uuid)
|
2016-09-13 17:00:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('to_email', [
|
|
|
|
|
|
'simulate-delivered@notifications.service.gov.uk',
|
2017-01-17 12:08:24 +00:00
|
|
|
|
'simulate-delivered-2@notifications.service.gov.uk',
|
|
|
|
|
|
'simulate-delivered-3@notifications.service.gov.uk'
|
2016-09-13 17:00:28 +01:00
|
|
|
|
])
|
2016-09-14 11:12:57 +01:00
|
|
|
|
def test_should_not_persist_notification_or_send_email_if_simulated_email(
|
|
|
|
|
|
client,
|
|
|
|
|
|
to_email,
|
|
|
|
|
|
sample_email_template,
|
|
|
|
|
|
mocker):
|
2016-09-28 15:05:50 +01:00
|
|
|
|
apply_async = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
2016-09-13 17:00:28 +01:00
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'to': to_email,
|
|
|
|
|
|
'template': sample_email_template.id
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(service_id=sample_email_template.service_id)
|
|
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
|
path='/notifications/email',
|
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
|
2016-09-14 10:25:09 +01:00
|
|
|
|
assert response.status_code == 201
|
2016-09-13 17:00:28 +01:00
|
|
|
|
apply_async.assert_not_called()
|
2016-09-14 13:07:57 +01:00
|
|
|
|
assert Notification.query.count() == 0
|
2016-09-13 17:00:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('to_sms', [
|
|
|
|
|
|
'07700 900000',
|
|
|
|
|
|
'07700 900111',
|
|
|
|
|
|
'07700 900222'
|
|
|
|
|
|
])
|
2016-09-14 11:12:57 +01:00
|
|
|
|
def test_should_not_persist_notification_or_send_sms_if_simulated_number(
|
|
|
|
|
|
client,
|
|
|
|
|
|
to_sms,
|
|
|
|
|
|
sample_template,
|
|
|
|
|
|
mocker):
|
2016-09-28 15:05:50 +01:00
|
|
|
|
apply_async = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
2016-09-13 17:00:28 +01:00
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'to': to_sms,
|
|
|
|
|
|
'template': sample_template.id
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(service_id=sample_template.service_id)
|
|
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
|
path='/notifications/sms',
|
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
|
2016-09-14 10:25:09 +01:00
|
|
|
|
assert response.status_code == 201
|
2016-09-13 17:00:28 +01:00
|
|
|
|
apply_async.assert_not_called()
|
2016-09-14 13:07:57 +01:00
|
|
|
|
assert Notification.query.count() == 0
|
2016-09-27 13:48:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-10-10 10:24:33 +01:00
|
|
|
|
@pytest.mark.parametrize('key_type', [
|
|
|
|
|
|
KEY_TYPE_NORMAL, KEY_TYPE_TEAM
|
|
|
|
|
|
])
|
|
|
|
|
|
@pytest.mark.parametrize('notification_type, to, _create_sample_template', [
|
|
|
|
|
|
('sms', '07827992635', create_sample_template),
|
2017-03-30 10:46:23 +01:00
|
|
|
|
('email', 'non_whitelist_recipient@mail.com', create_sample_email_template)]
|
|
|
|
|
|
)
|
2016-10-10 10:24:33 +01:00
|
|
|
|
def test_should_not_send_notification_to_non_whitelist_recipient_in_trial_mode(
|
|
|
|
|
|
client,
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
notification_type,
|
|
|
|
|
|
to,
|
|
|
|
|
|
_create_sample_template,
|
|
|
|
|
|
key_type,
|
2016-10-10 10:27:57 +01:00
|
|
|
|
mocker
|
|
|
|
|
|
):
|
2016-09-27 13:48:51 +01:00
|
|
|
|
service = create_sample_service(notify_db, notify_db_session, limit=2, restricted=True)
|
|
|
|
|
|
service_whitelist = create_sample_service_whitelist(notify_db, notify_db_session, service=service)
|
|
|
|
|
|
|
2016-10-03 14:26:36 +01:00
|
|
|
|
apply_async = mocker.patch('app.celery.provider_tasks.deliver_{}.apply_async'.format(notification_type))
|
2016-10-10 10:24:33 +01:00
|
|
|
|
template = _create_sample_template(notify_db, notify_db_session, service=service)
|
2016-09-27 13:48:51 +01:00
|
|
|
|
assert service_whitelist.service_id == service.id
|
2016-09-28 17:03:17 +01:00
|
|
|
|
assert to not in [member.recipient for member in service.whitelist]
|
2016-09-27 13:48:51 +01:00
|
|
|
|
|
2016-09-28 17:03:17 +01:00
|
|
|
|
create_sample_notification(notify_db, notify_db_session, template=template, service=service)
|
2016-09-27 13:48:51 +01:00
|
|
|
|
|
|
|
|
|
|
data = {
|
2016-09-28 17:03:17 +01:00
|
|
|
|
'to': to,
|
|
|
|
|
|
'template': str(template.id)
|
2016-09-27 13:48:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-09-28 17:03:17 +01:00
|
|
|
|
api_key = create_sample_api_key(notify_db, notify_db_session, service, key_type=key_type)
|
2017-06-19 14:32:22 +01:00
|
|
|
|
auth_header = create_jwt_token(secret=api_key.secret, client_id=str(api_key.service_id))
|
2016-09-27 13:48:51 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2016-09-28 17:03:17 +01:00
|
|
|
|
path='/notifications/{}'.format(notification_type),
|
2016-09-27 13:48:51 +01:00
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))])
|
|
|
|
|
|
|
|
|
|
|
|
expected_response_message = (
|
|
|
|
|
|
'Can’t send to this recipient when service is in trial mode '
|
|
|
|
|
|
'– see https://www.notifications.service.gov.uk/trial-mode'
|
2016-09-28 17:03:17 +01:00
|
|
|
|
) if key_type == KEY_TYPE_NORMAL else ('Can’t send to this recipient using a team-only API key')
|
2016-09-27 13:48:51 +01:00
|
|
|
|
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
assert json_resp['result'] == 'error'
|
|
|
|
|
|
assert expected_response_message in json_resp['message']['to']
|
|
|
|
|
|
apply_async.assert_not_called()
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-10-07 15:38:36 +01:00
|
|
|
|
@pytest.mark.parametrize('service_restricted', [
|
|
|
|
|
|
True, False
|
|
|
|
|
|
])
|
|
|
|
|
|
@pytest.mark.parametrize('key_type', [
|
|
|
|
|
|
KEY_TYPE_NORMAL, KEY_TYPE_TEAM
|
|
|
|
|
|
])
|
|
|
|
|
|
@pytest.mark.parametrize('notification_type, to, _create_sample_template', [
|
|
|
|
|
|
('sms', '07123123123', create_sample_template),
|
2017-03-30 10:46:23 +01:00
|
|
|
|
('email', 'whitelist_recipient@mail.com', create_sample_email_template)]
|
|
|
|
|
|
)
|
2016-10-07 15:38:36 +01:00
|
|
|
|
def test_should_send_notification_to_whitelist_recipient(
|
2017-03-30 10:46:23 +01:00
|
|
|
|
client,
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
notification_type,
|
|
|
|
|
|
to,
|
|
|
|
|
|
_create_sample_template,
|
|
|
|
|
|
key_type,
|
|
|
|
|
|
service_restricted,
|
|
|
|
|
|
mocker
|
2016-10-07 15:38:36 +01:00
|
|
|
|
):
|
|
|
|
|
|
service = create_sample_service(notify_db, notify_db_session, limit=2, restricted=service_restricted)
|
2017-03-30 10:46:23 +01:00
|
|
|
|
apply_async = mocker.patch('app.celery.provider_tasks.deliver_{}.apply_async'.format(notification_type))
|
2016-10-07 15:38:36 +01:00
|
|
|
|
template = _create_sample_template(notify_db, notify_db_session, service=service)
|
2016-09-28 17:03:17 +01:00
|
|
|
|
if notification_type == 'sms':
|
|
|
|
|
|
service_whitelist = create_sample_service_whitelist(notify_db, notify_db_session,
|
|
|
|
|
|
service=service, mobile_number=to)
|
|
|
|
|
|
elif notification_type == 'email':
|
|
|
|
|
|
service_whitelist = create_sample_service_whitelist(notify_db, notify_db_session,
|
|
|
|
|
|
service=service, email_address=to)
|
2016-09-27 13:48:51 +01:00
|
|
|
|
|
|
|
|
|
|
assert service_whitelist.service_id == service.id
|
2016-09-28 17:03:17 +01:00
|
|
|
|
assert to in [member.recipient for member in service.whitelist]
|
2016-09-27 13:48:51 +01:00
|
|
|
|
|
2016-09-28 17:03:17 +01:00
|
|
|
|
create_sample_notification(notify_db, notify_db_session, template=template, service=service)
|
2016-09-27 13:48:51 +01:00
|
|
|
|
|
|
|
|
|
|
data = {
|
2016-09-28 17:03:17 +01:00
|
|
|
|
'to': to,
|
|
|
|
|
|
'template': str(template.id)
|
2016-09-27 13:48:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-10-07 15:38:36 +01:00
|
|
|
|
sample_key = create_sample_api_key(notify_db, notify_db_session, service, key_type=key_type)
|
2017-06-19 14:32:22 +01:00
|
|
|
|
auth_header = create_jwt_token(secret=sample_key.secret, client_id=str(sample_key.service_id))
|
2016-09-27 13:48:51 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2016-09-28 17:03:17 +01:00
|
|
|
|
path='/notifications/{}'.format(notification_type),
|
2016-09-27 13:48:51 +01:00
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))])
|
|
|
|
|
|
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
assert json_resp['data']['notification']['id']
|
2016-09-28 17:03:17 +01:00
|
|
|
|
assert json_resp['data']['body'] == template.content
|
|
|
|
|
|
assert json_resp['data']['template_version'] == template.version
|
2016-10-03 14:26:36 +01:00
|
|
|
|
assert apply_async.called
|
2016-09-27 16:54:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-09-23 15:46:48 +01:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
'notification_type, template_type, to', [
|
|
|
|
|
|
('email', 'sms', 'notify@digital.cabinet-office.gov.uk'),
|
|
|
|
|
|
('sms', 'email', '+447700900986')
|
|
|
|
|
|
])
|
|
|
|
|
|
def test_should_error_if_notification_type_does_not_match_template_type(
|
|
|
|
|
|
client,
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
template_type,
|
|
|
|
|
|
notification_type,
|
|
|
|
|
|
to
|
|
|
|
|
|
):
|
|
|
|
|
|
template = create_sample_template(notify_db, notify_db_session, template_type=template_type)
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'to': to,
|
|
|
|
|
|
'template': template.id
|
|
|
|
|
|
}
|
|
|
|
|
|
auth_header = create_authorization_header(service_id=template.service_id)
|
|
|
|
|
|
response = client.post("/notifications/{}".format(notification_type),
|
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
assert json_resp['result'] == 'error'
|
2016-09-23 16:03:11 +01:00
|
|
|
|
assert '{0} template is not suitable for {1} notification'.format(template_type, notification_type) \
|
2016-09-23 15:46:48 +01:00
|
|
|
|
in json_resp['message']
|
2016-10-03 10:57:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_template_raises_invalid_request_exception_with_missing_personalisation(
|
|
|
|
|
|
sample_template_with_placeholders):
|
|
|
|
|
|
template = Template.query.get(sample_template_with_placeholders.id)
|
|
|
|
|
|
from app.notifications.rest import create_template_object_for_notification
|
|
|
|
|
|
with pytest.raises(InvalidRequest) as e:
|
|
|
|
|
|
create_template_object_for_notification(template, {})
|
2016-11-01 15:20:28 +00:00
|
|
|
|
assert {'template': ['Missing personalisation: Name']} == e.value.message
|
2016-10-03 10:57:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-03-07 16:03:10 +00:00
|
|
|
|
def test_create_template_doesnt_raise_with_too_much_personalisation(
|
2016-10-03 10:57:10 +01:00
|
|
|
|
sample_template_with_placeholders
|
|
|
|
|
|
):
|
|
|
|
|
|
from app.notifications.rest import create_template_object_for_notification
|
|
|
|
|
|
template = Template.query.get(sample_template_with_placeholders.id)
|
2017-03-07 16:03:10 +00:00
|
|
|
|
create_template_object_for_notification(template, {'name': 'Jo', 'extra': 'stuff'})
|
2016-10-03 10:57:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
'template_type, should_error', [
|
|
|
|
|
|
('sms', True),
|
|
|
|
|
|
('email', False)
|
|
|
|
|
|
]
|
|
|
|
|
|
)
|
|
|
|
|
|
def test_create_template_raises_invalid_request_when_content_too_large(
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
template_type,
|
|
|
|
|
|
should_error
|
|
|
|
|
|
):
|
|
|
|
|
|
sample = create_sample_template(
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
content="((long_text))",
|
|
|
|
|
|
template_type=template_type
|
|
|
|
|
|
)
|
|
|
|
|
|
limit = current_app.config.get('SMS_CHAR_COUNT_LIMIT')
|
|
|
|
|
|
template = Template.query.get(sample.id)
|
|
|
|
|
|
from app.notifications.rest import create_template_object_for_notification
|
|
|
|
|
|
try:
|
|
|
|
|
|
create_template_object_for_notification(template,
|
|
|
|
|
|
{'long_text':
|
|
|
|
|
|
''.join(
|
|
|
|
|
|
random.choice(string.ascii_uppercase + string.digits) for _ in
|
|
|
|
|
|
range(limit + 1))})
|
|
|
|
|
|
if should_error:
|
|
|
|
|
|
pytest.fail("expected an InvalidRequest")
|
|
|
|
|
|
except InvalidRequest as e:
|
|
|
|
|
|
if not should_error:
|
|
|
|
|
|
pytest.fail("do not expect an InvalidRequest")
|
|
|
|
|
|
assert e.message == {'content': ['Content has a character count greater than the limit of {}'.format(limit)]}
|
2017-01-17 13:16:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("notification_type, send_to",
|
|
|
|
|
|
[("sms", "07700 900 855"),
|
|
|
|
|
|
("email", "sample@email.com")])
|
|
|
|
|
|
def test_send_notification_uses_priority_queue_when_template_is_marked_as_priority(client, notify_db,
|
|
|
|
|
|
notify_db_session, mocker,
|
|
|
|
|
|
notification_type, send_to):
|
|
|
|
|
|
sample = create_sample_template(
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
template_type=notification_type,
|
|
|
|
|
|
process_type='priority'
|
|
|
|
|
|
)
|
2017-03-30 10:46:23 +01:00
|
|
|
|
mocked = mocker.patch('app.celery.provider_tasks.deliver_{}.apply_async'.format(notification_type))
|
2017-01-17 13:16:26 +00:00
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'to': send_to,
|
|
|
|
|
|
'template': str(sample.id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(service_id=sample.service_id)
|
|
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
|
path='/notifications/{}'.format(notification_type),
|
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
|
|
|
|
|
|
response_data = json.loads(response.data)['data']
|
|
|
|
|
|
notification_id = response_data['notification']['id']
|
|
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 201
|
2017-05-25 10:51:49 +01:00
|
|
|
|
mocked.assert_called_once_with([notification_id], queue='priority-tasks')
|
2017-04-24 14:15:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"notification_type, send_to",
|
|
|
|
|
|
[("sms", "07700 900 855"), ("email", "sample@email.com")]
|
|
|
|
|
|
)
|
|
|
|
|
|
def test_returns_a_429_limit_exceeded_if_rate_limit_exceeded(
|
|
|
|
|
|
client,
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
mocker,
|
|
|
|
|
|
notification_type,
|
|
|
|
|
|
send_to
|
|
|
|
|
|
):
|
|
|
|
|
|
sample = create_sample_template(
|
|
|
|
|
|
notify_db,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
template_type=notification_type
|
|
|
|
|
|
)
|
|
|
|
|
|
persist_mock = mocker.patch('app.notifications.rest.persist_notification')
|
|
|
|
|
|
deliver_mock = mocker.patch('app.notifications.rest.send_notification_to_queue')
|
|
|
|
|
|
|
|
|
|
|
|
mocker.patch(
|
|
|
|
|
|
'app.notifications.rest.check_rate_limiting',
|
|
|
|
|
|
side_effect=RateLimitError("LIMIT", "INTERVAL", "TYPE"))
|
|
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'to': send_to,
|
|
|
|
|
|
'template': str(sample.id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(service_id=sample.service_id)
|
|
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
|
path='/notifications/{}'.format(notification_type),
|
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
|
|
|
|
|
|
message = json.loads(response.data)['message']
|
|
|
|
|
|
result = json.loads(response.data)['result']
|
|
|
|
|
|
assert response.status_code == 429
|
|
|
|
|
|
assert result == 'error'
|
|
|
|
|
|
assert message == 'Exceeded rate limit for key type TYPE of LIMIT requests per INTERVAL seconds'
|
|
|
|
|
|
|
|
|
|
|
|
assert not persist_mock.called
|
|
|
|
|
|
assert not deliver_mock.called
|
2017-05-02 10:56:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-04-27 09:58:37 +01:00
|
|
|
|
def test_should_allow_store_original_number_on_sms_notification(client, sample_template, mocker):
|
|
|
|
|
|
mocked = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2017-04-27 09:58:37 +01:00
|
|
|
|
data = {
|
|
|
|
|
|
'to': '+(44) 7700-900 855',
|
|
|
|
|
|
'template': str(sample_template.id)
|
|
|
|
|
|
}
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2017-04-27 09:58:37 +01:00
|
|
|
|
auth_header = create_authorization_header(service_id=sample_template.service_id)
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2017-04-27 09:58:37 +01:00
|
|
|
|
response = client.post(
|
|
|
|
|
|
path='/notifications/sms',
|
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2017-04-27 09:58:37 +01:00
|
|
|
|
response_data = json.loads(response.data)['data']
|
|
|
|
|
|
notification_id = response_data['notification']['id']
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2017-05-25 10:51:49 +01:00
|
|
|
|
mocked.assert_called_once_with([notification_id], queue='send-tasks')
|
2017-04-27 09:58:37 +01:00
|
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
assert notification_id
|
|
|
|
|
|
notifications = Notification.query.all()
|
|
|
|
|
|
assert len(notifications) == 1
|
|
|
|
|
|
assert '+(44) 7700-900 855' == notifications[0].to
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-04-27 09:58:37 +01:00
|
|
|
|
def test_should_not_allow_international_number_on_sms_notification(client, sample_template, mocker):
|
|
|
|
|
|
mocked = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2017-04-27 09:58:37 +01:00
|
|
|
|
data = {
|
|
|
|
|
|
'to': '20-12-1234-1234',
|
|
|
|
|
|
'template': str(sample_template.id)
|
|
|
|
|
|
}
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2017-04-27 09:58:37 +01:00
|
|
|
|
auth_header = create_authorization_header(service_id=sample_template.service_id)
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2017-04-27 09:58:37 +01:00
|
|
|
|
response = client.post(
|
|
|
|
|
|
path='/notifications/sms',
|
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2017-04-27 09:58:37 +01:00
|
|
|
|
assert not mocked.called
|
|
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
error_json = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
assert error_json['result'] == 'error'
|
|
|
|
|
|
assert error_json['message']['to'][0] == 'Cannot send to international mobile numbers'
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-04-27 09:58:37 +01:00
|
|
|
|
def test_should_allow_international_number_on_sms_notification(client, notify_db, notify_db_session, mocker):
|
|
|
|
|
|
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2017-04-27 09:58:37 +01:00
|
|
|
|
service = sample_service(notify_db, notify_db_session, can_send_international_sms=True)
|
|
|
|
|
|
template = create_sample_template(notify_db, notify_db_session, service=service)
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2017-04-27 09:58:37 +01:00
|
|
|
|
data = {
|
|
|
|
|
|
'to': '20-12-1234-1234',
|
|
|
|
|
|
'template': str(template.id)
|
|
|
|
|
|
}
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2017-04-27 09:58:37 +01:00
|
|
|
|
auth_header = create_authorization_header(service_id=service.id)
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2017-04-27 09:58:37 +01:00
|
|
|
|
response = client.post(
|
|
|
|
|
|
path='/notifications/sms',
|
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2017-04-27 09:58:37 +01:00
|
|
|
|
assert response.status_code == 201
|