Set a notification.status=created in the send_sms task

This commit is contained in:
Rebecca Law
2016-06-21 14:38:17 +01:00
parent fa20e67941
commit b17aaaabfb
2 changed files with 30 additions and 30 deletions

View File

@@ -155,7 +155,7 @@ def send_sms(self, service_id, notification_id, encrypted_notification, created_
service_id=service_id,
job_id=notification.get('job', None),
job_row_number=notification.get('row_number', None),
status='sending',
status='created',
created_at=datetime.strptime(created_at, DATETIME_FORMAT),
personalisation=notification.get('personalisation')
)

View File

@@ -7,7 +7,7 @@ from mock import ANY
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm.exc import NoResultFound
from app import (aws_ses_client, encryption, DATETIME_FORMAT, statsd_client)
from app import (aws_ses_client, encryption, DATETIME_FORMAT, statsd_client, db)
from app.celery import provider_tasks
from app.celery import tasks
from app.celery.research_mode_tasks import send_email_response
@@ -21,6 +21,7 @@ from app.celery.tasks import (
from app.clients.email.aws_ses import AwsSesClientException
from app.dao import notifications_dao, jobs_dao, provider_details_dao
from app.dao.provider_statistics_dao import get_provider_statistics
from app.models import Notification
from tests.app import load_example_csv
from tests.app.conftest import (
sample_service,
@@ -346,14 +347,12 @@ def test_should_send_template_to_correct_sms_task_and_persist(sample_template_wi
)
statsd_client.incr.assert_called_once_with("notifications.tasks.send-sms")
persisted_notification = notifications_dao.get_notification(
sample_template_with_placeholders.service_id, notification_id
)
persisted_notification = Notification.query.filter_by(id=notification_id).one()
assert persisted_notification.id == notification_id
assert persisted_notification.to == '+447234123123'
assert persisted_notification.template_id == sample_template_with_placeholders.id
assert persisted_notification.template_version == sample_template_with_placeholders.version
assert persisted_notification.status == 'sending'
assert persisted_notification.status == 'created'
assert persisted_notification.created_at == now
assert not persisted_notification.sent_at
assert not persisted_notification.sent_by
@@ -386,6 +385,17 @@ def test_should_send_sms_if_restricted_service_and_valid_number(notify_db, notif
queue="sms"
)
persisted_notification = Notification.query.filter_by(id=notification_id).one()
assert persisted_notification.id == notification_id
assert persisted_notification.to == '+447700900890'
assert persisted_notification.template_id == template.id
assert persisted_notification.template_version == template.version
assert persisted_notification.status == 'created'
assert persisted_notification.created_at == now
assert not persisted_notification.sent_at
assert not persisted_notification.sent_by
assert not persisted_notification.job_id
def test_should_not_send_sms_if_restricted_service_and_invalid_number(notify_db, notify_db_session, mocker):
user = sample_user(notify_db, notify_db_session, mobile_numnber="07700 900205")
@@ -406,7 +416,7 @@ def test_should_not_send_sms_if_restricted_service_and_invalid_number(notify_db,
)
provider_tasks.send_sms_to_provider.apply_async.assert_not_called()
with pytest.raises(NoResultFound):
notifications_dao.get_notification(service.id, notification_id)
Notification.query.filter_by(id=notification_id).one()
def test_should_send_email_if_restricted_service_and_valid_email(notify_db, notify_db_session, mocker):
@@ -463,7 +473,7 @@ def test_should_not_send_email_if_restricted_service_and_invalid_email_address(n
aws_ses_client.send_email.assert_not_called()
with pytest.raises(NoResultFound):
notifications_dao.get_notification(service.id, notification_id)
Notification.query.filter_by(id=notification_id).one()
def test_should_send_template_to_and_persist_with_job_id(sample_job, mocker):
@@ -489,12 +499,12 @@ def test_should_send_template_to_and_persist_with_job_id(sample_job, mocker):
"encrypted-in-reality"),
queue="sms"
)
persisted_notification = notifications_dao.get_notification(sample_job.template.service_id, notification_id)
persisted_notification = Notification.query.filter_by(id=notification_id).one()
assert persisted_notification.id == notification_id
assert persisted_notification.to == '+447234123123'
assert persisted_notification.job_id == sample_job.id
assert persisted_notification.template_id == sample_job.template.id
assert persisted_notification.status == 'sending'
assert persisted_notification.status == 'created'
assert not persisted_notification.sent_at
assert persisted_notification.created_at == now
assert not persisted_notification.sent_by
@@ -548,9 +558,7 @@ def test_should_use_email_template_and_persist(sample_email_template_with_placeh
datetime(2016, 1, 1, 11, 9, 0, 00000)
)
statsd_client.timing.assert_called_once_with("notifications.tasks.send-email.task-time", ANY)
persisted_notification = notifications_dao.get_notification(
sample_email_template_with_placeholders.service_id, notification_id
)
persisted_notification = Notification.query.filter_by(id=notification_id).one()
assert persisted_notification.id == notification_id
assert persisted_notification.to == 'my_email@my_email.com'
@@ -593,7 +601,7 @@ def test_send_email_should_use_template_version_from_job_not_latest(sample_email
reply_to_addresses=None
)
persisted_notification = notifications_dao.get_notification(sample_email_template.service_id, notification_id)
persisted_notification = Notification.query.filter_by(id=notification_id).one()
assert persisted_notification.id == notification_id
assert persisted_notification.to == 'my_email@my_email.com'
assert persisted_notification.template_id == sample_email_template.id
@@ -627,9 +635,7 @@ def test_should_use_email_template_subject_placeholders(sample_email_template_wi
html_body=AnyStringWith("Hello Jo"),
reply_to_addresses=None
)
persisted_notification = notifications_dao.get_notification(
sample_email_template_with_placeholders.service_id, notification_id
)
persisted_notification = Notification.query.filter_by(id=notification_id).one()
assert persisted_notification.id == notification_id
assert persisted_notification.to == 'my_email@my_email.com'
assert persisted_notification.template_id == sample_email_template_with_placeholders.id
@@ -652,9 +658,7 @@ def test_should_use_email_template_and_persist_ses_reference(sample_email_templa
"encrypted-in-reality",
now.strftime(DATETIME_FORMAT)
)
persisted_notification = notifications_dao.get_notification(
sample_email_template_with_placeholders.service_id, notification_id
)
persisted_notification = Notification.query.filter_by(id=notification_id).one()
assert persisted_notification.reference == 'reference'
@@ -706,7 +710,7 @@ def test_should_persist_notification_as_failed_if_database_fails(sample_template
tasks.send_sms.retry.assert_called_with(exc=expected_exception, queue='retry')
with pytest.raises(NoResultFound) as e:
notifications_dao.get_notification(sample_template.service_id, notification_id)
Notification.query.filter_by(id=notification_id).one()
assert 'No row was found for one' in str(e.value)
@@ -734,7 +738,7 @@ def test_should_persist_notification_as_failed_if_email_client_fails(sample_emai
html_body=AnyStringWith(sample_email_template.content),
reply_to_addresses=None
)
persisted_notification = notifications_dao.get_notification(sample_email_template.service_id, notification_id)
persisted_notification = Notification.query.filter_by(id=notification_id).one()
assert persisted_notification.id == notification_id
assert persisted_notification.to == 'my_email@my_email.com'
assert persisted_notification.template_id == sample_email_template.id
@@ -762,7 +766,7 @@ def test_should_not_send_email_if_db_peristance_failed(sample_email_template, mo
)
aws_ses_client.send_email.assert_not_called()
with pytest.raises(NoResultFound) as e:
notifications_dao.get_notification(sample_email_template.service_id, notification_id)
Notification.query.filter_by(id=notification_id).one()
assert 'No row was found for one' in str(e.value)
@@ -823,7 +827,7 @@ def test_should_call_send_email_response_task_if_research_mode(
('ses', str(reference), 'john@smith.com'), queue="research-mode"
)
persisted_notification = notifications_dao.get_notification(sample_service.id, notification_id)
persisted_notification = Notification.query.filter_by(id=notification_id).one()
assert persisted_notification.id == notification_id
assert persisted_notification.to == 'john@smith.com'
assert persisted_notification.template_id == sample_email_template.id
@@ -900,9 +904,7 @@ def test_email_template_personalisation_persisted(sample_email_template_with_pla
datetime.utcnow().strftime(DATETIME_FORMAT)
)
persisted_notification = notifications_dao.get_notification(
sample_email_template_with_placeholders.service_id, notification_id
)
persisted_notification = Notification.query.filter_by(id=notification_id).one()
assert persisted_notification.id == notification_id
assert persisted_notification.personalisation == {"name": "Jo"}
@@ -934,9 +936,7 @@ def test_sms_template_personalisation_persisted(sample_template_with_placeholder
encrypted_notification),
queue="sms"
)
persisted_notification = notifications_dao.get_notification(
sample_template_with_placeholders.service_id, notification_id
)
persisted_notification = Notification.query.filter_by(id=notification_id).one()
assert persisted_notification.id == notification_id
assert persisted_notification.to == '+447234123123'