gpt4 book ai didi

python-3.x - user.check_password 断言失败

转载 作者:行者123 更新时间:2023-12-04 04:10:19 36 4
gpt4 key购买 nike

我有一些测试试图在 API 应用程序中验证我的用户创建过程。

我遇到的问题是,当我尝试验证用户密码是否创建为散列对象时,测试失败。

测试:

CREATE_USER_URL = reverse('user:create')


def create_user(**params):
return get_user_model().objects.create_user(**params)
...

def test_create_valid_user_success(self):
"""Test creating user with valid user is successful"""
payload = {
'email': 'test@email.com',
'password': 'testpass',
'name': 'Test Name'
}
res = self.client.post(CREATE_USER_URL, payload)

self.assertEqual(res.status_code, status.HTTP_201_CREATED)
user = get_user_model().objects.get(**res.data)
self.assertTrue(user.check_password(payload['password']))
self.assertNotIn('password', res.data)

序列化器:

from django.contrib.auth import get_user_model

from rest_framework import serializers


class UserSerializer(serializers.ModelSerializer):
"""Serializer for users object"""

class Meta:
model = get_user_model()
fields = ('email', 'password', 'name')
extra_kwargs = {
'password': {
'write_only': True,
'min_length': 8
}
}

def create(self, validated_data):
"""Create a new user with encrypted password and return it"""
return get_user_model().objects.create_user(**validated_data)

失败的测试:

FAIL: test_create_valid_user_success (user.tests.test_user_api.PublicUserApiTests)
Test creating user with valid user is successful
----------------------------------------------------------------------
Traceback (most recent call last):
File "/app/user/tests/test_user_api.py", line 33, in test_create_valid_user_success
self.assertTrue(user.check_password(payload['password']))
AssertionError: False is not true

根据我从文档等中可以看出的,我有正确的语法。

Wjhat 缺失/不正确,我需要修复此错误?

最佳答案

原来问题出在我的 UserSerializer 类中。

查看此问题:

Django users being created with cleartext passwords

关于python-3.x - user.check_password 断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61856983/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com