Why is django.test.client.Client not keeping me logged in.
Posted
by Mystic
on Stack Overflow
See other posts from Stack Overflow
or by Mystic
Published on 2010-05-20T23:27:47Z
Indexed on
2010/05/20
23:30 UTC
Read the original article
Hit count: 256
I'm using django.test.client.Client to test whether some text shows up when a user is logged in. However, I the Client object doesn't seem to be keeping me logged in.
This test passes if done manually with Firefox but not when done with the Client object.
class Test(TestCase):
def test_view(self):
user.set_password(password)
user.save()
client = self.client
# I thought a more manual way would work, but no luck
# client.post('/login', {'username':user.username, 'password':password})
login_successful = client.login(username=user.username, password=password)
# this assert passes
self.assertTrue(login_successful)
response = client.get("/path", follow=True)
#whether follow=True or not doesn't seem to work
self.assertContains(response, "needle" )
When I print response it returns the login form that is hidden by:
{% if not request.user.is_authenticated %}
... form ...
{% endif %}
This is confirmed when I run ipython manage.py shell
.
The problem seems to be that the Client object is not keeping the session authenticated.
© Stack Overflow or respective owner