This commit is contained in:
Rebecca Law
2018-06-05 17:23:24 +01:00
parent 64f077f2f4
commit c5524a3fe9
5 changed files with 60 additions and 65 deletions

View File

@@ -38,7 +38,7 @@ def process_ses_response(ses_request):
if notification_type == 'Bounce': if notification_type == 'Bounce':
notification_type = determine_notification_bounce_type(notification_type, ses_message) notification_type = determine_notification_bounce_type(notification_type, ses_message)
elif notification_type == 'Complaint': elif notification_type == 'Complaint':
handle_complaint(ses_request) handle_complaint(ses_message)
return return
try: try:
@@ -108,13 +108,12 @@ def remove_emails_from_bounce(bounce_dict):
recip.pop('emailAddress') recip.pop('emailAddress')
def handle_complaint(ses_request): def handle_complaint(ses_message):
ses_message = json.loads(ses_request['Message'])
remove_emails_from_complaint(ses_message) remove_emails_from_complaint(ses_message)
current_app.logger.info("Complaint from SES: \n{}".format(ses_message)) current_app.logger.info("Complaint from SES: \n{}".format(ses_message))
# It is possible that the we get a key error, let this fail so we can investigate. # It is possible that the we get a key error, let this fail so we can investigate.
try: try:
reference = ses_request['MessageId'] reference = ses_message['mail']['messageId']
except KeyError as e: except KeyError as e:
current_app.logger.exception("Complaint from SES failed to get reference from message", e) current_app.logger.exception("Complaint from SES failed to get reference from message", e)
return return

View File

@@ -7,7 +7,7 @@ from app.notifications.notifications_ses_callback import remove_emails_from_comp
from tests.app.db import ( from tests.app.db import (
create_notification, ses_complaint_callback, create_notification, ses_complaint_callback,
ses_notification_callback ses_notification_callback,
) )
@@ -40,8 +40,7 @@ def test_process_ses_results_retry_called(notify_db, mocker):
def test_process_ses_results_in_complaint(sample_email_template, mocker): def test_process_ses_results_in_complaint(sample_email_template, mocker):
notification = create_notification(template=sample_email_template, reference='ref1') notification = create_notification(template=sample_email_template, reference='ref1')
mocked = mocker.patch("app.dao.notifications_dao.update_notification_status_by_reference") mocked = mocker.patch("app.dao.notifications_dao.update_notification_status_by_reference")
response = json.loads(ses_complaint_callback()) process_ses_results(response=ses_complaint_callback())
process_ses_results(response=response)
assert mocked.call_count == 0 assert mocked.call_count == 0
complaints = Complaint.query.all() complaints = Complaint.query.all()
assert len(complaints) == 1 assert len(complaints) == 1
@@ -49,7 +48,6 @@ def test_process_ses_results_in_complaint(sample_email_template, mocker):
def test_remove_emails_from_complaint(): def test_remove_emails_from_complaint():
test_message = ses_complaint_callback() test_json = json.loads(ses_complaint_callback()['Message'])
test_json = json.loads(json.loads(test_message)['Message'])
remove_emails_from_complaint(test_json) remove_emails_from_complaint(test_json)
assert "recipient1@example.com" not in test_json assert "recipient1@example.com" not in test_json

View File

@@ -1,3 +1,4 @@
import json
from datetime import datetime, date from datetime import datetime, date
import uuid import uuid
@@ -567,14 +568,12 @@ def create_ft_billing(bst_date,
def create_complaint(service=None, def create_complaint(service=None,
notification=None): notification=None):
if not service: if not service:
service = create_service() service = create_service()
if not notification: if not notification:
template = create_template(service=service, template_type='email') template = create_template(service=service, template_type='email')
notification = create_notification(template=template) notification = create_notification(template=template)
complaint = Complaint(notification_id=notification.id, complaint = Complaint(notification_id=notification.id,
service_id=service.id, service_id=service.id,
ses_feedback_id=str(uuid.uuid4()), ses_feedback_id=str(uuid.uuid4()),
@@ -587,43 +586,43 @@ def create_complaint(service=None,
def ses_complaint_callback_malformed_message_id(): def ses_complaint_callback_malformed_message_id():
return '{\n "Type" : "Notification",\n "msgId" : "ref1",' \ return {
'\n "TopicArn" : "arn:aws:sns:eu-west-1:123456789012:testing",' \ 'Signature': 'bb',
'\n "Message" : "{\\"notificationType\\":\\"Complaint\\",' \ 'SignatureVersion': '1', 'MessageAttributes': {}, 'MessageId': '98c6e927-af5d-5f3b-9522-bab736f2cbde',
'\\"complaint\\": {\\"userAgent\\":\\"AnyCompany Feedback Loop (V0.01)\\",' \ 'UnsubscribeUrl': 'https://sns.eu-west-1.amazonaws.com',
'\\"complainedRecipients\\":[{\\"emailAddress\\":\\"recipient1@example.com\\"}],' \ 'TopicArn': 'arn:ses_notifications', 'Type': 'Notification',
'\\"arrivalDate\\":\\"2009-12-03T04:24:21.000-05:00\\", ' \ 'Timestamp': '2018-06-05T14:00:15.952Z', 'Subject': None,
'\\"timestamp\\":\\"2012-05-25T14:59:38.623Z\\", ' \ 'Message': '{"notificationType":"Complaint","complaint":{"complainedRecipients":[{"emailAddress":"someone@hotmail.com"}],"timestamp":"2018-06-05T13:59:58.000Z","feedbackId":"ses_feedback_id"},"mail":{"timestamp":"2018-06-05T14:00:15.950Z","source":"\\"Some Service\\" <someservicenotifications.service.gov.uk>","sourceArn":"arn:identity/notifications.service.gov.uk","sourceIp":"52.208.24.161","sendingAccountId":"888450439860","badMessageId":"ref1","destination":["someone@hotmail.com"]}}', # noqa
'\\"feedbackId\\":\\"someSESID\\"}}"\n}' 'SigningCertUrl': 'https://sns.pem'
}
def ses_complaint_callback_with_missing_complaint_type(): def ses_complaint_callback_with_missing_complaint_type():
""" """
https://docs.aws.amazon.com/ses/latest/DeveloperGuide/notification-contents.html#complaint-object https://docs.aws.amazon.com/ses/latest/DeveloperGuide/notification-contents.html#complaint-object
""" """
return '{\n "Type" : "Notification",\n "MessageId" : "ref1",' \ return {
'\n "TopicArn" : "arn:aws:sns:eu-west-1:123456789012:testing",' \ 'Signature': 'bb',
'\n "Message" : "{\\"notificationType\\":\\"Complaint\\",' \ 'SignatureVersion': '1', 'MessageAttributes': {}, 'MessageId': '98c6e927-af5d-5f3b-9522-bab736f2cbde',
'\\"complaint\\": {\\"userAgent\\":\\"AnyCompany Feedback Loop (V0.01)\\",' \ 'UnsubscribeUrl': 'https://sns.eu-west-1.amazonaws.com',
'\\"complainedRecipients\\":[{\\"emailAddress\\":\\"recipient1@example.com\\"}],' \ 'TopicArn': 'arn:ses_notifications', 'Type': 'Notification',
'\\"arrivalDate\\":\\"2009-12-03T04:24:21.000-05:00\\", ' \ 'Timestamp': '2018-06-05T14:00:15.952Z', 'Subject': None,
'\\"timestamp\\":\\"2012-05-25T14:59:38.623Z\\", ' \ 'Message': '{"notificationType":"Complaint","complaint":{"complainedRecipients":[{"emailAddress":"someone@hotmail.com"}],"timestamp":"2018-06-05T13:59:58.000Z","feedbackId":"ses_feedback_id"},"mail":{"timestamp":"2018-06-05T14:00:15.950Z","source":"\\"Some Service\\" <someservicenotifications.service.gov.uk>","sourceArn":"arn:identity/notifications.service.gov.uk","sourceIp":"52.208.24.161","sendingAccountId":"888450439860","messageId":"ref1","destination":["someone@hotmail.com"]}}', # noqa
'\\"feedbackId\\":\\"someSESID\\"}}"\n}' 'SigningCertUrl': 'https://sns.pem'
}
def ses_complaint_callback(): def ses_complaint_callback():
""" """
https://docs.aws.amazon.com/ses/latest/DeveloperGuide/notification-contents.html#complaint-object https://docs.aws.amazon.com/ses/latest/DeveloperGuide/notification-contents.html#complaint-object
""" """
return '{\n "Type" : "Notification",\n "MessageId" : "ref1",' \ return {
'\n "TopicArn" : "arn:aws:sns:eu-west-1:123456789012:testing",' \ 'Signature': 'bb',
'\n "Message" : "{\\"notificationType\\":\\"Complaint\\",' \ 'SignatureVersion': '1', 'MessageAttributes': {}, 'MessageId': '98c6e927-af5d-5f3b-9522-bab736f2cbde',
'\\"complaint\\": {\\"userAgent\\":\\"AnyCompany Feedback Loop (V0.01)\\",' \ 'UnsubscribeUrl': 'https://sns.eu-west-1.amazonaws.com',
'\\"complainedRecipients\\":[{\\"emailAddress\\":\\"recipient1@example.com\\"}],' \ 'TopicArn': 'arn:ses_notifications', 'Type': 'Notification',
'\\"complaintFeedbackType\\":\\"abuse\\", ' \ 'Timestamp': '2018-06-05T14:00:15.952Z', 'Subject': None,
'\\"arrivalDate\\":\\"2009-12-03T04:24:21.000-05:00\\", ' \ 'Message': '{"notificationType":"Complaint","complaint":{"complaintFeedbackType": "abuse", "complainedRecipients":[{"emailAddress":"someone@hotmail.com"}],"timestamp":"2018-06-05T13:59:58.000Z","feedbackId":"ses_feedback_id"},"mail":{"timestamp":"2018-06-05T14:00:15.950Z","source":"\\"Some Service\\" <someservicenotifications.service.gov.uk>","sourceArn":"arn:identity/notifications.service.gov.uk","sourceIp":"52.208.24.161","sendingAccountId":"888450439860","messageId":"ref1","destination":["someone@hotmail.com"]}}', # noqa
'\\"timestamp\\":\\"2012-05-25T14:59:38.623Z\\", ' \ 'SigningCertUrl': 'https://sns.pem'
'\\"feedbackId\\":\\"someSESID\\"}}"\n}' }
def ses_notification_callback(): def ses_notification_callback():

View File

@@ -201,23 +201,22 @@ def test_remove_emails_from_bounce():
def test_process_ses_results_in_complaint(sample_email_template): def test_process_ses_results_in_complaint(sample_email_template):
notification = create_notification(template=sample_email_template, reference='ref1') notification = create_notification(template=sample_email_template, reference='ref1')
response = json.loads(ses_complaint_callback()) handle_complaint(ses_complaint_callback())
handle_complaint(response)
complaints = Complaint.query.all() complaints = Complaint.query.all()
assert len(complaints) == 1 assert len(complaints) == 1
assert complaints[0].notification_id == notification.id assert complaints[0].notification_id == notification.id
def test_handle_complaint_does_not_raise_exception_if_reference_is_missing(notify_api): def test_handle_complaint_does_not_raise_exception_if_reference_is_missing(notify_api):
response = json.loads(ses_complaint_callback_malformed_message_id()) response = json.loads(ses_complaint_callback_malformed_message_id()['Message'])
handle_complaint(response) handle_complaint(response)
assert len(Complaint.query.all()) == 0 assert len(Complaint.query.all()) == 0
def test_process_ses_results_in_complaint_save_complaint_with_null_complaint_type(notify_api, sample_email_template): def test_process_ses_results_in_complaint_save_complaint_with_null_complaint_type(notify_api, sample_email_template):
notification = create_notification(template=sample_email_template, reference='ref1') notification = create_notification(template=sample_email_template, reference='ref1')
response = json.loads(ses_complaint_callback_with_missing_complaint_type()) msg = json.loads(ses_complaint_callback_with_missing_complaint_type()['Message'])
handle_complaint(response) handle_complaint(msg)
complaints = Complaint.query.all() complaints = Complaint.query.all()
assert len(complaints) == 1 assert len(complaints) == 1
assert complaints[0].notification_id == notification.id assert complaints[0].notification_id == notification.id