2018-06-06 10:37:31 +01:00
|
|
|
import pytest
|
2017-03-16 18:15:49 +00:00
|
|
|
from flask import json
|
2018-06-06 10:37:31 +01:00
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
2017-03-16 18:15:49 +00:00
|
|
|
|
|
|
|
|
from app.dao.notifications_dao import get_notification_by_id
|
2019-05-30 10:37:57 +01:00
|
|
|
from app.models import Complaint
|
2019-02-13 11:35:31 +00:00
|
|
|
from app.notifications.notifications_ses_callback import handle_complaint
|
2018-06-04 17:29:58 +01:00
|
|
|
from tests.app.db import (
|
2021-03-10 13:55:06 +00:00
|
|
|
create_notification,
|
|
|
|
|
create_notification_history,
|
2019-05-30 10:37:57 +01:00
|
|
|
ses_complaint_callback,
|
2021-03-10 13:55:06 +00:00
|
|
|
ses_complaint_callback_malformed_message_id,
|
|
|
|
|
ses_complaint_callback_with_missing_complaint_type,
|
2018-06-04 17:29:58 +01:00
|
|
|
)
|
2017-03-16 18:15:49 +00:00
|
|
|
|
|
|
|
|
|
2019-10-30 10:51:07 +00:00
|
|
|
def test_ses_callback_should_not_set_status_once_status_is_delivered(sample_email_template):
|
|
|
|
|
notification = create_notification(sample_email_template, status='delivered', )
|
2017-03-16 18:15:49 +00:00
|
|
|
|
|
|
|
|
assert get_notification_by_id(notification.id).status == 'delivered'
|
|
|
|
|
|
|
|
|
|
|
2018-06-04 17:29:58 +01:00
|
|
|
def test_process_ses_results_in_complaint(sample_email_template):
|
|
|
|
|
notification = create_notification(template=sample_email_template, reference='ref1')
|
2018-06-06 10:37:31 +01:00
|
|
|
handle_complaint(json.loads(ses_complaint_callback()['Message']))
|
2018-06-04 17:29:58 +01:00
|
|
|
complaints = Complaint.query.all()
|
|
|
|
|
assert len(complaints) == 1
|
|
|
|
|
assert complaints[0].notification_id == notification.id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_handle_complaint_does_not_raise_exception_if_reference_is_missing(notify_api):
|
2018-06-05 17:23:24 +01:00
|
|
|
response = json.loads(ses_complaint_callback_malformed_message_id()['Message'])
|
2018-06-04 17:29:58 +01:00
|
|
|
handle_complaint(response)
|
|
|
|
|
assert len(Complaint.query.all()) == 0
|
|
|
|
|
|
|
|
|
|
|
2018-06-06 10:37:31 +01:00
|
|
|
def test_handle_complaint_does_raise_exception_if_notification_not_found(notify_api):
|
|
|
|
|
response = json.loads(ses_complaint_callback()['Message'])
|
|
|
|
|
with pytest.raises(expected_exception=SQLAlchemyError):
|
|
|
|
|
handle_complaint(response)
|
|
|
|
|
|
|
|
|
|
|
2019-05-22 10:03:07 +01:00
|
|
|
def test_process_ses_results_in_complaint_if_notification_history_does_not_exist(sample_email_template):
|
|
|
|
|
notification = create_notification(template=sample_email_template, reference='ref1')
|
|
|
|
|
handle_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_ses_results_in_complaint_if_notification_does_not_exist(sample_email_template):
|
2019-05-30 10:37:57 +01:00
|
|
|
notification = create_notification_history(template=sample_email_template, reference='ref1')
|
2019-05-22 10:03:07 +01:00
|
|
|
handle_complaint(json.loads(ses_complaint_callback()['Message']))
|
|
|
|
|
complaints = Complaint.query.all()
|
|
|
|
|
assert len(complaints) == 1
|
|
|
|
|
assert complaints[0].notification_id == notification.id
|
|
|
|
|
|
|
|
|
|
|
2018-06-04 17:29:58 +01:00
|
|
|
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')
|
2018-06-05 17:23:24 +01:00
|
|
|
msg = json.loads(ses_complaint_callback_with_missing_complaint_type()['Message'])
|
|
|
|
|
handle_complaint(msg)
|
2018-06-04 17:29:58 +01:00
|
|
|
complaints = Complaint.query.all()
|
|
|
|
|
assert len(complaints) == 1
|
|
|
|
|
assert complaints[0].notification_id == notification.id
|
|
|
|
|
assert not complaints[0].complaint_type
|