Django测试客户端无法登录

mdw*_*326 15 python django unit-testing django-testing

我正在尝试使用其内置的登录功能登录测试客户端.我正在尝试单元测试视图,需要登录才能测试其中的一些.我一直试图这么做并需要帮助.几点说明:

create_user()确实创建了一个有效的用户,它已在其他位置使用.

从我所看到的client.login()它返回一个布尔值,当我运行我的测试失败是"假不是真",所以这似乎是正确的.

我成功登录的唯一方法是调用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)
Run Code Online (Sandbox Code Playgroud)

我已将其更改为以下内容:

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)
Run Code Online (Sandbox Code Playgroud)

它仍然无法登录.

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)
Run Code Online (Sandbox Code Playgroud)

fal*_*tru 21

您无法直接访问密码.该password属性已加密.(请参阅Django中的密码管理.)

例如,这里是密码的示例输出.

>>> user = User.objects.create_user(username='asdf', email='asdf@example.com', password='xxxx')
>>> user.password
'sha1$166e7$4028738f0c0df0e7ec3cec06843c35d2b5a1aae8'
Run Code Online (Sandbox Code Playgroud)

如你所见,user.password不是xxxx我给的.

我修改create_user为接受可选的密码参数.并传递密码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)
Run Code Online (Sandbox Code Playgroud)

UPDATE

create_user应该用User.objects.create_user而不是User.objects.create.并且应该返回创建的用户对象:

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 # <---
Run Code Online (Sandbox Code Playgroud)