Python 在Django Rest框架视图中测试身份验证--测试时无法进行身份验证
在为这个问题苦苦挣扎之后,我来寻求一些帮助。我正在为Django Rest框架视图编写一个测试,测试我是否可以在经过身份验证的同时访问数据。然而,即使我通过了身份验证,每次都会得到Python 在Django Rest框架视图中测试身份验证--测试时无法进行身份验证,python,django,django-rest-framework,Python,Django,Django Rest Framework,在为这个问题苦苦挣扎之后,我来寻求一些帮助。我正在为Django Rest框架视图编写一个测试,测试我是否可以在经过身份验证的同时访问数据。然而,即使我通过了身份验证,每次都会得到401个未经授权的。以下是我的测试: from django.test import TestCase from django.contrib.auth.models import User from rest_framework.authtoken.models import Token from rest_fra
401个未经授权的
。以下是我的测试:
from django.test import TestCase
from django.contrib.auth.models import User
from rest_framework.authtoken.models import Token
from rest_framework.test import APIRequestFactory, APIClient
from apps.core import models, admin
from apps.rest import views
class TestAPIViews(TestCase):
def setUp(self):
self.factory = APIRequestFactory()
self.client = APIClient()
self.user = User.objects.create_user('testuser', email='testuser@test.com', password='testing')
self.user.save()
token = Token.objects.create(user=self.user)
token.save()
def _require_login(self):
self.client.login(username='testuser', password='testing')
def test_ListAccounts_not_authenticated(self):
request = self.factory.get('/accounts/')
view = views.ListAccounts.as_view()
response = view(request)
self.assertEqual(response.status_code, 401,
'Expected Response Code 401, received {0} instead.'.format(response.status_code))
def test_ListAccounts_authenticated(self):
self.client._require_login()
print(self.user.is_authenticated()) # returns True
request = self.factory.get('/accounts/')
view = views.ListAccounts.as_view()
response = view(request)
self.assertEqual(response.status_code, 200,
'Expected Response Code 200, received {0} instead.'.format(response.status_code))
下面是我的DRF视图的代码:
from django.shortcuts import render
from django.contrib.auth import authenticate, login, logout
from django.db.models import Q
from apps.core import serializers, models
from apps.rest.permissions import IsAccountOwner
from rest_framework.views import APIView
from rest_framework import status, authentication, generics
from rest_framework.response import Response
from rest_framework.authtoken.models import Token
from rest_framework.authentication import SessionAuthentication, TokenAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from apps.core import serializers, models
'''
Auth Mixin for Account Admin Access
'''
class AuthMixin:
authentication_classes = (authentication.TokenAuthentication,
authentication.SessionAuthentication)
permission_classes = (IsAuthenticated,)
class GenericList(AuthMixin, generics.ListAPIView):
def get_queryset(self):
# Stubbed out - doing nothing special right now
qs = self.model.objects.filter()
return qs
class ListAccounts(GenericList):
model = models.Account
serializer_class = serializers.AccountSerializer
可以看到,我在
test\u ListAccounts\u authenticated
中调用login,然后打印出我是否经过身份验证(返回True
),但无论发生什么,我都会收到401 UNAUTHORIZED
错误。我遗漏了什么?提前感谢。不要调用self.factory.get()
,而是调用self.client.get()
我猜
self.client.login
对self.factory
没有影响不确定这是否是一个输入错误,但是您的AuthMixin没有子类化任何东西,应该通过AuthMixin(对象)
来实现。除此之外,它看起来还可以。我把SessionAuthentication作为第一项,不确定这是否重要。AuthMixin只是我创建的一个类。它不会也不应该对任何东西进行子类化。@第五,这可能很重要,不知道为什么,但当我把(SessionAuthentication作为第一个-不包括在内)放在测试中时,我解决了身份验证问题。