如何修改测试更改密码视图(Django REST Framework)?

时间:2018-04-12 08:29:17

标签: python django unit-testing django-rest-framework django-testing

在视图中,测试不会将request.data作为DRF的元素。 我有这个观点:

class ChangePasswordView(UpdateAPIView):

    serializer_class = ChangePasswordSerializer
    model = UserInfo
    permission_classes = (IsAuthenticated,)

    def get(self, request, *args, **kwargs):
        return Response(status=200)

    def put(self, request, *args, **kwargs):
        self.object = self.request.user
        serializer = self.get_serializer(data=request.data)

        if serializer.is_valid():
            if not self.object.check_password(serializer.data.get("old_password")):
                return Response(
                    {"old_password": ["Wrong password"]},
                    status=400
                )
            self.object.set_password(serializer.data.get("new_password"))
            self.object.save()
            return Response("Success", status=200)

        return Response(serializer.errors, status=400)

我有这种测试方法:

def test_change_password(self):
    client = APIClient()
    self.client.post('/api/user/login/', self.data, follow=True)
    self.client.login(username="testuser", password="secret")
    request = client.put('/api/user/change_password/', self.new_data)

    self.assertEqual(
         request, '<Response status_code=200, "application/json">'
    )

其中数据:

def setUp(self):
    self.data = {
        'username': 'testuser',
        'password': 'secret'
    }

    self.new_data = {
        'old_password': 'secret',
        'new_password': 'other_secret'
    }

    User.objects.create_user(**self.data)

为什么测试在serializer = self.get_serializer(data = request.data)中的reqest.data停止?

1 个答案:

答案 0 :(得分:0)

我猜你是让用户弄错了。

这可能会解决您的问题:

this.service.getData()
       .flatMap(data => {
                  if(data.xxx==='yyyy'){
                             return Observable.of({    //LINE 1
                                      payload : data.objectx;
                               });
                    }else{
                             return this.service.getAnotherData()
                                        .map(newData => {
                                                 return {
                                                         payload: newData.objectx;

                                                  }
                                         });
                    }

        }).Subscribe(res => this.payload =res.payload)

我正在使用from rest_framework import status class ChangePasswordView(UpdateAPIView): serializer_class = ChangePasswordSerializer model = UserInfo permission_classes = (IsAuthenticated,) def get(self, request, *args, **kwargs): return Response(status=status.HTTP_200_OK) def post(self, request, *args, **kwargs): user = request.user serializer = self.get_serializer(data=request.data) if serializer.is_valid(): if not self.object.check_password(serializer.data.get("old_password")): msg = _("Wrong old password.") raise ValidationError(msg) user.set_password(serializer.data.get("new_password")) user.save() return Response("Success", status=status.HTTP_200_OK) msg = _('Something went wrong while reading the passwords') raise ValidationError(msg) 中导入的status来更轻松地处理错误代码。

您使用rest_framework是否有任何特定原因?我已将其更改为put。在进行此API调用(更改密码)时,您应该使用post。

您可以使用post轻松获取用户,然后在put函数中使用变量request.user来设置密码并保存。

关于测试用例,这可能是一个帮助和/或为您工作。如果它不起作用,您可能需要稍微更改一下。

user