Merge pull request #1800 from alphagov/replay-created-emails

Replay emails and sms that are still in created
This commit is contained in:
Rebecca Law
2018-03-26 10:56:55 +01:00
committed by GitHub
6 changed files with 111 additions and 2 deletions

View File

@@ -34,7 +34,8 @@ from app.celery.scheduled_tasks import (
switch_current_sms_provider_on_slow_delivery,
timeout_notifications,
daily_stats_template_usage_by_month,
letter_raise_alert_if_no_ack_file_for_zip
letter_raise_alert_if_no_ack_file_for_zip,
replay_created_notifications
)
from app.clients.performance_platform.performance_platform_client import PerformancePlatformClient
from app.config import QueueNames, TaskNames
@@ -1191,3 +1192,33 @@ def test_letter_not_raise_alert_if_no_files_do_not_cause_error(mocker, notify_db
assert mock_file_list.call_count == 2
assert mock_get_file.call_count == 0
def test_replay_created_notifications(notify_db_session, sample_service, mocker):
email_delivery_queue = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
sms_delivery_queue = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
sms_template = create_template(service=sample_service, template_type='sms')
email_template = create_template(service=sample_service, template_type='email')
older_than = (60 * 60 * 4) + (60 * 15) # 4 hours 15 minutes
# notifications expected to be resent
old_sms = create_notification(template=sms_template, created_at=datetime.utcnow() - timedelta(seconds=older_than),
status='created')
old_email = create_notification(template=email_template,
created_at=datetime.utcnow() - timedelta(seconds=older_than),
status='created')
# notifications that are not to be resent
create_notification(template=sms_template, created_at=datetime.utcnow() - timedelta(seconds=older_than),
status='sending')
create_notification(template=email_template, created_at=datetime.utcnow() - timedelta(seconds=older_than),
status='delivered')
create_notification(template=sms_template, created_at=datetime.utcnow(),
status='created')
create_notification(template=email_template, created_at=datetime.utcnow(),
status='created')
replay_created_notifications()
email_delivery_queue.assert_called_once_with([str(old_email.id)],
queue='send-email-tasks')
sms_delivery_queue.assert_called_once_with([str(old_sms.id)],
queue="send-sms-tasks")

View File

@@ -33,6 +33,7 @@ from app.dao.notifications_dao import (
dao_get_notification_by_reference,
dao_get_notifications_by_references,
dao_get_notification_history_by_reference,
notifications_not_yet_sent
)
from app.dao.services_dao import dao_update_service
from app.models import (
@@ -2216,3 +2217,40 @@ def test_dao_get_count_of_letters_to_process_for_date_ignores_test_keys(sample_l
count_for_date = dao_get_count_of_letters_to_process_for_date()
assert count_for_date == 2
@pytest.mark.parametrize("notification_type",
["letter", "email", "sms"]
)
def test_notifications_not_yet_sent(sample_service, notification_type):
older_than = 4 # number of seconds the notification can not be older than
template = create_template(service=sample_service, template_type=notification_type)
old_notification = create_notification(template=template,
created_at=datetime.utcnow() - timedelta(seconds=older_than),
status='created')
create_notification(template=template,
created_at=datetime.utcnow() - timedelta(seconds=older_than),
status='sending')
create_notification(template=template, created_at=datetime.utcnow(), status='created')
results = notifications_not_yet_sent(older_than, notification_type)
assert len(results) == 1
assert results[0] == old_notification
@pytest.mark.parametrize("notification_type",
["letter", "email", "sms"]
)
def test_notifications_not_yet_sent_return_no_rows(sample_service, notification_type):
older_than = 5 # number of seconds the notification can not be older than
template = create_template(service=sample_service, template_type=notification_type)
create_notification(template=template,
created_at=datetime.utcnow(),
status='created')
create_notification(template=template,
created_at=datetime.utcnow(),
status='sending')
create_notification(template=template, created_at=datetime.utcnow(), status='delivered')
results = notifications_not_yet_sent(older_than, notification_type)
assert len(results) == 0