Agustin Machiavello
Agustin Machiavello

Reputation: 87

Client patch gives 400 error in django testing

So basically when i try to test my patch view it doesn't patch at all as i get a 400 error. However, when i call the same patch view outside the APITestCase, while running the test server, it does work.

what i've tried:

At APITestCase:

    def test_patch_update_data(self):
        self.client.force_authenticate(user=self.user)
        self.assertEqual(self.user.first_name, 'Robert')
        patch_data = {'first_name': 'test999'}
        response = self.client.patch('http://testserver/api/patch/{0}/'.format(
            self.user.id), patch_data, format='json')
        self.assertEqual(response.status_code, 200) # ERROR:GIVES 400!
        self.assertEqual(self.user.first_name, 'test999')

At views.py

class UsuariosUpdatePatchAPIView(UpdateAPIView):
    queryset = Usuarios.objects.all()
    serializer_class = UsuariosUpdateSerializer
    permission_classes = [IsAuthenticated, IsSelfUser]

The error:

self.assertEqual(response.status_code, 200)
AssertionError: 400 != 200

Thanks in advance! :)

Upvotes: 1

Views: 690

Answers (1)

Agustin Machiavello
Agustin Machiavello

Reputation: 87

How silly i am:

As stated in the comments, response.data gives very useful information. It turned out to be that the field fist_name only accepted letters! So the solution was changing test999 to something without numbers!

Upvotes: 2

Related Questions