2017-06-15 13:40:38 +01:00
|
|
|
|
import uuid
|
|
|
|
|
|
from unittest.mock import Mock
|
|
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
from notifications_utils.recipients import InvalidPhoneError
|
2017-10-30 13:36:49 +00:00
|
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
2017-06-15 13:40:38 +01:00
|
|
|
|
|
|
|
|
|
|
from app.v2.errors import BadRequestError, TooManyRequestsError
|
2017-07-19 13:50:29 +01:00
|
|
|
|
from app.config import QueueNames
|
2018-01-17 15:20:04 +00:00
|
|
|
|
from app.dao.service_whitelist_dao import dao_add_and_commit_whitelisted_contacts
|
2017-06-15 13:40:38 +01:00
|
|
|
|
from app.service.send_notification import send_one_off_notification
|
2017-10-11 16:23:31 +01:00
|
|
|
|
from app.models import (
|
|
|
|
|
|
KEY_TYPE_NORMAL,
|
2018-01-17 15:20:04 +00:00
|
|
|
|
MOBILE_TYPE,
|
2017-10-11 16:23:31 +01:00
|
|
|
|
PRIORITY,
|
|
|
|
|
|
SMS_TYPE,
|
2018-01-17 15:20:04 +00:00
|
|
|
|
Notification,
|
|
|
|
|
|
ServiceWhitelist,
|
2017-10-30 13:36:49 +00:00
|
|
|
|
)
|
2017-06-15 13:40:38 +01:00
|
|
|
|
|
2017-11-25 11:31:36 +00:00
|
|
|
|
from tests.app.db import (
|
|
|
|
|
|
create_user,
|
|
|
|
|
|
create_reply_to_email,
|
2017-12-15 17:13:55 +00:00
|
|
|
|
create_letter_contact,
|
2017-12-18 16:16:33 +00:00
|
|
|
|
create_service_sms_sender,
|
2017-11-25 11:31:36 +00:00
|
|
|
|
create_service,
|
|
|
|
|
|
create_template
|
|
|
|
|
|
)
|
2017-06-15 13:40:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
|
def persist_mock(mocker):
|
|
|
|
|
|
noti = Mock(id=uuid.uuid4())
|
|
|
|
|
|
return mocker.patch('app.service.send_notification.persist_notification', return_value=noti)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
|
def celery_mock(mocker):
|
|
|
|
|
|
return mocker.patch('app.service.send_notification.send_notification_to_queue')
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-11-25 11:31:36 +00:00
|
|
|
|
def test_send_one_off_notification_calls_celery_correctly(persist_mock, celery_mock, notify_db_session):
|
|
|
|
|
|
service = create_service()
|
|
|
|
|
|
template = create_template(service=service)
|
|
|
|
|
|
|
|
|
|
|
|
service = template.service
|
2017-06-15 13:40:38 +01:00
|
|
|
|
|
|
|
|
|
|
post_data = {
|
2017-11-25 11:31:36 +00:00
|
|
|
|
'template_id': str(template.id),
|
2017-06-15 13:40:38 +01:00
|
|
|
|
'to': '07700 900 001',
|
|
|
|
|
|
'created_by': str(service.created_by_id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
resp = send_one_off_notification(service.id, post_data)
|
|
|
|
|
|
|
|
|
|
|
|
assert resp == {
|
|
|
|
|
|
'id': str(persist_mock.return_value.id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
celery_mock.assert_called_once_with(
|
|
|
|
|
|
notification=persist_mock.return_value,
|
|
|
|
|
|
research_mode=False,
|
2018-03-01 10:17:59 +00:00
|
|
|
|
queue=None
|
2017-06-15 13:40:38 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_send_one_off_notification_calls_persist_correctly(
|
|
|
|
|
|
persist_mock,
|
|
|
|
|
|
celery_mock,
|
2017-11-25 11:31:36 +00:00
|
|
|
|
notify_db_session
|
2017-06-15 13:40:38 +01:00
|
|
|
|
):
|
2017-11-25 11:31:36 +00:00
|
|
|
|
service = create_service()
|
|
|
|
|
|
template = create_template(service=service, content="Hello (( Name))\nYour thing is due soon")
|
2017-06-15 13:40:38 +01:00
|
|
|
|
|
|
|
|
|
|
post_data = {
|
|
|
|
|
|
'template_id': str(template.id),
|
|
|
|
|
|
'to': '07700 900 001',
|
|
|
|
|
|
'personalisation': {'name': 'foo'},
|
|
|
|
|
|
'created_by': str(service.created_by_id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
send_one_off_notification(service.id, post_data)
|
|
|
|
|
|
|
|
|
|
|
|
persist_mock.assert_called_once_with(
|
|
|
|
|
|
template_id=template.id,
|
|
|
|
|
|
template_version=template.version,
|
|
|
|
|
|
recipient=post_data['to'],
|
|
|
|
|
|
service=template.service,
|
|
|
|
|
|
personalisation={'name': 'foo'},
|
|
|
|
|
|
notification_type=SMS_TYPE,
|
|
|
|
|
|
api_key_id=None,
|
|
|
|
|
|
key_type=KEY_TYPE_NORMAL,
|
2017-11-25 11:31:36 +00:00
|
|
|
|
created_by_id=str(service.created_by_id),
|
|
|
|
|
|
reply_to_text='testing'
|
2017-06-15 13:40:38 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-11-25 11:31:36 +00:00
|
|
|
|
def test_send_one_off_notification_honors_research_mode(notify_db_session, persist_mock, celery_mock):
|
|
|
|
|
|
service = create_service(research_mode=True)
|
|
|
|
|
|
template = create_template(service=service)
|
2017-06-15 13:40:38 +01:00
|
|
|
|
|
|
|
|
|
|
post_data = {
|
2017-11-25 11:31:36 +00:00
|
|
|
|
'template_id': str(template.id),
|
2017-06-15 13:40:38 +01:00
|
|
|
|
'to': '07700 900 001',
|
|
|
|
|
|
'created_by': str(service.created_by_id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
send_one_off_notification(service.id, post_data)
|
|
|
|
|
|
|
|
|
|
|
|
assert celery_mock.call_args[1]['research_mode'] is True
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-01 10:17:59 +00:00
|
|
|
|
def test_send_one_off_notification_honors_priority(notify_db_session, persist_mock, celery_mock):
|
2017-11-25 11:31:36 +00:00
|
|
|
|
service = create_service()
|
|
|
|
|
|
template = create_template(service=service)
|
|
|
|
|
|
template.process_type = PRIORITY
|
2017-06-15 13:40:38 +01:00
|
|
|
|
|
|
|
|
|
|
post_data = {
|
2017-11-25 11:31:36 +00:00
|
|
|
|
'template_id': str(template.id),
|
2017-06-15 13:40:38 +01:00
|
|
|
|
'to': '07700 900 001',
|
|
|
|
|
|
'created_by': str(service.created_by_id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
send_one_off_notification(service.id, post_data)
|
|
|
|
|
|
|
|
|
|
|
|
assert celery_mock.call_args[1]['queue'] == QueueNames.PRIORITY
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-11-25 11:31:36 +00:00
|
|
|
|
def test_send_one_off_notification_raises_if_invalid_recipient(notify_db_session):
|
|
|
|
|
|
service = create_service()
|
|
|
|
|
|
template = create_template(service=service)
|
2017-06-15 13:40:38 +01:00
|
|
|
|
|
|
|
|
|
|
post_data = {
|
2017-11-25 11:31:36 +00:00
|
|
|
|
'template_id': str(template.id),
|
2017-06-15 13:40:38 +01:00
|
|
|
|
'to': 'not a phone number',
|
|
|
|
|
|
'created_by': str(service.created_by_id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
with pytest.raises(InvalidPhoneError):
|
|
|
|
|
|
send_one_off_notification(service.id, post_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-01-17 15:20:04 +00:00
|
|
|
|
@pytest.mark.parametrize('recipient', [
|
|
|
|
|
|
'07700 900 001', # not in team or whitelist
|
|
|
|
|
|
'07700900123', # in whitelist
|
|
|
|
|
|
'+447700-900-123', # in whitelist in different format
|
|
|
|
|
|
])
|
|
|
|
|
|
def test_send_one_off_notification_raises_if_cant_send_to_recipient(
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
recipient,
|
|
|
|
|
|
):
|
2017-11-25 11:31:36 +00:00
|
|
|
|
service = create_service(restricted=True)
|
|
|
|
|
|
template = create_template(service=service)
|
2018-01-17 15:20:04 +00:00
|
|
|
|
dao_add_and_commit_whitelisted_contacts([
|
|
|
|
|
|
ServiceWhitelist.from_string(service.id, MOBILE_TYPE, '07700900123'),
|
|
|
|
|
|
])
|
2017-06-15 13:40:38 +01:00
|
|
|
|
|
|
|
|
|
|
post_data = {
|
2017-11-25 11:31:36 +00:00
|
|
|
|
'template_id': str(template.id),
|
2018-01-17 15:20:04 +00:00
|
|
|
|
'to': recipient,
|
2017-06-15 13:40:38 +01:00
|
|
|
|
'created_by': str(service.created_by_id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
with pytest.raises(BadRequestError) as e:
|
|
|
|
|
|
send_one_off_notification(service.id, post_data)
|
|
|
|
|
|
|
|
|
|
|
|
assert 'service is in trial mode' in e.value.message
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-12-04 14:13:43 +00:00
|
|
|
|
def test_send_one_off_notification_raises_if_over_limit(notify_db_session, mocker):
|
2017-11-25 11:31:36 +00:00
|
|
|
|
service = create_service(message_limit=0)
|
|
|
|
|
|
template = create_template(service=service)
|
2017-12-04 14:13:43 +00:00
|
|
|
|
mocker.patch(
|
|
|
|
|
|
'app.service.send_notification.check_service_over_daily_message_limit',
|
|
|
|
|
|
side_effect=TooManyRequestsError(1)
|
|
|
|
|
|
)
|
2017-06-15 13:40:38 +01:00
|
|
|
|
|
|
|
|
|
|
post_data = {
|
2017-11-25 11:31:36 +00:00
|
|
|
|
'template_id': str(template.id),
|
2017-06-15 13:40:38 +01:00
|
|
|
|
'to': '07700 900 001',
|
|
|
|
|
|
'created_by': str(service.created_by_id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
with pytest.raises(TooManyRequestsError):
|
|
|
|
|
|
send_one_off_notification(service.id, post_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-11-25 11:31:36 +00:00
|
|
|
|
def test_send_one_off_notification_raises_if_message_too_long(persist_mock, notify_db_session):
|
|
|
|
|
|
service = create_service()
|
|
|
|
|
|
template = create_template(service=service, content="Hello (( Name))\nYour thing is due soon")
|
2017-06-15 13:40:38 +01:00
|
|
|
|
|
|
|
|
|
|
post_data = {
|
|
|
|
|
|
'template_id': str(template.id),
|
|
|
|
|
|
'to': '07700 900 001',
|
|
|
|
|
|
'personalisation': {'name': '🚫' * 500},
|
|
|
|
|
|
'created_by': str(service.created_by_id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
with pytest.raises(BadRequestError) as e:
|
|
|
|
|
|
send_one_off_notification(service.id, post_data)
|
|
|
|
|
|
|
|
|
|
|
|
assert e.value.message == 'Content for template has a character count greater than the limit of 495'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_send_one_off_notification_fails_if_created_by_other_service(sample_template):
|
|
|
|
|
|
user_not_in_service = create_user(email='some-other-user@gov.uk')
|
|
|
|
|
|
|
|
|
|
|
|
post_data = {
|
|
|
|
|
|
'template_id': str(sample_template.id),
|
|
|
|
|
|
'to': '07700 900 001',
|
|
|
|
|
|
'created_by': str(user_not_in_service.id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
with pytest.raises(BadRequestError) as e:
|
|
|
|
|
|
send_one_off_notification(sample_template.service_id, post_data)
|
|
|
|
|
|
|
|
|
|
|
|
assert e.value.message == 'Can’t create notification - Test User is not part of the "Sample service" service'
|
2017-10-11 16:23:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-11-29 16:47:23 +00:00
|
|
|
|
def test_send_one_off_notification_should_add_email_reply_to_text_for_notification(sample_email_template, celery_mock):
|
2017-10-11 16:23:31 +01:00
|
|
|
|
reply_to_email = create_reply_to_email(sample_email_template.service, 'test@test.com')
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'to': 'ok@ok.com',
|
|
|
|
|
|
'template_id': str(sample_email_template.id),
|
|
|
|
|
|
'sender_id': reply_to_email.id,
|
|
|
|
|
|
'created_by': str(sample_email_template.service.created_by_id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
notification_id = send_one_off_notification(service_id=sample_email_template.service.id, post_data=data)
|
|
|
|
|
|
notification = Notification.query.get(notification_id['id'])
|
|
|
|
|
|
celery_mock.assert_called_once_with(
|
|
|
|
|
|
notification=notification,
|
|
|
|
|
|
research_mode=False,
|
2018-03-01 10:17:59 +00:00
|
|
|
|
queue=None
|
2017-10-11 16:23:31 +01:00
|
|
|
|
)
|
2017-12-15 17:13:55 +00:00
|
|
|
|
assert notification.reply_to_text == reply_to_email.email_address
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_send_one_off_letter_notification_should_use_template_reply_to_text(sample_letter_template, celery_mock):
|
|
|
|
|
|
letter_contact = create_letter_contact(sample_letter_template.service, "Edinburgh, ED1 1AA", is_default=False)
|
|
|
|
|
|
sample_letter_template.reply_to = str(letter_contact.id)
|
|
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'to': 'user@example.com',
|
|
|
|
|
|
'template_id': str(sample_letter_template.id),
|
|
|
|
|
|
'created_by': str(sample_letter_template.service.created_by_id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
notification_id = send_one_off_notification(service_id=sample_letter_template.service.id, post_data=data)
|
|
|
|
|
|
notification = Notification.query.get(notification_id['id'])
|
|
|
|
|
|
celery_mock.assert_called_once_with(
|
|
|
|
|
|
notification=notification,
|
|
|
|
|
|
research_mode=False,
|
2018-03-01 10:17:59 +00:00
|
|
|
|
queue=None
|
2017-12-15 17:13:55 +00:00
|
|
|
|
)
|
2017-12-18 16:16:33 +00:00
|
|
|
|
|
2017-12-15 17:13:55 +00:00
|
|
|
|
assert notification.reply_to_text == "Edinburgh, ED1 1AA"
|
2017-10-12 09:34:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-12-18 16:16:33 +00:00
|
|
|
|
def test_send_one_off_sms_notification_should_use_sms_sender_reply_to_text(sample_service, celery_mock):
|
|
|
|
|
|
template = create_template(service=sample_service, template_type=SMS_TYPE)
|
|
|
|
|
|
sms_sender = create_service_sms_sender(
|
|
|
|
|
|
service=sample_service,
|
|
|
|
|
|
sms_sender='07123123123',
|
|
|
|
|
|
is_default=False
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'to': '07111111111',
|
|
|
|
|
|
'template_id': str(template.id),
|
|
|
|
|
|
'created_by': str(sample_service.created_by_id),
|
|
|
|
|
|
'sender_id': str(sms_sender.id),
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
notification_id = send_one_off_notification(service_id=sample_service.id, post_data=data)
|
|
|
|
|
|
notification = Notification.query.get(notification_id['id'])
|
|
|
|
|
|
celery_mock.assert_called_once_with(
|
|
|
|
|
|
notification=notification,
|
|
|
|
|
|
research_mode=False,
|
2018-03-01 10:17:59 +00:00
|
|
|
|
queue=None
|
2017-12-18 16:16:33 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert notification.reply_to_text == "447123123123"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_send_one_off_sms_notification_should_use_default_service_reply_to_text(sample_service, celery_mock):
|
|
|
|
|
|
template = create_template(service=sample_service, template_type=SMS_TYPE)
|
|
|
|
|
|
sample_service.service_sms_senders[0].is_default = False
|
|
|
|
|
|
create_service_sms_sender(
|
|
|
|
|
|
service=sample_service,
|
|
|
|
|
|
sms_sender='07123123456',
|
|
|
|
|
|
is_default=True
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'to': '07111111111',
|
|
|
|
|
|
'template_id': str(template.id),
|
|
|
|
|
|
'created_by': str(sample_service.created_by_id),
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
notification_id = send_one_off_notification(service_id=sample_service.id, post_data=data)
|
|
|
|
|
|
notification = Notification.query.get(notification_id['id'])
|
|
|
|
|
|
celery_mock.assert_called_once_with(
|
|
|
|
|
|
notification=notification,
|
|
|
|
|
|
research_mode=False,
|
2018-03-01 10:17:59 +00:00
|
|
|
|
queue=None
|
2017-12-18 16:16:33 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert notification.reply_to_text == "447123123456"
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-10-12 09:34:37 +01:00
|
|
|
|
def test_send_one_off_notification_should_throw_exception_if_reply_to_id_doesnot_exist(
|
2017-10-30 13:36:49 +00:00
|
|
|
|
sample_email_template
|
2017-10-12 09:34:37 +01:00
|
|
|
|
):
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'to': 'ok@ok.com',
|
|
|
|
|
|
'template_id': str(sample_email_template.id),
|
|
|
|
|
|
'sender_id': str(uuid.uuid4()),
|
|
|
|
|
|
'created_by': str(sample_email_template.service.created_by_id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-30 13:36:49 +00:00
|
|
|
|
with pytest.raises(expected_exception=SQLAlchemyError):
|
2017-10-12 09:34:37 +01:00
|
|
|
|
send_one_off_notification(service_id=sample_email_template.service.id, post_data=data)
|
2017-10-30 13:36:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_send_one_off_notification_should_throw_exception_if_sms_sender_id_doesnot_exist(
|
|
|
|
|
|
sample_template
|
|
|
|
|
|
):
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'to': '07700 900 001',
|
|
|
|
|
|
'template_id': str(sample_template.id),
|
|
|
|
|
|
'sender_id': str(uuid.uuid4()),
|
|
|
|
|
|
'created_by': str(sample_template.service.created_by_id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
with pytest.raises(expected_exception=SQLAlchemyError):
|
|
|
|
|
|
send_one_off_notification(service_id=sample_template.service.id, post_data=data)
|