title: Authenticating with DRF API key in unit tests return error: "Authentication credentials were not provided"
body: <p>After recently discovering (and loving!) test driven development, I've started working on an API for a project of mine using Django with <a href="https://www.django-rest-framework.org/" rel="nofollow noreferrer">Django REST Framework</a> and <a href="https://florimondmanca.github.io/djangorestframework-api-key/" rel="nofollow noreferrer">Django REST Framework API Key</a>. However, I'm getting stuck trying to unit test some basic API functionality: every time I attempt to do a GET request to the API, it returns a 403: &quot;Authentication credentials were not provided.&quot;</p>
  <p>Which is odd; I <em>am</em> adding the credentials in the header of the request before I send it off. When I print out the header of the RequestsClient, I am getting this result:</p>
  <pre><code>{'User-Agent': 'python-requests/2.25.1', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'X-Api-Key': 'ADtIa4s9.joEWAwZH47uowolz60c1hYnskVujO8lQ'}
  </code></pre>
  <p>Should work... yet for some reason my server (Django's internal server, running on localhost:8000) will not see it. I've been going through a ton of Questions on here and I am left completely baffled.
  Can you help me out a bit? Thanks in advance.</p>
  <p><strong>settings.py</strong></p>
  <pre><code>INSTALLED_APPS = [
      'django.contrib.admin',
      'django.contrib.auth',
      'django.contrib.contenttypes',
      'django.contrib.sessions',
      'django.contrib.messages',
      'django.contrib.staticfiles',
      'rest_framework',
      'rest_framework_api_key',
      'core',
      'base',
      'api',
  ]
  
  MIDDLEWARE = [
      'django.middleware.security.SecurityMiddleware',
      'django.contrib.sessions.middleware.SessionMiddleware',
      'django.middleware.common.CommonMiddleware',
      # 'django.middleware.csrf.CsrfViewMiddleware',
      'django.contrib.auth.middleware.AuthenticationMiddleware',
      'django.contrib.messages.middleware.MessageMiddleware',
      'django.middleware.clickjacking.XFrameOptionsMiddleware',
  ]
  
  REST_FRAMEWORK = {
      'DEFAULT_PERMISSION_CLASSES': [
          'rest_framework_api_key.permissions.HasAPIKey',
      ]
  }
  API_KEY_CUSTOM_HEADER = &quot;HTTP_X_API_KEY&quot;
  </code></pre>
  <p><strong>urls.py</strong></p>
  <pre><code>from django.contrib import admin
  from django.urls import path
  from rest_framework import routers
  from api.viewsets import RestaurantViewSet
  
  router = routers.SimpleRouter()
  router.register(r'restaurant', RestaurantViewSet)
  
  urlpatterns = [
      path('admin/', admin.site.urls),
  ]
  urlpatterns += router.urls
  </code></pre>
  <p><strong>api/viewsets.py</strong></p>
  <pre><code>from rest_framework import viewsets
  from .serializers import RestaurantSerializer
  from base.models import Restaurant
  
  class RestaurantViewSet(viewsets.ReadOnlyModelViewSet):
      queryset = Restaurant.objects.all()
      serializer_class = RestaurantSerializer
  </code></pre>
  <p><strong>base/tests.py</strong></p>
  <pre><code>class RestaurantTest(APITestCase):
      @classmethod
      def setUpTestData(cls):
          manager = User.objects.create(username=&quot;TestUser&quot;)
          cls.restaurant = Restaurant.objects.create(name=&quot;Testaurant&quot;, manager=manager)
          client_key = cls.restaurant.create_api_key()
  
          ...
  
          cls.client = RequestsClient()
          header = {'X-Api-Key': 'Api-Key {}'.format(client_key)}
          cls.client.headers.update(header)
          print(header)  # Output: see description above
  
      # This test passes
      def test_resolves_to_correct_view(self):
          found = resolve('/restaurant/')
          self.assertEqual(found.func.__name__, RestaurantViewSet.__name__)
  
      # This test fails with &quot;Authentication credentials were not provided.&quot;
      def test_able_to_get(self):
          path = '/restaurant/'
          response = self.client.get(path)
          self.assertEqual(response.status_code, 200, msg=&quot;Request to '{}' failed: {} - {}&quot;.format(path, response.status_code, response.data['detail']))
  </code></pre>