我刚刚开始使用Django REST Framework,我试图测试一个简单的文件上传点。
我已按如下方式编写了准系统代码:
models.py
class Picture(models.Model):
some_field = models.ForeignKey(SomeModel)
image = models.ImageField()
views.py
class PhotoUpload(APIView):
def post(self, request, filename, format='multipart'):
image_file = request.FILES['file']
print(image_file)
return Response(status=204)
urls.py
url(r'^api/photos/', PhotoUpload.as_view()),
通过文档,我已经编写了一个示例测试用例如下:
tests.py
class PhotoUploadTests(TestCase):
def setUp(self):
self.oauth_header = _get_oauth2_header()
def test_photo_upload(self):
c = Client()
response = c.get('/api/photos/', **self.oauth_header)
然而,我被困在这里,我不知道如何继续。
我也尝试过如下使用curl:
curl --form image=@test.jpg http://localhost:8000/api/photos
但是这给了我
"Authentication credentials were not provided."
。如何测试我的终端?
答案 0 :(得分:0)
以下是您需要登录时的测试示例(在这种情况下,它使用超级用户):
# available to those with superuser permissions
self.superuser = User.objects.create_superuser(
username='superuser',
password='password'
)
self.client.login(username='superuser', password='password')
url = reverse('myurl', kwargs={})
request = self.client.get(url, format='json')