为 Django 编写测试用例已验证 API 查看
TestCase writing for Django authenticated API View
我已经成功编写了 TestCase,并且运行良好。
先看看我的代码:
下面是我的tests.py
from django.shortcuts import reverse
from rest_framework.test import APITestCase
from ng.models import Contact
class TestNoteApi(APITestCase):
def setUp(self):
# create movie
self.contact = Contact(userId=254, name="The Space Between Us", phone=2017, email='doe@f.com')
self.contact.save()
def test_movie_creation(self):
response = self.client.post(reverse('getAndPost'), {
'userId': 253,
'name': 'Bee Movie',
'phone': 2007,
'email': 'ad@kjfd.com'
})
self.assertEqual(Contact.objects.count(), 2)
上面的代码片段工作正常,但问题是..一旦我实现了身份验证系统,它就不起作用了
下面是我的settings.py
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
)
}
如果我在允许的情况下更改为 AllowAny
,测试效果很好,但如果保留 IsAuthenticated
而不是 AllowAny
,则无法正常工作。
我希望测试应该 运行 很好,即使我得到 IsAuthenticated
的许可。
谁能建议我该怎么做?我没有得到要更改的内容或添加到我的 tests.py
文件中的内容。
您应该在 setUp
方法中创建 user
对象,并使用 client.login()
or force_authenticate()
进行身份验证请求:
class TestNoteApi(APITestCase):
def setUp(self):
# create user
self.user = User.objects.create(username="test", password="test")
# create movie
self.contact = Contact(userId=254, name="The Space Between Us", phone=2017, email='doe@f.com')
self.contact.save()
def test_movie_creation(self):
# authenticate client before request
self.client.login(username='test', password='test')
# or
self.clint.force_authenticate(user=self.user)
response = self.client.post(reverse('getAndPost'), {
'userId': 253,
'name': 'Bee Movie',
'phone': 2007,
'email': 'ad@kjfd.com'
})
self.assertEqual(Contact.objects.count(), 2)
我已经成功编写了 TestCase,并且运行良好。
先看看我的代码:
下面是我的tests.py
from django.shortcuts import reverse
from rest_framework.test import APITestCase
from ng.models import Contact
class TestNoteApi(APITestCase):
def setUp(self):
# create movie
self.contact = Contact(userId=254, name="The Space Between Us", phone=2017, email='doe@f.com')
self.contact.save()
def test_movie_creation(self):
response = self.client.post(reverse('getAndPost'), {
'userId': 253,
'name': 'Bee Movie',
'phone': 2007,
'email': 'ad@kjfd.com'
})
self.assertEqual(Contact.objects.count(), 2)
上面的代码片段工作正常,但问题是..一旦我实现了身份验证系统,它就不起作用了
下面是我的settings.py
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
)
}
如果我在允许的情况下更改为 AllowAny
,测试效果很好,但如果保留 IsAuthenticated
而不是 AllowAny
,则无法正常工作。
我希望测试应该 运行 很好,即使我得到 IsAuthenticated
的许可。
谁能建议我该怎么做?我没有得到要更改的内容或添加到我的 tests.py
文件中的内容。
您应该在 setUp
方法中创建 user
对象,并使用 client.login()
or force_authenticate()
进行身份验证请求:
class TestNoteApi(APITestCase):
def setUp(self):
# create user
self.user = User.objects.create(username="test", password="test")
# create movie
self.contact = Contact(userId=254, name="The Space Between Us", phone=2017, email='doe@f.com')
self.contact.save()
def test_movie_creation(self):
# authenticate client before request
self.client.login(username='test', password='test')
# or
self.clint.force_authenticate(user=self.user)
response = self.client.post(reverse('getAndPost'), {
'userId': 253,
'name': 'Bee Movie',
'phone': 2007,
'email': 'ad@kjfd.com'
})
self.assertEqual(Contact.objects.count(), 2)