Created
May 5, 2014 14:20
-
-
Save brutasse/873d35192fb35c72fe0d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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