0

I have strange problems

I have next code Using TesCase and APIClient to get request with token

    class Profile(TestCase):
        fixtures = ['profiles.json']

        def setUp(self):
             self.client = APIClient()
             self.user = Profile.objects.get(username='some-username')
             self.token = Token.objects.create(user=self.user)
             self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)

        def test_profile(self):
                response = self.client.get('/api/some-username/profile/')               
                print(response.status_code)
                print(response.data)`

Authorization goes well,but the I received error. If i use requests.get with headers (token) to development django server all pass well i receive a correct json response

Full Traceback when i run this test:

Traceback (most recent call last):
  File "/home/work/project/profiles/tests.py", line 103, in test_user_profile
    response = self.client.get('/api/some-username/profile/')
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/test.py", line 291, in get
    response = super(APIClient, self).get(path, data=data, **extra)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/test.py", line 208, in get
    return self.generic('GET', path, **r)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/test.py", line 237, in generic
    method, path, data, content_type, secure, **extra)
  File "/usr/local/lib/python3.5/dist-packages/django/test/client.py", line 416, in generic
    return self.request(**r)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/test.py", line 288, in request
    return super(APIClient, self).request(**kwargs)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/test.py", line 240, in request
    request = super(APIRequestFactory, self).request(**kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/test/client.py", line 501, in request
    six.reraise(*exc_info)
  File "/usr/local/lib/python3.5/dist-packages/django/utils/six.py", line 686, in reraise
    raise value
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/views.py", line 489, in dispatch
    response = self.handle_exception(exc)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework_tracking/mixins.py", line 69, in handle_exception
    response = super(BaseLoggingMixin, self).handle_exception(exc)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/views.py", line 449, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/views.py", line 486, in dispatch
    response = handler(request, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/generics.py", line 254, in get
    return self.retrieve(request, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/mixins.py", line 58, in retrieve
    return Response(serializer.data)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/serializers.py", line 533, in data
    ret = super(Serializer, self).data
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/serializers.py", line 262, in data
    self._data = self.to_representation(self.instance)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/serializers.py", line 500, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "/home/work/project/profiles/serializers.py", line 10, in to_representation
    return institute.all()[0].slug
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 289, in __getitem__
    return list(qs)[0]
IndexError: list index out of range

How know what is the problem?

Beliaf
  • 577
  • 2
  • 8
  • 25

0 Answers0