Skip to content
Snippets Groups Projects
Commit e102ef5a authored by Tanner Prestegard's avatar Tanner Prestegard Committed by GraceDB
Browse files

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.
parent 4d73b94f
No related branches found
No related tags found
No related merge requests found
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')
......
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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment