Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango unittest session not working as expected
    text
    copied!<p>I have simple view test. I want to simple test '/users/' but the problem my app require users to login. This is explaned here </p> <p><a href="https://stackoverflow.com/questions/18578265/django-unittests-client-login-fails-in-test-suite-but-not-in-shell?rq=1">Django Unittests Client Login: fails in test suite, but not in Shell</a></p> <pre><code>login_ret = self._client.login(username=self._username, password=self._password) </code></pre> <p>and it works as expected. So I create test user as explained here and it gets logged in correctly. Now, I want to get response from '/user/' with this code</p> <pre><code>ret = self._client.get('/users/') </code></pre> <p>and this return </p> <p>status code: 403</p> <p>content: {"detail": "You do not have permission to perform this action."}</p> <p>It seams it doesn't retain session from login method.</p> <p>What's problem here? </p> <p>This is complete code</p> <pre><code>self._client = Client() self._username = 'testuser' self._email = 'test@test.com' self._password = 'test' self._test_user = User.objects.create_user(self._username, self._email, self._password) login_ret = self.client.login(username=self._username, password=self._password) self.assertEqual(login_ret, True) </code></pre> <p>I've just created basic Django REST API application and added auth module, so complete view code is this (rest_api is name of application)</p> <pre><code>from django.contrib.auth.models import User, Group from rest_framework import viewsets from rest_api.serializers import UserSerializer, GroupSerializer class UserViewSet(viewsets.ModelViewSet): queryset = User.objects.all() serializer_class = UserSerializer class GroupViewSet(viewsets.ModelViewSet): queryset = Group.objects.all() serializer_class = GroupSerializer </code></pre> <p>serializers.py</p> <pre><code>from django.contrib.auth.models import User, Group from rest_framework import serializers class UserSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = User fields = ('url', 'username', 'email', 'groups') class GroupSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Group fields = ('url', 'name') </code></pre> <p>models.py is empty</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload