#Testing admin-restricted viewsets in rest_framework

4 messages · Page 1 of 1 (latest)

pale pewter
#

Recently finished writing a REST API using rest_framework. I am trying to test my viewsets using APITestCase, to make sure that the viewset support exactly the CRUD actions accessible to the authenticated user.

All but one sort of requests pass the tests. The requests that don't pass the test are those made against the only viewset using permissions.IsAdminUser. Instead of the expected results I am getting 401: Unauthorized authentication credentials. Here's the code in a nutshell:

admin_user = User.objects.create_user(
  username="admin_user",
  password="password",
  is_staff=True,
)

data = ...
detail_url = ...
items_url = ...

client = APITestCase()
client.force_authenticate(user=admin_user)
client.put(detail_url, data) # testing 'update' CRUD action
client.patch(detail_url, data) # testing 'partial_update' CRUD action
client.post(items_url, data) # testing 'create' CRUD action

I would to authenticate these requests so that the above actions succeed. Any help would be greatly appreciated.

graceful isle
#

Try to print the authentication status after forcing the authentication

pale pewter
#

Thanks for the suggestion, will do

pale pewter
#

Oh and by the way, how do you get the authentication status?