mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 07:35:34 -05:00
Merge pull request #198 from alphagov/retain_all_notifications_for_7_days
Successful notifications are deleted after a week now. All tests pass…
This commit is contained in:
@@ -32,8 +32,7 @@ from app.dao.invited_user_dao import delete_invitations_created_more_than_two_da
|
|||||||
from app.dao.notifications_dao import (
|
from app.dao.notifications_dao import (
|
||||||
dao_create_notification,
|
dao_create_notification,
|
||||||
dao_update_notification,
|
dao_update_notification,
|
||||||
delete_failed_notifications_created_more_than_a_week_ago,
|
delete_notifications_created_more_than_a_week_ago,
|
||||||
delete_successful_notifications_created_more_than_a_day_ago,
|
|
||||||
dao_get_notification_statistics_for_service_and_day,
|
dao_get_notification_statistics_for_service_and_day,
|
||||||
update_notification_reference_by_id
|
update_notification_reference_by_id
|
||||||
)
|
)
|
||||||
@@ -67,7 +66,7 @@ def delete_verify_codes():
|
|||||||
def delete_successful_notifications():
|
def delete_successful_notifications():
|
||||||
try:
|
try:
|
||||||
start = datetime.utcnow()
|
start = datetime.utcnow()
|
||||||
deleted = delete_successful_notifications_created_more_than_a_day_ago()
|
deleted = delete_notifications_created_more_than_a_week_ago('sent')
|
||||||
current_app.logger.info(
|
current_app.logger.info(
|
||||||
"Delete job started {} finished {} deleted {} successful notifications".format(
|
"Delete job started {} finished {} deleted {} successful notifications".format(
|
||||||
start,
|
start,
|
||||||
@@ -84,7 +83,7 @@ def delete_successful_notifications():
|
|||||||
def delete_failed_notifications():
|
def delete_failed_notifications():
|
||||||
try:
|
try:
|
||||||
start = datetime.utcnow()
|
start = datetime.utcnow()
|
||||||
deleted = delete_failed_notifications_created_more_than_a_week_ago()
|
deleted = delete_notifications_created_more_than_a_week_ago('failed')
|
||||||
current_app.logger.info(
|
current_app.logger.info(
|
||||||
"Delete job started {} finished {} deleted {} failed notifications".format(
|
"Delete job started {} finished {} deleted {} failed notifications".format(
|
||||||
start,
|
start,
|
||||||
|
|||||||
@@ -236,19 +236,19 @@ def filter_query(query, filter_dict=None):
|
|||||||
return query
|
return query
|
||||||
|
|
||||||
|
|
||||||
def delete_successful_notifications_created_more_than_a_day_ago():
|
def delete_notifications_created_more_than_a_day_ago(status):
|
||||||
deleted = db.session.query(Notification).filter(
|
deleted = db.session.query(Notification).filter(
|
||||||
Notification.created_at < datetime.utcnow() - timedelta(days=1),
|
Notification.created_at < datetime.utcnow() - timedelta(days=1),
|
||||||
Notification.status == 'sent'
|
Notification.status == status
|
||||||
).delete()
|
).delete()
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return deleted
|
return deleted
|
||||||
|
|
||||||
|
|
||||||
def delete_failed_notifications_created_more_than_a_week_ago():
|
def delete_notifications_created_more_than_a_week_ago(status):
|
||||||
deleted = db.session.query(Notification).filter(
|
deleted = db.session.query(Notification).filter(
|
||||||
Notification.created_at < datetime.utcnow() - timedelta(days=7),
|
Notification.created_at < datetime.utcnow() - timedelta(days=7),
|
||||||
Notification.status == 'failed'
|
Notification.status == status
|
||||||
).delete()
|
).delete()
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return deleted
|
return deleted
|
||||||
|
|||||||
@@ -46,16 +46,16 @@ def firetext_error():
|
|||||||
return {'code': 0, 'description': 'error'}
|
return {'code': 0, 'description': 'error'}
|
||||||
|
|
||||||
|
|
||||||
def test_should_call_delete_successful_notifications_in_task(notify_api, mocker):
|
def test_should_call_delete_notifications_more_than_week_in_task(notify_api, mocker):
|
||||||
mocker.patch('app.celery.tasks.delete_successful_notifications_created_more_than_a_day_ago')
|
mocker.patch('app.celery.tasks.delete_notifications_created_more_than_a_week_ago')
|
||||||
delete_successful_notifications()
|
delete_successful_notifications()
|
||||||
assert tasks.delete_successful_notifications_created_more_than_a_day_ago.call_count == 1
|
assert tasks.delete_notifications_created_more_than_a_week_ago.call_count == 1
|
||||||
|
|
||||||
|
|
||||||
def test_should_call_delete_failed_notifications_in_task(notify_api, mocker):
|
def test_should_call_delete_notifications_more_than_week_in_task(notify_api, mocker):
|
||||||
mocker.patch('app.celery.tasks.delete_failed_notifications_created_more_than_a_week_ago')
|
mocker.patch('app.celery.tasks.delete_notifications_created_more_than_a_week_ago')
|
||||||
delete_failed_notifications()
|
delete_failed_notifications()
|
||||||
assert tasks.delete_failed_notifications_created_more_than_a_week_ago.call_count == 1
|
assert tasks.delete_notifications_created_more_than_a_week_ago.call_count == 1
|
||||||
|
|
||||||
|
|
||||||
def test_should_call_delete_codes_on_delete_verify_codes_task(notify_api, mocker):
|
def test_should_call_delete_codes_on_delete_verify_codes_task(notify_api, mocker):
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ from app.dao.notifications_dao import (
|
|||||||
get_notification_for_job,
|
get_notification_for_job,
|
||||||
get_notifications_for_job,
|
get_notifications_for_job,
|
||||||
dao_get_notification_statistics_for_service,
|
dao_get_notification_statistics_for_service,
|
||||||
delete_successful_notifications_created_more_than_a_day_ago,
|
delete_notifications_created_more_than_a_day_ago,
|
||||||
delete_failed_notifications_created_more_than_a_week_ago,
|
delete_notifications_created_more_than_a_week_ago,
|
||||||
dao_get_notification_statistics_for_service_and_day,
|
dao_get_notification_statistics_for_service_and_day,
|
||||||
update_notification_status_by_id,
|
update_notification_status_by_id,
|
||||||
update_notification_reference_by_id,
|
update_notification_reference_by_id,
|
||||||
@@ -701,21 +701,21 @@ def test_update_notification(sample_notification, sample_template):
|
|||||||
assert notification_from_db.status == 'failed'
|
assert notification_from_db.status == 'failed'
|
||||||
|
|
||||||
|
|
||||||
def test_should_delete_sent_notifications_after_one_day(notify_db, notify_db_session):
|
def test_should_delete_notifications_after_one_day(notify_db, notify_db_session):
|
||||||
created_at = datetime.utcnow() - timedelta(hours=24)
|
created_at = datetime.utcnow() - timedelta(hours=24)
|
||||||
sample_notification(notify_db, notify_db_session, created_at=created_at)
|
sample_notification(notify_db, notify_db_session, created_at=created_at)
|
||||||
sample_notification(notify_db, notify_db_session, created_at=created_at)
|
sample_notification(notify_db, notify_db_session, created_at=created_at)
|
||||||
assert len(Notification.query.all()) == 2
|
assert len(Notification.query.all()) == 2
|
||||||
delete_successful_notifications_created_more_than_a_day_ago()
|
delete_notifications_created_more_than_a_day_ago('sent')
|
||||||
assert len(Notification.query.all()) == 0
|
assert len(Notification.query.all()) == 0
|
||||||
|
|
||||||
|
|
||||||
def test_should_delete_failed_notifications_after_seven_days(notify_db, notify_db_session):
|
def test_should_delete_notifications_after_seven_days(notify_db, notify_db_session):
|
||||||
created_at = datetime.utcnow() - timedelta(hours=24 * 7)
|
created_at = datetime.utcnow() - timedelta(hours=24 * 7)
|
||||||
sample_notification(notify_db, notify_db_session, created_at=created_at, status="failed")
|
sample_notification(notify_db, notify_db_session, created_at=created_at, status="failed")
|
||||||
sample_notification(notify_db, notify_db_session, created_at=created_at, status="failed")
|
sample_notification(notify_db, notify_db_session, created_at=created_at, status="failed")
|
||||||
assert len(Notification.query.all()) == 2
|
assert len(Notification.query.all()) == 2
|
||||||
delete_failed_notifications_created_more_than_a_week_ago()
|
delete_notifications_created_more_than_a_week_ago('failed')
|
||||||
assert len(Notification.query.all()) == 0
|
assert len(Notification.query.all()) == 0
|
||||||
|
|
||||||
|
|
||||||
@@ -726,7 +726,7 @@ def test_should_not_delete_sent_notifications_before_one_day(notify_db, notify_d
|
|||||||
sample_notification(notify_db, notify_db_session, created_at=valid, to_field="valid")
|
sample_notification(notify_db, notify_db_session, created_at=valid, to_field="valid")
|
||||||
|
|
||||||
assert len(Notification.query.all()) == 2
|
assert len(Notification.query.all()) == 2
|
||||||
delete_successful_notifications_created_more_than_a_day_ago()
|
delete_notifications_created_more_than_a_day_ago('sent')
|
||||||
assert len(Notification.query.all()) == 1
|
assert len(Notification.query.all()) == 1
|
||||||
assert Notification.query.first().to == 'valid'
|
assert Notification.query.first().to == 'valid'
|
||||||
|
|
||||||
@@ -737,7 +737,7 @@ def test_should_not_delete_failed_notifications_before_seven_days(notify_db, not
|
|||||||
sample_notification(notify_db, notify_db_session, created_at=expired, status="failed", to_field="expired")
|
sample_notification(notify_db, notify_db_session, created_at=expired, status="failed", to_field="expired")
|
||||||
sample_notification(notify_db, notify_db_session, created_at=valid, status="failed", to_field="valid")
|
sample_notification(notify_db, notify_db_session, created_at=valid, status="failed", to_field="valid")
|
||||||
assert len(Notification.query.all()) == 2
|
assert len(Notification.query.all()) == 2
|
||||||
delete_failed_notifications_created_more_than_a_week_ago()
|
delete_notifications_created_more_than_a_week_ago('failed')
|
||||||
assert len(Notification.query.all()) == 1
|
assert len(Notification.query.all()) == 1
|
||||||
assert Notification.query.first().to == 'valid'
|
assert Notification.query.first().to == 'valid'
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user