2014-07-15 82 views
3

當我想測試登錄功能時,我的單元測試失敗。當我在我的瀏覽器中測試是可以的。Django-Python Django登錄測試失敗,出現在factory_boy和Authtools

我使用:

django==1.6.5 
factory-boy==2.4.1 
WebTest==2.0.15 
django-webtest==1.7.7 
nose==1.3.3 
django-nose==1.2 

有沒有自定義的用戶模型。用戶模型是authtools.models.User。

我的觀點:

from authtools.views import LoginView 
from django.views.generic import TemplateView 
from django.core.urlresolvers import reverse 

class Home_view(LoginView): 
    template_name = 'users/home.html' 
    disallow_authenticated = False 

    def get_success_url(self): 
     return reverse('users:blob') 


class Blob_view (TemplateView): 
    template_name = "users/blob.html" 

我的工廠:

import factory 


from django.conf import settings 
from django.contrib.auth.hashers import make_password 

class UserFactory (factory.DjangoModelFactory): 
    """ 
    Factory used to instanciate a :class: Users.User 
    """ 

    class Meta: 
     model = settings.AUTH_USER_MODEL 

    name = factory.Sequence(lambda n: 'user%d' % n) 
    email = factory.LazyAttribute(lambda obj: '%[email protected]' % obj.name) 
    password = make_password('eCRD') 
    is_active = True 

我的測試:有兩個測試。第一次測試客戶端登錄,第二次測試意見:

from django.core.urlresolvers import reverse 


from django_webtest import WebTest 
from authtools.models import User 
from rest_framework import status 


from .factories import UserFactory 


class UserLoginTests(WebTest): 

    def setUp(self): 
     self.user = UserFactory() 


    def tearDown(self): 
     User.objects.get(email = self.user.email).delete() 

    def test_user_login_client(self): 
     self.resp = self.client.login(username = self.user.email, password = self.user.password 
     ) 
     self.assertEqual(self.resp, True) 
     self.client.logout() 

    def test_user_logsin_view(self): 
     Home_View = self.app.get(reverse('users_home')) 
     Login_Form = Home_View.forms['login-form'] 
     Login_Form['username'] = self.user.email 
     Login_Form['password'] = self.user.password 
     response = Login_Form.submit().follow() 
     self.assertEqual(redirect.status_code, status_HTTP_200_ok) 
     self.client.logout() 

兩次測試都失敗了。但在瀏覽器中,它運行良好。

謝謝。

回答

4

我自己回答。解決方法是:在調用model.objects.create()函數的鉤子下的Factory_boy。在創建用戶auth模型時調用model.objects.create_user()。兩個作品!但不同的是,create_user使用django.contrib.auth.hashers.make_password來設置密碼,並將密碼保存爲純文本。那麼解決的辦法是:

from django.core.urlresolvers import reverse 


from django_webtest import WebTest 
from authtools.models import User 
from rest_framework import status 


from .factories import UserFactory, user_password 


class UserLoginTests(WebTest): 

    def setUp(self): 
     self.user = UserFactory() 


    def tearDown(self): 
     User.objects.get(email = self.user.email).delete() 

    def test_user_login_client(self): 
     self.resp = self.client.login(username = self.user.email, password = user_password 
     ) 
     self.assertEqual(self.resp, True) 
     self.client.logout() 

    def test_user_logsin_view(self): 
     Home_View = self.app.get(reverse('users_home')) 
     Login_Form = Home_View.forms['login-form'] 
     Login_Form['username'] = self.user.email 
     Login_Form['password'] = user_password 
     response = Login_Form.submit().follow() 
     self.assertEqual(redirect.status_code, status_HTTP_200_ok) 
     self.client.logout() 

而且工廠

import factory 


from django.conf import settings 
from django.contrib.auth.hashers import make_password 

user_passwprd = 'test'  

class UserFactory (factory.DjangoModelFactory): 
    """ 
    Factory used to instanciate a :class: Users.User 
    """ 

    class Meta: 
     model = settings.AUTH_USER_MODEL 

    name = factory.Sequence(lambda n: 'user%d' % n) 
    email = factory.LazyAttribute(lambda obj: '%[email protected]' % obj.name) 
    password = make_password(user_password) 
    is_active = True