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?
self.user = User.objects.create(username='testuser',password='!')
self.user.set_password('some_password')
self.user.save() # <--- You need this ;)
OR: from here
self.user = User.objects.create_user(username='user', email='[email protected]', password='pass')
While searching for a solution i found one more thing that would work, allthough i will most probably use Thomas User.objects.create_user solution, as that is really simple.
But for whatever it's worth, that's what i came up with:
from django.contrib.auth.hashers import make_password
pwd = make_password('some_password')
self.user = User.objects.create(username='testuser', password = pwd)
Works fine too.
As i just found out this is extremely helpfull when creating fixtures manually. When you create a fixture for testing you probably want a user like this one:
username: user_01 password: password_01
But how do i know what password_01 would look like when it is hashed? That hashed value is what has to be stored in the fixture for testing, and make_password is doing exactly that: it creates password hashes from a password. Just write the return value of make_password('password_01')
to the fixture and you're done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With