3👍
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 %-)
34👍
This testcase works for me:
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test.client import Client
import unittest
class 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.
- How to debug a Django MultiValueDictKeyError on Formset POST
- How is pip install using git different than just cloning a repository?
- Django Multiple Databases Fallback to Master if Slave is down
- How to download a filefield file in django view
- Cannot connect to localhost API from Android app
0👍
OK I was to facing same problem @resto solved my problem.
creating user this way below, lets the test client get the user logged in and get the response other than redirect (302)
self.user = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword')
- Django & TastyPie: request.POST is empty
- DRF – How to handle exception on serializer create()?
- Why does python new york time zone display 4:56 instead 4:00?
- 'Questions ' object is not iterable Django
- Django runserver custom database
-1👍
I think your problem is with login. In your temporary/test db, the password is ‘password1’, but when you run your test the password you are giving is hashed and salted to ‘779ty6098yui8@#$%^TR^$67yt’ which when compared won’t be == to ‘password1’.
solution:
change:
login = self.client.login(username=user.username,
password=self.data[‘password1’])
to:
login = self.client.force_login(username=user.username,
password=self.data[‘password1’])
- Using APITestCase with django-rest-framework
- How to output text from database with line breaks in a django template?
- Improving Performance of Django ForeignKey Fields in Admin
- Django Boolean Queryset Filter Not Working