mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-24 00:06:16 -04:00
Renamed the header of the CSV to 'to' from 'number' to allow for email jobs
- added new columns to Job and Notification to capture the start/end dates accurately
This commit is contained in:
@@ -11,6 +11,7 @@ from sqlalchemy.orm.exc import NoResultFound
|
||||
from app.celery.tasks import s3
|
||||
from app.celery import tasks
|
||||
from tests.app import load_example_csv
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
def test_should_process_sms_job(sample_job, mocker):
|
||||
@@ -26,12 +27,45 @@ def test_should_process_sms_job(sample_job, mocker):
|
||||
(str(sample_job.service_id),
|
||||
"uuid",
|
||||
"something_encrypted"),
|
||||
queue="sms"
|
||||
queue="bulk-sms"
|
||||
)
|
||||
job = jobs_dao.dao_get_job_by_id(sample_job.id)
|
||||
assert job.status == 'finished'
|
||||
|
||||
|
||||
def test_should_not_create_send_task_for_empty_file(sample_job, mocker):
|
||||
mocker.patch('app.celery.tasks.s3.get_job_from_s3', return_value=load_example_csv('empty'))
|
||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||
|
||||
process_job(sample_job.id)
|
||||
|
||||
s3.get_job_from_s3.assert_called_once_with(sample_job.bucket_name, sample_job.id)
|
||||
job = jobs_dao.dao_get_job_by_id(sample_job.id)
|
||||
assert job.status == 'finished'
|
||||
tasks.send_sms.apply_async.assert_not_called
|
||||
|
||||
|
||||
def test_should_process_email_job(sample_email_job, mocker):
|
||||
mocker.patch('app.celery.tasks.s3.get_job_from_s3', return_value=load_example_csv('email'))
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
mocker.patch('app.celery.tasks.create_uuid', return_value="uuid")
|
||||
|
||||
process_job(sample_email_job.id)
|
||||
|
||||
s3.get_job_from_s3.assert_called_once_with(sample_email_job.bucket_name, sample_email_job.id)
|
||||
tasks.send_email.apply_async.assert_called_once_with(
|
||||
(str(sample_email_job.service_id),
|
||||
"uuid",
|
||||
sample_email_job.template.subject,
|
||||
"{}@{}".format(sample_email_job.service.email_from, "test.notify.com"),
|
||||
"something_encrypted"),
|
||||
queue="bulk-email"
|
||||
)
|
||||
job = jobs_dao.dao_get_job_by_id(sample_email_job.id)
|
||||
assert job.status == 'finished'
|
||||
|
||||
|
||||
def test_should_process_all_sms_job(sample_job, mocker):
|
||||
mocker.patch('app.celery.tasks.s3.get_job_from_s3', return_value=load_example_csv('multiple_sms'))
|
||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||
@@ -55,11 +89,13 @@ def test_should_send_template_to_correct_sms_provider_and_persist(sample_templat
|
||||
mocker.patch('app.firetext_client.send_sms')
|
||||
|
||||
notification_id = uuid.uuid4()
|
||||
|
||||
now = datetime.utcnow()
|
||||
send_sms(
|
||||
sample_template.service_id,
|
||||
notification_id,
|
||||
"encrypted-in-reality")
|
||||
"encrypted-in-reality",
|
||||
now
|
||||
)
|
||||
|
||||
firetext_client.send_sms.assert_called_once_with("+441234123123", sample_template.content)
|
||||
persisted_notification = notifications_dao.get_notification(sample_template.service_id, notification_id)
|
||||
@@ -67,6 +103,8 @@ def test_should_send_template_to_correct_sms_provider_and_persist(sample_templat
|
||||
assert persisted_notification.to == '+441234123123'
|
||||
assert persisted_notification.template_id == sample_template.id
|
||||
assert persisted_notification.status == 'sent'
|
||||
assert persisted_notification.created_at == now
|
||||
assert persisted_notification.sent_at > now
|
||||
assert not persisted_notification.job_id
|
||||
|
||||
|
||||
@@ -80,11 +118,12 @@ def test_should_send_template_to_correct_sms_provider_and_persist_with_job_id(sa
|
||||
mocker.patch('app.firetext_client.send_sms')
|
||||
|
||||
notification_id = uuid.uuid4()
|
||||
|
||||
now = datetime.utcnow()
|
||||
send_sms(
|
||||
sample_job.service.id,
|
||||
notification_id,
|
||||
"encrypted-in-reality")
|
||||
"encrypted-in-reality",
|
||||
now)
|
||||
|
||||
firetext_client.send_sms.assert_called_once_with("+441234123123", sample_job.template.content)
|
||||
persisted_notification = notifications_dao.get_notification(sample_job.template.service_id, notification_id)
|
||||
@@ -93,9 +132,11 @@ 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.template_id == sample_job.template.id
|
||||
assert persisted_notification.status == 'sent'
|
||||
assert persisted_notification.sent_at > now
|
||||
assert persisted_notification.created_at == now
|
||||
|
||||
|
||||
def test_should_send_template_to_email_provider_and_persist(sample_email_template, mocker):
|
||||
def test_should_use_email_template_and_persist(sample_email_template, mocker):
|
||||
notification = {
|
||||
"template": sample_email_template.id,
|
||||
"to": "my_email@my_email.com"
|
||||
@@ -104,13 +145,14 @@ def test_should_send_template_to_email_provider_and_persist(sample_email_templat
|
||||
mocker.patch('app.aws_ses_client.send_email')
|
||||
|
||||
notification_id = uuid.uuid4()
|
||||
|
||||
now = datetime.utcnow()
|
||||
send_email(
|
||||
sample_email_template.service_id,
|
||||
notification_id,
|
||||
'subject',
|
||||
'email_from',
|
||||
"encrypted-in-reality")
|
||||
"encrypted-in-reality",
|
||||
now)
|
||||
|
||||
aws_ses_client.send_email.assert_called_once_with(
|
||||
"email_from",
|
||||
@@ -122,6 +164,8 @@ def test_should_send_template_to_email_provider_and_persist(sample_email_templat
|
||||
assert persisted_notification.id == notification_id
|
||||
assert persisted_notification.to == 'my_email@my_email.com'
|
||||
assert persisted_notification.template_id == sample_email_template.id
|
||||
assert persisted_notification.created_at == now
|
||||
assert persisted_notification.sent_at > now
|
||||
assert persisted_notification.status == 'sent'
|
||||
|
||||
|
||||
@@ -132,13 +176,15 @@ def test_should_persist_notification_as_failed_if_sms_client_fails(sample_templa
|
||||
}
|
||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||
mocker.patch('app.firetext_client.send_sms', side_effect=FiretextClientException())
|
||||
now = datetime.utcnow()
|
||||
|
||||
notification_id = uuid.uuid4()
|
||||
|
||||
send_sms(
|
||||
sample_template.service_id,
|
||||
notification_id,
|
||||
"encrypted-in-reality")
|
||||
"encrypted-in-reality",
|
||||
now)
|
||||
|
||||
firetext_client.send_sms.assert_called_once_with("+441234123123", sample_template.content)
|
||||
persisted_notification = notifications_dao.get_notification(sample_template.service_id, notification_id)
|
||||
@@ -146,6 +192,8 @@ def test_should_persist_notification_as_failed_if_sms_client_fails(sample_templa
|
||||
assert persisted_notification.to == '+441234123123'
|
||||
assert persisted_notification.template_id == sample_template.id
|
||||
assert persisted_notification.status == 'failed'
|
||||
assert persisted_notification.created_at == now
|
||||
assert persisted_notification.sent_at > now
|
||||
|
||||
|
||||
def test_should_persist_notification_as_failed_if_email_client_fails(sample_email_template, mocker):
|
||||
@@ -155,6 +203,7 @@ def test_should_persist_notification_as_failed_if_email_client_fails(sample_emai
|
||||
}
|
||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||
mocker.patch('app.aws_ses_client.send_email', side_effect=AwsSesClientException())
|
||||
now = datetime.utcnow()
|
||||
|
||||
notification_id = uuid.uuid4()
|
||||
|
||||
@@ -163,7 +212,8 @@ def test_should_persist_notification_as_failed_if_email_client_fails(sample_emai
|
||||
notification_id,
|
||||
'subject',
|
||||
'email_from',
|
||||
"encrypted-in-reality")
|
||||
"encrypted-in-reality",
|
||||
now)
|
||||
|
||||
aws_ses_client.send_email.assert_called_once_with(
|
||||
"email_from",
|
||||
@@ -176,6 +226,8 @@ def test_should_persist_notification_as_failed_if_email_client_fails(sample_emai
|
||||
assert persisted_notification.to == 'my_email@my_email.com'
|
||||
assert persisted_notification.template_id == sample_email_template.id
|
||||
assert persisted_notification.status == 'failed'
|
||||
assert persisted_notification.created_at == now
|
||||
assert persisted_notification.sent_at > now
|
||||
|
||||
|
||||
def test_should_not_send_sms_if_db_peristance_failed(sample_template, mocker):
|
||||
@@ -186,13 +238,15 @@ def test_should_not_send_sms_if_db_peristance_failed(sample_template, mocker):
|
||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||
mocker.patch('app.firetext_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")
|
||||
"encrypted-in-reality",
|
||||
now)
|
||||
|
||||
firetext_client.send_sms.assert_not_called()
|
||||
with pytest.raises(NoResultFound) as e:
|
||||
@@ -208,6 +262,7 @@ def test_should_not_send_email_if_db_peristance_failed(sample_email_template, mo
|
||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||
mocker.patch('app.aws_ses_client.send_email')
|
||||
mocker.patch('app.db.session.add', side_effect=SQLAlchemyError())
|
||||
now = datetime.utcnow()
|
||||
|
||||
notification_id = uuid.uuid4()
|
||||
|
||||
@@ -216,7 +271,8 @@ def test_should_not_send_email_if_db_peristance_failed(sample_email_template, mo
|
||||
notification_id,
|
||||
'subject',
|
||||
'email_from',
|
||||
"encrypted-in-reality")
|
||||
"encrypted-in-reality",
|
||||
now)
|
||||
|
||||
aws_ses_client.send_email.assert_not_called()
|
||||
with pytest.raises(NoResultFound) as e:
|
||||
|
||||
Reference in New Issue
Block a user