[Django]-Testing Django API login using credentials and djoser

3👍

Django REST framework has APIClient class which serves this purpose. I think rather than getting Token over http, it can pulled out from DB directly. The code looks like

from rest_framework.authtoken.models import Token
from rest_framework.test import APIClient

# Include an appropriate `Authorization:` header on all requests.
token = Token.objects.get(user__username=self.clark_user.username)
client = APIClient()
client.credentials(HTTP_AUTHORIZATION='Token ' + token.key)

response = client.patch(self.url_people, serializer.data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
client.logout()

Documentation can be found at Testing Page.

Leave a comment