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 # <---
Source:stackexchange.com