gpt4 book ai didi

python - 如何为受身份验证保护的 REST API 编写 Django 单元测试?

转载 作者:行者123 更新时间:2023-12-05 04:03:55 24 4
gpt4 key购买 nike

我已经看过关于 SO 的两个答案,它们可能已经有了我的答案,但坦率地说,我只是不理解它们。这些 SO 帖子是:Django Rest Framework API Authentication TestDjango authenticate unit test .

两个 SO 问题使用不同的方法。

我尝试使用的方法是这样的。我创建了这个类:

from rest_framework.test import APIClient
from django.test import testcases
from django.contrib.auth.models import User

class RemoteAuthenticatedTest(testcases.TestCase):
client_class = APIClient

def setUp(self):
self.username = 'mister_neutron'
self.user = User.objects.create_user(username='mister_neutron',
email='mister_neutron@example.com',
password='F4kePaSs0d')
super(RemoteAuthenticatedTest, self).setUp()

我的单元测试是这样的:

class InfoViewTestCase(RemoteAuthenticatedTest):
def create_info_record(self):
from random import randint
blade = 'test-blade-host-name-%s' % (randint(0, 100))
breachs = randint(0,100)
dimm = 'test dimm slot %s' % (randint(0,100))
url = reverse('info:info_creat')
data = {
'blade_hostname': blade,
'breach_count': breachs,
'dimm_slot': dimm,
}
response = self.client.post(url,
data,
format='json',
REMOTE_USER=self.username)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Info.objects.count(), 1)
self.assertEqual(Info.objects.get().blade_hostname, blade)

我的 settings.py 文件中有这个:

#Authentications
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
}

当我运行我的测试时,我得到了这个结果:

Failure
Traceback (most recent call last):
File "/myproject/info/tests.py", line 686, in create_info_record
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
AssertionError: 401 != 201

我做错了什么?

更新:在遵循@henriquesalvaro 的明智建议后,我将代码更新为:

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

class RemoteAuthenticatedTest(APITestCase):
client_class = APIClient

def setUp(self):
self.username = 'mister_neutron'
self.user = User.objects.create_user(username='mister_neutron',
email='mister_neutron@example.com',
password='F4kePaSs0d')
Token.objects.create(user=self.user)
super(RemoteAuthenticatedTest, self).setUp()

然后我将我的测试用例更新为:

class InfoViewTestCase(RemoteAuthenticatedTest):
def create_info_record(self):
from random import randint
blade = 'test-blade-host-name-%s' % (randint(0, 100))
breachs = randint(0,100)
dimm = 'test dimm slot %s' % (randint(0,100))
url = reverse('info:info_creat')
data = {
'blade_hostname': blade,
'breach_count': breachs,
'dimm_slot': dimm,
}
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.user.auth_token.key)
response = self.client.post(url,
data,
format='json',
REMOTE_USER=self.username)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Info.objects.count(), 1)
self.assertEqual(Info.objects.get().blade_hostname, blade)

现在我的单元测试通过了。

最佳答案

在遵循@henriquesalvaro(见评论)的明智建议后,我已将我的代码更新为:

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

class RemoteAuthenticatedTest(APITestCase):
client_class = APIClient

def setUp(self):
self.username = 'mister_neutron'
self.user = User.objects.create_user(username='mister_neutron',
email='mister_neutron@example.com',
password='F4kePaSs0d')
Token.objects.create(user=self.user)
super(RemoteAuthenticatedTest, self).setUp()

然后我将我的测试用例更新为:

class InfoViewTestCase(RemoteAuthenticatedTest):
def create_info_record(self):
from random import randint
blade = 'test-blade-host-name-%s' % (randint(0, 100))
breachs = randint(0,100)
dimm = 'test dimm slot %s' % (randint(0,100))
url = reverse('info:info_creat')
data = {
'blade_hostname': blade,
'breach_count': breachs,
'dimm_slot': dimm,
}
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.user.auth_token.key)
response = self.client.post(url,
data,
format='json',
REMOTE_USER=self.username)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Info.objects.count(), 1)
self.assertEqual(Info.objects.get().blade_hostname, blade)

现在我的单元测试通过了。

关于python - 如何为受身份验证保护的 REST API 编写 Django 单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53107824/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com