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
|
|
|
|
2022-10-03 17:16:59 -07:00
|
|
|
from app.celery.process_ses_receipts_tasks import (
|
2021-11-26 15:57:31 +00:00
|
|
|
check_and_queue_callback_task,
|
|
|
|
|
handle_complaint,
|
|
|
|
|
)
|
2022-10-03 17:16:59 -07:00
|
|
|
from app.dao.notifications_dao import get_notification_by_id
|
|
|
|
|
from app.models import 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,
|
2021-11-26 15:57:31 +00:00
|
|
|
create_service_callback_api,
|
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
|
2021-11-26 15:57:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_check_and_queue_callback_task(mocker, sample_notification):
|
|
|
|
|
mock_create = mocker.patch(
|
|
|
|
|
'app.notifications.notifications_ses_callback.create_delivery_status_callback_data'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
mock_send = mocker.patch(
|
|
|
|
|
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
callback_api = create_service_callback_api(service=sample_notification.service)
|
|
|
|
|
mock_create.return_value = 'encrypted_status_update'
|
|
|
|
|
|
|
|
|
|
check_and_queue_callback_task(sample_notification)
|
|
|
|
|
|
|
|
|
|
# callback_api doesn't match by equality for some
|
|
|
|
|
# reason, so we need to take this approach instead
|
|
|
|
|
mock_create_args = mock_create.mock_calls[0][1]
|
|
|
|
|
assert mock_create_args[0] == sample_notification
|
|
|
|
|
assert mock_create_args[1].id == callback_api.id
|
|
|
|
|
|
|
|
|
|
mock_send.assert_called_once_with(
|
|
|
|
|
[str(sample_notification.id), mock_create.return_value], queue="service-callbacks"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_check_and_queue_callback_task_no_callback_api(mocker, sample_notification):
|
|
|
|
|
mock_send = mocker.patch(
|
|
|
|
|
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
check_and_queue_callback_task(sample_notification)
|
|
|
|
|
mock_send.assert_not_called()
|