Django: test failing on a view with @login_required
This testcase works for me:
from django.contrib.auth.models import Userfrom django.core.urlresolvers import reversefrom django.test.client import Clientimport unittestclass LoginTestCase(unittest.TestCase): def setUp(self): self.client = Client() self.user = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword') def testLogin(self): self.client.login(username='john', password='johnpassword') response = self.client.get(reverse('testlogin-view')) self.assertEqual(response.status_code, 200)
I suggest you (if you don't use them already) to use the reverse() function and name your URLs. This way you are sure that you get always the right URL.
Here is the answer:
Python 2.6.5 made a change to the way cookies are stored which is subtly incompatible with the test client. This problem has been fixed in the 1.1.X and trunk branches, but the fix hasn't yet made it into a formal release.
If you are using 1.1.X and Python 2.6.5, you're going to have problems with any test activity involving cookies. You either need to downgrade Python, or use the 1.1.X branch rather than the 1.1.1 release.
A 1.1.2 release (that will include the fix for the problem you describe) will be made at the same time that we release 1.2 - hopefully, very very soon.
Yours, Russ Magee %-)