Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django REST API:测试代码中的重复数据消除授权检查_Python_Django_Django Rest Framework - Fatal编程技术网

Python Django REST API:测试代码中的重复数据消除授权检查

Python Django REST API:测试代码中的重复数据消除授权检查,python,django,django-rest-framework,Python,Django,Django Rest Framework,在我正在制作的聊天应用程序中,我将这个(类似的)复制并粘贴在我编写的每个API测试的顶部,以检查未经授权的访问 def test_message(self): # Unauthorized - 401 client = APIClient() url = reverse("chat-messages-list", host="api", kwargs={"pk": self.cha

在我正在制作的聊天应用程序中,我将这个(类似的)复制并粘贴在我编写的每个API测试的顶部,以检查未经授权的访问

    def test_message(self):
        # Unauthorized - 401
        client = APIClient()
        url = reverse("chat-messages-list", host="api", kwargs={"pk": self.chatroom.id},)
        response = client.post(url, {"text": "this is a test message"})
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

        # Blocked - 403
        client.force_authenticate(user=self.user1)
        response = client.post(url, {"text": "this is a test message"})
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
是否有一种更具可组合性的方法来构造它,这样我就不需要在每个API测试的顶部复制和粘贴它