Django的self.client.login(...)在单元测试中不起作用 [英] Django's self.client.login(...) does not work in unit tests
问题描述
我已经通过两种方式为单元测试创建了用户:
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",
...
}
}
不重要的部分。
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')
请注意,两种情况下的密码均为辛普森。
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')始终失败。我对为什么感到困惑。我想我已经阅读了与此相关的每个互联网讨论。有人可以帮忙吗?
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)
谢谢。
推荐答案
无效的代码:
$ b来自django.contrib.auth.models的$ b
The code that doesn't work:
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屋!