def test_get_current_sms_provider_returns_active_only(restore_provider_details): current_provider = get_current_provider("sms") current_provider.active = False dao_update_provider_details(current_provider) new_current_provider = get_current_provider("sms") assert new_current_provider is None
def test_toggle_sms_provider_switches_provider_stores_notify_user_id_in_history( restore_provider_details, sample_user, mocker ): mocker.patch('app.provider_details.switch_providers.get_user_by_id', return_value=sample_user) old_provider = get_current_provider('sms') dao_toggle_sms_provider(old_provider.identifier) new_provider = get_current_provider('sms') old_provider_from_history = ProviderDetailsHistory.query.filter_by( identifier=old_provider.identifier, version=old_provider.version ).order_by( asc(ProviderDetailsHistory.priority) ).first() new_provider_from_history = ProviderDetailsHistory.query.filter_by( identifier=new_provider.identifier, version=new_provider.version ).order_by( asc(ProviderDetailsHistory.priority) ).first() assert old_provider.version == old_provider_from_history.version assert new_provider.version == new_provider_from_history.version assert new_provider_from_history.created_by_id == sample_user.id assert old_provider_from_history.created_by_id == sample_user.id
def test_switch_providers_on_slow_delivery_switches_once_then_does_not_switch_if_already_switched( notify_api, mocker, prepare_current_provider, sample_user, sample_template): mocker.patch('app.provider_details.switch_providers.get_user_by_id', return_value=sample_user) starting_provider = get_current_provider('sms') _create_slow_delivery_notification(sample_template, starting_provider.identifier) _create_slow_delivery_notification(sample_template, starting_provider.identifier) with set_config_values(notify_api, {'SWITCH_SLOW_SMS_PROVIDER_ENABLED': True}): switch_current_sms_provider_on_slow_delivery() new_provider = get_current_provider('sms') _create_slow_delivery_notification(sample_template, new_provider.identifier) _create_slow_delivery_notification(sample_template, new_provider.identifier) switch_current_sms_provider_on_slow_delivery() final_provider = get_current_provider('sms') assert new_provider.identifier != starting_provider.identifier assert new_provider.priority < starting_provider.priority assert final_provider.identifier == new_provider.identifier
def test_get_current_sms_provider_returns_active_only(restore_provider_details): current_provider = get_current_provider('sms') current_provider.active = False dao_update_provider_details(current_provider) new_current_provider = get_current_provider('sms') assert current_provider.identifier != new_current_provider.identifier
def test_switch_sms_provider_to_inactive_provider_does_not_switch(setup_sms_providers): [inactive_provider, current_provider, _] = setup_sms_providers assert get_current_provider('sms').identifier == current_provider.identifier dao_switch_sms_provider_to_provider_with_identifier(inactive_provider.identifier) new_provider = get_current_provider('sms') assert new_provider.id == current_provider.id assert new_provider.identifier == current_provider.identifier
def test_toggle_sms_provider_switches_provider_stores_notify_user_id(restore_provider_details, sample_user, mocker): mocker.patch("app.provider_details.switch_providers.get_user_by_id", return_value=sample_user) current_provider = get_current_provider("sms") dao_toggle_sms_provider(current_provider.identifier) new_provider = get_current_provider("sms") assert current_provider.identifier != new_provider.identifier assert new_provider.created_by.id == sample_user.id assert new_provider.created_by_id == sample_user.id
def switch_current_sms_provider_on_slow_delivery(): """ Switch providers if at least 30% of notifications took more than four minutes to be delivered in the last ten minutes. Search from the time we last switched to the current provider. """ if not current_app.config['SWITCH_SLOW_SMS_PROVIDER_ENABLED']: current_app.logger.info("Feature SWITCH_SLOW_SMS_PROVIDER is Diabled.") return current_provider = get_current_provider('sms') # TODO: If no provider changes the below lines throws error: # TypeError: '>' not supported between instances of 'NoneType' and 'datetime.datetime' if current_provider.updated_at > datetime.utcnow() - timedelta(minutes=10): current_app.logger.info( "Slow delivery notifications provider switched less than 10 minutes ago." ) return slow_delivery_notifications = is_delivery_slow_for_provider( provider=current_provider.identifier, threshold=0.3, created_at=datetime.utcnow() - timedelta(minutes=10), delivery_time=timedelta(minutes=4), ) if slow_delivery_notifications: current_app.logger.warning( 'Slow delivery notifications detected for provider {}'.format( current_provider.identifier)) dao_toggle_sms_provider(current_provider.identifier)
def test_toggle_sms_provider_switches_provider_stores_notify_user_id_in_history( mocker, sample_user, setup_sms_providers_with_history): [inactive_provider, old_provider, alternative_provider] = setup_sms_providers_with_history mocker.patch('app.provider_details.switch_providers.get_user_by_id', return_value=sample_user) mocker.patch('app.dao.provider_details_dao.get_alternative_sms_provider', return_value=alternative_provider) dao_toggle_sms_provider(old_provider.identifier) new_provider = get_current_provider('sms') old_provider_from_history = ProviderDetailsHistory.query.filter_by( identifier=old_provider.identifier, version=old_provider.version).order_by( asc(ProviderDetailsHistory.priority)).first() new_provider_from_history = ProviderDetailsHistory.query.filter_by( identifier=new_provider.identifier, version=new_provider.version).order_by( asc(ProviderDetailsHistory.priority)).first() assert old_provider.version == old_provider_from_history.version assert new_provider.version == new_provider_from_history.version assert new_provider_from_history.created_by_id == sample_user.id assert old_provider_from_history.created_by_id == sample_user.id
def test_get_current_sms_provider_returns_correct_provider( restore_provider_details): set_primary_sms_provider('twilio') provider = get_current_provider('sms') assert provider.identifier == 'twilio'
def test_update_sms_provider_to_inactive_sets_inactive(restore_provider_details): primary_provider = get_current_provider("sms") primary_provider.active = False dao_update_provider_details(primary_provider) assert not primary_provider.active
def switch_current_sms_provider_on_slow_delivery(): """ Switch providers if there are at least two slow delivery notifications (more than four minutes) in the last ten minutes. Search from the time we last switched to the current provider. """ functional_test_provider_service_id = current_app.config.get( 'FUNCTIONAL_TEST_PROVIDER_SERVICE_ID') functional_test_provider_template_id = current_app.config.get( 'FUNCTIONAL_TEST_PROVIDER_SMS_TEMPLATE_ID') if functional_test_provider_service_id and functional_test_provider_template_id: current_provider = get_current_provider('sms') sent_at = datetime.utcnow() - timedelta(minutes=10) if current_provider.updated_at is not None: sent_at = max(sent_at, current_provider.updated_at) slow_delivery_notifications = is_delivery_slow_for_provider( provider=current_provider.identifier, threshold=2, sent_at=sent_at, delivery_time=timedelta(minutes=4), service_id=functional_test_provider_service_id, template_id=functional_test_provider_template_id) if slow_delivery_notifications: current_app.logger.warning( 'Slow delivery notifications detected for provider {}'.format( current_provider.identifier)) dao_toggle_sms_provider(current_provider.identifier)
def test_switch_sms_provider_to_current_provider_does_not_switch( restore_provider_details, current_sms_provider): dao_switch_sms_provider_to_provider_with_identifier( current_sms_provider.identifier) new_provider = get_current_provider('sms') assert current_sms_provider.id == new_provider.id assert current_sms_provider.identifier == new_provider.identifier
def test_toggle_sms_provider_switches_provider(mocker, restore_provider_details, current_sms_provider, sample_user): mocker.patch("app.provider_details.switch_providers.get_user_by_id", return_value=sample_user) dao_toggle_sms_provider(current_sms_provider.identifier) new_provider = get_current_provider("sms") old_starting_provider = get_provider_details_by_identifier(current_sms_provider.identifier) assert new_provider.identifier != old_starting_provider.identifier assert new_provider.priority < old_starting_provider.priority
def test_switch_providers_on_slow_delivery_does_nothing_if_toggle_is_off( notify_api, mocker, prepare_current_provider, sample_user, sample_template): mocker.patch('app.provider_details.switch_providers.get_user_by_id', return_value=sample_user) starting_provider = get_current_provider('sms') _create_slow_delivery_notification(sample_template, starting_provider.identifier) _create_slow_delivery_notification(sample_template, starting_provider.identifier) with set_config_values(notify_api, {'SWITCH_SLOW_SMS_PROVIDER_ENABLED': False}): switch_current_sms_provider_on_slow_delivery() new_provider = get_current_provider('sms') assert new_provider.identifier == starting_provider.identifier
def test_switch_sms_provider_to_inactive_provider_does_not_switch(restore_provider_details, current_sms_provider): alternative_sms_provider = get_alternative_sms_provider(current_sms_provider.identifier) alternative_sms_provider.active = False dao_update_provider_details(alternative_sms_provider) dao_switch_sms_provider_to_provider_with_identifier(alternative_sms_provider.identifier) new_provider = get_current_provider("sms") assert new_provider.id == current_sms_provider.id assert new_provider.identifier == current_sms_provider.identifier
def test_get_sms_provider_with_equal_priority_returns_provider( restore_provider_details, with_active_telstra_provider): current_provider = get_current_provider('sms') new_provider = get_alternative_sms_provider(current_provider.identifier) current_provider.priority = new_provider.priority dao_update_provider_details(current_provider) conflicting_provider = \ dao_get_sms_provider_with_equal_priority(current_provider.identifier, current_provider.priority) assert conflicting_provider
def test_toggle_sms_provider_switches_provider(mocker, sample_user, setup_sms_providers): [inactive_provider, old_provider, alternative_provider] = setup_sms_providers mocker.patch('app.provider_details.switch_providers.get_user_by_id', return_value=sample_user) mocker.patch('app.dao.provider_details_dao.get_alternative_sms_provider', return_value=alternative_provider) dao_toggle_sms_provider(old_provider.identifier) new_provider = get_current_provider('sms') assert new_provider.identifier != old_provider.identifier assert new_provider.priority < old_provider.priority
def test_toggle_sms_provider_switches_when_provider_priorities_are_equal(mocker, restore_provider_details, sample_user): mocker.patch("app.provider_details.switch_providers.get_user_by_id", return_value=sample_user) current_provider = get_current_provider("sms") new_provider = get_alternative_sms_provider(current_provider.identifier) current_provider.priority = new_provider.priority dao_update_provider_details(current_provider) dao_toggle_sms_provider(current_provider.identifier) old_starting_provider = get_provider_details_by_identifier(current_provider.identifier) assert new_provider.identifier != old_starting_provider.identifier assert new_provider.priority < old_starting_provider.priority
def update_letter_notifications_to_sent_to_dvla(self, notification_references): # This task will be called by the FTP app to update notifications as sent to DVLA provider = get_current_provider(LETTER_TYPE) updated_count, _ = dao_update_notifications_by_reference( notification_references, { 'status': NOTIFICATION_SENDING, 'sent_by': provider.identifier, 'sent_at': datetime.utcnow(), 'updated_at': datetime.utcnow() }) current_app.logger.info( "Updated {} letter notifications to sending".format(updated_count))
def test_toggle_sms_provider_switches_provider_stores_notify_user_id( mocker, sample_user, setup_sms_providers): [inactive_provider, current_provider, alternative_provider] = setup_sms_providers mocker.patch('app.provider_details.switch_providers.get_user_by_id', return_value=sample_user) mocker.patch('app.dao.provider_details_dao.get_alternative_sms_provider', return_value=alternative_provider) dao_toggle_sms_provider(current_provider.identifier) new_provider = get_current_provider('sms') assert current_provider.identifier != new_provider.identifier assert new_provider.created_by.id == sample_user.id assert new_provider.created_by_id == sample_user.id
def switch_current_sms_provider_on_slow_delivery(): """ Switch providers if at least 30% of notifications took more than four minutes to be delivered in the last ten minutes. Search from the time we last switched to the current provider. """ current_provider = get_current_provider("sms") if current_provider.updated_at > datetime.utcnow() - timedelta(minutes=10): current_app.logger.info( "Slow delivery notifications provider switched less than 10 minutes ago." ) return slow_delivery_notifications = is_delivery_slow_for_provider( provider=current_provider.identifier, threshold=0.3, created_at=datetime.utcnow() - timedelta(minutes=10), delivery_time=timedelta(minutes=4), ) if slow_delivery_notifications: current_app.logger.warning( "Slow delivery notifications detected for provider {}".format( current_provider.identifier)) dao_toggle_sms_provider(current_provider.identifier)
def prepare_current_provider(restore_provider_details): initial_provider = get_current_provider('sms') dao_update_provider_details(initial_provider) initial_provider.updated_at = datetime.utcnow() - timedelta(minutes=30) db.session.commit()
def test_get_current_sms_provider_returns_correct_provider(restore_provider_details): provider = get_current_provider("sms") assert provider.identifier == "sns"
def test_get_current_sms_provider_returns_provider_highest_priority_active_provider( setup_sms_providers): provider = get_current_provider('sms') assert provider.identifier == setup_sms_providers[1].identifier