mercredi 14 novembre 2018

How do I write a test to test this custom serializer for password reset?

Version:

  • Django 1.11
  • Python 3.6

I am close to 100% test coverage and I got this custom serializer, which I am not sure how to write tests for.

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

How do I write a unit test for this serializer class?

Aucun commentaire:

Enregistrer un commentaire