django测试用例无法通过视图函数上的@login_required装饰器

时间:2017-12-15 03:46:40

标签: python django django-views django-testing

我已经搜索了stackoverflow并尝试了几种不同的解决方案,但我的测试代码总是转到登录重定向。

这就是我所拥有的:

war {
doFirst {
    def propertyFile = file("build/resources/main/env_${branch}.properties")
    if(propertyFile.exists()){
        delete 'build/resources/main/env.properties'

        propertyFile.renameTo(file("build/resources/main/env.properties"))
    }
}}

1 个答案:

答案 0 :(得分:3)

您无法像这样设置密码 - 存储在数据库中的密码是散列值,您将其设置为密码字符串本身。这意味着验证将失败。

你需要set the password这样:

self.user = User.objects.create(username="lolwutboi")
self.user.set_password("whatisthepassword")
self.user.save()

但是,由于您真正需要的是登录测试用户,因此请在测试中使用force_login

self.client.force_login(self.user)

这将在不必担心密码的情况下登录用户。