title: How to test an API endpoint with Django-rest-framework using Django-oauth-toolkit for authentication
body: <p>I have a Django-rest-framework viewset/router to define an API endpoint.  The viewset is defined as such:</p>
  
  <pre><code>class DocumentViewSet(viewsets.ModelViewSet):
      permission_classes = [permissions.IsAuthenticated, TokenHasReadWriteScope]
      model = Document
  </code></pre>
  
  <p>And the router is defined as</p>
  
  <pre><code>router = DefaultRouter()
  router.register(r'documents', viewsets.DocumentViewSet)
  </code></pre>
  
  <p>with url pattern <code>url(r'^api/', include(router.urls))</code></p>
  
  <p>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.</p>
  
  <p>Here is what I've tried:</p>
  
  <pre><code>class DocumentAPITests(APITestCase):
      def test_get_all_documents(self):
          user = User.objects.create_user('test', 'test@test.com', 'test')
          client = APIClient()
          client.credentials(username="test", password="test")
          response = client.get("/api/documents/")
          self.assertEqual(response.status_code, 200) 
  </code></pre>
  
  <p>This fails with an HTTP 401 response from the <code>client.get()</code> call. What is the right way to test an API endpoint in DRF using django-oauth-toolkit for oauth2 authentication?</p>