【问题标题】:Why does Django Redirect Test Fail?为什么 Django 重定向测试失败?
【发布时间】:2013-08-06 13:17:31
【问题描述】:

我有一个视图单元测试失败,但我不知道原因。我相信这与测试数据库有关。有问题的视图是默认的 Django 登录视图,django.contrib.auth.views.login。在我的项目中,用户登录后,他们会被重定向到显示哪些成员已登录的页面。我只删除了该页面。

这是单元测试:

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 工厂,测试都会失败并出现相同的错误:

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'  

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

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

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

我还查看了 Django 的 testcases.py 文件。我不明白它所做的一切,但它促使我在发帖时尝试设置“follow=True”,但这并没有什么不同。谁能告诉我我做错了什么?顺便说一句,我正在使用鼻子测试作为我的测试运行器。

谢谢!

【问题讨论】:

  • 您可以发布您的登录视图吗?

标签: django django-views django-testing


【解决方案1】:

您的测试在 POST 中发送 {'username': self.user.username, 'password': self.user.password}。但是,self.user.password 是散列密码而不是纯文本密码,这就是它们不匹配的原因,您看到的是表单错误而不是重定向。将此更改为 {'username': self.user.username, 'password': 'jdoepass'} 应该验证用户名/密码组合。

【讨论】:

  • 谢谢马克。自从他首先回答以来,我不得不给分给阿克沙尔,但我感谢您的回应。
【解决方案2】:

在您的测试test_user_enters_valid_data 中,您将密码作为self.user.password 传递。这将是密码的 SHA,因为 Django 将密码的 sha 存储在 db 上。这就是为什么您永远无法使用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)
    #### 
    ####

然后它应该可以工作了。

【讨论】:

  • 这就是问题所在。我应该意识到这就是正在发生的事情。谢谢!!
猜你喜欢
  • 2012-04-21
  • 2021-02-09
  • 2014-04-19
  • 1970-01-01
  • 2018-04-04
  • 1970-01-01
  • 2023-03-24
  • 2019-12-21
  • 1970-01-01
相关资源
最近更新 更多