[英]Django test client does not log in
I am attempting to log in the test client using its built in login function. 我正在尝试使用其内置的登录功能登录测试客户端。 I am trying to unit test views and need to log in to test some of them.
我正在尝试单元测试视图,需要登录才能测试其中的一些。 I have been trying to do this for too long and need help.
我一直试图这么做并需要帮助。 A few notes:
几点说明:
create_user() does create a valid user, It has been used in other locations. create_user()确实创建了一个有效的用户,它已在其他位置使用。
From what I have seen about client.login() it returns a boolean, when I ran my tests the failure was "False is not True", so this appears to be correct. 从我所看到的client.login()它返回一个布尔值,当我运行我的测试失败是“假不是真”,所以这似乎是正确的。
The only way I have successfully logged in is by calling client.post("/my/login/url", {username and password in dict.}) However, for some reason It does not stay logged in for all of my test cases which I find VERY strange. 我成功登录的唯一方法是调用client.post(“/ my / login / url”,{dict中的用户名和密码。}}但是,出于某种原因它并没有为我的所有测试用例保持登录状态我觉得很奇怪。
def setUp(self):
"""
Initializes the test client and logs it in.
"""
self.user = create_user()
self.logged_in = self.client.login(username=self.user.username, password=self.user.password)
def test_valid(self):
self.assertTrue(self.logged_in)
I have changed it to the following: 我已将其更改为以下内容:
def setUp(self):
"""
Initializes the test client and logs it in.
"""
self.password = "password"
self.user = create_user(password=self.password)
self.logged_in = self.client.login(username=self.user.username, password=self.password)
It still fails to log in. 它仍然无法登录。
create user is in class "Static" and has a user_count initialized as 0, the function is as follows: create user在类“Static”中并且user_count初始化为0,函数如下:
def create_user(username=None, password=None, email=None, is_superuser=False):
if username is None:
username = "user%d" % Static.user_count
while User.objects.filter(username=username).count() != 0:
Static.user_count += 1
username = "user%d" % Static.user_count
if password is None:
password = "password"
if email is None:
email="user%d@test.com" % Static.user_count
Static.user_count += 1
user = User.objects.create(username=username, password=password, is_superuser=is_superuser)
You cannot access the password directly. 您无法直接访问密码。 The
password
attribute is encrypted. password
属性已加密。 (See Password management in Django .) (请参阅Django中的密码管理 。)
For example, here sample output of password. 例如,这里是密码的示例输出。
>>> user = User.objects.create_user(username='asdf', email='asdf@example.com', password='xxxx')
>>> user.password
'sha1$166e7$4028738f0c0df0e7ec3cec06843c35d2b5a1aae8'
As you can see, user.password
is not xxxx
I given. 如您所见,
user.password
不是我给出的xxxx
。
I'd modify create_user
to accept optional password parameter. 我修改
create_user
以接受可选的密码参数。 And pass a password both to create_user
, and client.login
as follow: 并将密码传递给
create_user
和client.login
,如下所示:
def setUp(self):
"""
Initializes the test client and logs it in.
"""
password = 'secret'
self.user = create_user(password=password)
self.logged_in = self.client.login(username=self.user.username, password=password)
UPDATE UPDATE
create_user
should use User.objects.create_user
instead of User.objects.create
. create_user
应使用User.objects.create_user
而不是User.objects.create
。 And the created user object should be returned: 并且应该返回创建的用户对象:
def create_user(username=None, password=None, email=None, is_superuser=False):
if username is None:
username = "user%d" % Static.user_count
while User.objects.filter(username=username).count() != 0:
Static.user_count += 1
username = "user%d" % Static.user_count
if password is None:
password = "password"
if email is None:
email="user%d@test.com" % Static.user_count
Static.user_count += 1
user = User.objects.create_user(username=username, password=password)
# ^^^^^^^^^^^
user.is_superuser = is_superuser
user.save()
return user # <---
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.