I have a Django-rest-framework viewset/router to define an API endpoint. The viewset is defined as such:
permission_classes = [permissions.IsAuthenticated, TokenHasReadWriteScope]
model = Document
And the router is defined as
router = DefaultRouter()
with url pattern url(r'^api/', include(router.urls))
I can hit this endpoint in the browser/through curl just fine by getting the right access token and using it for authorization. However, it's not clear how to write tests against this endpoint.
Here is what I've tried:
user = User.objects.create_user('test', 'firstname.lastname@example.org', 'test')
client = APIClient()
response = client.get("/api/documents/")
This fails with an HTTP 401 response from the client.get() call. What is the right way to test an API endpoint in DRF using django-oauth-toolkit for oauth2 authentication?