15

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)
falsetru
  • 357,413
  • 63
  • 732
  • 636
mdw7326
  • 434
  • 7
  • 28

1 Answers1

21

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 # <---
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • So is there a way to get the password from user? or Would I just have to know the password? – mdw7326 Nov 07 '13 at 03:25
  • @user2278110, As mentioned in the answer, password is encrypted (hashed), and cannot be decrypted. You should pass it to `create_user` (probably need modification to the `create_user`) and use that password instead of `self.user.password`. – falsetru Nov 07 '13 at 03:26
  • @user2278110, Could you show the content of the `create_user` function? – falsetru Nov 07 '13 at 03:39
  • It is now there but I am fairly certain the issue is not in create_user – mdw7326 Nov 07 '13 at 03:46
  • @user2278110, Where is the return statement in `create_user`? – falsetru Nov 07 '13 at 03:48
  • @user2278110, And if if `create_user` in the `Static` class, why don't you call it as `Static.create_user(..)`? – falsetru Nov 07 '13 at 03:51
  • @user2278110, I updated the answer with `create_user`. Check it out. – falsetru Nov 07 '13 at 04:23