title: How to force_authenticate in Django Rest Framework tests
body: <p>I have problem with testing API built in DRF tutorial: <a href="https://www.django-rest-framework.org/tutorial/1-serialization/" rel="nofollow noreferrer">https://www.django-rest-framework.org/tutorial/1-serialization/</a></p>
  <p>I have view:</p>
  <pre><code>class SnippetList(generics.ListCreateAPIView):
      permission_classes = [permissions.IsAuthenticatedOrReadOnly]
      queryset = Snippet.objects.all()
      serializer_class = SnippetSerializer
      def perform_create(self, serializer):
          serializer.save(owner=self.request.user)
  </code></pre>
  <p>And test class for testing that view:</p>
  <pre><code>class SnippetsList(APITestCase):
      def setUp(self):
          self.user = User.objects.create_superuser(username='testowy', password='test')
          self.client = APIClient()
          Snippet.objects.create(code=&quot;print('hello')&quot;, owner=self.user)
          Snippet.objects.create(code=&quot;print('world')&quot;, owner=self.user)
          self.payload = {
              &quot;code&quot;: &quot;print(edit)&quot;
          }
  
      def test_get_snippets_list(self):
          response = self.client.get(reverse('snippet_list'))
          snippets = Snippet.objects.all()
          serializer = SnippetSerializer(snippets, many=True)
          self.assertEqual(response.data, serializer.data)
          self.assertEqual(response.status_code, status.HTTP_200_OK)
  
      def test_post_snippets_list(self):
          self.client.force_authenticate(self.user)
          response = self.client.post(reverse('snippet_list'), json.dumps(self.payload), format='json')
          self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  </code></pre>
  <p>When I run my tests with python manage.py test first test (for get method) is passing but for the second I get this output:</p>
  <pre><code>    self.assertEqual(response.status_code, status.HTTP_201_CREATED) AssertionError: 400 != 201
  </code></pre>
  <p>In manual tests after login in everything works perfectly, does anyone knows what am I missing here?</p>