def _get_per_user_submission_rate_definition(domain): return PerUserRateDefinition( per_user_rate_definition=get_dynamic_rate_definition( 'submissions_per_user', default=get_standard_ratio_rate_definition(events_per_day=46), ), constant_rate_definition=get_dynamic_rate_definition( 'baseline_submissions_per_project', default=RateDefinition( per_week=100, per_day=50, per_hour=30, per_minute=10, per_second=1, ), ), ).get_rate_limits(domain)
def test_get_dynamic_rate_definition(self): self.addCleanup( lambda: DynamicRateDefinition.objects.get(key='test').delete()) # On the first call, the return value is the default given self.assertEqual( get_dynamic_rate_definition( 'test', default=get_standard_ratio_rate_definition(events_per_day=50)), get_standard_ratio_rate_definition(events_per_day=50)) # once it's created changing the default doesn't affect the return value self.assertEqual( get_dynamic_rate_definition( 'test', default=get_standard_ratio_rate_definition( events_per_day=1000)), get_standard_ratio_rate_definition(events_per_day=50)) # The following lines simulates editing through the Django Admin dynamic_rate_definition = DynamicRateDefinition.objects.get(key='test') dynamic_rate_definition.per_week = 64 dynamic_rate_definition.per_day = 32 dynamic_rate_definition.per_hour = 16 dynamic_rate_definition.per_minute = 8 dynamic_rate_definition.per_second = 4 dynamic_rate_definition.save() # After editing, the return value is the newly saved value # (and the default doesn't matter) self.assertEqual( get_dynamic_rate_definition( 'test', default=get_standard_ratio_rate_definition(events_per_day=50)), rate_definition_from_db_object(dynamic_rate_definition)) # Deleting the db object makes it take on the given default value again dynamic_rate_definition.delete() self.assertEqual( get_dynamic_rate_definition( 'test', default=get_standard_ratio_rate_definition(events_per_day=95)), get_standard_ratio_rate_definition(events_per_day=95))
metrics_counter('commcare.two_factor.setup_requests', 1, tags={ 'status': status, 'method': method, 'window': window or 'none', }) return status != _status_accepted two_factor_rate_limiter_per_ip = RateLimiter( feature_key='two_factor_attempts_per_ip', get_rate_limits=lambda scope: get_dynamic_rate_definition( 'two_factor_attempts_per_ip', default=RateDefinition( per_week=20000, per_day=2000, per_hour=1200, per_minute=700, per_second=60, ) ).get_rate_limits(), scope_length=1, ) two_factor_rate_limiter_per_user = RateLimiter( feature_key='two_factor_attempts_per_user', get_rate_limits=lambda scope: get_dynamic_rate_definition( 'two_factor_attempts_per_user', default=RateDefinition( per_week=120, per_day=40, per_hour=8,
) from corehq.apps.data_dictionary.util import save_case_property from corehq.apps.domain.decorators import login_and_domain_required from corehq.apps.hqwebapp.decorators import use_jquery_ui from corehq.apps.hqwebapp.utils import get_bulk_upload_form from corehq.apps.settings.views import BaseProjectDataView from corehq.util.files import file_extention_from_filename from corehq.util.workbook_reading import open_any_workbook data_dictionary_rebuild_rate_limiter = RateLimiter( feature_key='data_dictionary_rebuilds_per_user', get_rate_limits=lambda scope: get_dynamic_rate_definition( 'data_dictionary_rebuilds_per_user', default=RateDefinition( per_hour=3, per_minute=2, per_second=1, ) ).get_rate_limits(), scope_length=1, ) @login_and_domain_required @toggles.DATA_DICTIONARY.required_decorator() def generate_data_dictionary(request, domain): if data_dictionary_rebuild_rate_limiter.allow_usage(domain): data_dictionary_rebuild_rate_limiter.report_usage(domain) try: util.generate_data_dictionary(domain) except util.OldExportsEnabledException: return JsonResponse({
status = _status_bad_request metrics_counter('commcare.two_factor.setup_requests', 1, tags={ 'status': status, 'method': method, }) return status != _status_accepted two_factor_setup_rate_limiter = RateLimiter( feature_key='two_factor_setup_attempts', get_rate_limits=lambda scope: get_dynamic_rate_definition( 'two_factor_setup_attempts', default=RateDefinition( per_week=15, per_day=8, per_hour=5, per_minute=3, per_second=1, ) ).get_rate_limits(), scope_length=1, # per user OR per IP ) global_two_factor_setup_rate_limiter = RateLimiter( feature_key='global_two_factor_setup_attempts', get_rate_limits=lambda: get_dynamic_rate_definition( 'global_two_factor_setup_attempts', default=RateDefinition( per_day=100, ) ).get_rate_limits(),
default=RateDefinition( per_week=100, per_day=50, per_hour=30, per_minute=10, per_second=1, ), ), ).get_rate_limits(domain) global_submission_rate_limiter = RateLimiter( feature_key='global_submissions', get_rate_limits=lambda: get_dynamic_rate_definition('global_submissions', default=RateDefinition( per_hour=17000, per_minute=400, per_second=30, )).get_rate_limits(), scope_length=0, ) SHOULD_RATE_LIMIT_SUBMISSIONS = settings.RATE_LIMIT_SUBMISSIONS and not settings.UNIT_TESTING @run_only_when(SHOULD_RATE_LIMIT_SUBMISSIONS) @silence_and_report_error("Exception raised in the submission rate limiter", 'commcare.xform_submissions.rate_limiter_errors') def rate_limit_submission(domain): if TEST_FORM_SUBMISSION_RATE_LIMIT_RESPONSE.enabled(domain): return True should_allow_usage = (global_submission_rate_limiter.allow_usage()