mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-04 05:30:53 -04:00
Added tests for statsd and provider stats outcomes with the new provider stats tasks
- note large change to DAO to remove provider from create notification. Added on send now not creation.
This commit is contained in:
@@ -2,19 +2,21 @@ import uuid
|
||||
|
||||
from mock import ANY, call
|
||||
|
||||
from app import statsd_client, mmg_client, DATETIME_FORMAT
|
||||
from app import statsd_client, mmg_client
|
||||
from app.celery.provider_tasks import send_sms_to_provider
|
||||
from app.celery.tasks import provider_to_use
|
||||
from app.dao import provider_details_dao
|
||||
from datetime import datetime, timedelta
|
||||
from app.dao import provider_statistics_dao
|
||||
from datetime import datetime
|
||||
from freezegun import freeze_time
|
||||
from app.dao import notifications_dao, jobs_dao, provider_details_dao
|
||||
from app.dao import notifications_dao, provider_details_dao
|
||||
from notifications_utils.recipients import validate_phone_number, format_phone_number
|
||||
from app.celery.research_mode_tasks import send_sms_response
|
||||
|
||||
from tests.app.conftest import (
|
||||
sample_notification
|
||||
)
|
||||
|
||||
|
||||
def test_should_return_highest_priority_active_provider(notify_db, notify_db_session):
|
||||
providers = provider_details_dao.get_provider_details_by_notification_type('sms')
|
||||
first = providers[0]
|
||||
@@ -79,7 +81,6 @@ def test_should_send_personalised_template_to_correct_sms_provider_and_persist(
|
||||
)
|
||||
freezer.stop()
|
||||
|
||||
|
||||
mmg_client.send_sms.assert_called_once_with(
|
||||
to=format_phone_number(validate_phone_number("+447234123123")),
|
||||
content="Sample service: Hello Jo",
|
||||
@@ -108,7 +109,6 @@ def test_should_send_template_to_correct_sms_provider_and_persist(
|
||||
mocker):
|
||||
db_notification = sample_notification(notify_db, notify_db_session, template=sample_template)
|
||||
|
||||
|
||||
notification = _notification_json(
|
||||
sample_template,
|
||||
to="+447234123123"
|
||||
@@ -140,86 +140,162 @@ def test_should_send_template_to_correct_sms_provider_and_persist(
|
||||
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 test_send_sms_should_use_template_version_from_notification_not_latest(
|
||||
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, '+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
|
||||
|
||||
now = datetime.utcnow()
|
||||
|
||||
send_sms_to_provider(
|
||||
db_notification.service_id,
|
||||
db_notification.id,
|
||||
"encrypted-in-reality"
|
||||
)
|
||||
|
||||
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)
|
||||
)
|
||||
|
||||
persisted_notification = notifications_dao.get_notification(sample_template.service_id, db_notification.id)
|
||||
assert persisted_notification.to == db_notification.to
|
||||
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.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_call_send_sms_response_task_if_research_mode(notify_db, sample_service, sample_notification, mocker):
|
||||
notification = _notification_json(
|
||||
sample_notification.template,
|
||||
to=sample_notification.to
|
||||
)
|
||||
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()
|
||||
|
||||
now = datetime.utcnow()
|
||||
send_sms_to_provider(
|
||||
sample_notification.service_id,
|
||||
sample_notification.id,
|
||||
"encrypted-in-reality"
|
||||
)
|
||||
assert not mmg_client.send_sms.called
|
||||
send_sms_response.apply_async.assert_called_once_with(
|
||||
('mmg', str(sample_notification.id), sample_notification.to), queue='research-mode'
|
||||
)
|
||||
|
||||
persisted_notification = notifications_dao.get_notification(sample_service.id, sample_notification.id)
|
||||
assert persisted_notification.to == sample_notification.to
|
||||
assert persisted_notification.template_id == sample_notification.template_id
|
||||
assert persisted_notification.status == 'sending'
|
||||
assert persisted_notification.sent_at > now
|
||||
assert persisted_notification.sent_by == 'mmg'
|
||||
|
||||
|
||||
def test_should_update_provider_stats_on_success(notify_db, sample_service, sample_notification, mocker):
|
||||
provider_stats = provider_statistics_dao.get_provider_statistics(sample_service).all()
|
||||
assert len(provider_stats) == 0
|
||||
|
||||
notification = _notification_json(
|
||||
sample_notification.template,
|
||||
to=sample_notification.to
|
||||
)
|
||||
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')
|
||||
|
||||
send_sms_to_provider(
|
||||
sample_notification.service_id,
|
||||
sample_notification.id,
|
||||
"encrypted-in-reality"
|
||||
)
|
||||
|
||||
updated_provider_stats = provider_statistics_dao.get_provider_statistics(sample_service).all()
|
||||
assert updated_provider_stats[0].provider.identifier == 'mmg'
|
||||
assert updated_provider_stats[0].unit_count == 1
|
||||
|
||||
|
||||
def test_not_should_update_provider_stats_on_success(notify_db, sample_service, sample_notification, mocker):
|
||||
provider_stats = provider_statistics_dao.get_provider_statistics(sample_service).all()
|
||||
assert len(provider_stats) == 0
|
||||
|
||||
notification = _notification_json(
|
||||
sample_notification.template,
|
||||
to=sample_notification.to
|
||||
)
|
||||
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()
|
||||
|
||||
send_sms_to_provider(
|
||||
sample_notification.service_id,
|
||||
sample_notification.id,
|
||||
"encrypted-in-reality"
|
||||
)
|
||||
|
||||
updated_provider_stats = provider_statistics_dao.get_provider_statistics(sample_service).all()
|
||||
assert len(updated_provider_stats) == 0
|
||||
|
||||
|
||||
def test_statsd_updates(notify_db, notify_db_session, sample_service, sample_notification, mocker):
|
||||
notification = _notification_json(
|
||||
sample_notification.template,
|
||||
to=sample_notification.to
|
||||
)
|
||||
|
||||
mocker.patch('app.statsd_client.incr')
|
||||
mocker.patch('app.statsd_client.timing')
|
||||
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.celery.research_mode_tasks.send_sms_response.apply_async')
|
||||
|
||||
send_sms_to_provider(
|
||||
sample_notification.service_id,
|
||||
sample_notification.id,
|
||||
"encrypted-in-reality"
|
||||
)
|
||||
|
||||
statsd_client.incr.assert_called_once_with("notifications.tasks.send-sms-to-provider")
|
||||
statsd_client.timing.assert_has_calls([
|
||||
call("notifications.tasks.send-sms-to-provider.task-time", ANY),
|
||||
call("notifications.sms.total-time", ANY)
|
||||
])
|
||||
|
||||
|
||||
def _notification_json(template, to, personalisation=None, job_id=None, row_number=None):
|
||||
@@ -234,4 +310,4 @@ def _notification_json(template, to, personalisation=None, job_id=None, row_numb
|
||||
notification.update({"job": job_id})
|
||||
if row_number:
|
||||
notification['row_number'] = row_number
|
||||
return notification
|
||||
return notification
|
||||
|
||||
@@ -333,16 +333,15 @@ def test_should_process_all_sms_job(sample_job,
|
||||
)
|
||||
assert encryption.encrypt.call_args[0][0]['to'] == '+441234123120'
|
||||
assert encryption.encrypt.call_args[0][0]['template'] == str(sample_job_with_placeholdered_template.template.id)
|
||||
assert encryption.encrypt.call_args[0][0]['template_version'] == sample_job_with_placeholdered_template.template.version # noqa
|
||||
assert encryption.encrypt.call_args[0][0][
|
||||
'template_version'] == sample_job_with_placeholdered_template.template.version # noqa
|
||||
assert encryption.encrypt.call_args[0][0]['personalisation'] == {'name': 'chris'}
|
||||
tasks.send_sms.apply_async.call_count == 10
|
||||
job = jobs_dao.dao_get_job_by_id(sample_job_with_placeholdered_template.id)
|
||||
assert job.status == 'finished'
|
||||
|
||||
|
||||
### 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_task_and_persist(sample_template_with_placeholders, mocker):
|
||||
notification = _notification_json(sample_template_with_placeholders,
|
||||
to="+447234123123", personalisation={"name": "Jo"})
|
||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||
@@ -393,29 +392,6 @@ def test_should_send_template_to_correct_sms_provider_and_persist(sample_templat
|
||||
assert not persisted_notification.job_id
|
||||
|
||||
|
||||
def test_should_send_sms_without_personalisation(sample_template, mocker):
|
||||
notification = _notification_json(sample_template, "+447234123123")
|
||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
|
||||
|
||||
notification_id = uuid.uuid4()
|
||||
now = datetime.utcnow()
|
||||
send_sms(
|
||||
sample_template.service_id,
|
||||
notification_id,
|
||||
"encrypted-in-reality",
|
||||
now.strftime(DATETIME_FORMAT)
|
||||
)
|
||||
|
||||
|
||||
provider_tasks.send_sms_to_provider.apply_async.assert_called_once_with(
|
||||
(sample_template.service_id,
|
||||
notification_id,
|
||||
"encrypted-in-reality"),
|
||||
queue="sms"
|
||||
)
|
||||
|
||||
|
||||
def test_should_send_sms_if_restricted_service_and_valid_number(notify_db, notify_db_session, mocker):
|
||||
user = sample_user(notify_db, notify_db_session, mobile_numnber="07700 900890")
|
||||
service = sample_service(notify_db, notify_db_session, user=user, restricted=True)
|
||||
@@ -437,8 +413,8 @@ def test_should_send_sms_if_restricted_service_and_valid_number(notify_db, notif
|
||||
|
||||
provider_tasks.send_sms_to_provider.apply_async.assert_called_once_with(
|
||||
(service.id,
|
||||
notification_id,
|
||||
"encrypted-in-reality"),
|
||||
notification_id,
|
||||
"encrypted-in-reality"),
|
||||
queue="sms"
|
||||
)
|
||||
|
||||
@@ -772,6 +748,7 @@ def test_should_persist_notification_as_failed_if_database_fails(sample_template
|
||||
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):
|
||||
notification = _notification_json(sample_email_template, "my_email@my_email.com")
|
||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||
|
||||
Reference in New Issue
Block a user