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)
def test_should_technical_error_and_not_retry_if_invalid_email(sample_notification, mocker): mocker.patch('app.delivery.send_to_providers.send_email_to_provider', side_effect=InvalidEmailError('bad email')) mocker.patch('app.celery.provider_tasks.deliver_email.retry') deliver_email(sample_notification.id) assert provider_tasks.deliver_email.retry.called is False assert sample_notification.status == 'technical-failure'
def test_should_go_into_technical_error_if_exceeds_retries_on_deliver_email_task(sample_notification, mocker): mocker.patch('app.delivery.send_to_providers.send_email_to_provider', side_effect=Exception("EXPECTED")) mocker.patch('app.celery.provider_tasks.deliver_email.retry', side_effect=MaxRetriesExceededError()) deliver_email(sample_notification.id) provider_tasks.deliver_email.retry.assert_called_with(queue='retry', countdown=10) assert sample_notification.status == 'technical-failure'
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", countdown=10)
def test_should_call_send_email_to_provider_from_deliver_email_task( notify_db, notify_db_session, 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)
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")
def test_should_go_into_technical_error_if_exceeds_retries_on_deliver_email_task(sample_notification, mocker): mocker.patch('app.delivery.send_to_providers.send_email_to_provider', side_effect=Exception("EXPECTED")) mocker.patch('app.celery.provider_tasks.deliver_email.retry', side_effect=MaxRetriesExceededError()) with pytest.raises(NotificationTechnicalFailureException) as e: deliver_email(sample_notification.id) assert str(sample_notification.id) in str(e.value) provider_tasks.deliver_email.retry.assert_called_with(queue="retry-tasks") assert sample_notification.status == 'technical-failure'
def test_should_technical_error_and_not_retry_if_invalid_provider( sample_notification, mocker): mocker.patch('app.delivery.send_to_providers.send_email_to_provider', side_effect=InvalidProviderException('invalid provider')) mocker.patch('app.celery.provider_tasks.deliver_email.retry') with pytest.raises(NotificationTechnicalFailureException): deliver_email(sample_notification.id) assert provider_tasks.deliver_email.retry.called is False assert sample_notification.status == 'technical-failure'
def test_should_retry_and_log_exception(sample_notification, mocker): 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') deliver_email(sample_notification.id) assert provider_tasks.deliver_email.retry.called is True assert sample_notification.status == 'created'
def test_should_retry_and_log_exception(sample_notification, mocker): 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") deliver_email(sample_notification.id) assert provider_tasks.deliver_email.retry.called is True assert sample_notification.status == "created"
def test_should_technical_error_and_not_retry_if_invalid_email( sample_notification, mocker): mocker.patch( "app.delivery.send_to_providers.send_email_to_provider", side_effect=InvalidEmailError("bad email"), ) mocker.patch("app.celery.provider_tasks.deliver_email.retry") logger = mocker.patch("app.celery.provider_tasks.current_app.logger.info") queued_callback = mocker.patch( "app.celery.provider_tasks._check_and_queue_callback_task") deliver_email(sample_notification.id) assert provider_tasks.deliver_email.retry.called is False assert sample_notification.status == "technical-failure" assert (call( f"Cannot send notification {sample_notification.id}, got an invalid email address: bad email." ) in logger.call_args_list) queued_callback.assert_called_once_with(sample_notification)
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') deliver_email(sample_notification.id) assert provider_tasks.deliver_email.retry.called is True assert sample_notification.status == 'created' assert not mock_logger_exception.called assert mock_logger_warning.called