Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def find_api_views(urls, views=None):
if views is None:
views = []
for entry in urls:
if hasattr(entry, 'url_patterns'):
views.extend(find_api_views(entry.url_patterns))
else:
view = entry.callback
if (
hasattr(view, 'cls') and
issubclass(view.cls, APIView)
):
views.append(view)
return views
drf_views = find_api_views(urlpatterns)
@contextmanager
def override_drf_settings(**kwargs):
old_settings = defaultdict(dict)
for key, value in kwargs.items():
attr = key[len('DEFAULT_'):].lower()
for view in drf_views:
old_settings[view.func_name][attr] = getattr(view.cls, attr)
setattr(view.cls, attr, value)
yield
for key, value in kwargs.items():
attr = key[len('DEFAULT_'):].lower()
for view in drf_views:
setattr(view.cls, attr, old_settings[view.func_name][attr])
class DRFSettings(object):
def drf_settings(self, **kwargs):
return override_drf_settings(**kwargs)
class LiveServerTestCase(DRFSettings, BaseLiveServerTestCase):
def test_foo(self):
with self.drf_settings(DEFAULT_AUTHENTICATION_CLASSES=()):
# stuff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.