Note that there are some explanatory texts on larger screens.

plurals
  1. POTesting authentication in Django Rest Framework Views -- Cannot authenticate when testing
    primarykey
    data
    text
    <p>After struggling mightily with this issue, I've come asking for a bit of help. I'm writing a test for a Django Rest Framework view, testing whether or not I can access the data whilst authenticated, and not. However, even when I'm authenticated, I still get <code>401 UNAUTHORIZED</code> every time. Here's my tests:</p> <pre><code>from django.test import TestCase from django.contrib.auth.models import User from rest_framework.authtoken.models import Token from rest_framework.test import APIRequestFactory, APIClient from apps.core import models, admin from apps.rest import views class TestAPIViews(TestCase): def setUp(self): self.factory = APIRequestFactory() self.client = APIClient() self.user = User.objects.create_user('testuser', email='testuser@test.com', password='testing') self.user.save() token = Token.objects.create(user=self.user) token.save() def _require_login(self): self.client.login(username='testuser', password='testing') def test_ListAccounts_not_authenticated(self): request = self.factory.get('/accounts/') view = views.ListAccounts.as_view() response = view(request) self.assertEqual(response.status_code, 401, 'Expected Response Code 401, received {0} instead.'.format(response.status_code)) def test_ListAccounts_authenticated(self): self.client._require_login() print(self.user.is_authenticated()) # returns True request = self.factory.get('/accounts/') view = views.ListAccounts.as_view() response = view(request) self.assertEqual(response.status_code, 200, 'Expected Response Code 200, received {0} instead.'.format(response.status_code)) </code></pre> <p>And here is the code for my DRF View:</p> <pre><code>from django.shortcuts import render from django.contrib.auth import authenticate, login, logout from django.db.models import Q from apps.core import serializers, models from apps.rest.permissions import IsAccountOwner from rest_framework.views import APIView from rest_framework import status, authentication, generics from rest_framework.response import Response from rest_framework.authtoken.models import Token from rest_framework.authentication import SessionAuthentication, TokenAuthentication from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from apps.core import serializers, models ''' Auth Mixin for Account Admin Access ''' class AuthMixin: authentication_classes = (authentication.TokenAuthentication, authentication.SessionAuthentication) permission_classes = (IsAuthenticated,) class GenericList(AuthMixin, generics.ListAPIView): def get_queryset(self): # Stubbed out - doing nothing special right now qs = self.model.objects.filter() return qs class ListAccounts(GenericList): model = models.Account serializer_class = serializers.AccountSerializer </code></pre> <p>As one can see, I'm calling login in the <code>test_ListAccounts_authenticated</code>, and then printing out whether or not I'm authenticated (Which returns <code>True</code>), but I get a <code>401 UNAUTHORIZED</code> Error no matter what. Anything I'm missing? Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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