如何在Django Rest框架中通过用户令牌进行API测试? [英] How to pass the user token for API testing in django rest framework?

查看:49
本文介绍了如何在Django Rest框架中通过用户令牌进行API测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些测试来检查与我的API的连接.

I'm writing some tests to check the connection to my API.

我已经通过令牌进行了身份识别,并且成功通过以下方式为特定的测试用户检索了令牌:

I've put in place identification via tokens and I am successful with retrieving a token for a specific test user with :

token = Token.objects.get(user__username='testuser')

我正在努力的是使用该令牌创建一个成功的API请求,就像这样:

What I'm struggling with is to use that token to create a successful API request as this one :

client = APIClient(HTTP_AUTHORIZATION='Token ' + token.key)
response = client.get('/patientFull/1/',headers={'Authorization': 'Token ' + token.key})

我一直在寻找许多方法来完成这项工作,而这些是我试图做到的一些方法:

I have been looking at many ways to make this work and these are some ways I tried to do it :

response = requests.get('http://127.0.0.1:8000/patientFull/1/',headers={'Authorization': 'Token ' + token.key} )


client = APIClient()
client.credentials(HTTP_AUTHORIZATION='Token ' + token.key)
response = client.get('/patientFull/1/')

该测试是一个简单的断言,用于检查响应是否具有来自服务器的200 OK HTTP答案.

The test is a simple assert to check response has a 200 OK HTTP answer from the server.

以上所有这些方式均会返回403 HTTP响应.

All of these ways above returns a 403 HTTP response.

这是我的测试的完整代码(我正在使用夹具用测试数据填充我的测试数据库):

here's the full code of my test (I'm using fixtures to populate my test database with testing data):

import json
import requests
from rest_framework.authtoken.models import Token
from rest_framework.test import APIRequestFactory, APITestCase, APIClient


class CustomerAPITestBack(APITestCase):
    fixtures = ['new-fixtures.json']

    def testDE(self):
        token = Token.objects.get(user__username='jpmichel')
        client = APIClient(HTTP_AUTHORIZATION='Token ' + token.key)
        response = client.get('/patientFull/1/',headers={'Authorization': 'Token ' + token.key})
        self.assertEqual(200, response.status_code)

我已经将我的settings.py文件配置为令牌:

I have configured my settings.py file as so for the tokens :

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'PHCAdmin.authentication.tokenAuthentication.ExpiringTokenAuthentication',
    ),
    'EXCEPTION_HANDLER': 'PHCAdmin.functions.pxlth_exception_handler',
}

REST_FRAMEWORK_EXPIRY_TIME = 12 # in hours
REST_FRAMEWORK_PASSWORD_RENEWALS = 90 # in days

如果我禁用令牌身份验证,则此测试通过(GET返回200 OK)

If I disable the token authentication, this test passes (the GET returns a 200 OK)

我应该如何执行GET请求,以便它使用令牌将其标识为有效用户并返回200 OK HTTP响应?

How should I do my GET request so that it uses the token to identify as a valid user and returns a 200 OK HTTP response?

推荐答案

只需关闭此问题:经过研究后,我发现服务器上的令牌与本地计算机上的令牌不同,我只需要在两边都更新令牌.上面的代码可以正常工作.

Just to close this question: after some research, I found out the token on the server was not the same as on the local machine, I just needed to update tokens on both side. The code above works fine.

这篇关于如何在Django Rest框架中通过用户令牌进行API测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆