Search code examples
djangodjango-rest-frameworkpython-unittestdjango-testing

Strange TyperError put() missing 1 required positional argument: 'path'


I've been trying to test my PUT method in the following APITestcase:

    def test_story_delete(self):
    c = APIRequestFactory
    user = User.objects.get(username='test1')
    payload = {'storyName': 'changetest'}
    request = c.put(reverse('storyFunctions',args=[self.room.pk,self.story.pk]), format='json')
    force_authenticate(request,user=user)

My URL:

    path('room/<int:pk>/story/<int:idStory>/adm', APIHstory.as_view(),name='storyFunctions'),

And I'm keep receiving this error:

TypeError: put() missing 1 required positional argument: 'path'

I don't understand what is going on because i declared the path inside request. Can someone help me?

Edit: sharing the view that I'm testing

class APIstory(APIView):
permission_classes = [IsAuthenticated]

def put(self,request, pk, idStory):
    data = self.request.data
    pk = self.kwargs['pk']
    
    id = self.kwargs['idStory']
    name = data['storyName'].strip()
    if name == "" or str.isspace(name) or len(name) < 4:
            return Response({"error": "minimum of 4 words"}, status=400)
    

    if PokerRoom.objects.filter(id=pk).exists():
        session = PokerRoom.objects.filter(id=pk)

        if session[0].status == 'Finished':
            return Response({'error': 'Session already ended'}, status=400)
        else:
            session = PokerRoom(id=pk)
            if Story.objects.filter(id=id, idRoom=session).exists():
                if not Story.objects.filter(idRoom=session, storyName=name).exists():
                    for hist in Story.objects.filter(id=id, idRoom=session):
                        if  hist.status == 'Pending':
                            if hist.storyName == name:
                                return Response({'error':'story with the same name'}, status=400)
                            hist.storyName = name
                            hist.save()
                            status = hist.status
                            body = {"id":id,"storyName":name,"status":status}
                            message_socket("STORY_UPDATE", pk, body)
                            return Response({'success': 'story {} edited, status is {}.'.format(hist.id,hist.status)})
                        else:
                            return Response({'error': 'story already ended'}, status= 400)
                else:
                    return Response({'error': 'story with the same name'}, status= 400)

           
        return Response({'error': 'session doesnt exists.'}, status=400)
    else:
        return Response({'error': 'session doesnt exists.'}, status=400)

Edit2: I was forgetting to put the body in my request, but now I'm getting the following error: TypeError: super(type, obj): obj must be an instance or subtype of type


Solution

  • Shouldn't you add the request body also in a put request? Something like this?

    request = c.put(reverse('storyFunctions', kwargs={'pk': self.room.pk, 'idStory': self.story.pk}), payload, format='json')
    

    As per the docs