12

django 1.2.5までは、次のコードを使用してテスト用のユーザーを作成し、ログインすることができました。

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)

django1.4で同じコードを使用しても機能しなくなりました。

ValueError:不明なパスワードハッシュアルゴリズム'some_password'。PASSWORD_HASHERS設定で指定しましたか?

これは新しいパスワードハッシュシステムに関係していることを理解しています。PASSWORD_HASHERS設定を使用しないため、Djangoはデフォルトを使用する必要があります。

Djangoのドキュメントは、そのようなものを今実装する方法についてはかなりまばらです。テストセクションでは、何も変更されていません。そして、パスワードの作成とそのハッシュ方法に関するセクションから、おそらく次のようなパスワードを作成できることがわかりました。

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?

4

2 に答える 2

16
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@example.com', password='pass')
于 2012-04-12T09:12:27.873 に答える
15

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.

于 2012-04-12T09:20:56.827 に答える