如何编写测试以测试此自定义序列化程序的密码重置?

时间:2018-11-15 06:26:00

标签: django unit-testing testing

版本:

  • Django 1.11
  • Python 3.6

我接近100%的测试覆盖率,并且得到了这个自定义序列化程序,我不确定该如何编写测试程序。

from rest_auth.serializers import PasswordResetSerializer, \
    PasswordResetConfirmSerializer as BasePasswordResetConfirmSerializer, TokenSerializer as BaseTokenSerializer
from rest_framework import serializers

class PasswordResetConfirmSerializer(BasePasswordResetConfirmSerializer):

    def validate(self, attrs):
        self._errors = {}

        # Decode the uidb64 to uid to get User object
        try:
            uid = url_str_to_user_pk(attrs['uid'])
            self.user = UserModel._default_manager.get(pk=uid)
        except (TypeError, ValueError, OverflowError, UserModel.DoesNotExist):
            raise ValidationError({'uid': ['Invalid value']})

        self.custom_validation(attrs)
        # Construct SetPasswordForm instance
        self.set_password_form = self.set_password_form_class(
            user=self.user, data=attrs
        )
        if not self.set_password_form.is_valid():
            raise serializers.ValidationError(self.set_password_form.errors)
        if not default_token_generator.check_token(self.user, attrs['token']):
            raise ValidationError({'token': ['Invalid value']})

        return attrs

如何为该序列化程序类编写单元测试?

0 个答案:

没有答案