def test_should_not_retry_on_non_retryable_exception(client, mocker, notification): notification.notification_type = EMAIL_TYPE mocker.patch( 'app.celery.contact_information_tasks.get_notification_by_id', return_value=notification ) mocked_va_profile_client = mocker.Mock(VAProfileClient) exception = VAProfileNonRetryableException mocked_va_profile_client.get_email = mocker.Mock(side_effect=exception) mocker.patch( 'app.celery.contact_information_tasks.va_profile_client', new=mocked_va_profile_client ) mocked_update_notification_status_by_id = mocker.patch( 'app.celery.contact_information_tasks.update_notification_status_by_id' ) mocked_retry = mocker.patch('app.celery.contact_information_tasks.lookup_contact_info.retry') with pytest.raises(NotificationTechnicalFailureException): lookup_contact_info(notification.id) mocked_va_profile_client.get_email.assert_called_with(EXAMPLE_VA_PROFILE_ID) mocked_update_notification_status_by_id.assert_called_with( notification.id, NOTIFICATION_TECHNICAL_FAILURE, status_reason=exception.failure_reason ) mocked_retry.assert_not_called()
def test_exception_sets_failure_reason_if_thrown( client, mocker, notification, exception, throws_additional_exception, notification_status, exception_reason ): notification.notification_type = EMAIL_TYPE mocker.patch( 'app.celery.contact_information_tasks.get_notification_by_id', return_value=notification ) mocked_va_profile_client = mocker.Mock(VAProfileClient) mocked_va_profile_client.get_email = mocker.Mock(side_effect=exception) mocker.patch( 'app.celery.contact_information_tasks.va_profile_client', new=mocked_va_profile_client ) mocked_update_notification_status_by_id = mocker.patch( 'app.celery.contact_information_tasks.update_notification_status_by_id' ) mocker.patch( 'app.celery.contact_information_tasks.lookup_contact_info.retry', side_effect=lookup_contact_info.MaxRetriesExceededError ) if throws_additional_exception: with pytest.raises(NotificationTechnicalFailureException): lookup_contact_info(notification.id) else: lookup_contact_info(notification.id) mocked_update_notification_status_by_id.assert_called_once_with( notification.id, notification_status, status_reason=exception_reason )
def test_should_update_notification_to_permanent_failure_on_no_contact_info_exception(client, mocker, notification): notification.notification_type = EMAIL_TYPE mocker.patch( 'app.celery.contact_information_tasks.get_notification_by_id', return_value=notification ) mocked_va_profile_client = mocker.Mock(VAProfileClient) mocked_va_profile_client.get_email = mocker.Mock(side_effect=NoContactInfoException('some error')) mocker.patch( 'app.celery.contact_information_tasks.va_profile_client', new=mocked_va_profile_client ) mocked_update_notification_status_by_id = mocker.patch( 'app.celery.contact_information_tasks.update_notification_status_by_id' ) mocked_request = mocker.Mock() mocked_chain = mocker.PropertyMock() mocked_chain.return_value = ['some-task-to-be-executed-next'] type(mocked_request).chain = mocked_chain mocker.patch( 'celery.app.task.Task.request', new=mocked_request ) lookup_contact_info(notification.id) mocked_va_profile_client.get_email.assert_called_with(EXAMPLE_VA_PROFILE_ID) mocked_update_notification_status_by_id.assert_called_with(notification.id, NOTIFICATION_PERMANENT_FAILURE) mocked_chain.assert_called_with(None)
def test_should_update_notification_to_technical_failure_on_max_retries(client, mocker, notification): notification.notification_type = EMAIL_TYPE mocker.patch( 'app.celery.contact_information_tasks.get_notification_by_id', return_value=notification ) mocked_va_profile_client = mocker.Mock(VAProfileClient) mocked_va_profile_client.get_email = mocker.Mock(side_effect=VAProfileRetryableException('some error')) mocker.patch( 'app.celery.contact_information_tasks.va_profile_client', new=mocked_va_profile_client ) mocked_update_notification_status_by_id = mocker.patch( 'app.celery.contact_information_tasks.update_notification_status_by_id' ) mocker.patch( 'app.celery.contact_information_tasks.lookup_contact_info.retry', side_effect=lookup_contact_info.MaxRetriesExceededError ) with pytest.raises(NotificationTechnicalFailureException): lookup_contact_info(notification.id) mocked_va_profile_client.get_email.assert_called_with(EXAMPLE_VA_PROFILE_ID) mocked_update_notification_status_by_id.assert_called_with(notification.id, NOTIFICATION_TECHNICAL_FAILURE)
def test_should_log_message_for_contact_information_tasks(client, mocker): mock_logger = mocker.patch( 'app.celery.contact_information_tasks.current_app.logger.info') notification_id = uuid.uuid4() lookup_contact_info(notification_id) mock_logger.assert_called_with( 'This task will look up contact information.') lookup_va_profile_id(notification_id) mock_logger.assert_called_with('This task will look up VA Profile ID.')
def test_should_retry_on_retryable_exception(client, mocker, notification): notification.notification_type = EMAIL_TYPE mocker.patch( 'app.celery.contact_information_tasks.get_notification_by_id', return_value=notification ) mocked_va_profile_client = mocker.Mock(VAProfileClient) mocked_va_profile_client.get_email = mocker.Mock(side_effect=VAProfileRetryableException('some error')) mocker.patch( 'app.celery.contact_information_tasks.va_profile_client', new=mocked_va_profile_client ) mocked_retry = mocker.patch('app.celery.contact_information_tasks.lookup_contact_info.retry') lookup_contact_info(notification.id) mocked_va_profile_client.get_email.assert_called_with(EXAMPLE_VA_PROFILE_ID) mocked_retry.assert_called()
def test_should_get_phone_number_and_update_notification(client, mocker, notification): notification.notification_type = SMS_TYPE mocked_get_notification_by_id = mocker.patch( 'app.celery.contact_information_tasks.get_notification_by_id', return_value=notification ) mocked_va_profile_client = mocker.Mock(VAProfileClient) mocked_va_profile_client.get_telephone = mocker.Mock(return_value='+15555555555') mocker.patch( 'app.celery.contact_information_tasks.va_profile_client', new=mocked_va_profile_client ) mocked_update_notification = mocker.patch( 'app.celery.contact_information_tasks.dao_update_notification' ) lookup_contact_info(notification.id) mocked_get_notification_by_id.assert_called() mocked_va_profile_client.get_telephone.assert_called_with(EXAMPLE_VA_PROFILE_ID) mocked_update_notification.assert_called_with(notification) assert notification.to == '+15555555555'
def test_should_get_email_address_and_update_notification(client, mocker, notification): notification.notification_type = EMAIL_TYPE mocked_get_notification_by_id = mocker.patch( 'app.celery.contact_information_tasks.get_notification_by_id', return_value=notification ) mocked_va_profile_client = mocker.Mock(VAProfileClient) mocked_va_profile_client.get_email = mocker.Mock(return_value='*****@*****.**') mocker.patch( 'app.celery.contact_information_tasks.va_profile_client', new=mocked_va_profile_client ) mocked_update_notification = mocker.patch( 'app.celery.contact_information_tasks.dao_update_notification' ) lookup_contact_info(notification.id) mocked_get_notification_by_id.assert_called() mocked_va_profile_client.get_email.assert_called_with(EXAMPLE_VA_PROFILE_ID) mocked_update_notification.assert_called_with(notification) assert notification.to == '*****@*****.**'