Search code examples
djangoauthenticationdjango-rest-frameworkdjango-unittestdjango-rest-viewsets

How can I skip authentication in a Django APIRequestFactory call?


So essentially I want to make a request to a ViewSet without requiring to authenticate, does Django APIRequestFactory make this possible? This works:

from django.test import TestCase
from .views.SomeViewSet
from rest_framework.test import APIRequestFactory


class ViewSetsTest(TestCase):
    ...
    ...
    ...
    def test_db_instance_viewset(self):
        api_request = APIRequestFactory.patch("", HTTP_AUTHORIZATION="Bearer xyz123ppouu")
        detail_view = SomeViewSet.as_view({'patch': 'update'})
        response = detail_view(api_request)
        self.assertEqual(response.status_code, 200)

But the issue is, the bearer token is something that is generated 'somewhere far' every 24 hours. Hence, I want to skip authentication.

Thank you in advance.


Solution

  • You need to use forcing authentication