diff --git a/app/notifications/notifications_ses_callback.py b/app/notifications/notifications_ses_callback.py index 962aaf482..8c4ffd9db 100644 --- a/app/notifications/notifications_ses_callback.py +++ b/app/notifications/notifications_ses_callback.py @@ -95,8 +95,8 @@ def process_ses_response(ses_request): def determine_notification_bounce_type(notification_type, ses_message): - remove_emails_from_bounce(ses_message['bounce']) - current_app.logger.info('SES bounce dict: {}'.format(ses_message['bounce'])) + remove_emails_from_bounce(ses_message) + current_app.logger.info('SES bounce dict: {}'.format(ses_message)) if ses_message['bounce']['bounceType'] == 'Permanent': notification_type = ses_message['bounce']['bounceType'] # permanent or not else: @@ -104,11 +104,6 @@ def determine_notification_bounce_type(notification_type, ses_message): return notification_type -def remove_emails_from_bounce(bounce_dict): - for recip in bounce_dict['bouncedRecipients']: - recip.pop('emailAddress') - - def handle_complaint(ses_message): remove_emails_from_complaint(ses_message) current_app.logger.info("Complaint from SES: \n{}".format(ses_message)) @@ -130,7 +125,21 @@ def handle_complaint(ses_message): save_complaint(complaint) +def remove_mail_headers(dict_to_edit): + if dict_to_edit['mail'].get('headers'): + dict_to_edit['mail'].pop('headers') + if dict_to_edit['mail'].get('commonHeaders'): + dict_to_edit['mail'].pop('commonHeaders') + + +def remove_emails_from_bounce(bounce_dict): + remove_mail_headers(bounce_dict) + bounce_dict['mail'].pop('destination') + bounce_dict['bounce'].pop('bouncedRecipients') + + def remove_emails_from_complaint(complaint_dict): + remove_mail_headers(complaint_dict) complaint_dict['complaint'].pop('complainedRecipients') complaint_dict['mail'].pop('destination') diff --git a/tests/app/celery/test_process_ses_receipts_tasks.py b/tests/app/celery/test_process_ses_receipts_tasks.py index 99eb02c2b..80ff799aa 100644 --- a/tests/app/celery/test_process_ses_receipts_tasks.py +++ b/tests/app/celery/test_process_ses_receipts_tasks.py @@ -2,8 +2,9 @@ import json from datetime import datetime from app.celery.process_ses_receipts_tasks import process_ses_results +from app.celery.research_mode_tasks import ses_hard_bounce_callback from app.models import Complaint -from app.notifications.notifications_ses_callback import remove_emails_from_complaint +from app.notifications.notifications_ses_callback import remove_emails_from_complaint, remove_emails_from_bounce from tests.app.db import ( create_notification, ses_complaint_callback, @@ -51,3 +52,9 @@ def test_remove_emails_from_complaint(): test_json = json.loads(ses_complaint_callback()['Message']) remove_emails_from_complaint(test_json) assert "recipient1@example.com" not in json.dumps(test_json) + + +def test_remove_email_from_bounce(): + test_json = json.loads(ses_hard_bounce_callback(reference='ref1')['Message']) + remove_emails_from_bounce(test_json) + assert "bounce@simulator.amazonses.com" not in json.dumps(test_json) diff --git a/tests/app/notifications/test_notifications_ses_callback.py b/tests/app/notifications/test_notifications_ses_callback.py index 82c30e547..701c1a21c 100644 --- a/tests/app/notifications/test_notifications_ses_callback.py +++ b/tests/app/notifications/test_notifications_ses_callback.py @@ -9,7 +9,7 @@ from app import statsd_client from app.dao.notifications_dao import get_notification_by_id from app.models import Notification, Complaint from app.notifications.notifications_ses_callback import ( - process_ses_response, remove_emails_from_bounce, + process_ses_response, handle_complaint ) from app.celery.research_mode_tasks import ses_hard_bounce_callback, ses_soft_bounce_callback, ses_notification_callback @@ -192,15 +192,6 @@ def test_ses_callback_should_set_status_to_permanent_failure(client, assert send_mock.called -def test_remove_emails_from_bounce(): - # an actual bouncedict example - message_dict = json.loads(ses_hard_bounce_callback(reference='ref')['Message']) - - remove_emails_from_bounce(message_dict['bounce']) - - assert 'not-real@gmail.com' not in json.dumps(message_dict) - - def test_process_ses_results_in_complaint(sample_email_template): notification = create_notification(template=sample_email_template, reference='ref1') handle_complaint(json.loads(ses_complaint_callback()['Message']))