mirror of
https://github.com/GSA/notifications-api.git
synced 2026-05-05 08:40:29 -04:00
canada UK ses callbacks monster mash
This commit is contained in:
@@ -10,12 +10,16 @@ from app.celery.research_mode_tasks import (
|
||||
ses_notification_callback,
|
||||
ses_soft_bounce_callback,
|
||||
)
|
||||
from app.celery.service_callback_tasks import (
|
||||
create_delivery_status_callback_data,
|
||||
)
|
||||
from app.dao.notifications_dao import get_notification_by_id
|
||||
from app.models import Complaint, Notification
|
||||
from app.notifications.notifications_ses_callback import (
|
||||
remove_emails_from_bounce,
|
||||
remove_emails_from_complaint,
|
||||
)
|
||||
from tests.app.conftest import create_sample_notification
|
||||
from tests.app.db import (
|
||||
create_notification,
|
||||
create_service_callback_api,
|
||||
@@ -23,16 +27,87 @@ from tests.app.db import (
|
||||
)
|
||||
|
||||
|
||||
def test_notifications_ses_400_with_invalid_header(client):
|
||||
data = json.dumps({"foo": "bar"})
|
||||
response = client.post(
|
||||
path='/notifications/email/ses',
|
||||
data=data,
|
||||
headers=[('Content-Type', 'application/json')]
|
||||
)
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
def test_notifications_ses_400_with_invalid_message_type(client):
|
||||
data = json.dumps({"foo": "bar"})
|
||||
response = client.post(
|
||||
path='/notifications/email/ses',
|
||||
data=data,
|
||||
headers=[('Content-Type', 'application/json'), ('x-amz-sns-message-type', 'foo')]
|
||||
)
|
||||
assert response.status_code == 400
|
||||
assert "SES-SNS callback failed: invalid message type" in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_notifications_ses_400_with_invalid_json(client):
|
||||
data = "FOOO"
|
||||
response = client.post(
|
||||
path='/notifications/email/ses',
|
||||
data=data,
|
||||
headers=[('Content-Type', 'application/json'), ('x-amz-sns-message-type', 'Notification')]
|
||||
)
|
||||
assert response.status_code == 400
|
||||
assert "SES-SNS callback failed: invalid JSON given" in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_notifications_ses_400_with_certificate(client):
|
||||
data = json.dumps({"foo": "bar"})
|
||||
response = client.post(
|
||||
path='/notifications/email/ses',
|
||||
data=data,
|
||||
headers=[('Content-Type', 'application/json'), ('x-amz-sns-message-type', 'Notification')]
|
||||
)
|
||||
assert response.status_code == 400
|
||||
assert "SES-SNS callback failed: validation failed" in response.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_notifications_ses_200_autoconfirms_subscription(client, mocker):
|
||||
mocker.patch("validatesns.validate")
|
||||
requests_mock = mocker.patch("requests.get")
|
||||
data = json.dumps({"Type": "SubscriptionConfirmation", "SubscribeURL": "https://foo"})
|
||||
response = client.post(
|
||||
path='/notifications/email/ses',
|
||||
data=data,
|
||||
headers=[('Content-Type', 'application/json'), ('x-amz-sns-message-type', 'SubscriptionConfirmation')]
|
||||
)
|
||||
|
||||
requests_mock.assert_called_once_with("https://foo")
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_notifications_ses_200_call_process_task(client, mocker):
|
||||
mocker.patch("validatesns.validate")
|
||||
process_mock = mocker.patch("app.celery.process_ses_receipts_tasks.process_ses_results.apply_async")
|
||||
data = {"Type": "Notification", "foo": "bar"}
|
||||
json_data = json.dumps(data)
|
||||
response = client.post(
|
||||
path='/notifications/email/ses',
|
||||
data=json_data,
|
||||
headers=[('Content-Type', 'application/json'), ('x-amz-sns-message-type', 'Notification')]
|
||||
)
|
||||
|
||||
process_mock.assert_called_once_with([{'Message': None}], queue='notify-internal-tasks')
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_process_ses_results(sample_email_template):
|
||||
create_notification(sample_email_template, reference='ref1', sent_at=datetime.utcnow(), status='sending')
|
||||
|
||||
assert process_ses_results(response=ses_notification_callback(reference='ref1'))
|
||||
|
||||
|
||||
def test_process_ses_results_retry_called(sample_email_template, mocker):
|
||||
def test_process_ses_results_retry_called(sample_email_template, _notify_db, mocker):
|
||||
create_notification(sample_email_template, reference='ref1', sent_at=datetime.utcnow(), status='sending')
|
||||
|
||||
mocker.patch("app.dao.notifications_dao.dao_update_notifications_by_reference", side_effect=Exception("EXPECTED"))
|
||||
mocker.patch("app.dao.notifications_dao._update_notification_status", side_effect=Exception("EXPECTED"))
|
||||
mocked = mocker.patch('app.celery.process_ses_receipts_tasks.process_ses_results.retry')
|
||||
process_ses_results(response=ses_notification_callback(reference='ref1'))
|
||||
assert mocked.call_count != 0
|
||||
@@ -62,6 +137,7 @@ def test_remove_email_from_bounce():
|
||||
|
||||
def test_ses_callback_should_update_notification_status(
|
||||
client,
|
||||
_notify_db,
|
||||
notify_db_session,
|
||||
sample_email_template,
|
||||
mocker):
|
||||
@@ -69,140 +145,159 @@ def test_ses_callback_should_update_notification_status(
|
||||
mocker.patch('app.statsd_client.incr')
|
||||
mocker.patch('app.statsd_client.timing_with_dates')
|
||||
send_mock = mocker.patch(
|
||||
'app.celery.process_ses_receipts_tasks.check_and_queue_callback_task'
|
||||
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
|
||||
)
|
||||
notification = create_notification(
|
||||
notification = create_sample_notification(
|
||||
_notify_db,
|
||||
notify_db_session,
|
||||
template=sample_email_template,
|
||||
status='sending',
|
||||
reference='ref',
|
||||
status='sending',
|
||||
sent_at=datetime.utcnow()
|
||||
)
|
||||
callback_api = create_service_callback_api(service=sample_email_template.service, url="https://original_url.com")
|
||||
assert get_notification_by_id(notification.id).status == 'sending'
|
||||
|
||||
assert process_ses_results(ses_notification_callback(reference='ref'))
|
||||
assert get_notification_by_id(notification.id).status == 'delivered'
|
||||
statsd_client.timing_with_dates.assert_any_call(
|
||||
"callback.ses.delivered.elapsed-time", datetime.utcnow(), notification.sent_at
|
||||
"callback.ses.elapsed-time", datetime.utcnow(), notification.sent_at
|
||||
)
|
||||
statsd_client.incr.assert_any_call("callback.ses.delivered")
|
||||
updated_notification = Notification.query.get(notification.id)
|
||||
send_mock.assert_called_once_with(updated_notification)
|
||||
encrypted_data = create_delivery_status_callback_data(updated_notification, callback_api)
|
||||
send_mock.assert_called_once_with([str(notification.id), encrypted_data], queue="service-callbacks")
|
||||
|
||||
|
||||
def test_ses_callback_should_not_update_notification_status_if_already_delivered(sample_email_template, mocker):
|
||||
mock_dup = mocker.patch('app.celery.process_ses_receipts_tasks.notifications_dao._duplicate_update_warning')
|
||||
mock_upd = mocker.patch(
|
||||
'app.celery.process_ses_receipts_tasks.notifications_dao.dao_update_notifications_by_reference'
|
||||
)
|
||||
mock_upd = mocker.patch('app.celery.process_ses_receipts_tasks.notifications_dao._update_notification_status')
|
||||
notification = create_notification(template=sample_email_template, reference='ref', status='delivered')
|
||||
|
||||
assert process_ses_results(ses_notification_callback(reference='ref')) is None
|
||||
assert get_notification_by_id(notification.id).status == 'delivered'
|
||||
|
||||
mock_dup.assert_called_once_with(notification=notification, status='delivered')
|
||||
mock_dup.assert_called_once_with(notification, 'delivered')
|
||||
assert mock_upd.call_count == 0
|
||||
|
||||
|
||||
def test_ses_callback_should_retry_if_notification_is_new(client, notify_db_session, mocker):
|
||||
def test_ses_callback_should_retry_if_notification_is_new(client, _notify_db, mocker):
|
||||
mock_retry = mocker.patch('app.celery.process_ses_receipts_tasks.process_ses_results.retry')
|
||||
mock_logger = mocker.patch('app.celery.process_ses_receipts_tasks.current_app.logger.error')
|
||||
|
||||
with freeze_time('2017-11-17T12:14:03.646Z'):
|
||||
assert process_ses_results(ses_notification_callback(reference='ref')) is None
|
||||
assert mock_logger.call_count == 0
|
||||
assert mock_retry.call_count == 1
|
||||
|
||||
|
||||
def test_ses_callback_should_log_if_notification_is_missing(client, notify_db_session, mocker):
|
||||
def test_ses_callback_should_log_if_notification_is_missing(client, _notify_db, mocker):
|
||||
mock_retry = mocker.patch('app.celery.process_ses_receipts_tasks.process_ses_results.retry')
|
||||
mock_logger = mocker.patch('app.celery.process_ses_receipts_tasks.current_app.logger.warning')
|
||||
|
||||
with freeze_time('2017-11-17T12:34:03.646Z'):
|
||||
assert process_ses_results(ses_notification_callback(reference='ref')) is None
|
||||
assert mock_retry.call_count == 0
|
||||
mock_logger.assert_called_once_with('notification not found for reference: ref (update to delivered)')
|
||||
|
||||
|
||||
def test_ses_callback_should_not_retry_if_notification_is_old(client, notify_db_session, mocker):
|
||||
def test_ses_callback_should_not_retry_if_notification_is_old(client, _notify_db, mocker):
|
||||
mock_retry = mocker.patch('app.celery.process_ses_receipts_tasks.process_ses_results.retry')
|
||||
mock_logger = mocker.patch('app.celery.process_ses_receipts_tasks.current_app.logger.error')
|
||||
|
||||
with freeze_time('2017-11-21T12:14:03.646Z'):
|
||||
assert process_ses_results(ses_notification_callback(reference='ref')) is None
|
||||
assert mock_logger.call_count == 0
|
||||
assert mock_retry.call_count == 0
|
||||
|
||||
|
||||
def test_ses_callback_should_update_multiple_notification_status_sent(
|
||||
def test_ses_callback_does_not_call_send_delivery_status_if_no_db_entry(
|
||||
client,
|
||||
_notify_db,
|
||||
notify_db_session,
|
||||
sample_email_template,
|
||||
mocker):
|
||||
with freeze_time('2001-01-01T12:00:00'):
|
||||
send_mock = mocker.patch(
|
||||
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
|
||||
)
|
||||
notification = create_sample_notification(
|
||||
_notify_db,
|
||||
notify_db_session,
|
||||
template=sample_email_template,
|
||||
reference='ref',
|
||||
status='sending',
|
||||
sent_at=datetime.utcnow()
|
||||
)
|
||||
assert get_notification_by_id(notification.id).status == 'sending'
|
||||
assert process_ses_results(ses_notification_callback(reference='ref'))
|
||||
assert get_notification_by_id(notification.id).status == 'delivered'
|
||||
send_mock.assert_not_called()
|
||||
def test_ses_callback_should_update_multiple_notification_status_sent(
|
||||
client,
|
||||
_notify_db,
|
||||
notify_db_session,
|
||||
sample_email_template,
|
||||
mocker):
|
||||
|
||||
send_mock = mocker.patch(
|
||||
'app.celery.process_ses_receipts_tasks.check_and_queue_callback_task'
|
||||
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
|
||||
)
|
||||
create_notification(
|
||||
create_sample_notification(
|
||||
_notify_db,
|
||||
notify_db_session,
|
||||
template=sample_email_template,
|
||||
status='sending',
|
||||
reference='ref1',
|
||||
)
|
||||
create_notification(
|
||||
sent_at=datetime.utcnow(),
|
||||
status='sending')
|
||||
create_sample_notification(
|
||||
_notify_db,
|
||||
notify_db_session,
|
||||
template=sample_email_template,
|
||||
status='sending',
|
||||
reference='ref2',
|
||||
)
|
||||
create_notification(
|
||||
sent_at=datetime.utcnow(),
|
||||
status='sending')
|
||||
create_sample_notification(
|
||||
_notify_db,
|
||||
notify_db_session,
|
||||
template=sample_email_template,
|
||||
status='sending',
|
||||
reference='ref3',
|
||||
)
|
||||
sent_at=datetime.utcnow(),
|
||||
status='sending')
|
||||
create_service_callback_api(service=sample_email_template.service, url="https://original_url.com")
|
||||
assert process_ses_results(ses_notification_callback(reference='ref1'))
|
||||
assert process_ses_results(ses_notification_callback(reference='ref2'))
|
||||
assert process_ses_results(ses_notification_callback(reference='ref3'))
|
||||
assert send_mock.called
|
||||
|
||||
|
||||
def test_ses_callback_should_set_status_to_temporary_failure(client,
|
||||
_notify_db,
|
||||
notify_db_session,
|
||||
sample_email_template,
|
||||
mocker):
|
||||
send_mock = mocker.patch(
|
||||
'app.celery.process_ses_receipts_tasks.check_and_queue_callback_task'
|
||||
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
|
||||
)
|
||||
mock_logger = mocker.patch('app.celery.process_ses_receipts_tasks.current_app.logger.info')
|
||||
notification = create_notification(
|
||||
notification = create_sample_notification(
|
||||
_notify_db,
|
||||
notify_db_session,
|
||||
template=sample_email_template,
|
||||
status='sending',
|
||||
reference='ref',
|
||||
status='sending',
|
||||
sent_at=datetime.utcnow()
|
||||
)
|
||||
create_service_callback_api(service=notification.service, url="https://original_url.com")
|
||||
assert get_notification_by_id(notification.id).status == 'sending'
|
||||
assert process_ses_results(ses_soft_bounce_callback(reference='ref'))
|
||||
assert get_notification_by_id(notification.id).status == 'temporary-failure'
|
||||
assert send_mock.called
|
||||
assert f'SES bounce for notification ID {notification.id}: ' in mock_logger.call_args[0][0]
|
||||
|
||||
|
||||
def test_ses_callback_should_set_status_to_permanent_failure(client,
|
||||
_notify_db,
|
||||
notify_db_session,
|
||||
sample_email_template,
|
||||
mocker):
|
||||
send_mock = mocker.patch(
|
||||
'app.celery.process_ses_receipts_tasks.check_and_queue_callback_task'
|
||||
'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async'
|
||||
)
|
||||
mock_logger = mocker.patch('app.celery.process_ses_receipts_tasks.current_app.logger.info')
|
||||
notification = create_notification(
|
||||
notification = create_sample_notification(
|
||||
_notify_db,
|
||||
notify_db_session,
|
||||
template=sample_email_template,
|
||||
status='sending',
|
||||
reference='ref',
|
||||
status='sending',
|
||||
sent_at=datetime.utcnow()
|
||||
)
|
||||
|
||||
create_service_callback_api(service=sample_email_template.service, url="https://original_url.com")
|
||||
assert get_notification_by_id(notification.id).status == 'sending'
|
||||
assert process_ses_results(ses_hard_bounce_callback(reference='ref'))
|
||||
assert get_notification_by_id(notification.id).status == 'permanent-failure'
|
||||
assert send_mock.called
|
||||
assert f'SES bounce for notification ID {notification.id}: ' in mock_logger.call_args[0][0]
|
||||
|
||||
|
||||
def test_ses_callback_should_send_on_complaint_to_user_callback_api(sample_email_template, mocker):
|
||||
send_mock = mocker.patch(
|
||||
'app.celery.service_callback_tasks.send_complaint_to_service.apply_async'
|
||||
@@ -210,13 +305,11 @@ def test_ses_callback_should_send_on_complaint_to_user_callback_api(sample_email
|
||||
create_service_callback_api(
|
||||
service=sample_email_template.service, url="https://original_url.com", callback_type="complaint"
|
||||
)
|
||||
|
||||
notification = create_notification(
|
||||
template=sample_email_template, reference='ref1', sent_at=datetime.utcnow(), status='sending'
|
||||
)
|
||||
response = ses_complaint_callback()
|
||||
assert process_ses_results(response)
|
||||
|
||||
assert send_mock.call_count == 1
|
||||
assert encryption.decrypt(send_mock.call_args[0][0][0]) == {
|
||||
'complaint_date': '2018-06-05T13:59:58.000000Z',
|
||||
@@ -227,3 +320,4 @@ def test_ses_callback_should_send_on_complaint_to_user_callback_api(sample_email
|
||||
'service_callback_api_url': 'https://original_url.com',
|
||||
'to': 'recipient1@example.com'
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ from app.models import (
|
||||
KEY_TYPE_TEAM,
|
||||
KEY_TYPE_TEST,
|
||||
LETTER_TYPE,
|
||||
NOTIFICATION_STATUS_TYPES_COMPLETED,
|
||||
SERVICE_PERMISSION_TYPES,
|
||||
SMS_TYPE,
|
||||
ApiKey,
|
||||
@@ -69,6 +70,96 @@ def rmock():
|
||||
yield rmock
|
||||
|
||||
|
||||
def create_sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=None,
|
||||
template=None,
|
||||
job=None,
|
||||
job_row_number=None,
|
||||
to_field=None,
|
||||
status="created",
|
||||
provider_response=None,
|
||||
reference=None,
|
||||
created_at=None,
|
||||
sent_at=None,
|
||||
billable_units=1,
|
||||
personalisation=None,
|
||||
api_key=None,
|
||||
key_type=KEY_TYPE_NORMAL,
|
||||
sent_by=None,
|
||||
international=False,
|
||||
client_reference=None,
|
||||
rate_multiplier=1.0,
|
||||
scheduled_for=None,
|
||||
normalised_to=None,
|
||||
postage=None,
|
||||
):
|
||||
if created_at is None:
|
||||
created_at = datetime.utcnow()
|
||||
if service is None:
|
||||
service = create_service(check_if_service_exists=True)
|
||||
if template is None:
|
||||
template = create_template(service=service)
|
||||
|
||||
if job is None and api_key is None:
|
||||
# we didn't specify in test - lets create it
|
||||
api_key = ApiKey.query.filter(ApiKey.service == template.service, ApiKey.key_type == key_type).first()
|
||||
if not api_key:
|
||||
api_key = create_api_key(template.service, key_type=key_type)
|
||||
|
||||
notification_id = uuid.uuid4()
|
||||
|
||||
if to_field:
|
||||
to = to_field
|
||||
else:
|
||||
to = "+16502532222"
|
||||
|
||||
data = {
|
||||
"id": notification_id,
|
||||
"to": to,
|
||||
"job_id": job.id if job else None,
|
||||
"job": job,
|
||||
"service_id": service.id,
|
||||
"service": service,
|
||||
"template_id": template.id,
|
||||
"template_version": template.version,
|
||||
"status": status,
|
||||
"provider_response": provider_response,
|
||||
"reference": reference,
|
||||
"created_at": created_at,
|
||||
"sent_at": sent_at,
|
||||
"billable_units": billable_units,
|
||||
"personalisation": personalisation,
|
||||
"notification_type": template.template_type,
|
||||
"api_key": api_key,
|
||||
"api_key_id": api_key and api_key.id,
|
||||
"key_type": api_key.key_type if api_key else key_type,
|
||||
"sent_by": sent_by,
|
||||
"updated_at": created_at if status in NOTIFICATION_STATUS_TYPES_COMPLETED else None,
|
||||
"client_reference": client_reference,
|
||||
"rate_multiplier": rate_multiplier,
|
||||
"normalised_to": normalised_to,
|
||||
"postage": postage,
|
||||
}
|
||||
if job_row_number is not None:
|
||||
data["job_row_number"] = job_row_number
|
||||
notification = Notification(**data)
|
||||
dao_create_notification(notification)
|
||||
# if scheduled_for:
|
||||
# scheduled_notification = ScheduledNotification(
|
||||
# id=uuid.uuid4(),
|
||||
# notification_id=notification.id,
|
||||
# scheduled_for=datetime.strptime(scheduled_for, "%Y-%m-%d %H:%M"),
|
||||
# )
|
||||
# if status != "created":
|
||||
# scheduled_notification.pending = False
|
||||
# db.session.add(scheduled_notification)
|
||||
# db.session.commit()
|
||||
|
||||
return notification
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def service_factory(sample_user):
|
||||
class ServiceFactory(object):
|
||||
|
||||
@@ -68,6 +68,7 @@ def test_get_notification_by_id_returns_200(
|
||||
'completed_at': sample_notification.completed_at(),
|
||||
'scheduled_for': None,
|
||||
'postage': None,
|
||||
'provider_response': None
|
||||
}
|
||||
|
||||
assert json_response == expected_response
|
||||
@@ -120,6 +121,7 @@ def test_get_notification_by_id_with_placeholders_returns_200(
|
||||
'completed_at': sample_notification.completed_at(),
|
||||
'scheduled_for': None,
|
||||
'postage': None,
|
||||
'provider_response': None
|
||||
}
|
||||
|
||||
assert json_response == expected_response
|
||||
|
||||
Reference in New Issue
Block a user