使用API​​RequestFactory测试基于令牌的身份验证的正确方法是什么?

时间:2014-08-27 18:17:45

标签: python django authentication integration-testing django-rest-framework

对我的端点的查询工作正常(只要我传递一个有效的令牌),它返回我的响应数据的json表示。

服务api中的代码,它调用我的端点,在头文件中传递一个身份验证令牌:

headers = {'content-type': 'application/json',
           'Authorization': 'Token {}'.format(myToken)}
url = 'http://localhost:8000/my_endpoint/'
r = session.get(url=url, params=params, headers=headers)

在views.py中,我有一个方法装饰器,它在视图上包装dispatch方法(viewsets.ReadOnlyModelViewSet):

def login_required(f):
    def check_login_and_call(request, *args, **kwargs):
        authentication = request.META.get('HTTP_AUTHORIZATION', b'')
        if isinstance(authentication, str):
            authentication = authentication.encode(HTTP_HEADER_ENCODING)
        key = authentication.split()
        if not key or len(key) != 2:
            raise PermissionDenied('Authentication failed.')
        user, token = authenticate_credentials(key[1])
        return f(request, *args, **kwargs)
    return check_login_and_call

我正在尝试使用令牌编写测试来验证请求:

from rest_framework.authtoken.models import Token
from rest_framework.test import APIRequestFactory
from rest_framework.test import APITestCase
from rest_framework.test import force_authenticate

class EndpointViewTest(APITestCase):
    def setUp(self):
        self.factory = APIRequestFactory()
        self.user = User.objects.create_user(
            username='user@foo.com', email='user@foo.com', password='top_secret')
        self.token = Token.objects.create(user=self.user)
        self.token.save()

    def test_token_auth(self):
        request = self.factory.get('/my_endpoint')
        force_authenticate(request, token=self.token.key)
        view = views.EndpointViewSet.as_view({'get': 'list'})
        response = view(request)
        self.assertEqual(response.status_code, 200)
        json_response = json.loads(response.render().content)['results']

出于某种原因,我无法获得正确传递此测试令牌的请求。使用force_authenticate似乎不会更改我用于验证令牌的标头。当前输出正在提高“PermissionDenied:身份验证失败”。因为没有在请求上设置令牌。

有没有一种正确的方法在我的测试中的请求标头中设置它或者重构我在第一时间使用它的方式?

4 个答案:

答案 0 :(得分:13)

我找到了一种让测试通过的方法,但如果您对如何处理这些问题有更好的了解,请发帖。

request = self.factory.get('/my_endpoint', HTTP_AUTHORIZATION='Token {}'.format(self.token))
force_authenticate(request, user=self.user)

更改上述两行测试后,似乎可以根据令牌进行正确的身份验证。

答案 1 :(得分:3)

很抱歉挖掘这个旧帖子,但如果有人使用APIClient()进行测试,您可以执行以下操作:

from rest_framework.test import APITestCase
from rest_framework.test import APIClient
from rest_framework.authtoken.models import Token
from django.contrib.auth.models import User


class VehicleCreationTests(APITestCase):

    def setUp(self):
        self.client = APIClient()
        self.user = User.objects.create_superuser('admin', 'admin@admin.com', 'admin123')
        self.token = Token.objects.create(user=self.user)

    def testcase(self):
        self.client.force_login(user=self.user)
        response = self.client.post('/api/vehicles/', data=vehicle_data, format='json', HTTP_AUTHORIZATION=self.token)
        self.assertEqual(response.status_code, 201)

我曾经提出的非常好的资源是django-rest-framework-jwt tests

答案 2 :(得分:2)

我想测试身份验证功能本身,因此强制进行身份验证不是一种选择。

正确传递令牌的一种方法是使用已经导入的APIClient

client = APIClient()
client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
response = client.get('/api/vehicles/')

这会将您指定的令牌设置到请求标头中,并让后端确定其是否有效。

答案 3 :(得分:0)

使用force_authentication中的内置方法来APITestCase的更简单方法是:

class Test(APITestCase):
    def setUp(self):
        user1 = User.objects.create_user(username='foo')
        self.client.force_authenticate(user=user1) # self.client is from APITestCase

    ... the rest of your tests ...
相关问题