diff --git a/gracedb/api/tests/test_throttling.py b/gracedb/api/tests/test_throttling.py index 3347618fbca7b1b9fd432dbfd25063efb7813e76..6f2d6e5932cbe584b6cd5600f31cf75c9309f963 100644 --- a/gracedb/api/tests/test_throttling.py +++ b/gracedb/api/tests/test_throttling.py @@ -1,4 +1,4 @@ -from copy import deepcopy +import mock from django.conf import settings from django.core.cache import caches @@ -8,22 +8,13 @@ from django.urls import reverse from api.tests.utils import GraceDbApiTestBase -# Copy REST_FRAMEWORK settings dict and override here -drf_settings = settings.REST_FRAMEWORK.copy() -drf_settings['DEFAULT_THROTTLE_RATES']['anon_burst'] = '1/hour' - - class TestThrottling(GraceDbApiTestBase): """Test API throttles""" - def tearDown(self): - super(TestThrottling, self).tearDown() - - # Clear throttle cache - caches['throttles'].clear() - - @override_settings(REST_FRAMEWORK=drf_settings) - def test_anon_burst_throttle(self): + @mock.patch('api.throttling.BurstAnonRateThrottle.get_rate', + return_value='1/hour' + ) + def test_anon_burst_throttle(self, mock_get_rate): """Test anonymous user burst throttle""" url = reverse('api:default:root') diff --git a/gracedb/api/tests/utils.py b/gracedb/api/tests/utils.py index f5bc297e31c02e360a0a3d9cb99d84e8bf4b4365..eaad41c519637089560d2a9d1f52f77c78020781 100644 --- a/gracedb/api/tests/utils.py +++ b/gracedb/api/tests/utils.py @@ -1,14 +1,43 @@ +from copy import deepcopy +import mock + +from django.conf import settings +from django.core.cache import caches from django.test import override_settings from rest_framework.test import APIClient from core.tests.utils import GraceDbTestBase -# Need to allow requests without a client version header -# to access the API for tests, unless we want to set and -# update that header ourselves... + +def fix_settings(key, value): + """ + Dynamically override settings for testing. But, it will only + ever be useful if rest_framework fixes the way that their + settings work so that override_settings actually works. + + 'key' should be x.y.z for nested dictionaries. + """ + api_settings = deepcopy(settings.REST_FRAMEWORK) + + key_list = key.split('.') + new_dict = reduce(dict.get, key_list[:-1], api_settings) + new_dict[key_list[-1]] = value + + return api_settings + + +@mock.patch('api.throttling.BurstAnonRateThrottle.get_rate', + return_value='1000/second' +) @override_settings( ALLOW_BLANK_USER_AGENT_TO_API=True, ) class GraceDbApiTestBase(GraceDbTestBase): client_class = APIClient + + def tearDown(self): + super(GraceDbApiTestBase, self).tearDown() + + # Clear throttle cache + caches['throttles'].clear()