如何在 Django APIRequestFactory 调用中跳过身份验证?

How can I skip authentication in a Django APIRequestFactory call?

所以基本上我想在不需要身份验证的情况下向 ViewSet 发出请求,Django APIRequestFactory 是否使这成为可能?这有效:

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)

但问题是,不记名令牌是每 24 小时 'somewhere far' 生成的东西。因此,我想跳过身份验证。

提前致谢。

您需要使用forcing authentication