There is Django app that uses Django Rest Framework. The settings file contains an object with the settings:
settings.py
REST_FRAMEWORK = {
...
'DEFAULT_THROTTLE_RATES': {
'burst': '30/second',
},
...
}
The unittest is supposed to test the throttling actually works. However none of the tools provided by Django Test module (SimpleTestCase.settings, override_settings, modify_settings) actually work:
SimpleTestCase.settings
class ThrottlingTest(RestApiTestCase):
def test_per_second_throttling(self):
new_config = settings.REST_FRAMEWORK
new_config['DEFAULT_THROTTLE_RATES']['burst'] = '1/second'
with self.settings(REST_FRAMEWORK=new_config):
for _ in range(0, 2):
response = self.client.get(self.api_reverse('foo'))
self.assertEqual(response.status_code, 429) # fails, 200 != 429
override_settings
class ThrottlingTest(RestApiTestCase):
new_config = settings.REST_FRAMEWORK
new_config['DEFAULT_THROTTLE_RATES']['burst'] = '1/second'
@override_settings(REST_FRAMEWORK=new_config)
def test_per_second_throttling(self):
for _ in range(0, 2):
response = self.client.get(self.api_reverse('foo'))
self.assertEqual(response.status_code, 429) # fails, 200 != 429
Both methods work fine for primitive variables and lists, however fail for the object.
The docs state:
When given a class, these decorators modify the class directly and return it; they don’t create and return a modified copy of it.
So it actually supposed to work.
Any clues how to handle this?