From e102ef5aca02fecd0a6e8800e085e891c7f9767c Mon Sep 17 00:00:00 2001 From: Tanner Prestegard <tanner.prestegard@ligo.org> Date: Thu, 7 Feb 2019 09:35:41 -0600 Subject: [PATCH] Bugfix to throttling unit test Have to use mock to properly modify throttles for unit testing since 'override_settings' doesn't really work with the API settings used by rest_framework. --- gracedb/api/tests/test_throttling.py | 19 ++++----------- gracedb/api/tests/utils.py | 35 +++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/gracedb/api/tests/test_throttling.py b/gracedb/api/tests/test_throttling.py index 3347618fb..6f2d6e593 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 f5bc297e3..eaad41c51 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() -- GitLab