title: Django unit testing authentication token not accepted
body: <p>I've checked the DRF official documentation, have read the below posts and copy-pasted code from some answers to avoid any typo, but I still can't authenticate in my unit tests. I always get a 401 response code.</p>
  <p>I have a custom User model that just has a couple of none required fields so far.</p>
  <p>Can anyone see any logic issue, or error in the code?
  Could the issue originate from having a proper local DB and a testing DB and the authentication mixing both up somehow?</p>
  <p>I've had to use <code>self.client.force_authenticate(user=self.user, token=None)</code> to bypass the authentication issue. But that defeats the purpose of the tests.</p>
  <p>Checked posts that did not solve the problem:</p>
  <ul>
  <li><a href="https://www.django-rest-framework.org/api-guide/testing/#forcing-authentication" rel="nofollow noreferrer">DRF documentation</a></li>
  <li><a href="https://stackoverflow.com/questions/54242535/how-do-i-pass-authentication-bearer-tokens-to-http-methods-in-django-test-client">SO post 1</a></li>
  <li><a href="https://stackoverflow.com/questions/31377207/django-authenticate-unit-test">SO post 2</a></li>
  <li><a href="https://stackoverflow.com/questions/66291815/cannot-authenticate-test-requests-with-both-user-token-and-api-key-in-django-res">SO post 3</a></li>
  </ul>
  <p>So here's my code.</p>
  <p>Unit test</p>
  <pre class="lang-py prettyprint-override"><code>from django.test.testcases import SimpleTestCase
  from django.urls import reverse, resolve
  from rest_framework import status
  from rest_framework.test import APITestCase
  from rest_framework.authtoken.models import Token
  from test_project.models.user import *
  from test_project.views import UserViewSet
  # from django.contrib.auth.models import User
  from test_project.models import *
  
  
  class UserAPIViewTests(APITestCase):
  
    def setUp(self) -&gt; None:
      self.users_list_url = reverse('user-list')
      self.users_detail_url = reverse('user-detail', args=[1])
      self.user =User.objects.create(
        username=&quot;admin&quot;,
        password=&quot;admin&quot;,
        email=&quot;test@necktie.com&quot;,
        first_name=&quot;test first name&quot;,
        last_name=&quot;test last name&quot;,
        is_superuser=True,
        is_staff=True,
        is_active=True
      )
      self.token = Token.objects.create(user=user)
      self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + self.token.key)
  
    # The testing DB is automatically torn down, no implementation required for those tests
    def tearDown(self) -&gt; None:
      pass
  
  
    def test_get_users_authenticated(self):
      &quot;&quot;&quot;
      Test that an authenticated user can get the users' list
      &quot;&quot;&quot;
      response = self.client.get(self.users_list_url)
      self.assertEquals(response.status_code, status.HTTP_200_OK)
  
  </code></pre>
  <p>User model</p>
  <pre class="lang-py prettyprint-override"><code>from django.db import models
  from django.contrib.auth.models import AbstractUser
  from django.utils.translation import gettext_lazy as _
  
  class User(AbstractUser):
    &quot;&quot;&quot;auth/login-related fields&quot;&quot;&quot;
    gender = models.CharField(_('gender'), max_length=50, null=True)
    nationality = models.CharField(_('nationality'), max_length=50, null=True)
  
  
    def __str__(self):
      return &quot;{} {}&quot;.format(self.first_name, self.last_name)
  
  </code></pre>
  <p>User View</p>
  <pre class="lang-py prettyprint-override"><code>from rest_framework import viewsets
  from rest_framework.response import Response
  from rest_framework import permissions
  from ..models.user import User
  from ..serializers.user import *
  from ..permissions import *
  
  class UserViewSet(viewsets.ModelViewSet):
    &quot;&quot;&quot;
    This viewset automatically provides `list`, `create`, `retrieve`,
    `update` and `destroy` actions.
    &quot;&quot;&quot;
    queryset = User.objects.all()
    serializer_class = UserSerializer
  
    def get_permissions(self):
      &quot;&quot;&quot;
      Method to apply permissions depending on request type (GET, PUT etc.)
      &quot;&quot;&quot;
      if self.request.method == 'GET':
        return [permissions.IsAuthenticated(), IsStaff()]
      else:
        return [permissions.IsAuthenticated(), IsSuperuser()]
  </code></pre>
  <p>User serializers</p>
  <pre class="lang-py prettyprint-override"><code>from rest_framework import serializers
  from ..models.user import User
  
  
  class UserSerializer(serializers.ModelSerializer):
    &quot;&quot;&quot;
    Serializer for all actions on User model
    &quot;&quot;&quot;
  
    class Meta:
      model = User
      fields = [
        'id',
        'username',
        'first_name',
        'last_name',
        'is_staff',
        'is_superuser',
        'is_active',
        'date_joined'
      ]
  
  </code></pre>
  <p>Thanks for the help!</p>