mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-11 02:23:32 -04:00
Do not delete letters if not in final state
A few weeks ago, we deleted some pdf letters that had reached their retention period. However, these letters were in the 'created' state so it's very arguable that we should not have deleted them because we were expecting to resend them and were unable to. Part of the reason for this is that we marked the letters back to `created` as the status but we did not nullify the `sent_at` timestamp, meaning the check on https://github.com/alphagov/notifications-api/blob/ebb43082d51b9f27b17190abb0537a710a544408/app/dao/notifications_dao.py#L346 did not catch it. Regardless of that check, which controls whether the files were removed from S3, they were also archived into the `notification_history` table as by default. This commit does changes our code such that letters that are not in their final state do not go through our retention process. This could mean they violate their retention policy but that is likely the lesser of two evils (the other being we delete them and are unable to resend them). Note, `sending` letters have been included in those not to be removed because there is a risk that we give the letter to DVLA and put it in `sending` but then they come back to us later telling us they've had problems and require us to resend.
This commit is contained in:
@@ -213,9 +213,9 @@ def test_delete_notifications_delete_notification_type_for_default_time_if_no_da
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'notification_status', ['created', 'validation-failed', 'virus-scan-failed', 'pending-virus-check']
|
||||
'notification_status', ['validation-failed', 'virus-scan-failed']
|
||||
)
|
||||
def test_delete_notifications_deletes_letters_not_sent_from_table_but_not_s3(
|
||||
def test_delete_notifications_deletes_letters_not_sent_and_in_final_state_from_table_but_not_s3(
|
||||
sample_service, mocker, notification_status
|
||||
):
|
||||
mock_get_s3 = mocker.patch("app.dao.notifications_dao.get_s3_bucket_objects")
|
||||
@@ -236,8 +236,8 @@ def test_delete_notifications_deletes_letters_not_sent_from_table_but_not_s3(
|
||||
mock_get_s3.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('notification_status', ['sending', 'delivered', 'returned-letter', 'technical-failure'])
|
||||
def test_delete_notifications_deletes_letters_sent_from_table_and_s3(
|
||||
@pytest.mark.parametrize('notification_status', ['delivered', 'returned-letter', 'technical-failure'])
|
||||
def test_delete_notifications_deletes_letters_sent_and_in_final_state_from_table_and_s3(
|
||||
sample_service, mocker, notification_status
|
||||
):
|
||||
mock_get_s3 = mocker.patch("app.dao.notifications_dao.get_s3_bucket_objects")
|
||||
@@ -266,6 +266,28 @@ def test_delete_notifications_deletes_letters_sent_from_table_and_s3(
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('notification_status', ['pending-virus-check', 'created', 'sending'])
|
||||
def test_delete_notifications_does_not_delete_letters_not_yet_in_final_state(
|
||||
sample_service, mocker, notification_status
|
||||
):
|
||||
mock_get_s3 = mocker.patch("app.dao.notifications_dao.get_s3_bucket_objects")
|
||||
letter_template = create_template(service=sample_service, template_type='letter')
|
||||
create_notification(
|
||||
template=letter_template,
|
||||
status=notification_status,
|
||||
reference='LETTER_REF',
|
||||
created_at=datetime.utcnow() - timedelta(days=8),
|
||||
)
|
||||
assert Notification.query.count() == 1
|
||||
assert NotificationHistory.query.count() == 0
|
||||
|
||||
delete_notifications_older_than_retention_by_type('letter')
|
||||
|
||||
assert Notification.query.count() == 1
|
||||
assert NotificationHistory.query.count() == 0
|
||||
mock_get_s3.assert_not_called()
|
||||
|
||||
|
||||
@freeze_time('2020-03-25 00:01')
|
||||
def test_delete_notifications_calls_subquery_multiple_times(sample_template):
|
||||
create_notification(template=sample_template, created_at=datetime.now() - timedelta(days=7, minutes=3),
|
||||
|
||||
Reference in New Issue
Block a user