我正在使用Django和Django REST framework编写自定义用户模型并进行JWT令牌身份验证。在我的单元测试用例中,我遇到了以下错误:
Creating test database for alias 'default'...
Destroying test database for alias 'default'...
E
======================================================================
ERROR: test_token_sign_in (edea.authentication.tests.tests.AuthenticationTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\Home\midoriiro\Programming\workspace\edea\edea\authentication\tests\tests.py", line 17, in test_token_sign_in
response = self.client.post(url, data, format='json')
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\rest_framework\test.py", line 170, in post
path, data=data, format=format, content_type=content_type, **extra)
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\rest_framework\test.py", line 92, in post
return self.generic('POST', path, data, content_type, **extra)
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\django\test\client.py", line 380, in generic
return self.request(**r)
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\rest_framework\test.py", line 159, in request
return super(APIClient, self).request(**kwargs)
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\rest_framework\test.py", line 111, in request
request = super(APIRequestFactory, self).request(**kwargs)
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\django\test\client.py", line 467, in request
six.reraise(*exc_info)
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\django\utils\six.py", line 686, in reraise
raise value
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\django\core\handlers\base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\django\core\handlers\base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\django\views\decorators\csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\django\views\generic\base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\rest_framework\views.py", line 451, in dispatch
self.headers = self.default_response_headers # deprecate?
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\rest_framework\views.py", line 150, in default_response_headers
if len(self.renderer_classes) > 1:
TypeError: object of type 'type' has no len()
----------------------------------------------------------------------
Ran 1 test in 0.065s
FAILED (errors=1)
Process finished with exit code 1
我的测试代码:
from django.core.urlresolvers import reverse_lazy
from rest_framework import status
from rest_framework.test import APITestCase
from edea.authentication.models import User
class AuthenticationTestCase(APITestCase):
@classmethod
def setUpTestData(cls):
cls.user = User.objects.create_user('lol@lol.com', '0123456789')
def test_token_sign_in(self):
url = reverse_lazy('authentication-sign-in')
data = {'email': 'lol@lol.com', 'password': '0123456789'}
response = self.client.post(url, data, format='json')
token = response.json()['token']
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(token, self.user.token)
我的观点:
from datetime import timedelta
from rest_framework.response import Response
from rest_framework import status
from rest_framework_jwt.settings import api_settings
from rest_framework_jwt.views import JSONWebTokenAPIView
from edea.authentication.serializers import GetJWTSerializer, VerifyJWTSerializer, RefreshJWTSerializer, DestroyJWTSerializer
class GetJWTView(JSONWebTokenAPIView):
serializer_class = GetJWTSerializer
class VerifyJWTView(JSONWebTokenAPIView):
serializer_class = VerifyJWTSerializer
class RefreshJWTView(JSONWebTokenAPIView):
serializer_class = RefreshJWTSerializer
class DestroyJWTView(JSONWebTokenAPIView):
serializer_class = DestroyJWTSerializer
def post(self, request):
response = super(DestroyJWTView, self).post(request)
if response.status_code is not status.HTTP_200_OK:
return response
return Response(status=status.HTTP_200_OK)
class TimeJWTView(JSONWebTokenAPIView):
serializer_class = VerifyJWTSerializer
def post(self, request):
response = super(TimeJWTView, self).post(request)
if response.status_code is status.HTTP_200_OK:
expiration_time = None
refresh_time = None
if api_settings.JWT_VERIFY_EXPIRATION:
expiration_time = api_settings.JWT_EXPIRATION_DELTA
if isinstance(expiration_time, timedelta):
expiration_time = expiration_time.total_seconds()
else:
expiration_time = -1
if api_settings.JWT_ALLOW_REFRESH:
refresh_time = api_settings.JWT_REFRESH_EXPIRATION_DELTA
if isinstance(refresh_time, timedelta):
refresh_time = refresh_time.total_seconds()
else:
refresh_time = -1
response.data['expiration_time'] = expiration_time
response.data['refresh_time'] = refresh_time
return response
我不理解这个错误,我的视图中没有定义任何renderer_classes,所以renderer_classes必须为0吗?
感谢帮助!