2018-03-16 17:18:44 +00:00
|
|
|
import pytest
|
2018-03-26 15:24:21 +01:00
|
|
|
from botocore.exceptions import ClientError
|
2016-06-23 09:41:21 +01:00
|
|
|
from celery.exceptions import MaxRetriesExceededError
|
2016-10-13 15:27:47 +01:00
|
|
|
|
|
|
|
|
import app
|
2016-06-13 16:40:46 +01:00
|
|
|
from app.celery import provider_tasks
|
2016-10-13 15:53:01 +01:00
|
|
|
from app.celery.provider_tasks import deliver_sms, deliver_email
|
2021-01-18 15:43:50 +00:00
|
|
|
from app.clients.sms import SmsClientResponseException
|
2020-12-30 17:06:49 +00:00
|
|
|
from app.clients.email import EmailClientNonRetryableException
|
2020-08-13 17:18:19 +01:00
|
|
|
from app.clients.email.aws_ses import AwsSesClientException, AwsSesClientThrottlingSendRateException
|
2018-03-16 17:18:44 +00:00
|
|
|
from app.exceptions import NotificationTechnicalFailureException
|
2016-06-07 11:09:54 +01:00
|
|
|
|
2016-06-13 11:38:25 +01:00
|
|
|
|
2016-08-05 10:44:43 +01:00
|
|
|
def test_should_have_decorated_tasks_functions():
|
2016-09-28 15:05:50 +01:00
|
|
|
assert deliver_sms.__wrapped__.__name__ == 'deliver_sms'
|
|
|
|
|
assert deliver_email.__wrapped__.__name__ == 'deliver_email'
|
2016-08-05 10:44:43 +01:00
|
|
|
|
|
|
|
|
|
2016-09-22 09:52:23 +01:00
|
|
|
def test_should_call_send_sms_to_provider_from_deliver_sms_task(
|
|
|
|
|
sample_notification,
|
|
|
|
|
mocker):
|
|
|
|
|
mocker.patch('app.delivery.send_to_providers.send_sms_to_provider')
|
|
|
|
|
|
|
|
|
|
deliver_sms(sample_notification.id)
|
|
|
|
|
app.delivery.send_to_providers.send_sms_to_provider.assert_called_with(sample_notification)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_add_to_retry_queue_if_notification_not_found_in_deliver_sms_task(
|
|
|
|
|
notify_db_session,
|
|
|
|
|
mocker):
|
|
|
|
|
mocker.patch('app.delivery.send_to_providers.send_sms_to_provider')
|
|
|
|
|
mocker.patch('app.celery.provider_tasks.deliver_sms.retry')
|
|
|
|
|
|
|
|
|
|
notification_id = app.create_uuid()
|
|
|
|
|
|
|
|
|
|
deliver_sms(notification_id)
|
|
|
|
|
app.delivery.send_to_providers.send_sms_to_provider.assert_not_called()
|
2019-08-07 14:27:58 +01:00
|
|
|
app.celery.provider_tasks.deliver_sms.retry.assert_called_with(queue="retry-tasks", countdown=0)
|
2016-09-22 09:52:23 +01:00
|
|
|
|
|
|
|
|
|
2021-01-18 16:25:40 +00:00
|
|
|
def test_send_sms_should_not_switch_providers_on_non_provider_failure(
|
|
|
|
|
sample_notification,
|
|
|
|
|
mocker
|
|
|
|
|
):
|
|
|
|
|
mocker.patch(
|
|
|
|
|
'app.delivery.send_to_providers.send_sms_to_provider',
|
|
|
|
|
side_effect=Exception("Non Provider Exception")
|
|
|
|
|
)
|
|
|
|
|
mock_dao_reduce_sms_provider_priority = mocker.patch(
|
|
|
|
|
'app.delivery.send_to_providers.dao_reduce_sms_provider_priority'
|
|
|
|
|
)
|
|
|
|
|
mocker.patch('app.celery.provider_tasks.deliver_sms.retry')
|
|
|
|
|
|
|
|
|
|
deliver_sms(sample_notification.id)
|
|
|
|
|
|
|
|
|
|
assert mock_dao_reduce_sms_provider_priority.called is False
|
|
|
|
|
|
|
|
|
|
|
2021-01-18 15:43:50 +00:00
|
|
|
def test_should_retry_and_log_warning_if_SmsClientResponseException_for_deliver_sms_task(sample_notification, mocker):
|
|
|
|
|
mocker.patch('app.delivery.send_to_providers.send_sms_to_provider', side_effect=SmsClientResponseException("something went wrong"))
|
|
|
|
|
mocker.patch('app.celery.provider_tasks.deliver_sms.retry')
|
|
|
|
|
mock_logger_warning = mocker.patch('app.celery.tasks.current_app.logger.warning')
|
|
|
|
|
|
|
|
|
|
deliver_sms(sample_notification.id)
|
|
|
|
|
|
|
|
|
|
assert provider_tasks.deliver_sms.retry.called is True
|
|
|
|
|
assert sample_notification.status == 'created'
|
|
|
|
|
assert mock_logger_warning.called
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_retry_and_log_exception_for_non_SmsClientResponseException_exceptions_for_deliver_sms_task(
|
|
|
|
|
sample_notification, mocker
|
|
|
|
|
):
|
|
|
|
|
mocker.patch('app.delivery.send_to_providers.send_sms_to_provider', side_effect=Exception("something went wrong"))
|
|
|
|
|
mocker.patch('app.celery.provider_tasks.deliver_sms.retry')
|
|
|
|
|
mock_logger_exception = mocker.patch('app.celery.tasks.current_app.logger.exception')
|
|
|
|
|
|
|
|
|
|
deliver_sms(sample_notification.id)
|
|
|
|
|
|
|
|
|
|
assert provider_tasks.deliver_sms.retry.called is True
|
|
|
|
|
assert sample_notification.status == 'created'
|
|
|
|
|
assert mock_logger_exception.called
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_go_into_technical_error_if_exceeds_retries_on_deliver_sms_task(sample_notification, mocker):
|
|
|
|
|
mocker.patch('app.delivery.send_to_providers.send_sms_to_provider', side_effect=Exception("EXPECTED"))
|
|
|
|
|
mocker.patch('app.celery.provider_tasks.deliver_sms.retry', side_effect=MaxRetriesExceededError())
|
|
|
|
|
mock_logger_exception = mocker.patch('app.celery.tasks.current_app.logger.exception')
|
|
|
|
|
|
|
|
|
|
with pytest.raises(NotificationTechnicalFailureException) as e:
|
|
|
|
|
deliver_sms(sample_notification.id)
|
|
|
|
|
assert str(sample_notification.id) in str(e.value)
|
|
|
|
|
|
|
|
|
|
provider_tasks.deliver_sms.retry.assert_called_with(queue="retry-tasks", countdown=0)
|
|
|
|
|
|
|
|
|
|
assert sample_notification.status == 'technical-failure'
|
|
|
|
|
assert mock_logger_exception.called
|
|
|
|
|
|
|
|
|
|
|
2021-01-18 16:25:40 +00:00
|
|
|
# end of deliver_sms task tests, now deliver_email task tests
|
|
|
|
|
|
|
|
|
|
|
2021-01-18 15:43:50 +00:00
|
|
|
def test_should_call_send_email_to_provider_from_deliver_email_task(
|
|
|
|
|
sample_notification,
|
|
|
|
|
mocker):
|
|
|
|
|
mocker.patch('app.delivery.send_to_providers.send_email_to_provider')
|
|
|
|
|
|
|
|
|
|
deliver_email(sample_notification.id)
|
|
|
|
|
app.delivery.send_to_providers.send_email_to_provider.assert_called_with(sample_notification)
|
|
|
|
|
|
|
|
|
|
|
2021-01-18 16:25:40 +00:00
|
|
|
def test_should_add_to_retry_queue_if_notification_not_found_in_deliver_email_task(mocker):
|
|
|
|
|
mocker.patch('app.delivery.send_to_providers.send_email_to_provider')
|
|
|
|
|
mocker.patch('app.celery.provider_tasks.deliver_email.retry')
|
|
|
|
|
|
|
|
|
|
notification_id = app.create_uuid()
|
|
|
|
|
|
|
|
|
|
deliver_email(notification_id)
|
|
|
|
|
app.delivery.send_to_providers.send_email_to_provider.assert_not_called()
|
|
|
|
|
app.celery.provider_tasks.deliver_email.retry.assert_called_with(queue="retry-tasks")
|
|
|
|
|
|
|
|
|
|
|
2020-08-13 17:18:19 +01:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
'exception_class', [
|
|
|
|
|
Exception(),
|
|
|
|
|
AwsSesClientException(),
|
|
|
|
|
AwsSesClientThrottlingSendRateException(),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
def test_should_go_into_technical_error_if_exceeds_retries_on_deliver_email_task(
|
|
|
|
|
sample_notification, mocker, exception_class
|
|
|
|
|
):
|
|
|
|
|
mocker.patch('app.delivery.send_to_providers.send_email_to_provider', side_effect=exception_class)
|
2016-09-23 09:54:53 +01:00
|
|
|
mocker.patch('app.celery.provider_tasks.deliver_email.retry', side_effect=MaxRetriesExceededError())
|
2016-07-05 15:48:27 +01:00
|
|
|
|
2018-03-16 17:18:44 +00:00
|
|
|
with pytest.raises(NotificationTechnicalFailureException) as e:
|
|
|
|
|
deliver_email(sample_notification.id)
|
2019-09-13 11:40:05 +01:00
|
|
|
assert str(sample_notification.id) in str(e.value)
|
2016-08-03 16:26:11 +01:00
|
|
|
|
2017-05-25 11:12:40 +01:00
|
|
|
provider_tasks.deliver_email.retry.assert_called_with(queue="retry-tasks")
|
2016-10-13 15:53:01 +01:00
|
|
|
assert sample_notification.status == 'technical-failure'
|
2016-08-03 16:26:11 +01:00
|
|
|
|
2016-10-13 15:27:47 +01:00
|
|
|
|
2020-12-30 17:06:49 +00:00
|
|
|
def test_should_technical_error_and_not_retry_if_EmailClientNonRetryableException(sample_notification, mocker):
|
|
|
|
|
mocker.patch(
|
|
|
|
|
'app.delivery.send_to_providers.send_email_to_provider',
|
|
|
|
|
side_effect=EmailClientNonRetryableException('bad email')
|
|
|
|
|
)
|
2016-10-13 15:27:47 +01:00
|
|
|
mocker.patch('app.celery.provider_tasks.deliver_email.retry')
|
|
|
|
|
|
|
|
|
|
deliver_email(sample_notification.id)
|
|
|
|
|
|
|
|
|
|
assert provider_tasks.deliver_email.retry.called is False
|
2019-08-12 13:51:24 +01:00
|
|
|
assert sample_notification.status == 'technical-failure'
|
2017-01-17 15:46:02 +00:00
|
|
|
|
|
|
|
|
|
2021-01-18 15:43:50 +00:00
|
|
|
def test_should_retry_and_log_exception_for_deliver_email_task(sample_notification, mocker):
|
2018-03-26 15:24:21 +01:00
|
|
|
error_response = {
|
|
|
|
|
'Error': {
|
|
|
|
|
'Code': 'SomeError',
|
|
|
|
|
'Message': 'some error message from amazon',
|
|
|
|
|
'Type': 'Sender'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ex = ClientError(error_response=error_response, operation_name='opname')
|
|
|
|
|
mocker.patch('app.delivery.send_to_providers.send_email_to_provider', side_effect=AwsSesClientException(str(ex)))
|
|
|
|
|
mocker.patch('app.celery.provider_tasks.deliver_email.retry')
|
2020-08-13 17:18:19 +01:00
|
|
|
mock_logger_exception = mocker.patch('app.celery.tasks.current_app.logger.exception')
|
|
|
|
|
|
|
|
|
|
deliver_email(sample_notification.id)
|
|
|
|
|
|
|
|
|
|
assert provider_tasks.deliver_email.retry.called is True
|
|
|
|
|
assert sample_notification.status == 'created'
|
|
|
|
|
assert mock_logger_exception.called
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_if_ses_send_rate_throttle_then_should_retry_and_log_warning(sample_notification, mocker):
|
|
|
|
|
error_response = {
|
|
|
|
|
'Error': {
|
|
|
|
|
'Code': 'Throttling',
|
|
|
|
|
'Message': 'Maximum sending rate exceeded.',
|
|
|
|
|
'Type': 'Sender'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ex = ClientError(error_response=error_response, operation_name='opname')
|
|
|
|
|
mocker.patch(
|
|
|
|
|
'app.delivery.send_to_providers.send_email_to_provider',
|
|
|
|
|
side_effect=AwsSesClientThrottlingSendRateException(str(ex))
|
|
|
|
|
)
|
|
|
|
|
mocker.patch('app.celery.provider_tasks.deliver_email.retry')
|
|
|
|
|
mock_logger_warning = mocker.patch('app.celery.tasks.current_app.logger.warning')
|
|
|
|
|
mock_logger_exception = mocker.patch('app.celery.tasks.current_app.logger.exception')
|
2018-03-26 15:24:21 +01:00
|
|
|
|
|
|
|
|
deliver_email(sample_notification.id)
|
|
|
|
|
|
|
|
|
|
assert provider_tasks.deliver_email.retry.called is True
|
|
|
|
|
assert sample_notification.status == 'created'
|
2020-08-13 17:18:19 +01:00
|
|
|
assert not mock_logger_exception.called
|
|
|
|
|
assert mock_logger_warning.called
|