在Django Rest Framework问题401中通过身份验证测试POST

时间:2020-09-14 22:24:19

标签: django-rest-framework

我正在尝试在需要验证的django-rest-framework中测试将数据发布到视图。但是我不能。我已经阅读了许多假定解决方案的线程,但找不到任何可以解决我的问题。

测试

     class TodoListCreateAPIViewTestCase(APITestCase):
            url = reverse("todolist:add")
        
            def setUp(self):
                self.username = "john"
                self.email = "john@snow.com"
                self.password = "you_know_nothing"
                self.user = User.objects.create_user(self.username, self.email, self.password)
                self.token = Token.objects.create(user=self.user)

 


  #checking token here
  def test_create_todo(self):
        self.client.login(email=self.email, password='you_know_nothing')
        client = APIClient()
        client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)

        response = self.client.post(self.url, {"list_name": "Clean the room!"}, format='json')
        self.assertEqual(201, response.status_code)

查看

class Addtodolist(APIView):
    authentication_classes = (JSONWebTokenAuthentication,TokenAuthentication)
    permission_classes = [IsAuthenticated, ]

    def post(self, request):
        data = request.data
        todolist_instance = Todolist.objects.filter(for_user=self.request.user).first()

        if not todolist_instance:
            list_serilaizer = AddtodolistSerializers(data=data, context={'user': request.user})
            if list_serilaizer.is_valid():
               list_serilaizer.save()
               return Response(data=success_response(data=list_serilaizer.data, msg='Successfully Created list!'),
                            status=status.HTTP_200_OK)
            else:
                return Response(
                    failure_response(data={'detail': list_serilaizer.errors()}, msg='Following errors occured'),
                    status=status.HTTP_400_BAD_REQUEST)
        else:
            return Response(
                failure_response(data={'detail': 'User can have only 1 List'}, msg='User can have only 1 List'),
                status=status.HTTP_409_CONFLICT)

1 个答案:

答案 0 :(得分:1)

像这样更改代码:

from django.shortcuts import reverse
from django.contrib.auth import get_user_model
from rest_framework.test import APIClient, APITestCase
from rest_framework.authtoken.models import Token


class TodoListCreateAPIViewTestCase(APITestCase):
    url = reverse("todolist:add")

    def setUp(self):
        self.username = "john"
        self.email = "john@snow.com"
        self.password = "you_know_nothing"
        self.user = get_user_model().objects.create_user(self.username,
                                                         self.email,
                                                         self.password)

        self.token = Token.objects.create(user=self.user)

    def test_create_todo(self):

        client = APIClient()
        client.login(username=self.username,
                     email=self.email,
                     password=self.password)
        client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)

        response = client.post(self.url,
                               {"list_name": "Clean the room!"},
                               format='json')

        self.assertEqual(response.status_code, 201)