> 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"
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.
> 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.
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.
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