Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a simple password for unittest user using PASSWORD_HASHERS

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?

like image 562
marue Avatar asked Apr 12 '12 09:04

marue


2 Answers

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')
like image 153
Thomas Avatar answered Nov 18 '22 07:11

Thomas


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.

like image 15
marue Avatar answered Nov 18 '22 05:11

marue