from rest_framework.test import APIClient client = APIClient() # Authenticate a user with username and password client.force_authenticate(user=username, password=password)
from rest_framework.test import APIClient client = APIClient() # Authenticate a user with a token client.force_authenticate(user=None, token=token)In the above examples, the `APIClient` object is created and used to authenticate a user by either providing the user's username and password or a token. The `force_authenticate` method is called with either `user` and `password` arguments or `user=None` and a `token` argument. Package Library: `rest_framework.test` is part of the Django REST framework.