You can use patch
from unittest
module to solve the problem.
During schema generation, the request object is being checked for each view based on the list of permissions. We declare them in permission_classes
attribute and they typically have has_permission method which is being called during generation. And if it raises some(exceptions.APIException
, Http404
, PermissionDenied
) exceptions, generator skips that view.
If you want to patch the method of IsAuthenticated
class you can do it this way:
from unittest.mock import patch
from rest_framework_swagger.views import get_swagger_view
def patch_the_method(func):
def inner(*args, **kwargs):
with patch('rest_framework.permissions.IsAuthenticated.has_permission', return_value=True):
response = func(*args, **kwargs)
return response
return inner
schema_view = patch_the_method(get_swagger_view(title='Some API'))
Note that this won't include views with other permissions into the schema. To avoid patching all permissions classes you can patch a method from SchemaGenerator
class called has_view_permissions.
Here is how:
...
with patch('rest_framework.schemas.SchemaGenerator.has_view_permissions', return_value=True):
...