Until django 1.2.5 i could use the following code to create a user for testing and then log it in:
class TestSomeLoginRequiredView(TestCase):
urls = 'sonloop.tests.test_urls'
def setUp(self):
self.user = User.objects.create(username='testuser',password='some_password')
def test_the_view(self):
response = self.client.get('/test_view_url/')
self.assertEqual(response.status_code,401)
self.client.login(username='testuser',password='some_password')
response = self.client.get('/test_view_url/')
self.assertEqual(response.status_code,200)
Now using the same code with django 1.4 doesn't work any more:
ValueError: Unknown password hashing algorithm 'some_password'. Did you specify it in the PASSWORD_HASHERS setting?
I understand this has to do with the new password hashing system. I do not use the PASSWORD_HASHERS setting, so Django should use some default.
Django docs are quite sparse about how to implement something like that now. In the testing section nothing has changed. And from the section about creating passwords and how to hash them, i could see that i could probably create a password like this:
self.user = User.objects.create(username='testuser')
self.user.set_password('some_password')
But that only raises this eception in the first line (when creating the user, not when assigning the password):
ValueError: Unknown password hashing algorithm ''. Did you specify it in the PASSWORD_HASHERS setting?
This is some problem with django not accepting empty passwords, so i changed that to:
self.user = User.objects.create(username='testuser',password='!')
self.user.set_password('some_password')
And then try to log the user in like that:
login = self.client.login(username='testuser',password='some_password')
self.assertTrue(login)
Which now raises an AssertionError: False is not True
sigh - i almost expected that...
My question now is: How can i create a user with a password, and log this user in with the django test client?