diff --git a/app/notifications/notifications_ses_callback.py b/app/notifications/notifications_ses_callback.py index 962aaf482..81e73ff42 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: @@ -105,8 +105,12 @@ def determine_notification_bounce_type(notification_type, ses_message): def remove_emails_from_bounce(bounce_dict): - for recip in bounce_dict['bouncedRecipients']: - recip.pop('emailAddress') + if bounce_dict.get('mail').get('headers', None): + bounce_dict['mail'].pop('headers') + if bounce_dict.get('mail').get('commonHeaders'): + bounce_dict['mail'].pop('commonHeaders') + bounce_dict['mail'].pop('destination') + bounce_dict['bounce'].pop('bouncedRecipients') def handle_complaint(ses_message): @@ -131,6 +135,10 @@ def handle_complaint(ses_message): def remove_emails_from_complaint(complaint_dict): + if complaint_dict.get('headers', None): + complaint_dict.pop('headers') + if complaint_dict.get('commonHeaders'): + complaint_dict.pop('commonHeaders') 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']))