def test_process_ses_smtp_results_in_complaint(sample_email_template): notification = create_notification(template=sample_email_template, reference='ref1') handle_smtp_complaint(json.loads(ses_complaint_callback()['Message'])) complaints = Complaint.query.all() assert len(complaints) == 1 assert complaints[0].notification_id == notification.id
def test_process_smtp_results_in_complaint_save_complaint_with_null_complaint_type(notify_api, sample_email_template): notification = create_notification(template=sample_email_template, reference='ref1') msg = json.loads(ses_complaint_callback_with_missing_complaint_type()['Message']) handle_smtp_complaint(msg) complaints = Complaint.query.all() assert len(complaints) == 1 assert complaints[0].notification_id == notification.id assert not complaints[0].complaint_type
def process_ses_smtp_results(self, response): try: ses_message = json.loads(response['Message']) notification_type = ses_message['notificationType'] headers = ses_message['mail']['commonHeaders'] source = ses_message['mail']['source'] recipients = ses_message['mail']['destination'] if notification_type == 'Bounce': notification_type = determine_notification_bounce_type( notification_type, ses_message) aws_response_dict = get_aws_responses(notification_type) notification_status = aws_response_dict['notification_status'] try: # Get service based on SMTP name service = services_dao.dao_services_by_partial_smtp_name( source.split("@")[-1]) # Create a sent notification based on details from the payload template = templates_dao.dao_get_template_by_id( current_app.config['SMTP_TEMPLATE_ID']) for recipient in recipients: message = "".join(( 'A message was sent from: \n', # noqa: E126 source, '\n\n to: \n', recipient, '\n\n on: \n', headers["date"], '\n\n with the subject: \n', headers["subject"])) notification = process_notifications.persist_notification( template_id=template.id, template_version=template.version, recipient=recipient, service=service, personalisation={ 'subject': headers["subject"], 'message': message }, notification_type=EMAIL_TYPE, api_key_id=None, key_type=KEY_TYPE_NORMAL, reply_to_text=recipient, created_at=headers["date"], status=notification_status, reference=ses_message['mail']['messageId']) if notification_type == 'Complaint': _check_and_queue_complaint_callback_task( *handle_smtp_complaint(ses_message)) else: _check_and_queue_callback_task(notification) except NoResultFound: reference = ses_message['mail']['messageId'] current_app.logger.warning( "SMTP service not found for reference: {} (update to {})". format(reference, notification_status)) return statsd_client.incr('callback.ses-smtp.{}'.format(notification_status)) return True except Retry: raise except Exception as e: current_app.logger.exception( 'Error processing SES SMTP results: {}'.format(type(e))) self.retry(queue=QueueNames.RETRY)
def test_handle_smtp_complaint_does_not_raise_exception_if_reference_is_missing( notify_api): response = json.loads( ses_complaint_callback_malformed_message_id()['Message']) handle_smtp_complaint(response) assert len(Complaint.query.all()) == 0
def test_handle_smtp_complaint_does_raise_exception_if_notification_not_found( notify_api): response = json.loads(ses_complaint_callback()['Message']) with pytest.raises(expected_exception=SQLAlchemyError): handle_smtp_complaint(response)