2013-02-01 43 views
0

我想編寫一個用於發送發佈數據到登錄頁面的測試用例。這是行不通的。我在這裏發佈我的代碼,希望你能幫助我。謝謝。django testcase發佈數據

def setUp(self): 
    """set up""" 
    un = '[email protected]' 
    pw = '123' 
    self.user = User.objects.create_user(un, un) 
    self.user.is_staff = True 
    self.user.is_superuser = True 
    self.user.firstname = "John" 
    self.user.lastname = "Smith" 
    self.user.password = '123' 
    self.user.save() 
    print '*** password: ', self.user.password 


def testPost(self): 
    """test POST requests""" 
    post_data = { 
     'email': '[email protected]', 
     'password': '123', 
    }   

    response = self.client.post(reverse('myapp_home', post_data))   
    print response.status_code 

錯誤輸出在下面。

ERROR: testPost (submngr.tests.model_tests.model_tests.FormsTestCase) 
test POST requests 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "tests/model_tests/model_tests.py", line 117, in testPost 
    response = self.client.post('/', post_data) 
    File "/usr/local/lib/python2.7/dist-packages/django/test/client.py", line 449, in post 
    response = super(Client, self).post(path, data=data, content_type=content_type, **extra) 
    File "/usr/local/lib/python2.7/dist-packages/django/test/client.py", line 262, in post 
    return self.request(**r) 
    File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 111, in get_response 
    response = callback(request, *callback_args, **callback_kwargs) 
    File "views.py", line 84, in homepage 
    print results[0].check_password(form.cleaned_data['password']) 
    File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/models.py", line 304, in check_password 
    return check_password(raw_password, self.password, setter) 
    File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/hashers.py", line 42, in check_password 
    hasher = get_hasher(algorithm) 
    File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/hashers.py", line 115, in get_hasher 
    "setting?" % algorithm) 
ValueError: Unknown password hashing algorithm '123'. Did you specify it in the PASSWORD_HASHERS setting? 

回答

1

您已經使用散列算法,這就是爲什麼您收到的錯誤直接存儲在用戶passowrd視爲純字符串,self.user.password = 123但Django的存儲用戶密碼。您可以使用set_password用戶的方法設置用戶密碼,在保存之前應用哈希算法:

user.set_password('123') 
user.save()