TestCase Client.login() fails

280 views
Skip to first unread message

jondbaker

unread,
Mar 28, 2012, 2:05:49 AM3/28/12
to django...@googlegroups.com
I'm trying to write a unit test that will verify that the login form authenticates a user. Whenever I run 'manage.py test' the runner fails with this message:
AssertionError: False is not True

tests.py
from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User

class TestAccountLogin(TestCase):
    def setUp(self):
        self.client = Client()

    def test_login(self):
        user = User.objects.create_user('test', 'te...@test.com', 't3stp@s$')
        response = self.client.login(username=user.username, password=user.password)
        self.assertTrue(response)

After creating the user, I can verify that user.is_active is in fact True, but unfortunately response returns False. Both 'django.contrib.auth.middleware.AuthenticationMiddleware' and 'django.contrib.auth' are declared in settings.py.

Any thoughts?

jondbaker

unread,
Mar 28, 2012, 2:26:54 AM3/28/12
to django...@googlegroups.com
Problem solved. I forgot that .create_user will create and return a hashed password, which obviously won't work if said returned object password property is entered into the password field of the login form.

Reinout van Rees

unread,
Mar 28, 2012, 4:27:44 AM3/28/12
to django...@googlegroups.com
On 28-03-12 08:05, jondbaker wrote:

> def test_login(self):
> user = User.objects.create_user('test', 'te...@test.com', 't3stp@s$')
> response = self.client.login(username=user.username, password=user.password)
> self.assertTrue(response)
>
> After creating the user, I can verify that user.is_active is in fact
> True, but unfortunately response returns False. Both
> 'django.contrib.auth.middleware.AuthenticationMiddleware' and
> 'django.contrib.auth' are declared in settings.py.
>
> Any thoughts?

Yes: You have to call user.save() after creating it. Otherwise the user
object exists, but it isn't saved to the database yet. And
"self.client.login()" queries the database, not user objects local to
the test.


Creating model objects in tests and testing them afterwards is something
that needs a bit of care. I've made several mistakes with them already
:-) Look at [1] for an example error.


Reinout

[1]:
http://reinout.vanrees.org/weblog/2011/11/18/django_unicodedecodeerror.html

--
Reinout van Rees http://reinout.vanrees.org/
rei...@vanrees.org http://www.nelen-schuurmans.nl/
"If you're not sure what to do, make something. -- Paul Graham"

Alexey Kinyov

unread,
Mar 28, 2012, 3:28:56 AM3/28/12
to django...@googlegroups.com
Hello, Jonathan!

I think issue is in the fragment 'password=user.password' in the line:

>         response = self.client.login(username=user.username,
> password=user.password)

'user.password' - is not plain text password, it's encrypted and it's
not equal 't3stp@s$' !

Alexey rudyryk
///

> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/5zI85qKS4acJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.

Alexey Kinyov

unread,
Mar 28, 2012, 5:17:48 AM3/28/12
to django...@googlegroups.com
Hello, Reinout!

> Yes: You have to call user.save() after creating it. Otherwise the user
> object exists, but it isn't saved to the database yet.

That' wrong.

Documentation says:

create_user(username, email=None, password=None)

Creates, saves and returns a User.

https://docs.djangoproject.com/en/1.4/topics/auth/#django.contrib.auth.models.UserManager.create_user

I think the problem is that 'user.password' is hashed value but
login() method receives raw plain text password, as I answered above.

Alexey
///

> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.

Reinout van Rees

unread,
Mar 28, 2012, 9:08:28 AM3/28/12
to django...@googlegroups.com
On 28-03-12 11:17, Alexey Kinyov wrote:
>> Yes: You have to call user.save() after creating it. Otherwise the user
>> > object exists, but it isn't saved to the database yet.
> That' wrong.
>
> Documentation says:
>
> create_user(username, email=None, password=None)
>
> Creates, saves and returns a User.

You're right, I was wrong :-) I overlooked that create_user is a
special call and thought it was just a generic create statement.


Reinout

jondbaker

unread,
Mar 28, 2012, 11:24:01 AM3/28/12
to django...@googlegroups.com
Thanks for all the responses. I actually solved the problem about 15 minutes after posting this question and indicated so in the first reply. Perhaps that reply did not go out in time? Anyway, thanks for the help!

> django-users+unsubscribe@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages