Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django rest framework error unit test: TypeError: object of type 'type' has no len()

i'm currently writting a custom user model in django with jwt token authentication in django-rest-framework. In my unit test case i get this error:

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

And my test code:

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('[email protected]', '0123456789')

    def test_token_sign_in(self):
        url = reverse_lazy('authentication-sign-in')
        data = {'email': '[email protected]', '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)

My views:

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

i don't understand this error, i haven't define any renderer_classes in my views so renderer_classes will must be at 0 ?

Thank for help !

like image 422
midoriiro Avatar asked Jan 28 '16 05:01

midoriiro


1 Answers

The problem is that you are missing the trailing command in your REST_FRAMEWORK settings, specifically the key for the DEFAULT_RENDERER_CLASSES.

REST_FRAMEWORK = {
    "DEFAULT_RENDERER_CLASSES": (
        "rest_framework.renderers.JSONRenderer"
    )
}

As long as you add a comma after each element in the list, you should never see this issue.

REST_FRAMEWORK = {
    "DEFAULT_RENDERER_CLASSES": (
        "rest_framework.renderers.JSONRenderer",
    )
}

That's because otherwise it will be interpreted as

REST_FRAMEWORK = {
    "DEFAULT_RENDERER_CLASSES": "rest_framework.renderers.JSONRenderer",
}

Which is definitely not what you were looking for.

like image 100
Kevin Brown-Silva Avatar answered Sep 28 '22 02:09

Kevin Brown-Silva