mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-14 23:07:49 -04:00
Pulled out the tests that send SMSs into provider_tasks file/tests
- updated tests to validate creation of next task, not sending direct to provider
This commit is contained in:
@@ -35,6 +35,7 @@ retry_iteration_to_delay = {
|
|||||||
4: 60 * 30 # 30 minutes
|
4: 60 * 30 # 30 minutes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@notify_celery.task(bind=True, name="send-sms-to-provider", max_retries=5, default_retry_delay=5)
|
@notify_celery.task(bind=True, name="send-sms-to-provider", max_retries=5, default_retry_delay=5)
|
||||||
def send_sms_to_provider(self, service_id, notification_id, encrypted_notification):
|
def send_sms_to_provider(self, service_id, notification_id, encrypted_notification):
|
||||||
task_start = monotonic()
|
task_start = monotonic()
|
||||||
@@ -92,7 +93,7 @@ def send_sms_to_provider(self, service_id, notification_id, encrypted_notificati
|
|||||||
)
|
)
|
||||||
statsd_client.incr("notifications.tasks.send-sms-to-provider")
|
statsd_client.incr("notifications.tasks.send-sms-to-provider")
|
||||||
statsd_client.timing("notifications.tasks.send-sms-to-provider.task-time", monotonic() - task_start)
|
statsd_client.timing("notifications.tasks.send-sms-to-provider.task-time", monotonic() - task_start)
|
||||||
statsd_client.timing("notifications.sms.total-time", monotonic() - notification.created_at)
|
statsd_client.timing("notifications.sms.total-time", notification.sent_at - notification.created_at)
|
||||||
|
|
||||||
|
|
||||||
def provider_to_use(notification_type, notification_id):
|
def provider_to_use(notification_type, notification_id):
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ from datetime import datetime
|
|||||||
|
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
from monotonic import monotonic
|
from monotonic import monotonic
|
||||||
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
|
|
||||||
from app import clients, statsd_client
|
from app import clients, statsd_client
|
||||||
from app.clients.email import EmailClientException
|
from app.clients.email import EmailClientException
|
||||||
@@ -10,7 +11,7 @@ from app.clients.sms import SmsClientException
|
|||||||
from app.dao.services_dao import dao_fetch_service_by_id
|
from app.dao.services_dao import dao_fetch_service_by_id
|
||||||
from app.dao.templates_dao import dao_get_template_by_id
|
from app.dao.templates_dao import dao_get_template_by_id
|
||||||
from app.dao.provider_details_dao import get_provider_details_by_notification_type
|
from app.dao.provider_details_dao import get_provider_details_by_notification_type
|
||||||
from app.celery.research_mode_tasks import send_email_response, send_sms_response
|
from app.celery.research_mode_tasks import send_email_response
|
||||||
from app.celery.provider_tasks import (
|
from app.celery.provider_tasks import (
|
||||||
send_sms_to_provider
|
send_sms_to_provider
|
||||||
)
|
)
|
||||||
@@ -285,7 +286,7 @@ def send_email(service_id, notification_id, from_address, encrypted_notification
|
|||||||
sent_by=provider.get_name()
|
sent_by=provider.get_name()
|
||||||
)
|
)
|
||||||
|
|
||||||
dao_create_notification(notification_db_object, TEMPLATE_TYPE_EMAIL, provider.get_name())
|
dao_create_notification(notification_db_object, TEMPLATE_TYPE_EMAIL)
|
||||||
statsd_client.timing_with_dates(
|
statsd_client.timing_with_dates(
|
||||||
"notifications.tasks.send-email.queued-for",
|
"notifications.tasks.send-email.queued-for",
|
||||||
sent_at,
|
sent_at,
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from mock import ANY
|
from mock import ANY, call
|
||||||
|
|
||||||
from app import statsd_client, mmg_client, DATETIME_FORMAT
|
from app import statsd_client, mmg_client, DATETIME_FORMAT
|
||||||
from app.celery.provider_tasks import send_sms_to_provider
|
from app.celery.provider_tasks import send_sms_to_provider
|
||||||
@@ -12,11 +12,6 @@ from app.dao import notifications_dao, jobs_dao, provider_details_dao
|
|||||||
from notifications_utils.recipients import validate_phone_number, format_phone_number
|
from notifications_utils.recipients import validate_phone_number, format_phone_number
|
||||||
|
|
||||||
from tests.app.conftest import (
|
from tests.app.conftest import (
|
||||||
sample_service,
|
|
||||||
sample_user,
|
|
||||||
sample_template,
|
|
||||||
sample_job,
|
|
||||||
sample_email_template,
|
|
||||||
sample_notification
|
sample_notification
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -50,22 +45,25 @@ def test_should_return_highest_priority_active_provider(notify_db, notify_db_ses
|
|||||||
assert provider_to_use('sms', '1234').name == first.identifier
|
assert provider_to_use('sms', '1234').name == first.identifier
|
||||||
|
|
||||||
|
|
||||||
def test_should_send_template_to_correct_sms_provider_and_persist(sample_notification, sample_template_with_placeholders, mocker):
|
def test_should_send_personalised_template_to_correct_sms_provider_and_persist(
|
||||||
|
notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
sample_template_with_placeholders,
|
||||||
|
mocker):
|
||||||
|
db_notification = sample_notification(notify_db, notify_db_session, template=sample_template_with_placeholders)
|
||||||
|
|
||||||
notification = _notification_json(
|
notification = _notification_json(
|
||||||
sample_template_with_placeholders,
|
sample_template_with_placeholders,
|
||||||
to="+447234123123",
|
to="+447234123123",
|
||||||
personalisation={"name": "Jo"}
|
personalisation={"name": "Jo"}
|
||||||
)
|
)
|
||||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||||
mocker.patch('app.dao.notifications_dao.get_notification_by_id', return_value=sample_notification)
|
|
||||||
mocker.patch('app.mmg_client.send_sms')
|
mocker.patch('app.mmg_client.send_sms')
|
||||||
mocker.patch('app.mmg_client.get_name', return_value="mmg")
|
mocker.patch('app.mmg_client.get_name', return_value="mmg")
|
||||||
mocker.patch('app.statsd_client.incr')
|
mocker.patch('app.statsd_client.incr')
|
||||||
mocker.patch('app.statsd_client.timing_with_dates')
|
mocker.patch('app.statsd_client.timing_with_dates')
|
||||||
mocker.patch('app.statsd_client.timing')
|
mocker.patch('app.statsd_client.timing')
|
||||||
|
|
||||||
notification_id = uuid.uuid4()
|
|
||||||
|
|
||||||
freezer = freeze_time("2016-01-01 11:09:00.00000")
|
freezer = freeze_time("2016-01-01 11:09:00.00000")
|
||||||
freezer.start()
|
freezer.start()
|
||||||
now = datetime.utcnow()
|
now = datetime.utcnow()
|
||||||
@@ -75,8 +73,8 @@ def test_should_send_template_to_correct_sms_provider_and_persist(sample_notific
|
|||||||
freezer.start()
|
freezer.start()
|
||||||
|
|
||||||
send_sms_to_provider(
|
send_sms_to_provider(
|
||||||
sample_template_with_placeholders.service_id,
|
db_notification.service_id,
|
||||||
notification_id,
|
db_notification.id,
|
||||||
"encrypted-in-reality"
|
"encrypted-in-reality"
|
||||||
)
|
)
|
||||||
freezer.stop()
|
freezer.stop()
|
||||||
@@ -85,22 +83,145 @@ def test_should_send_template_to_correct_sms_provider_and_persist(sample_notific
|
|||||||
mmg_client.send_sms.assert_called_once_with(
|
mmg_client.send_sms.assert_called_once_with(
|
||||||
to=format_phone_number(validate_phone_number("+447234123123")),
|
to=format_phone_number(validate_phone_number("+447234123123")),
|
||||||
content="Sample service: Hello Jo",
|
content="Sample service: Hello Jo",
|
||||||
reference=str(notification_id)
|
reference=str(db_notification.id)
|
||||||
)
|
)
|
||||||
|
|
||||||
statsd_client.incr.assert_called_once_with("notifications.tasks.send-sms")
|
statsd_client.incr.assert_called_once_with("notifications.tasks.send-sms-to-provider")
|
||||||
statsd_client.timing.assert_called_once_with("notifications.tasks.send-sms-to-provider.task-time", ANY)
|
statsd_client.timing.assert_has_calls([
|
||||||
statsd_client.timing.assert_called_once_with("notifications.sms.total-time", ANY)
|
call("notifications.tasks.send-sms-to-provider.task-time", ANY),
|
||||||
|
call("notifications.sms.total-time", ANY)
|
||||||
|
])
|
||||||
|
|
||||||
notification = notifications_dao.get_notification(
|
notification = notifications_dao.get_notification(
|
||||||
sample_template_with_placeholders.service_id, notification_id
|
db_notification.service_id, db_notification.id
|
||||||
)
|
)
|
||||||
|
|
||||||
assert notification.status == 'sending'
|
assert notification.status == 'sending'
|
||||||
assert notification.sent_at > now
|
assert notification.sent_at > now
|
||||||
assert notification.sent_by == 'mmg'
|
assert notification.sent_by == 'mmg'
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_send_template_to_correct_sms_provider_and_persist(
|
||||||
|
notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
sample_template,
|
||||||
|
mocker):
|
||||||
|
db_notification = sample_notification(notify_db, notify_db_session, template=sample_template)
|
||||||
|
|
||||||
|
|
||||||
|
notification = _notification_json(
|
||||||
|
sample_template,
|
||||||
|
to="+447234123123"
|
||||||
|
)
|
||||||
|
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||||
|
mocker.patch('app.mmg_client.send_sms')
|
||||||
|
mocker.patch('app.mmg_client.get_name', return_value="mmg")
|
||||||
|
mocker.patch('app.statsd_client.incr')
|
||||||
|
mocker.patch('app.statsd_client.timing_with_dates')
|
||||||
|
mocker.patch('app.statsd_client.timing')
|
||||||
|
|
||||||
|
freezer = freeze_time("2016-01-01 11:09:00.00000")
|
||||||
|
freezer.start()
|
||||||
|
now = datetime.utcnow()
|
||||||
|
freezer.stop()
|
||||||
|
|
||||||
|
freezer = freeze_time("2016-01-01 11:10:00.00000")
|
||||||
|
freezer.start()
|
||||||
|
|
||||||
|
send_sms_to_provider(
|
||||||
|
db_notification.service_id,
|
||||||
|
db_notification.id,
|
||||||
|
"encrypted-in-reality"
|
||||||
|
)
|
||||||
|
freezer.stop()
|
||||||
|
|
||||||
|
mmg_client.send_sms.assert_called_once_with(
|
||||||
|
to=format_phone_number(validate_phone_number("+447234123123")),
|
||||||
|
content="Sample service: This is a template",
|
||||||
|
reference=str(db_notification.id)
|
||||||
|
)
|
||||||
|
|
||||||
|
### FIXME
|
||||||
|
def test_send_sms_should_use_template_version_from_job_not_latest(sample_template, mocker):
|
||||||
|
notification = _notification_json(sample_template, '+447234123123')
|
||||||
|
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||||
|
mocker.patch('app.mmg_client.send_sms')
|
||||||
|
mocker.patch('app.mmg_client.get_name', return_value="mmg")
|
||||||
|
version_on_notification = sample_template.version
|
||||||
|
|
||||||
|
# Change the template
|
||||||
|
from app.dao.templates_dao import dao_update_template, dao_get_template_by_id
|
||||||
|
sample_template.content = sample_template.content + " another version of the template"
|
||||||
|
dao_update_template(sample_template)
|
||||||
|
t = dao_get_template_by_id(sample_template.id)
|
||||||
|
assert t.version > version_on_notification
|
||||||
|
|
||||||
|
notification_id = uuid.uuid4()
|
||||||
|
now = datetime.utcnow()
|
||||||
|
send_sms(
|
||||||
|
sample_template.service_id,
|
||||||
|
notification_id,
|
||||||
|
"encrypted-in-reality",
|
||||||
|
now.strftime(DATETIME_FORMAT)
|
||||||
|
)
|
||||||
|
|
||||||
|
mmg_client.send_sms.assert_called_once_with(
|
||||||
|
to=format_phone_number(validate_phone_number("+447234123123")),
|
||||||
|
content="Sample service: This is a template",
|
||||||
|
reference=str(notification_id)
|
||||||
|
)
|
||||||
|
|
||||||
|
persisted_notification = notifications_dao.get_notification(sample_template.service_id, notification_id)
|
||||||
|
assert persisted_notification.id == notification_id
|
||||||
|
assert persisted_notification.to == '+447234123123'
|
||||||
|
assert persisted_notification.template_id == sample_template.id
|
||||||
|
assert persisted_notification.template_version == version_on_notification
|
||||||
|
assert persisted_notification.template_version != sample_template.version
|
||||||
|
assert persisted_notification.created_at == now
|
||||||
|
assert persisted_notification.sent_at > now
|
||||||
|
assert persisted_notification.status == 'sending'
|
||||||
|
assert persisted_notification.sent_by == 'mmg'
|
||||||
|
assert persisted_notification.content_char_count == len("Sample service: This is a template")
|
||||||
|
|
||||||
|
|
||||||
|
### FIXME
|
||||||
|
def test_should_call_send_sms_response_task_if_research_mode(notify_db, sample_service, sample_template, mocker):
|
||||||
|
notification = _notification_json(
|
||||||
|
sample_template,
|
||||||
|
to="+447234123123"
|
||||||
|
)
|
||||||
|
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||||
|
mocker.patch('app.mmg_client.send_sms')
|
||||||
|
mocker.patch('app.mmg_client.get_name', return_value="mmg")
|
||||||
|
mocker.patch('app.celery.research_mode_tasks.send_sms_response.apply_async')
|
||||||
|
|
||||||
|
sample_service.research_mode = True
|
||||||
|
notify_db.session.add(sample_service)
|
||||||
|
notify_db.session.commit()
|
||||||
|
|
||||||
|
notification_id = uuid.uuid4()
|
||||||
|
now = datetime.utcnow()
|
||||||
|
send_sms(
|
||||||
|
sample_service.id,
|
||||||
|
notification_id,
|
||||||
|
"encrypted-in-reality",
|
||||||
|
now.strftime(DATETIME_FORMAT)
|
||||||
|
)
|
||||||
|
assert not mmg_client.send_sms.called
|
||||||
|
send_sms_response.apply_async.assert_called_once_with(
|
||||||
|
('mmg', str(notification_id), "+447234123123"), queue='research-mode'
|
||||||
|
)
|
||||||
|
|
||||||
|
persisted_notification = notifications_dao.get_notification(sample_service.id, notification_id)
|
||||||
|
assert persisted_notification.id == notification_id
|
||||||
|
assert persisted_notification.to == '+447234123123'
|
||||||
|
assert persisted_notification.template_id == sample_template.id
|
||||||
|
assert persisted_notification.status == 'sending'
|
||||||
|
assert persisted_notification.sent_at > now
|
||||||
|
assert persisted_notification.created_at == now
|
||||||
|
assert persisted_notification.sent_by == 'mmg'
|
||||||
|
|
||||||
|
|
||||||
def _notification_json(template, to, personalisation=None, job_id=None, row_number=None):
|
def _notification_json(template, to, personalisation=None, job_id=None, row_number=None):
|
||||||
notification = {
|
notification = {
|
||||||
"template": template.id,
|
"template": template.id,
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ from flask import current_app
|
|||||||
from mock import ANY
|
from mock import ANY
|
||||||
from notifications_utils.recipients import validate_phone_number, format_phone_number
|
from notifications_utils.recipients import validate_phone_number, format_phone_number
|
||||||
|
|
||||||
|
from app.celery import provider_tasks
|
||||||
from app.celery.tasks import (
|
from app.celery.tasks import (
|
||||||
send_sms,
|
send_sms,
|
||||||
send_sms_code,
|
send_sms_code,
|
||||||
@@ -340,15 +341,15 @@ def test_should_process_all_sms_job(sample_job,
|
|||||||
|
|
||||||
|
|
||||||
### START OF SEND-SMS
|
### START OF SEND-SMS
|
||||||
|
|
||||||
def test_should_send_template_to_correct_sms_provider_and_persist(sample_template_with_placeholders, mocker):
|
def test_should_send_template_to_correct_sms_provider_and_persist(sample_template_with_placeholders, mocker):
|
||||||
notification = _notification_json(sample_template_with_placeholders,
|
notification = _notification_json(sample_template_with_placeholders,
|
||||||
to="+447234123123", personalisation={"name": "Jo"})
|
to="+447234123123", personalisation={"name": "Jo"})
|
||||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||||
mocker.patch('app.mmg_client.send_sms')
|
|
||||||
mocker.patch('app.mmg_client.get_name', return_value="mmg")
|
|
||||||
mocker.patch('app.statsd_client.incr')
|
mocker.patch('app.statsd_client.incr')
|
||||||
mocker.patch('app.statsd_client.timing_with_dates')
|
mocker.patch('app.statsd_client.timing_with_dates')
|
||||||
mocker.patch('app.statsd_client.timing')
|
mocker.patch('app.statsd_client.timing')
|
||||||
|
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
|
||||||
|
|
||||||
notification_id = uuid.uuid4()
|
notification_id = uuid.uuid4()
|
||||||
|
|
||||||
@@ -368,18 +369,15 @@ def test_should_send_template_to_correct_sms_provider_and_persist(sample_templat
|
|||||||
)
|
)
|
||||||
freezer.stop()
|
freezer.stop()
|
||||||
|
|
||||||
statsd_client.timing_with_dates.assert_called_once_with(
|
|
||||||
"notifications.tasks.send-sms.queued-for",
|
|
||||||
datetime(2016, 1, 1, 11, 10, 0, 00000),
|
|
||||||
datetime(2016, 1, 1, 11, 9, 0, 00000)
|
|
||||||
)
|
|
||||||
statsd_client.timing.assert_called_once_with("notifications.tasks.send-sms.task-time", ANY)
|
statsd_client.timing.assert_called_once_with("notifications.tasks.send-sms.task-time", ANY)
|
||||||
|
|
||||||
mmg_client.send_sms.assert_called_once_with(
|
provider_tasks.send_sms_to_provider.apply_async.assert_called_once_with(
|
||||||
to=format_phone_number(validate_phone_number("+447234123123")),
|
(sample_template_with_placeholders.service_id,
|
||||||
content="Sample service: Hello Jo",
|
notification_id,
|
||||||
reference=str(notification_id)
|
"encrypted-in-reality"),
|
||||||
|
queue="sms"
|
||||||
)
|
)
|
||||||
|
|
||||||
statsd_client.incr.assert_called_once_with("notifications.tasks.send-sms")
|
statsd_client.incr.assert_called_once_with("notifications.tasks.send-sms")
|
||||||
persisted_notification = notifications_dao.get_notification(
|
persisted_notification = notifications_dao.get_notification(
|
||||||
sample_template_with_placeholders.service_id, notification_id
|
sample_template_with_placeholders.service_id, notification_id
|
||||||
@@ -390,16 +388,15 @@ def test_should_send_template_to_correct_sms_provider_and_persist(sample_templat
|
|||||||
assert persisted_notification.template_version == sample_template_with_placeholders.version
|
assert persisted_notification.template_version == sample_template_with_placeholders.version
|
||||||
assert persisted_notification.status == 'sending'
|
assert persisted_notification.status == 'sending'
|
||||||
assert persisted_notification.created_at == now
|
assert persisted_notification.created_at == now
|
||||||
assert persisted_notification.sent_at > now
|
assert not persisted_notification.sent_at
|
||||||
assert persisted_notification.sent_by == 'mmg'
|
assert not persisted_notification.sent_by
|
||||||
assert not persisted_notification.job_id
|
assert not persisted_notification.job_id
|
||||||
|
|
||||||
|
|
||||||
def test_should_send_sms_without_personalisation(sample_template, mocker):
|
def test_should_send_sms_without_personalisation(sample_template, mocker):
|
||||||
notification = _notification_json(sample_template, "+447234123123")
|
notification = _notification_json(sample_template, "+447234123123")
|
||||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||||
mocker.patch('app.mmg_client.send_sms')
|
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
|
||||||
mocker.patch('app.mmg_client.get_name', return_value="mmg")
|
|
||||||
|
|
||||||
notification_id = uuid.uuid4()
|
notification_id = uuid.uuid4()
|
||||||
now = datetime.utcnow()
|
now = datetime.utcnow()
|
||||||
@@ -410,10 +407,12 @@ def test_should_send_sms_without_personalisation(sample_template, mocker):
|
|||||||
now.strftime(DATETIME_FORMAT)
|
now.strftime(DATETIME_FORMAT)
|
||||||
)
|
)
|
||||||
|
|
||||||
mmg_client.send_sms.assert_called_once_with(
|
|
||||||
to=format_phone_number(validate_phone_number("+447234123123")),
|
provider_tasks.send_sms_to_provider.apply_async.assert_called_once_with(
|
||||||
content="Sample service: This is a template",
|
(sample_template.service_id,
|
||||||
reference=str(notification_id)
|
notification_id,
|
||||||
|
"encrypted-in-reality"),
|
||||||
|
queue="sms"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -425,8 +424,7 @@ def test_should_send_sms_if_restricted_service_and_valid_number(notify_db, notif
|
|||||||
notification = _notification_json(template, "+447700900890") # The user’s own number, but in a different format
|
notification = _notification_json(template, "+447700900890") # The user’s own number, but in a different format
|
||||||
|
|
||||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||||
mocker.patch('app.mmg_client.send_sms')
|
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
|
||||||
mocker.patch('app.mmg_client.get_name', return_value="mmg")
|
|
||||||
|
|
||||||
notification_id = uuid.uuid4()
|
notification_id = uuid.uuid4()
|
||||||
now = datetime.utcnow()
|
now = datetime.utcnow()
|
||||||
@@ -437,10 +435,11 @@ def test_should_send_sms_if_restricted_service_and_valid_number(notify_db, notif
|
|||||||
now.strftime(DATETIME_FORMAT)
|
now.strftime(DATETIME_FORMAT)
|
||||||
)
|
)
|
||||||
|
|
||||||
mmg_client.send_sms.assert_called_once_with(
|
provider_tasks.send_sms_to_provider.apply_async.assert_called_once_with(
|
||||||
to=format_phone_number(validate_phone_number("+447700900890")),
|
(service.id,
|
||||||
content="Sample service: This is a template",
|
notification_id,
|
||||||
reference=str(notification_id)
|
"encrypted-in-reality"),
|
||||||
|
queue="sms"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -451,8 +450,7 @@ def test_should_not_send_sms_if_restricted_service_and_invalid_number(notify_db,
|
|||||||
|
|
||||||
notification = _notification_json(template, "07700 900849")
|
notification = _notification_json(template, "07700 900849")
|
||||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||||
mocker.patch('app.mmg_client.send_sms')
|
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
|
||||||
mocker.patch('app.mmg_client.get_name', return_value="mmg")
|
|
||||||
|
|
||||||
notification_id = uuid.uuid4()
|
notification_id = uuid.uuid4()
|
||||||
now = datetime.utcnow()
|
now = datetime.utcnow()
|
||||||
@@ -462,53 +460,11 @@ def test_should_not_send_sms_if_restricted_service_and_invalid_number(notify_db,
|
|||||||
"encrypted-in-reality",
|
"encrypted-in-reality",
|
||||||
now.strftime(DATETIME_FORMAT)
|
now.strftime(DATETIME_FORMAT)
|
||||||
)
|
)
|
||||||
mmg_client.send_sms.assert_not_called()
|
provider_tasks.send_sms_to_provider.apply_async.assert_not_called()
|
||||||
with pytest.raises(NoResultFound):
|
with pytest.raises(NoResultFound):
|
||||||
notifications_dao.get_notification(service.id, notification_id)
|
notifications_dao.get_notification(service.id, notification_id)
|
||||||
|
|
||||||
|
|
||||||
def test_send_sms_should_use_template_version_from_job_not_latest(sample_template, mocker):
|
|
||||||
notification = _notification_json(sample_template, '+447234123123')
|
|
||||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
|
||||||
mocker.patch('app.mmg_client.send_sms')
|
|
||||||
mocker.patch('app.mmg_client.get_name', return_value="mmg")
|
|
||||||
version_on_notification = sample_template.version
|
|
||||||
|
|
||||||
# Change the template
|
|
||||||
from app.dao.templates_dao import dao_update_template, dao_get_template_by_id
|
|
||||||
sample_template.content = sample_template.content + " another version of the template"
|
|
||||||
dao_update_template(sample_template)
|
|
||||||
t = dao_get_template_by_id(sample_template.id)
|
|
||||||
assert t.version > version_on_notification
|
|
||||||
|
|
||||||
notification_id = uuid.uuid4()
|
|
||||||
now = datetime.utcnow()
|
|
||||||
send_sms(
|
|
||||||
sample_template.service_id,
|
|
||||||
notification_id,
|
|
||||||
"encrypted-in-reality",
|
|
||||||
now.strftime(DATETIME_FORMAT)
|
|
||||||
)
|
|
||||||
|
|
||||||
mmg_client.send_sms.assert_called_once_with(
|
|
||||||
to=format_phone_number(validate_phone_number("+447234123123")),
|
|
||||||
content="Sample service: This is a template",
|
|
||||||
reference=str(notification_id)
|
|
||||||
)
|
|
||||||
|
|
||||||
persisted_notification = notifications_dao.get_notification(sample_template.service_id, notification_id)
|
|
||||||
assert persisted_notification.id == notification_id
|
|
||||||
assert persisted_notification.to == '+447234123123'
|
|
||||||
assert persisted_notification.template_id == sample_template.id
|
|
||||||
assert persisted_notification.template_version == version_on_notification
|
|
||||||
assert persisted_notification.template_version != sample_template.version
|
|
||||||
assert persisted_notification.created_at == now
|
|
||||||
assert persisted_notification.sent_at > now
|
|
||||||
assert persisted_notification.status == 'sending'
|
|
||||||
assert persisted_notification.sent_by == 'mmg'
|
|
||||||
assert persisted_notification.content_char_count == len("Sample service: This is a template")
|
|
||||||
|
|
||||||
|
|
||||||
def test_should_send_email_if_restricted_service_and_valid_email(notify_db, notify_db_session, mocker):
|
def test_should_send_email_if_restricted_service_and_valid_email(notify_db, notify_db_session, mocker):
|
||||||
user = sample_user(notify_db, notify_db_session, email="test@restricted.com")
|
user = sample_user(notify_db, notify_db_session, email="test@restricted.com")
|
||||||
service = sample_service(notify_db, notify_db_session, user=user, restricted=True)
|
service = sample_service(notify_db, notify_db_session, user=user, restricted=True)
|
||||||
@@ -568,15 +524,14 @@ def test_should_not_send_email_if_restricted_service_and_invalid_email_address(n
|
|||||||
notifications_dao.get_notification(service.id, notification_id)
|
notifications_dao.get_notification(service.id, notification_id)
|
||||||
|
|
||||||
|
|
||||||
def test_should_send_template_to_correct_sms_provider_and_persist_with_job_id(sample_job, mocker):
|
def test_should_send_template_to_and_persist_with_job_id(sample_job, mocker):
|
||||||
notification = _notification_json(
|
notification = _notification_json(
|
||||||
sample_job.template,
|
sample_job.template,
|
||||||
to="+447234123123",
|
to="+447234123123",
|
||||||
job_id=sample_job.id,
|
job_id=sample_job.id,
|
||||||
row_number=2)
|
row_number=2)
|
||||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||||
mocker.patch('app.mmg_client.send_sms')
|
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
|
||||||
mocker.patch('app.mmg_client.get_name', return_value="mmg")
|
|
||||||
|
|
||||||
notification_id = uuid.uuid4()
|
notification_id = uuid.uuid4()
|
||||||
now = datetime.utcnow()
|
now = datetime.utcnow()
|
||||||
@@ -586,10 +541,11 @@ def test_should_send_template_to_correct_sms_provider_and_persist_with_job_id(sa
|
|||||||
"encrypted-in-reality",
|
"encrypted-in-reality",
|
||||||
now.strftime(DATETIME_FORMAT)
|
now.strftime(DATETIME_FORMAT)
|
||||||
)
|
)
|
||||||
mmg_client.send_sms.assert_called_once_with(
|
provider_tasks.send_sms_to_provider.apply_async.assert_called_once_with(
|
||||||
to=format_phone_number(validate_phone_number("+447234123123")),
|
(sample_job.service.id,
|
||||||
content="Sample service: This is a template",
|
notification_id,
|
||||||
reference=str(notification_id)
|
"encrypted-in-reality"),
|
||||||
|
queue="sms"
|
||||||
)
|
)
|
||||||
persisted_notification = notifications_dao.get_notification(sample_job.template.service_id, notification_id)
|
persisted_notification = notifications_dao.get_notification(sample_job.template.service_id, notification_id)
|
||||||
assert persisted_notification.id == notification_id
|
assert persisted_notification.id == notification_id
|
||||||
@@ -597,9 +553,9 @@ def test_should_send_template_to_correct_sms_provider_and_persist_with_job_id(sa
|
|||||||
assert persisted_notification.job_id == sample_job.id
|
assert persisted_notification.job_id == sample_job.id
|
||||||
assert persisted_notification.template_id == sample_job.template.id
|
assert persisted_notification.template_id == sample_job.template.id
|
||||||
assert persisted_notification.status == 'sending'
|
assert persisted_notification.status == 'sending'
|
||||||
assert persisted_notification.sent_at > now
|
assert not persisted_notification.sent_at
|
||||||
assert persisted_notification.created_at == now
|
assert persisted_notification.created_at == now
|
||||||
assert persisted_notification.sent_by == 'mmg'
|
assert not persisted_notification.sent_by
|
||||||
assert persisted_notification.job_row_number == 2
|
assert persisted_notification.job_row_number == 2
|
||||||
|
|
||||||
|
|
||||||
@@ -789,36 +745,32 @@ def test_should_use_email_template_and_persist_without_personalisation(sample_em
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_should_persist_notification_as_failed_if_sms_client_fails(sample_template, mocker):
|
def test_should_persist_notification_as_failed_if_database_fails(sample_template, mocker):
|
||||||
notification = _notification_json(sample_template, "+447234123123")
|
notification = _notification_json(sample_template, "+447234123123")
|
||||||
|
|
||||||
|
expected_exception = SQLAlchemyError()
|
||||||
|
|
||||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||||
mocker.patch('app.mmg_client.send_sms', side_effect=MMGClientException(mmg_error))
|
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
|
||||||
mocker.patch('app.mmg_client.get_name', return_value="mmg")
|
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()
|
now = datetime.utcnow()
|
||||||
|
|
||||||
notification_id = uuid.uuid4()
|
notification_id = uuid.uuid4()
|
||||||
|
|
||||||
send_sms(
|
with pytest.raises(Exception):
|
||||||
sample_template.service_id,
|
send_sms(
|
||||||
notification_id,
|
sample_template.service_id,
|
||||||
"encrypted-in-reality",
|
notification_id,
|
||||||
now.strftime(DATETIME_FORMAT)
|
"encrypted-in-reality",
|
||||||
)
|
now.strftime(DATETIME_FORMAT)
|
||||||
mmg_client.send_sms.assert_called_once_with(
|
)
|
||||||
to=format_phone_number(validate_phone_number("+447234123123")),
|
provider_tasks.send_sms_to_provider.apply_async.assert_not_called()
|
||||||
content="Sample service: This is a template",
|
tasks.send_sms.retry.assert_called_with(exc=expected_exception, queue='retry')
|
||||||
reference=str(notification_id)
|
|
||||||
)
|
|
||||||
persisted_notification = notifications_dao.get_notification(sample_template.service_id, notification_id)
|
|
||||||
assert persisted_notification.id == notification_id
|
|
||||||
assert persisted_notification.to == '+447234123123'
|
|
||||||
assert persisted_notification.template_id == sample_template.id
|
|
||||||
assert persisted_notification.template_version == sample_template.version
|
|
||||||
assert persisted_notification.status == 'technical-failure'
|
|
||||||
assert persisted_notification.created_at == now
|
|
||||||
assert persisted_notification.sent_at > now
|
|
||||||
assert persisted_notification.sent_by == 'mmg'
|
|
||||||
|
|
||||||
|
with pytest.raises(NoResultFound) as e:
|
||||||
|
notifications_dao.get_notification(sample_template.service_id, notification_id)
|
||||||
|
assert 'No row was found for one' in str(e.value)
|
||||||
|
|
||||||
def test_should_persist_notification_as_failed_if_email_client_fails(sample_email_template, mocker):
|
def test_should_persist_notification_as_failed_if_email_client_fails(sample_email_template, mocker):
|
||||||
notification = _notification_json(sample_email_template, "my_email@my_email.com")
|
notification = _notification_json(sample_email_template, "my_email@my_email.com")
|
||||||
@@ -856,27 +808,6 @@ def test_should_persist_notification_as_failed_if_email_client_fails(sample_emai
|
|||||||
assert persisted_notification.sent_at > now
|
assert persisted_notification.sent_at > now
|
||||||
|
|
||||||
|
|
||||||
def test_should_not_send_sms_if_db_peristance_failed(sample_template, mocker):
|
|
||||||
notification = _notification_json(sample_template, "+447234123123")
|
|
||||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
|
||||||
mocker.patch('app.mmg_client.send_sms')
|
|
||||||
mocker.patch('app.db.session.add', side_effect=SQLAlchemyError())
|
|
||||||
now = datetime.utcnow()
|
|
||||||
|
|
||||||
notification_id = uuid.uuid4()
|
|
||||||
|
|
||||||
send_sms(
|
|
||||||
sample_template.service_id,
|
|
||||||
notification_id,
|
|
||||||
"encrypted-in-reality",
|
|
||||||
now.strftime(DATETIME_FORMAT)
|
|
||||||
)
|
|
||||||
mmg_client.send_sms.assert_not_called()
|
|
||||||
with pytest.raises(NoResultFound) as e:
|
|
||||||
notifications_dao.get_notification(sample_template.service_id, notification_id)
|
|
||||||
assert 'No row was found for one' in str(e.value)
|
|
||||||
|
|
||||||
|
|
||||||
def test_should_not_send_email_if_db_peristance_failed(sample_email_template, mocker):
|
def test_should_not_send_email_if_db_peristance_failed(sample_email_template, mocker):
|
||||||
notification = _notification_json(sample_email_template, "my_email@my_email.com")
|
notification = _notification_json(sample_email_template, "my_email@my_email.com")
|
||||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||||
@@ -1002,43 +933,6 @@ def test_process_email_job_should_use_reply_to_email_if_present(sample_email_job
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_should_call_send_sms_response_task_if_research_mode(notify_db, sample_service, sample_template, mocker):
|
|
||||||
notification = _notification_json(
|
|
||||||
sample_template,
|
|
||||||
to="+447234123123"
|
|
||||||
)
|
|
||||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
|
||||||
mocker.patch('app.mmg_client.send_sms')
|
|
||||||
mocker.patch('app.mmg_client.get_name', return_value="mmg")
|
|
||||||
mocker.patch('app.celery.research_mode_tasks.send_sms_response.apply_async')
|
|
||||||
|
|
||||||
sample_service.research_mode = True
|
|
||||||
notify_db.session.add(sample_service)
|
|
||||||
notify_db.session.commit()
|
|
||||||
|
|
||||||
notification_id = uuid.uuid4()
|
|
||||||
now = datetime.utcnow()
|
|
||||||
send_sms(
|
|
||||||
sample_service.id,
|
|
||||||
notification_id,
|
|
||||||
"encrypted-in-reality",
|
|
||||||
now.strftime(DATETIME_FORMAT)
|
|
||||||
)
|
|
||||||
assert not mmg_client.send_sms.called
|
|
||||||
send_sms_response.apply_async.assert_called_once_with(
|
|
||||||
('mmg', str(notification_id), "+447234123123"), queue='research-mode'
|
|
||||||
)
|
|
||||||
|
|
||||||
persisted_notification = notifications_dao.get_notification(sample_service.id, notification_id)
|
|
||||||
assert persisted_notification.id == notification_id
|
|
||||||
assert persisted_notification.to == '+447234123123'
|
|
||||||
assert persisted_notification.template_id == sample_template.id
|
|
||||||
assert persisted_notification.status == 'sending'
|
|
||||||
assert persisted_notification.sent_at > now
|
|
||||||
assert persisted_notification.created_at == now
|
|
||||||
assert persisted_notification.sent_by == 'mmg'
|
|
||||||
|
|
||||||
|
|
||||||
def test_should_call_send_email_response_task_if_research_mode(
|
def test_should_call_send_email_response_task_if_research_mode(
|
||||||
notify_db,
|
notify_db,
|
||||||
sample_service,
|
sample_service,
|
||||||
@@ -1132,47 +1026,6 @@ def test_should_call_send_not_update_provider_email_stats_if_research_mode(
|
|||||||
providers=[ses_provider.identifier]).first()
|
providers=[ses_provider.identifier]).first()
|
||||||
|
|
||||||
|
|
||||||
def test_should_call_send_sms_response_task_if_research_mode(
|
|
||||||
notify_db,
|
|
||||||
sample_service,
|
|
||||||
sample_template,
|
|
||||||
mmg_provider,
|
|
||||||
mocker):
|
|
||||||
notification = _notification_json(
|
|
||||||
sample_template,
|
|
||||||
to="+447234123123"
|
|
||||||
)
|
|
||||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
|
||||||
mocker.patch('app.mmg_client.send_sms')
|
|
||||||
mocker.patch('app.mmg_client.get_name', return_value="mmg")
|
|
||||||
mocker.patch('app.celery.research_mode_tasks.send_sms_response.apply_async')
|
|
||||||
|
|
||||||
sample_service.research_mode = True
|
|
||||||
notify_db.session.add(sample_service)
|
|
||||||
notify_db.session.commit()
|
|
||||||
|
|
||||||
assert not get_provider_statistics(
|
|
||||||
sample_template.service,
|
|
||||||
providers=[mmg_provider.identifier]).first()
|
|
||||||
|
|
||||||
notification_id = uuid.uuid4()
|
|
||||||
now = datetime.utcnow()
|
|
||||||
send_sms(
|
|
||||||
sample_service.id,
|
|
||||||
notification_id,
|
|
||||||
"encrypted-in-reality",
|
|
||||||
now.strftime(DATETIME_FORMAT)
|
|
||||||
)
|
|
||||||
assert not mmg_client.send_sms.called
|
|
||||||
send_sms_response.apply_async.assert_called_once_with(
|
|
||||||
('mmg', str(notification_id), "+447234123123"), queue='research-mode'
|
|
||||||
)
|
|
||||||
|
|
||||||
assert not get_provider_statistics(
|
|
||||||
sample_template.service,
|
|
||||||
providers=[mmg_provider.identifier]).first()
|
|
||||||
|
|
||||||
|
|
||||||
def _notification_json(template, to, personalisation=None, job_id=None, row_number=None):
|
def _notification_json(template, to, personalisation=None, job_id=None, row_number=None):
|
||||||
notification = {
|
notification = {
|
||||||
"template": template.id,
|
"template": template.id,
|
||||||
|
|||||||
@@ -356,7 +356,7 @@ def sample_notification(notify_db,
|
|||||||
data['job_row_number'] = job_row_number
|
data['job_row_number'] = job_row_number
|
||||||
notification = Notification(**data)
|
notification = Notification(**data)
|
||||||
if create:
|
if create:
|
||||||
dao_create_notification(notification, template.template_type, provider_name)
|
dao_create_notification(notification, template.template_type)
|
||||||
return notification
|
return notification
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user