简体   繁体   中英

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.

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.

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.

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:

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. (See Password management in 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.

I'd modify create_user to accept optional password parameter. And pass a password both to create_user , and client.login as follow:

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

create_user should use User.objects.create_user instead of 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 # <---

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM