django - 为什么 Django 重定向测试失败?

标签 django django-views django-testing

我的 View 单元测试失败了,我无法找出原因。我认为这与测试数据库有关。有问题的 View 是默认的 Django 登录 View 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 = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f298969d97b2978a939f829e97dc919d9f" rel="noreferrer noopener nofollow">[email protected]</a>'  

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

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

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

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

谢谢!

最佳答案

在测试 test_user_enters_valid_data 中,您将密码作为 self.user.password 传递。这将是密码的 SHA,因为 Django 将密码的 SHA 存储在数据库上。这就是为什么您永远无法使用 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 - 为什么 Django 重定向测试失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18064832/

相关文章:

jquery添加向下滚动分页以显示结果

python - Django 在 View 中测试 ajax 端点

python - 如何根据url请求更改django listview中的模板名称?

python - Django Rest Framework - 如何路由到函数 View

python - Django - 形式参数?

python - Django 使用从模型填充的数据加载表单

python - 如何对 ValidationError 进行单元测试并断言使用了正确的代码?

python - 如何为 Django 管理页面的功能编写测试?

python - Django 唯一文件名方法

python - Django url反向和正则表达式