为什么Django重定向测试失败? [英] Why does Django Redirect Test Fail?

查看:48
本文介绍了为什么Django重定向测试失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图单元测试失败,我无法弄清原因.我相信这与测试数据库有关.有问题的视图是默认的Django登录视图django.contrib.auth.views.login.在我的项目中,用户登录后,他们将被重定向到一个页面,该页面显示了哪些成员已登录.我仅对该页面进行了存根处理.

I have a view unit test that is failing and I can't figure out the reason why. I believe it has something to do with the test database. The view in question is the default Django login view, django.contrib.auth.views.login. In my project, after the user logs in, they are redirected to a page that show which members are logged in. I've only stubbed out that page.

这是单元测试:

from django.test import TestCase
from django.contrib.auth.models import User
from django.test.client import Client, RequestFactory
from django.core.urlresolvers import reverse
from utils.factories import UserFactory

class TestSignInView(TestCase):
    def setUp(self):
        self.client = Client()
        # self.user = UserFactory()
        self.user = User.objects.create_user(username='jdoe', password='jdoepass')

    def tearDown(self):
        self.user.delete()

    def test_user_enters_valid_data(self):
        response = self.client.post(reverse('login'), {'username': self.user.username, 'password': self.user.password}, follow=True)
        print response.context['form'].errors
        self.assertRedirects(response, reverse('show-members-online'))

这是我得到的错误:

File "/Users/me/.virtualenvs/sp/lib/python2.7/site-packages/django/test/testcases.py", line 576, in assertRedirects
(response.status_code, status_code))
AssertionError: Response didn't redirect as expected: Response code was 200 (expected 302)
<ul class="errorlist"><li>__all__<ul class="errorlist"><li>Please enter a correct username and password. Note that both fields may be case-sensitive.</li></ul></li></ul>

无论我是通过create_user函数手动创建用户还是使用此factory_boy工厂,测试都会失败,并显示相同的错误:

The test fails with the same error whether I create the user manually with the create_user function or if I use this factory_boy factory:

from django.contrib.auth.models import User
class UserFactory(factory.DjangoModelFactory):
    FACTORY_FOR = User
    username = 'jdoe'
    # password = 'jdoepass'
    password = factory.PostGenerationMethodCall('set_password', 'jdoepass')
    email = 'jdoe@example.com'  

这是用户成功登录后将其重定向到的视图:

Here's the view I'm redirecting the user to after they log in successfully:

from django.shortcuts import render
def show_members_online(request, template):
    return render(request, template)

我打印出错误,表明该测试无法识别用户名/密码对.我还打印了测试中的用户名和密码,以确认它们与我在setUp中将其初始化为的值相同.最初,当我使用User工厂时,我以为是因为我在创建用户时没有对密码进行加密.那时我进行了一些研究,得知需要使用PostGenerationMethodCall设置密码.

I printed out the error which shows that the test isn't recognizing the username/password pair. I've also printed out the username and password inside the test to confirm that they're the same values as what I initialize them to in setUp. At first, when I was utilizing the User factory, I thought it was because I wasn't encrypting the password when I created the user. That's when I did some research and learned that I needed to use the PostGenerationMethodCall to set the password.

我还查看了Django的testcases.py文件.我不了解它正在做的所有事情,但是它促使我尝试在发布文章时尝试设置"follow = True",但这并没有改变.谁能告诉我我在做什么错?顺便说一下,我正在使用鼻子测试作为我的测试跑步者.

I also looked at Django's testcases.py file. I don't understand everything that it's doing but it prompted me to try setting 'follow=True' when I do the post but that didn't make a difference. Can anyone tell me what I'm doing wrong? By the way, I'm using nosetests as my test runner.

谢谢!

推荐答案

在测试 test_user_enters_valid_data 中,您将密码作为 self.user.password 传递.这将是密码的SHA,因为Django将密码的sha存储在db上.这就是为什么您永远无法使用 user.password 读取特定用户的密码的原因.

In your test test_user_enters_valid_data, you are passing the password as self.user.password. This will be the SHA of the password because Django stores the sha of password on db. That's why you can never read the password for a particular user using user.password.

因此,请更改您的 test_user_enters_valid_data .

def test_user_enters_valid_data(self):
    response = self.client.post(reverse('login'), {'username': self.user.username, 'password': 'jdoepass'}, follow=True)
    #### 
    ####

然后它应该可以工作.

这篇关于为什么Django重定向测试失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆