Django Rest Framework api测试上传文本文件application / x-www-form-urlencoded

时间:2018-04-24 19:01:14

标签: django django-rest-framework

我正在尝试测试我的Django REST API以进行文件上传。

问题是我的服务器尝试验证文件,以便文件具有可识别的文件类型。目前只允许基于文本的文件类型,例如:docs,pdf,txt。

我尝试过使用临时文件,现在我只是尝试从磁盘读取文件然后放弃了。

每当我使用临时文件时,服务器都会响应:

    {
        "cover_letter": [
            "The submitted data was not a file. Check the encoding type on the form."
        ],
        "manuscript": [
            "The submitted data was not a file. Check the encoding type on the form."
        ]
    }

这是我的测试:

 def test_user_can_submit_a_paper(self):
     """
         Ensure that an user is able to upload a paper.

         This test is not working.. yet
     """
     tmp_file = open("__init__.py", "w")
     data = {
         "title": "paper",
         "authors": "me",
         "description": "ma detailed description",
         "manuscript": base64.b64encode(tmp_file.read()).decode(),
         "cover_letter": base64.b64encode(tmp_file.read()).decode()
     }
     tmp_file.close()


     response = self.client.post(self.papers_submitted, data=urlencode(MultiValueDict(data)), content_type='application/x-www-form-urlencoded',
                                 HTTP_AUTHORIZATION=self.authorization_header)
     print(response.data)
     self.assertEqual(response.status_code, status.HTTP_200_OK)

     del data["cover_letter"]
     response = self.client.post(self.papers_submitted, data=urlencode(MultiValueDict(data)), content_type='application/x-www-form-urlencoded',
                                 HTTP_AUTHORIZATION=self.authorization_header)
     print(response.data)
     self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

我已经通过postman成功测试了这个端点,但我不知道如何在Python中进行测试。

2 个答案:

答案 0 :(得分:1)

如果我理解你的要求,你想使用python测试你的DRF端点文件上传。您应该使用模拟请求的RequestFactory - 它更像是使用Postman。

此回答Django: simulate HTTP requests in shell有一个使用RequestFactory的示例,此处有多个答案django RequestFactory file upload,针对如何使用RequestFactory包含上传文件的具体解决方案。

答案 1 :(得分:0)

感谢Mark的回答,我设法找到了解决方案。 如果有人有兴趣,这是代码:

def test_user_can_submit_a_paper(self):
    from api.journal import PaperListSubmitted
    from django.test.client import RequestFactory
    from django.core.files import temp as tempfile
    """
        Ensure that an user is able to upload a paper.
    """

    request_factory = RequestFactory()
    manuscript = tempfile.NamedTemporaryFile(suffix=".txt")
    cover_letter = tempfile.NamedTemporaryFile(suffix=".txt")
    manuscript.write(b"This is my stupid paper that required me to research writing this test for over 5h")
    cover_letter.write(b"This is my stupid paper that required me to research writing this test for over 5h")
    manuscript.seek(0)
    cover_letter.seek(0)

    post_data = {
        "title": "My post title",
        "description": "this is my paper description",
        "authors": "no authors",
        "manuscript": manuscript,
        "cover_letter": cover_letter
    }

    request = request_factory.post(self.papers_submitted, HTTP_AUTHORIZATION=self.authorization_header,
                                   data=post_data)

    response = PaperListSubmitted.as_view()(request)
    self.assertEqual(response.status_code, status.HTTP_200_OK)