diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index 5e31b3888..29632e69d 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -346,13 +346,10 @@ def _filter_query(query, filter_dict=None): @statsd(namespace="dao") def delete_notifications_created_more_than_a_week_ago(status): seven_days_ago = date.today() - timedelta(days=7) - deleted = db.session.query(Notification).filter( func.date(Notification.created_at) < seven_days_ago, Notification.status == status, - Notification.notification_type != LETTER_TYPE ).delete(synchronize_session='fetch') - db.session.commit() return deleted diff --git a/tests/app/celery/test_scheduled_tasks.py b/tests/app/celery/test_scheduled_tasks.py index cbec1e43c..038d2ca1c 100644 --- a/tests/app/celery/test_scheduled_tasks.py +++ b/tests/app/celery/test_scheduled_tasks.py @@ -105,12 +105,7 @@ def test_should_call_delete_invotations_on_delete_invitations_task(notify_api, m assert scheduled_tasks.delete_invitations_created_more_than_two_days_ago.call_count == 1 -def test_update_status_of_notifications_after_timeout(notify_api, - notify_db, - notify_db_session, - sample_service, - sample_template, - mmg_provider): +def test_update_status_of_notifications_after_timeout(notify_api, sample_template): with notify_api.test_request_context(): not1 = create_notification( template=sample_template, @@ -133,12 +128,7 @@ def test_update_status_of_notifications_after_timeout(notify_api, assert not3.status == 'temporary-failure' -def test_not_update_status_of_notification_before_timeout(notify_api, - notify_db, - notify_db_session, - sample_service, - sample_template, - mmg_provider): +def test_not_update_status_of_notification_before_timeout(notify_api, sample_template): with notify_api.test_request_context(): not1 = create_notification( template=sample_template, @@ -149,6 +139,17 @@ def test_not_update_status_of_notification_before_timeout(notify_api, assert not1.status == 'sending' +def test_should_not_update_status_of_letter_notifications(client, sample_letter_template): + created_at = datetime.utcnow() - timedelta(days=5) + not1 = create_notification(template=sample_letter_template, status='sending', created_at=created_at) + not2 = create_notification(template=sample_letter_template, status='created', created_at=created_at) + + timeout_notifications() + + assert not1.status == 'sending' + assert not2.status == 'created' + + def test_should_update_scheduled_jobs_and_put_on_queue(notify_db, notify_db_session, mocker): mocked = mocker.patch('app.celery.tasks.process_job.apply_async') diff --git a/tests/app/dao/test_notification_dao.py b/tests/app/dao/test_notification_dao.py index bf66a12d5..99d1200ec 100644 --- a/tests/app/dao/test_notification_dao.py +++ b/tests/app/dao/test_notification_dao.py @@ -862,6 +862,15 @@ def test_should_not_delete_failed_notifications_before_seven_days(notify_db, not assert Notification.query.first().to == 'do_not_delete' +def test_should_delete_letter_notifications(sample_letter_template): + should_delete = datetime.utcnow() - timedelta(days=8) + + create_notification(sample_letter_template, created_at=should_delete) + + delete_notifications_created_more_than_a_week_ago('created') + assert len(Notification.query.all()) == 0 + + @freeze_time("2016-01-10") def test_should_limit_notifications_return_by_day_limit_plus_one(notify_db, notify_db_session, sample_service): assert len(Notification.query.all()) == 0 @@ -1036,12 +1045,12 @@ def _notification_json(sample_template, job_id=None, id=None, status=None): return data -def test_dao_timeout_notifications(notify_db, notify_db_session, ): +def test_dao_timeout_notifications(sample_template): with freeze_time(datetime.utcnow() - timedelta(minutes=2)): - created = sample_notification(notify_db, notify_db_session, status='created') - sending = sample_notification(notify_db, notify_db_session, status='sending') - pending = sample_notification(notify_db, notify_db_session, status='pending') - delivered = sample_notification(notify_db, notify_db_session, status='delivered') + created = create_notification(sample_template, status='created') + sending = create_notification(sample_template, status='sending') + pending = create_notification(sample_template, status='pending') + delivered = create_notification(sample_template, status='delivered') assert Notification.query.get(created.id).status == 'created' assert Notification.query.get(sending.id).status == 'sending' @@ -1059,12 +1068,12 @@ def test_dao_timeout_notifications(notify_db, notify_db_session, ): assert updated == 3 -def test_dao_timeout_notifications_only_updates_for_older_notifications(notify_db, notify_db_session): +def test_dao_timeout_notifications_only_updates_for_older_notifications(sample_template): with freeze_time(datetime.utcnow() + timedelta(minutes=10)): - created = sample_notification(notify_db, notify_db_session, status='created') - sending = sample_notification(notify_db, notify_db_session, status='sending') - pending = sample_notification(notify_db, notify_db_session, status='pending') - delivered = sample_notification(notify_db, notify_db_session, status='delivered') + created = create_notification(sample_template, status='created') + sending = create_notification(sample_template, status='sending') + pending = create_notification(sample_template, status='pending') + delivered = create_notification(sample_template, status='delivered') assert Notification.query.get(created.id).status == 'created' assert Notification.query.get(sending.id).status == 'sending' @@ -1078,6 +1087,27 @@ def test_dao_timeout_notifications_only_updates_for_older_notifications(notify_d assert updated == 0 +def test_dao_timeout_notifications_doesnt_affect_letters(sample_letter_template): + with freeze_time(datetime.utcnow() - timedelta(minutes=2)): + created = create_notification(sample_letter_template, status='created') + sending = create_notification(sample_letter_template, status='sending') + pending = create_notification(sample_letter_template, status='pending') + delivered = create_notification(sample_letter_template, status='delivered') + + assert Notification.query.get(created.id).status == 'created' + assert Notification.query.get(sending.id).status == 'sending' + assert Notification.query.get(pending.id).status == 'pending' + assert Notification.query.get(delivered.id).status == 'delivered' + + updated = dao_timeout_notifications(1) + + assert NotificationHistory.query.get(created.id).status == 'created' + assert NotificationHistory.query.get(sending.id).status == 'sending' + assert NotificationHistory.query.get(pending.id).status == 'pending' + assert NotificationHistory.query.get(delivered.id).status == 'delivered' + assert updated == 0 + + def test_should_return_notifications_excluding_jobs_by_default(notify_db, notify_db_session, sample_service): assert len(Notification.query.all()) == 0 diff --git a/tests/app/db.py b/tests/app/db.py index e2a24f1c3..195a77ea7 100644 --- a/tests/app/db.py +++ b/tests/app/db.py @@ -86,6 +86,7 @@ def create_notification( 'to': to_field, 'job_id': job.id if job else None, 'job': job, + 'service_id': template.service.id, 'service': template.service, 'template_id': template.id if template else None, 'template': template,