【问题标题】:How can I skip authentication in a Django APIRequestFactory call?如何在 Django APIRequestFactory 调用中跳过身份验证?
【发布时间】:2021-04-01 13:00:03
【问题描述】:

所以本质上我想向 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 小时在“某处”生成的东西。因此,我想跳过身份验证。

提前谢谢你。

【问题讨论】:

    标签: django authentication django-rest-framework django-unittest django-rest-viewsets


    【解决方案1】:

    你需要使用forcing authentication

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-11
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      • 2020-08-27
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      相关资源
      最近更新 更多