2017-11-17 13:41:45 +00:00
|
|
|
import json
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
2017-12-01 16:15:21 +00:00
|
|
|
from app.celery.process_ses_receipts_tasks import process_ses_results
|
2018-06-07 12:30:04 +01:00
|
|
|
from app.celery.research_mode_tasks import ses_hard_bounce_callback
|
2018-06-04 17:29:58 +01:00
|
|
|
from app.models import Complaint
|
2018-06-07 12:30:04 +01:00
|
|
|
from app.notifications.notifications_ses_callback import remove_emails_from_complaint, remove_emails_from_bounce
|
2017-11-17 13:41:45 +00:00
|
|
|
|
2018-06-04 17:29:58 +01:00
|
|
|
from tests.app.db import (
|
|
|
|
|
create_notification, ses_complaint_callback,
|
2018-06-05 17:23:24 +01:00
|
|
|
ses_notification_callback,
|
2018-06-04 17:29:58 +01:00
|
|
|
)
|
2017-11-17 13:41:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_process_ses_results(sample_email_template):
|
|
|
|
|
create_notification(
|
|
|
|
|
sample_email_template,
|
|
|
|
|
reference='ref1',
|
|
|
|
|
sent_at=datetime.utcnow(),
|
|
|
|
|
status='sending')
|
|
|
|
|
|
|
|
|
|
response = json.loads(ses_notification_callback())
|
|
|
|
|
assert process_ses_results(response=response) is None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_process_ses_results_does_not_retry_if_errors(notify_db, mocker):
|
2017-12-01 16:15:21 +00:00
|
|
|
mocked = mocker.patch('app.celery.process_ses_receipts_tasks.process_ses_results.retry')
|
2017-11-17 13:41:45 +00:00
|
|
|
response = json.loads(ses_notification_callback())
|
|
|
|
|
process_ses_results(response=response)
|
|
|
|
|
assert mocked.call_count == 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_process_ses_results_retry_called(notify_db, mocker):
|
|
|
|
|
mocker.patch("app.dao.notifications_dao.update_notification_status_by_reference", side_effect=Exception("EXPECTED"))
|
2017-12-01 16:15:21 +00:00
|
|
|
mocked = mocker.patch('app.celery.process_ses_receipts_tasks.process_ses_results.retry')
|
2017-11-17 13:41:45 +00:00
|
|
|
response = json.loads(ses_notification_callback())
|
|
|
|
|
process_ses_results(response=response)
|
|
|
|
|
assert mocked.call_count != 0
|
|
|
|
|
|
|
|
|
|
|
2018-06-04 17:29:58 +01:00
|
|
|
def test_process_ses_results_in_complaint(sample_email_template, mocker):
|
|
|
|
|
notification = create_notification(template=sample_email_template, reference='ref1')
|
2018-05-30 16:16:36 +01:00
|
|
|
mocked = mocker.patch("app.dao.notifications_dao.update_notification_status_by_reference")
|
2018-06-05 17:23:24 +01:00
|
|
|
process_ses_results(response=ses_complaint_callback())
|
2018-05-30 16:16:36 +01:00
|
|
|
assert mocked.call_count == 0
|
2018-06-04 17:29:58 +01:00
|
|
|
complaints = Complaint.query.all()
|
|
|
|
|
assert len(complaints) == 1
|
|
|
|
|
assert complaints[0].notification_id == notification.id
|
2018-05-30 16:16:36 +01:00
|
|
|
|
|
|
|
|
|
2018-05-30 16:45:18 +01:00
|
|
|
def test_remove_emails_from_complaint():
|
2018-06-05 17:23:24 +01:00
|
|
|
test_json = json.loads(ses_complaint_callback()['Message'])
|
2018-05-30 16:45:18 +01:00
|
|
|
remove_emails_from_complaint(test_json)
|
2018-06-06 10:37:31 +01:00
|
|
|
assert "recipient1@example.com" not in json.dumps(test_json)
|
2018-06-07 12:30:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_remove_email_from_bounce():
|
|
|
|
|
test_json = json.loads(ses_hard_bounce_callback(reference='ref1')['Message'])
|
|
|
|
|
remove_emails_from_bounce(test_json)
|
|
|
|
|
assert "bounce@simulator.amazonses.com" not in json.dumps(test_json)
|