当我想测试登录功能时,我的单元测试失败了。当我在浏览器中测试时没问题。
我正在使用:
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: '%s@eCRD.com' % 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()
两次测试都失败了。但在浏览器中它运作良好。
感谢。
答案 0 :(得分:7)
我自己回答。解决方案是:Factory_boy在对model.objects.create()函数的钩子调用下。当您为用户创建对model.objects.create_user()的auth模型调用时。两者都有效!但不同的是create_user使用django.contrib.auth.hashers.make_password来设置密码并创建fucntion保存密码作为纯文本。然后解决方案是:
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: '%s@eCRD.com' % obj.name)
password = make_password(user_password)
is_active = True