notify-api-317 fix the scrubbing of pii for successful notifications

This commit is contained in:
Kenneth Kehl
2023-06-27 10:48:14 -07:00
parent 12f3a7ee5d
commit 17e9fc1e8f
4 changed files with 31 additions and 10 deletions

View File

@@ -11,7 +11,7 @@ from app.clients.sms import SmsClientResponseException
from app.config import QueueNames from app.config import QueueNames
from app.dao import notifications_dao from app.dao import notifications_dao
from app.dao.notifications_dao import ( from app.dao.notifications_dao import (
sanitize_notifications_by_id, sanitize_successful_notification_by_id,
update_notification_status_by_id, update_notification_status_by_id,
) )
from app.delivery import send_to_providers from app.delivery import send_to_providers
@@ -37,14 +37,16 @@ def check_sms_delivery_receipt(self, message_id, notification_id, sent_at):
status, provider_response = aws_cloudwatch_client.check_sms(message_id, notification_id, sent_at) status, provider_response = aws_cloudwatch_client.check_sms(message_id, notification_id, sent_at)
if status == 'success': if status == 'success':
status = NOTIFICATION_DELIVERED status = NOTIFICATION_DELIVERED
else: elif status == 'failure':
status = NOTIFICATION_FAILED status = NOTIFICATION_FAILED
update_notification_status_by_id(notification_id, status, provider_response=provider_response) # if status is not success or failure the client raised an exception and this method will retry
current_app.logger.info(f"Updated notification {notification_id} with response '{provider_response}'")
if status == NOTIFICATION_DELIVERED: if status == NOTIFICATION_DELIVERED:
sanitize_notifications_by_id(notification_id) sanitize_successful_notification_by_id(notification_id)
current_app.logger.info(f"Sanitized notification {notification_id} that was successfully delivered") current_app.logger.info(f"Sanitized notification {notification_id} that was successfully delivered")
else:
update_notification_status_by_id(notification_id, status, provider_response=provider_response)
current_app.logger.info(f"Updated notification {notification_id} with response '{provider_response}'")
@notify_celery.task(bind=True, name="deliver_sms", max_retries=48, default_retry_delay=300) @notify_celery.task(bind=True, name="deliver_sms", max_retries=48, default_retry_delay=300)

View File

@@ -84,6 +84,6 @@ class AwsCloudwatchClient(Client):
if all_failed_events and len(all_failed_events) > 0: if all_failed_events and len(all_failed_events) > 0:
event = all_failed_events[0] event = all_failed_events[0]
message = json.loads(event['message']) message = json.loads(event['message'])
return "fail", message['delivery']['providerResponse'] return "failure", message['delivery']['providerResponse']
raise Exception(f'No event found for message_id {message_id} notification_id {notification_id}') raise Exception(f'No event found for message_id {message_id} notification_id {notification_id}')

View File

@@ -272,7 +272,7 @@ def _filter_query(query, filter_dict=None):
@autocommit @autocommit
def sanitize_notifications_by_id( def sanitize_successful_notification_by_id(
notification_id notification_id
): ):
# TODO what to do for international? # TODO what to do for international?
@@ -280,12 +280,10 @@ def sanitize_notifications_by_id(
Notification.query.filter( Notification.query.filter(
Notification.id.in_([notification_id]), Notification.id.in_([notification_id]),
).update( ).update(
{'to': phone_prefix, 'normalised_to': phone_prefix}, {'to': phone_prefix, 'normalised_to': phone_prefix, 'status': 'delivered'},
synchronize_session=False synchronize_session=False
) )
db.session.commit()
@autocommit @autocommit
def insert_notification_history_delete_notifications( def insert_notification_history_delete_notifications(

View File

@@ -24,6 +24,7 @@ from app.dao.notifications_dao import (
get_notifications_for_service, get_notifications_for_service,
get_service_ids_with_notifications_on_date, get_service_ids_with_notifications_on_date,
notifications_not_yet_sent, notifications_not_yet_sent,
sanitize_successful_notification_by_id,
update_notification_status_by_id, update_notification_status_by_id,
update_notification_status_by_reference, update_notification_status_by_reference,
) )
@@ -84,6 +85,26 @@ def test_should_by_able_to_update_status_by_id(sample_template, sample_job, sns_
assert notification.status == 'delivered' assert notification.status == 'delivered'
def test_should_be_able_to_sanitize_successful_notification(sample_template, sample_job, sns_provider):
with freeze_time('2000-01-01 12:00:00'):
data = _notification_json(sample_template, job_id=sample_job.id, status='sending')
notification = Notification(**data)
notification.to = '15555555555'
notification.normalised_to = '15555555555'
dao_create_notification(notification)
assert notification.status == 'sending'
assert notification.normalised_to == '15555555555'
assert notification.to == '15555555555'
assert Notification.query.get(notification.id).status == 'sending'
with freeze_time('2000-01-02 12:00:00'):
sanitize_successful_notification_by_id(notification.id)
assert Notification.query.get(notification.id).status == 'delivered'
assert Notification.query.get(notification.id).normalised_to == '1'
assert Notification.query.get(notification.id).to == '1'
def test_should_not_update_status_by_id_if_not_sending_and_does_not_update_job(sample_job): def test_should_not_update_status_by_id_if_not_sending_and_does_not_update_job(sample_job):
notification = create_notification(template=sample_job.template, status='delivered', job=sample_job) notification = create_notification(template=sample_job.template, status='delivered', job=sample_job)
assert Notification.query.get(notification.id).status == 'delivered' assert Notification.query.get(notification.id).status == 'delivered'