Merge pull request #1900 from alphagov/add-ses-ref

Add some useful information to the log.
This commit is contained in:
Rebecca Law
2018-06-07 15:18:31 +01:00
committed by GitHub
3 changed files with 25 additions and 18 deletions

View File

@@ -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')

View File

@@ -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)

View File

@@ -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']))