Django 的 self.client.login(...) 在单元测试中不起作用 [英] Django's self.client.login(...) does not work in unit tests

查看:31
本文介绍了Django 的 self.client.login(...) 在单元测试中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过两种方式为我的单元测试创​​建了用户:

I have created users for my unit tests in two ways:

1) 为auth.user"创建一个夹具,大致如下所示:

1) Create a fixture for "auth.user" that looks roughly like this:

    { 
        "pk": 1, 
        "model": "auth.user", 
        "fields": { 
            "username": "homer", 
            "is_active": 1, 
            "password": 
"sha1$72cd3$4935449e2cd7efb8b3723fb9958fe3bb100a30f2", 
            ... 
        } 
    }

我省略了看似不重要的部分.

I've left out the seemingly unimportant parts.

2) 在 setUp 函数中使用create_user"(虽然我宁愿保留我的固定装置课程中的所有内容):

2) Use 'create_user' in the setUp function (although I'd rather keep everything in my fixtures class):

def setUp(self): 
       User.objects.create_user('homer', 'ho...@simpson.net', 'simpson') 

请注意,两种情况下的密码都是 simpson.

Note that the password is simpson in both cases.

我已经一次又一次地验证了这些信息是否正确加载到测试数据库中.我可以使用 User.objects.get 获取 User 对象.我可以使用check_password"验证密码是否正确.用户处于活动状态.

I've verified that this info is correctly being loaded into the test database time and time again. I can grab the User object using User.objects.get. I can verify the password is correct using 'check_password.' The user is active.

然而,self.client.login(username='homer', password='simpson') 总是失败.我很困惑为什么.我想我已经阅读了与此相关的每一个 Internet 讨论.有人可以帮忙吗?

Yet, invariably, self.client.login(username='homer', password='simpson') FAILS. I'm baffled as to why. I think I've read every single Internet discussion pertaining to this. Can anybody help?

我的单元测试中的登录代码如下所示:

The login code in my unit test looks like this:

    login = self.client.login(username='homer', password='simpson') 
    self.assertTrue(login) 

谢谢.

推荐答案

行不通的代码:

from django.contrib.auth.models import User
from django.test import Client

user = User.objects.create(username='testuser', password='12345')

c = Client()
logged_in = c.login(username='testuser', password='12345')

为什么它不起作用?

在上面的代码段中,当创建 User 时,实际的密码哈希设置为 12345.当客户端调用 login 方法时,password 参数的值,12345,通过哈希函数传递,结果类似于

Why doesn't it work?

In the snippet above, when the User is created the actual password hash is set to be 12345. When the client calls the login method, the value of the password argument, 12345, is passed through the hash function, resulting in something like

hash('12345') = 'adkfh5lkad438....'

然后将其与存储在数据库中的哈希值进行比较,客户端被拒绝访问,因为 'adkfh5lkad438....' !='12345'

This is then compared to the hash stored in the database, and the client is denied access because 'adkfh5lkad438....' != '12345'

正确的做法是调用set_password 函数,该函数通过散列函数传递给定的字符串并将结果存储在User.password 中.

The proper thing to do is call the set_password function, which passes the given string through the hash function and stores the result in User.password.

另外,在调用set_password之后,我们必须将更新后的User对象保存到数据库中:

In addition, after calling set_password we must save the updated User object to the database:

user = User.objects.create(username='testuser')
user.set_password('12345')
user.save()

c = Client()
logged_in = c.login(username='testuser', password='12345')

这篇关于Django 的 self.client.login(...) 在单元测试中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆