Tuesday, 15 September 2015

python - Can't login a just-created user, in a Django test -



python - Can't login a just-created user, in a Django test -

in django testing class, self.client.login(...) returning false, , don't understand why. testing module

"""tests views.""" django.contrib.auth.models import user .models import userprofile django.test import testcase test_user = {"username": "kermit", "password": "timrek", "first_name": "kermit", "last_name": "the frog", "birth_year": 1955, "email": "kermit@muppets.com"} class viewtestcase(testcase): """tests views.""" def setup(self_ignored): """insert demo users.""" user = user( username=test_user['username'], password=test_user['password'], first_name=test_user['first_name'], last_name=test_user['last_name'], email=test_user['email']) user.save() profile = userprofile(user_id=user.id, birth_year=test_user['birth_year']) profile.save()

the function attempts log user in (this bottom portion of same file):

def test__view_for_logged_in_user(self): """verify expected content on main birth-stats page, logged-in user. """ print("test_user=" + test_user) did_login_succeed = self.client.login( username=test_user['username'], password=test_user['password']) #print inserted users users = user.objects.all() print("total users in database=" + str(users.count())) user in users: print('actually-inserted user: ' + str(user)) self.asserttrue(did_login_succeed) #fails #test view here

as can see in debugging output, test user indeed beingness inserted:

$ python manage.py test auth_lifecycle.test__views2 creating test database alias 'default'... test_user={'username': 'kermit', 'email': 'kermit@muppets.com', 'password': 'timrek', 'birth_year': 1955, 'last_name': 'the frog', 'first_name': 'kermit'} total users in database=1 actually-inserted user: kermit f ====================================================================== fail: test__view_for_logged_in_user (auth_lifecycle.test__views2.viewtestcase) verify expected content on main birth-stats page, ---------------------------------------------------------------------- traceback (most recent phone call last): file "/home/jeffy/django_files/django_auth_lifecycle/auth_lifecycle/test__views2.py", line 47, in test__view_for_logged_in_user self.asserttrue(did_login_succeed) assertionerror: false not true ---------------------------------------------------------------------- ran 1 test in 0.009s failed (failures=1) destroying test database alias 'default'...

why login failing (returning false)?

here's model

"""defines single user-profile field user-authentication lifecycle demo project: birth year. """ django.contrib.auth.models import user django.db import models class userprofile(models.model): """one piece of info user: birth year. ---notes--- useful related sql: - `select id auth_user username <> 'admin';` - `select * auth_lifecycle_userprofile user_id=(x,x,...);` """ # line required. links userprofile user model instance. user = models.onetoonefield(user, related_name="profile") # additional attributes wish include. birth_year = models.integerfield( blank=true, verbose_name="year born") # override __str__() method homecoming out meaningful def __str__(self): homecoming self.user.username

although

user = user( username=test_user['username'], password=test_user['password'], first_name=test_user['first_name'], last_name=test_user['last_name'], email=test_user['email']) user.save()

saves user database, password not hashed , salted (i'm think that's problem). therefore, attempting login same password fails (login returns false).

self.client.login( username=test_user['username'], password=test_user['password']) #not same in database!

the solution utilize create_user function instead:

user = user.objects.create_user( username=test_user['username'], password=test_user['password'], first_name=test_user['first_name'], last_name=test_user['last_name'], email=test_user['email'])

python django

No comments:

Post a Comment