Use the new task wrapper methods rather than creating a task directly

This commit is contained in:
Martyn Inglis
2017-05-09 18:17:55 +01:00
parent f2a47044a4
commit caed193647
4 changed files with 145 additions and 36 deletions

View File

@@ -18,6 +18,8 @@ from app.dao.templates_dao import dao_get_template_by_id
from app.models import SMS_TYPE, KEY_TYPE_TEST, BRANDING_ORG, EMAIL_TYPE, NOTIFICATION_TECHNICAL_FAILURE, \ from app.models import SMS_TYPE, KEY_TYPE_TEST, BRANDING_ORG, EMAIL_TYPE, NOTIFICATION_TECHNICAL_FAILURE, \
NOTIFICATION_SENT, NOTIFICATION_SENDING NOTIFICATION_SENT, NOTIFICATION_SENDING
from app.celery.statistics_tasks import record_initial_job_statistics, create_initial_notification_statistic_tasks
def send_sms_to_provider(notification): def send_sms_to_provider(notification):
service = notification.service service = notification.service
@@ -57,6 +59,8 @@ def send_sms_to_provider(notification):
notification.billable_units = template.fragment_count notification.billable_units = template.fragment_count
update_notification(notification, provider, notification.international) update_notification(notification, provider, notification.international)
create_initial_notification_statistic_tasks(notification)
current_app.logger.info( current_app.logger.info(
"SMS {} sent to provider {} at {}".format(notification.id, provider.get_name(), notification.sent_at) "SMS {} sent to provider {} at {}".format(notification.id, provider.get_name(), notification.sent_at)
) )
@@ -107,6 +111,8 @@ def send_email_to_provider(notification):
notification.reference = reference notification.reference = reference
update_notification(notification, provider) update_notification(notification, provider)
create_initial_notification_statistic_tasks(notification)
current_app.logger.info( current_app.logger.info(
"Email {} sent to provider at {}".format(notification.id, notification.sent_at) "Email {} sent to provider at {}".format(notification.id, notification.sent_at)
) )

View File

@@ -13,7 +13,7 @@ from app.clients.email.aws_ses import get_aws_responses
from app.dao import ( from app.dao import (
notifications_dao notifications_dao
) )
from app.celery.statistics_tasks import create_outcome_notification_statistic_tasks
from app.notifications.process_client_response import validate_callback_data from app.notifications.process_client_response import validate_callback_data
from app.notifications.utils import confirm_subscription from app.notifications.utils import confirm_subscription
@@ -95,6 +95,9 @@ def process_ses_response():
datetime.utcnow(), datetime.utcnow(),
notification.sent_at notification.sent_at
) )
create_outcome_notification_statistic_tasks(notification)
return jsonify( return jsonify(
result="success", message="SES callback succeeded" result="success", message="SES callback succeeded"
), 200 ), 200

View File

@@ -1,7 +1,7 @@
import uuid import uuid
from datetime import datetime from datetime import datetime
from collections import namedtuple from collections import namedtuple
from unittest.mock import ANY from unittest.mock import ANY, call
import pytest import pytest
from notifications_utils.recipients import validate_and_format_phone_number from notifications_utils.recipients import validate_and_format_phone_number
@@ -18,8 +18,7 @@ from app.models import (
KEY_TYPE_TEST, KEY_TYPE_TEST,
KEY_TYPE_TEAM, KEY_TYPE_TEAM,
BRANDING_ORG, BRANDING_ORG,
BRANDING_BOTH, BRANDING_BOTH)
ProviderDetails)
from tests.app.db import create_service, create_template, create_notification from tests.app.db import create_service, create_template, create_notification
@@ -64,6 +63,7 @@ def test_should_send_personalised_template_to_correct_sms_provider_and_persist(
status='created') status='created')
mocker.patch('app.mmg_client.send_sms') mocker.patch('app.mmg_client.send_sms')
stats_mock = mocker.patch('app.delivery.send_to_providers.create_initial_notification_statistic_tasks')
send_to_providers.send_sms_to_provider( send_to_providers.send_sms_to_provider(
db_notification db_notification
@@ -75,6 +75,9 @@ def test_should_send_personalised_template_to_correct_sms_provider_and_persist(
reference=str(db_notification.id), reference=str(db_notification.id),
sender=None sender=None
) )
stats_mock.assert_called_once_with(db_notification)
notification = Notification.query.filter_by(id=db_notification.id).one() notification = Notification.query.filter_by(id=db_notification.id).one()
assert notification.status == 'sending' assert notification.status == 'sending'
@@ -95,6 +98,7 @@ def test_should_send_personalised_template_to_correct_email_provider_and_persist
) )
mocker.patch('app.aws_ses_client.send_email', return_value='reference') mocker.patch('app.aws_ses_client.send_email', return_value='reference')
stats_mock = mocker.patch('app.delivery.send_to_providers.create_initial_notification_statistic_tasks')
send_to_providers.send_email_to_provider( send_to_providers.send_email_to_provider(
db_notification db_notification
@@ -108,6 +112,8 @@ def test_should_send_personalised_template_to_correct_email_provider_and_persist
html_body=ANY, html_body=ANY,
reply_to_address=None reply_to_address=None
) )
stats_mock.assert_called_once_with(db_notification)
assert '<!DOCTYPE html' in app.aws_ses_client.send_email.call_args[1]['html_body'] assert '<!DOCTYPE html' in app.aws_ses_client.send_email.call_args[1]['html_body']
assert '&lt;em&gt;some HTML&lt;/em&gt;' in app.aws_ses_client.send_email.call_args[1]['html_body'] assert '&lt;em&gt;some HTML&lt;/em&gt;' in app.aws_ses_client.send_email.call_args[1]['html_body']
@@ -118,17 +124,29 @@ def test_should_send_personalised_template_to_correct_email_provider_and_persist
assert notification.personalisation == {"name": "Jo"} assert notification.personalisation == {"name": "Jo"}
@pytest.mark.parametrize("client_send", def test_should_not_send_email_message_when_service_is_inactive_notifcation_is_in_tech_failure(
["app.aws_ses_client.send_email", sample_service, sample_notification, mocker
"app.mmg_client.send_sms", ):
"app.firetext_client.send_sms"])
def test_should_not_send_message_when_service_is_inactive_notiifcation_is_in_tech_failure(
sample_service, sample_notification, mocker, client_send):
sample_service.active = False sample_service.active = False
send_mock = mocker.patch(client_send, return_value='reference') send_mock = mocker.patch("app.aws_ses_client.send_email", return_value='reference')
stats_mock = mocker.patch('app.delivery.send_to_providers.create_initial_notification_statistic_tasks')
send_to_providers.send_email_to_provider(sample_notification) send_to_providers.send_email_to_provider(sample_notification)
send_mock.assert_not_called() send_mock.assert_not_called()
stats_mock.assert_not_called()
assert Notification.query.get(sample_notification.id).status == 'technical-failure'
@pytest.mark.parametrize("client_send", ["app.mmg_client.send_sms", "app.firetext_client.send_sms"])
def test_should_not_send_sms_message_when_service_is_inactive_notifcation_is_in_tech_failure(
sample_service, sample_notification, mocker, client_send):
sample_service.active = False
send_mock = mocker.patch(client_send, return_value='reference')
stats_mock = mocker.patch('app.delivery.send_to_providers.create_initial_notification_statistic_tasks')
send_to_providers.send_sms_to_provider(sample_notification)
send_mock.assert_not_called()
stats_mock.assert_not_called()
assert Notification.query.get(sample_notification.id).status == 'technical-failure' assert Notification.query.get(sample_notification.id).status == 'technical-failure'
@@ -138,6 +156,8 @@ def test_send_sms_should_use_template_version_from_notification_not_latest(
db_notification = create_notification(template=sample_template, to_field='+447234123123', status='created') db_notification = create_notification(template=sample_template, to_field='+447234123123', status='created')
mocker.patch('app.mmg_client.send_sms') mocker.patch('app.mmg_client.send_sms')
mocker.patch('app.delivery.send_to_providers.create_initial_notification_statistic_tasks')
version_on_notification = sample_template.version version_on_notification = sample_template.version
# Change the template # Change the template
@@ -171,10 +191,12 @@ def test_send_sms_should_use_template_version_from_notification_not_latest(
(True, KEY_TYPE_NORMAL), (True, KEY_TYPE_NORMAL),
(False, KEY_TYPE_TEST) (False, KEY_TYPE_TEST)
]) ])
def test_should_call_send_sms_response_task_if_research_mode(notify_db, sample_service, sample_notification, mocker, def test_should_call_send_sms_response_task_if_research_mode(
research_mode, key_type): notify_db, sample_service, sample_notification, mocker, research_mode, key_type
):
mocker.patch('app.mmg_client.send_sms') mocker.patch('app.mmg_client.send_sms')
mocker.patch('app.delivery.send_to_providers.send_sms_response') mocker.patch('app.delivery.send_to_providers.send_sms_response')
stats_mock = mocker.patch('app.delivery.send_to_providers.create_initial_notification_statistic_tasks')
if research_mode: if research_mode:
sample_service.research_mode = True sample_service.research_mode = True
@@ -187,6 +209,8 @@ def test_should_call_send_sms_response_task_if_research_mode(notify_db, sample_s
sample_notification sample_notification
) )
assert not mmg_client.send_sms.called assert not mmg_client.send_sms.called
stats_mock.assert_called_once_with(sample_notification)
app.delivery.send_to_providers.send_sms_response.assert_called_once_with( app.delivery.send_to_providers.send_sms_response.assert_called_once_with(
'mmg', str(sample_notification.id), sample_notification.to 'mmg', str(sample_notification.id), sample_notification.to
) )
@@ -209,6 +233,8 @@ def test_should_set_billable_units_to_zero_in_research_mode_or_test_key(
mocker.patch('app.mmg_client.send_sms') mocker.patch('app.mmg_client.send_sms')
mocker.patch('app.delivery.send_to_providers.send_sms_response') mocker.patch('app.delivery.send_to_providers.send_sms_response')
stats_mock = mocker.patch('app.delivery.send_to_providers.create_initial_notification_statistic_tasks')
if research_mode: if research_mode:
sample_service.research_mode = True sample_service.research_mode = True
notify_db.session.add(sample_service) notify_db.session.add(sample_service)
@@ -218,7 +244,7 @@ def test_should_set_billable_units_to_zero_in_research_mode_or_test_key(
send_to_providers.send_sms_to_provider( send_to_providers.send_sms_to_provider(
sample_notification sample_notification
) )
stats_mock.assert_called_once_with(sample_notification)
assert notifications_dao.get_notification_by_id(sample_notification.id).billable_units == 0 assert notifications_dao.get_notification_by_id(sample_notification.id).billable_units == 0
@@ -228,14 +254,16 @@ def test_should_not_send_to_provider_when_status_is_not_created(
): ):
notification = create_notification(template=sample_template, status='sending') notification = create_notification(template=sample_template, status='sending')
mocker.patch('app.mmg_client.send_sms') mocker.patch('app.mmg_client.send_sms')
mocker.patch('app.delivery.send_to_providers.send_sms_response') response_mock = mocker.patch('app.delivery.send_to_providers.send_sms_response')
stats_mock = mocker.patch('app.delivery.send_to_providers.create_initial_notification_statistic_tasks')
send_to_providers.send_sms_to_provider( send_to_providers.send_sms_to_provider(
notification notification
) )
app.mmg_client.send_sms.assert_not_called() app.mmg_client.send_sms.assert_not_called()
app.delivery.send_to_providers.send_sms_response.assert_not_called() response_mock.assert_not_called()
stats_mock.assert_not_called()
def test_should_send_sms_sender_from_service_if_present( def test_should_send_sms_sender_from_service_if_present(
@@ -249,6 +277,7 @@ def test_should_send_sms_sender_from_service_if_present(
sample_service.sms_sender = 'elevenchars' sample_service.sms_sender = 'elevenchars'
mocker.patch('app.mmg_client.send_sms') mocker.patch('app.mmg_client.send_sms')
mocker.patch('app.delivery.send_to_providers.create_initial_notification_statistic_tasks')
send_to_providers.send_sms_to_provider( send_to_providers.send_sms_to_provider(
db_notification db_notification
@@ -276,6 +305,7 @@ def test_should_send_sms_with_downgraded_content(notify_db_session, mocker):
) )
mocker.patch('app.mmg_client.send_sms') mocker.patch('app.mmg_client.send_sms')
mocker.patch('app.delivery.send_to_providers.create_initial_notification_statistic_tasks')
send_to_providers.send_sms_to_provider(db_notification) send_to_providers.send_sms_to_provider(db_notification)
@@ -308,14 +338,16 @@ def test_send_email_to_provider_should_call_research_mode_task_response_task_if_
mocker.patch('app.uuid.uuid4', return_value=reference) mocker.patch('app.uuid.uuid4', return_value=reference)
mocker.patch('app.aws_ses_client.send_email') mocker.patch('app.aws_ses_client.send_email')
mocker.patch('app.delivery.send_to_providers.send_email_response') mocker.patch('app.delivery.send_to_providers.send_email_response')
stats_mock = mocker.patch('app.delivery.send_to_providers.create_initial_notification_statistic_tasks')
send_to_providers.send_email_to_provider( send_to_providers.send_email_to_provider(
notification notification
) )
assert not app.aws_ses_client.send_email.called assert not app.aws_ses_client.send_email.called
stats_mock.assert_called_once_with(notification)
app.delivery.send_to_providers.send_email_response.assert_called_once_with('ses', str(reference), 'john@smith.com') app.delivery.send_to_providers.send_email_response.assert_called_once_with('ses', str(reference), 'john@smith.com')
persisted_notification = Notification.query.filter_by(id=notification.id).one() persisted_notification = Notification.query.filter_by(id=notification.id).one()
assert persisted_notification.to == 'john@smith.com' assert persisted_notification.to == 'john@smith.com'
assert persisted_notification.template_id == sample_email_template.id assert persisted_notification.template_id == sample_email_template.id
assert persisted_notification.status == 'sending' assert persisted_notification.status == 'sending'
@@ -333,11 +365,12 @@ def test_send_email_to_provider_should_not_send_to_provider_when_status_is_not_c
notification = create_notification(template=sample_email_template, status='sending') notification = create_notification(template=sample_email_template, status='sending')
mocker.patch('app.aws_ses_client.send_email') mocker.patch('app.aws_ses_client.send_email')
mocker.patch('app.delivery.send_to_providers.send_email_response') mocker.patch('app.delivery.send_to_providers.send_email_response')
stats_mock = mocker.patch('app.delivery.send_to_providers.create_initial_notification_statistic_tasks')
send_to_providers.send_sms_to_provider( send_to_providers.send_sms_to_provider(
notification notification
) )
stats_mock.assert_not_called()
app.aws_ses_client.send_email.assert_not_called() app.aws_ses_client.send_email.assert_not_called()
app.delivery.send_to_providers.send_email_response.assert_not_called() app.delivery.send_to_providers.send_email_response.assert_not_called()
@@ -347,6 +380,7 @@ def test_send_email_should_use_service_reply_to_email(
sample_email_template, sample_email_template,
mocker): mocker):
mocker.patch('app.aws_ses_client.send_email', return_value='reference') mocker.patch('app.aws_ses_client.send_email', return_value='reference')
mocker.patch('app.delivery.send_to_providers.create_initial_notification_statistic_tasks')
db_notification = create_notification(template=sample_email_template) db_notification = create_notification(template=sample_email_template)
sample_service.reply_to_email_address = 'foo@bar.com' sample_service.reply_to_email_address = 'foo@bar.com'
@@ -430,6 +464,7 @@ def test_get_logo_url_works_for_different_environments(base_url, expected_url):
def test_should_not_set_billable_units_if_research_mode(notify_db, sample_service, sample_notification, mocker): def test_should_not_set_billable_units_if_research_mode(notify_db, sample_service, sample_notification, mocker):
mocker.patch('app.mmg_client.send_sms') mocker.patch('app.mmg_client.send_sms')
mocker.patch('app.delivery.send_to_providers.send_sms_response') mocker.patch('app.delivery.send_to_providers.send_sms_response')
stats_mock = mocker.patch('app.delivery.send_to_providers.create_initial_notification_statistic_tasks')
sample_service.research_mode = True sample_service.research_mode = True
notify_db.session.add(sample_service) notify_db.session.add(sample_service)
@@ -438,7 +473,7 @@ def test_should_not_set_billable_units_if_research_mode(notify_db, sample_servic
send_to_providers.send_sms_to_provider( send_to_providers.send_sms_to_provider(
sample_notification sample_notification
) )
stats_mock.assert_called_once_with(sample_notification)
persisted_notification = notifications_dao.get_notification_by_id(sample_notification.id) persisted_notification = notifications_dao.get_notification_by_id(sample_notification.id)
assert persisted_notification.billable_units == 0 assert persisted_notification.billable_units == 0
@@ -451,15 +486,19 @@ def test_should_not_set_billable_units_if_research_mode(notify_db, sample_servic
(True, KEY_TYPE_TEAM, 0), (True, KEY_TYPE_TEAM, 0),
(False, KEY_TYPE_TEAM, 1) (False, KEY_TYPE_TEAM, 1)
]) ])
def test_should_update_billable_units_according_to_research_mode_and_key_type(notify_db, def test_should_update_billable_units_according_to_research_mode_and_key_type(
sample_service, notify_db,
sample_notification, sample_service,
mocker, sample_notification,
research_mode, mocker,
key_type, research_mode,
billable_units): key_type,
billable_units
):
mocker.patch('app.mmg_client.send_sms') mocker.patch('app.mmg_client.send_sms')
mocker.patch('app.delivery.send_to_providers.send_sms_response') mocker.patch('app.delivery.send_to_providers.send_sms_response')
stats_mock = mocker.patch('app.delivery.send_to_providers.create_initial_notification_statistic_tasks')
if research_mode: if research_mode:
sample_service.research_mode = True sample_service.research_mode = True
notify_db.session.add(sample_service) notify_db.session.add(sample_service)
@@ -470,7 +509,7 @@ def test_should_update_billable_units_according_to_research_mode_and_key_type(no
send_to_providers.send_sms_to_provider( send_to_providers.send_sms_to_provider(
sample_notification sample_notification
) )
stats_mock.assert_called_once_with(sample_notification)
assert sample_notification.billable_units == billable_units assert sample_notification.billable_units == billable_units
@@ -500,6 +539,7 @@ def test_should_send_sms_to_international_providers(
mocker.patch('app.mmg_client.send_sms') mocker.patch('app.mmg_client.send_sms')
mocker.patch('app.firetext_client.send_sms') mocker.patch('app.firetext_client.send_sms')
stats_mock = mocker.patch('app.delivery.send_to_providers.create_initial_notification_statistic_tasks')
send_to_providers.send_sms_to_provider( send_to_providers.send_sms_to_provider(
db_notification_uk db_notification_uk
@@ -526,6 +566,11 @@ def test_should_send_sms_to_international_providers(
notification_uk = Notification.query.filter_by(id=db_notification_uk.id).one() notification_uk = Notification.query.filter_by(id=db_notification_uk.id).one()
notification_int = Notification.query.filter_by(id=db_notification_international.id).one() notification_int = Notification.query.filter_by(id=db_notification_international.id).one()
stats_mock.assert_has_calls([
call(db_notification_uk),
call(db_notification_international)
])
assert notification_uk.status == 'sending' assert notification_uk.status == 'sending'
assert notification_uk.sent_by == 'firetext' assert notification_uk.sent_by == 'firetext'
assert notification_int.status == 'sent' assert notification_int.status == 'sent'
@@ -545,6 +590,7 @@ def test_should_send_international_sms_with_formatted_phone_number(
send_notification_mock = mocker.patch('app.mmg_client.send_sms') send_notification_mock = mocker.patch('app.mmg_client.send_sms')
mocker.patch('app.delivery.send_to_providers.send_sms_response') mocker.patch('app.delivery.send_to_providers.send_sms_response')
mocker.patch('app.delivery.send_to_providers.create_initial_notification_statistic_tasks')
send_to_providers.send_sms_to_provider( send_to_providers.send_sms_to_provider(
notification notification
@@ -566,6 +612,7 @@ def test_should_set_international_phone_number_to_sent_status(
mocker.patch('app.mmg_client.send_sms') mocker.patch('app.mmg_client.send_sms')
mocker.patch('app.delivery.send_to_providers.send_sms_response') mocker.patch('app.delivery.send_to_providers.send_sms_response')
mocker.patch('app.delivery.send_to_providers.create_initial_notification_statistic_tasks')
send_to_providers.send_sms_to_provider( send_to_providers.send_sms_to_provider(
notification notification

View File

@@ -1,4 +1,5 @@
from datetime import datetime from datetime import datetime
from unittest.mock import call
from flask import json from flask import json
from freezegun import freeze_time from freezegun import freeze_time
@@ -19,7 +20,9 @@ def test_ses_callback_should_not_need_auth(client):
assert response.status_code == 404 assert response.status_code == 404
def test_ses_callback_should_fail_if_invalid_json(client): def test_ses_callback_should_fail_if_invalid_json(client, mocker):
stats_mock = mocker.patch('app.notifications.notifications_ses_callback.create_outcome_notification_statistic_tasks')
response = client.post( response = client.post(
path='/notifications/email/ses', path='/notifications/email/ses',
data="nonsense", data="nonsense",
@@ -29,9 +32,12 @@ def test_ses_callback_should_fail_if_invalid_json(client):
assert response.status_code == 400 assert response.status_code == 400
assert json_resp['result'] == 'error' assert json_resp['result'] == 'error'
assert json_resp['message'] == 'SES callback failed: invalid json' assert json_resp['message'] == 'SES callback failed: invalid json'
stats_mock.assert_not_called()
def test_ses_callback_should_autoconfirm_subscriptions(client, rmock): def test_ses_callback_should_autoconfirm_subscriptions(client, rmock, mocker):
stats_mock = mocker.patch('app.notifications.notifications_ses_callback.create_outcome_notification_statistic_tasks')
endpoint = json.loads(ses_confirmation_callback())['SubscribeURL'] endpoint = json.loads(ses_confirmation_callback())['SubscribeURL']
rmock.request( rmock.request(
"GET", "GET",
@@ -51,9 +57,12 @@ def test_ses_callback_should_autoconfirm_subscriptions(client, rmock):
assert response.status_code == 200 assert response.status_code == 200
assert json_resp['result'] == 'success' assert json_resp['result'] == 'success'
assert json_resp['message'] == 'SES callback succeeded' assert json_resp['message'] == 'SES callback succeeded'
stats_mock.assert_not_called()
def test_ses_callback_autoconfirm_raises_exception_if_not_200(client, rmock): def test_ses_callback_autoconfirm_raises_exception_if_not_200(client, rmock, mocker):
stats_mock = mocker.patch('app.notifications.notifications_ses_callback.create_outcome_notification_statistic_tasks')
endpoint = json.loads(ses_confirmation_callback())['SubscribeURL'] endpoint = json.loads(ses_confirmation_callback())['SubscribeURL']
rmock.request( rmock.request(
"GET", "GET",
@@ -71,9 +80,12 @@ def test_ses_callback_autoconfirm_raises_exception_if_not_200(client, rmock):
assert rmock.called assert rmock.called
assert rmock.request_history[0].url == endpoint assert rmock.request_history[0].url == endpoint
assert exc.value.response.status_code == 405 assert exc.value.response.status_code == 405
stats_mock.assert_not_called()
def test_ses_callback_should_fail_if_invalid_notification_type(client): def test_ses_callback_should_fail_if_invalid_notification_type(client, mocker):
stats_mock = mocker.patch('app.notifications.notifications_ses_callback.create_outcome_notification_statistic_tasks')
response = client.post( response = client.post(
path='/notifications/email/ses', path='/notifications/email/ses',
data=ses_invalid_notification_type_callback(), data=ses_invalid_notification_type_callback(),
@@ -83,9 +95,12 @@ def test_ses_callback_should_fail_if_invalid_notification_type(client):
assert response.status_code == 400 assert response.status_code == 400
assert json_resp['result'] == 'error' assert json_resp['result'] == 'error'
assert json_resp['message'] == 'SES callback failed: status Unknown not found' assert json_resp['message'] == 'SES callback failed: status Unknown not found'
stats_mock.assert_not_called()
def test_ses_callback_should_fail_if_missing_message_id(client): def test_ses_callback_should_fail_if_missing_message_id(client, mocker):
stats_mock = mocker.patch('app.notifications.notifications_ses_callback.create_outcome_notification_statistic_tasks')
response = client.post( response = client.post(
path='/notifications/email/ses', path='/notifications/email/ses',
data=ses_missing_notification_id_callback(), data=ses_missing_notification_id_callback(),
@@ -95,9 +110,12 @@ def test_ses_callback_should_fail_if_missing_message_id(client):
assert response.status_code == 400 assert response.status_code == 400
assert json_resp['result'] == 'error' assert json_resp['result'] == 'error'
assert json_resp['message'] == 'SES callback failed: messageId missing' assert json_resp['message'] == 'SES callback failed: messageId missing'
stats_mock.assert_not_called()
def test_ses_callback_should_fail_if_notification_cannot_be_found(notify_db, notify_db_session, client): def test_ses_callback_should_fail_if_notification_cannot_be_found(notify_db, notify_db_session, client, mocker):
stats_mock = mocker.patch('app.notifications.notifications_ses_callback.create_outcome_notification_statistic_tasks')
response = client.post( response = client.post(
path='/notifications/email/ses', path='/notifications/email/ses',
data=ses_invalid_notification_id_callback(), data=ses_invalid_notification_id_callback(),
@@ -107,6 +125,7 @@ def test_ses_callback_should_fail_if_notification_cannot_be_found(notify_db, not
assert response.status_code == 404 assert response.status_code == 404
assert json_resp['result'] == 'error' assert json_resp['result'] == 'error'
assert json_resp['message'] == 'SES callback failed: notification either not found or already updated from sending. Status delivered for notification reference missing' # noqa assert json_resp['message'] == 'SES callback failed: notification either not found or already updated from sending. Status delivered for notification reference missing' # noqa
stats_mock.assert_not_called()
def test_ses_callback_should_update_notification_status( def test_ses_callback_should_update_notification_status(
@@ -118,6 +137,10 @@ def test_ses_callback_should_update_notification_status(
with freeze_time('2001-01-01T12:00:00'): with freeze_time('2001-01-01T12:00:00'):
mocker.patch('app.statsd_client.incr') mocker.patch('app.statsd_client.incr')
mocker.patch('app.statsd_client.timing_with_dates') mocker.patch('app.statsd_client.timing_with_dates')
stats_mock = mocker.patch(
'app.notifications.notifications_ses_callback.create_outcome_notification_statistic_tasks'
)
notification = create_sample_notification( notification = create_sample_notification(
notify_db, notify_db,
notify_db_session, notify_db_session,
@@ -143,6 +166,7 @@ def test_ses_callback_should_update_notification_status(
"callback.ses.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") statsd_client.incr.assert_any_call("callback.ses.delivered")
stats_mock.assert_called_once_with(notification)
def test_ses_callback_should_update_multiple_notification_status_sent( def test_ses_callback_should_update_multiple_notification_status_sent(
@@ -151,6 +175,11 @@ def test_ses_callback_should_update_multiple_notification_status_sent(
notify_db_session, notify_db_session,
sample_email_template, sample_email_template,
mocker): mocker):
stats_mock = mocker.patch(
'app.notifications.notifications_ses_callback.create_outcome_notification_statistic_tasks'
)
notification1 = create_sample_notification( notification1 = create_sample_notification(
notify_db, notify_db,
notify_db_session, notify_db_session,
@@ -194,12 +223,23 @@ def test_ses_callback_should_update_multiple_notification_status_sent(
assert resp1.status_code == 200 assert resp1.status_code == 200
assert resp2.status_code == 200 assert resp2.status_code == 200
assert resp3.status_code == 200 assert resp3.status_code == 200
stats_mock.assert_has_calls([
call(notification1),
call(notification2),
call(notification3)
])
def test_ses_callback_should_set_status_to_temporary_failure(client, def test_ses_callback_should_set_status_to_temporary_failure(client,
notify_db, notify_db,
notify_db_session, notify_db_session,
sample_email_template): sample_email_template,
mocker):
stats_mock = mocker.patch(
'app.notifications.notifications_ses_callback.create_outcome_notification_statistic_tasks'
)
notification = create_sample_notification( notification = create_sample_notification(
notify_db, notify_db,
notify_db_session, notify_db_session,
@@ -220,12 +260,18 @@ def test_ses_callback_should_set_status_to_temporary_failure(client,
assert json_resp['result'] == 'success' assert json_resp['result'] == 'success'
assert json_resp['message'] == 'SES callback succeeded' assert json_resp['message'] == 'SES callback succeeded'
assert get_notification_by_id(notification.id).status == 'temporary-failure' assert get_notification_by_id(notification.id).status == 'temporary-failure'
stats_mock.assert_called_once_with(notification)
def test_ses_callback_should_not_set_status_once_status_is_delivered(client, def test_ses_callback_should_not_set_status_once_status_is_delivered(client,
notify_db, notify_db,
notify_db_session, notify_db_session,
sample_email_template): sample_email_template,
mocker):
stats_mock = mocker.patch(
'app.notifications.notifications_ses_callback.create_outcome_notification_statistic_tasks'
)
notification = create_sample_notification( notification = create_sample_notification(
notify_db, notify_db,
notify_db_session, notify_db_session,
@@ -247,12 +293,18 @@ def test_ses_callback_should_not_set_status_once_status_is_delivered(client,
assert json_resp['result'] == 'error' assert json_resp['result'] == 'error'
assert json_resp['message'] == 'SES callback failed: notification either not found or already updated from sending. Status temporary-failure for notification reference ref' # noqa assert json_resp['message'] == 'SES callback failed: notification either not found or already updated from sending. Status temporary-failure for notification reference ref' # noqa
assert get_notification_by_id(notification.id).status == 'delivered' assert get_notification_by_id(notification.id).status == 'delivered'
stats_mock.assert_not_called()
def test_ses_callback_should_set_status_to_permanent_failure(client, def test_ses_callback_should_set_status_to_permanent_failure(client,
notify_db, notify_db,
notify_db_session, notify_db_session,
sample_email_template): sample_email_template,
mocker):
stats_mock = mocker.patch(
'app.notifications.notifications_ses_callback.create_outcome_notification_statistic_tasks'
)
notification = create_sample_notification( notification = create_sample_notification(
notify_db, notify_db,
notify_db_session, notify_db_session,
@@ -274,6 +326,7 @@ def test_ses_callback_should_set_status_to_permanent_failure(client,
assert json_resp['result'] == 'success' assert json_resp['result'] == 'success'
assert json_resp['message'] == 'SES callback succeeded' assert json_resp['message'] == 'SES callback succeeded'
assert get_notification_by_id(notification.id).status == 'permanent-failure' assert get_notification_by_id(notification.id).status == 'permanent-failure'
stats_mock.assert_called_once_with(notification)
def ses_notification_callback(ref='ref'): def ses_notification_callback(ref='ref'):