python - Django 2.2 - 测试用例 - client.logout() 方法不注销用户

标签 python django unit-testing testing

我正在为我的简单 Django 网络应用程序编写测试,检查用户是否可以正确登录和注销。 View :user_loginuser_logout

def user_login(request):
    if request.method == 'POST':
        email = request.POST.get('email address')
        password = request.POST.get('password')
        user = authenticate(username=email, password=password)
        if user:
            if user.is_active:
                login(request, user)
                return HttpResponseRedirect(reverse('main_page:index'))
        else:
            print("\nWARNING: invalid login detected\n\nemail: {}\npassword: {}\n".format(email, password))
            messages.error(request, 'Ivalid email or password. Try again.')
            return HttpResponseRedirect(reverse('users:login'))
    else:
        return render(request, 'users/login.html', {})


@login_required
def user_logout(request):
    messages.add_message(request, messages.INFO, 'See you Space Cowboy!')
    logout(request)
    return HttpResponseRedirect(reverse('main_page:index'))

测试本身:

class LoginLogoutTests(TestCase):

    def setUp(self):
        User = get_user_model()
        self.user = User.objects.create_user(email='test_user@email.com', password='S6y4Mb9Aqfu79YcF')
        print("\nsetup")

    def tearDown(self):
        self.user
        print("teardown\n")

    def test_login(self):
        # test if email is equal
        self.assertEqual(self.user.email, 'test_user@email.com')
        # test authentication
        self.assertTrue(self.user.is_authenticated)
        self.assertFalse(self.user.is_anonymous)
        # test if login request index.html
        self.assertTrue(self.client.get('main_page.index.html'))
        # test login with valid credentials
        self.assertTrue(self.client.login(email='test_user@email.com', password='S6y4Mb9Aqfu79YcF'))
        # test login with invalid credentials
        self.assertFalse(self.client.login(email='test_user@email.com', password='password'))
        print("test login")

    def test_logout(self):
        self.client.logout()
        self.assertTrue(self.user.is_authenticated)
        self.assertFalse(self.user.is_anonymous)
        print("test logout\n")

测试返回此输出:

(venv) [user@manjaro django-project]$ ./manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).

setup
test login
teardown

setup
test logout
teardown

..
----------------------------------------------------------------------
Ran 2 tests in 0.515s

OK
Destroying test database for alias 'default'...

上次测试不应该通过,但矛盾的是它通过了。这里出了什么问题?

编辑: 我重建了使用电子邮件作为登录名的用户模型。我使用了本教程:https://testdriven.io/blog/django-custom-user-model/

最佳答案

您尚未测试您的应用,您提供的那些 View 与提供的测试没有任何共同之处。您已尝试测试 Django 本身,它的 TestCase 内部结构,但未能正确测试它:在注销后执行与登录测试类似的操作,断言您的客户端无法访问您的索引页面。

测试您的 View 的正确方法是将客户端指向他们相应的 url,包括 AnonymousUser 和已记录的 url。

关于python - Django 2.2 - 测试用例 - client.logout() 方法不注销用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57121077/

相关文章:

ruby-on-rails - BDD with Cucumber 和 rspec - 什么时候这是多余的?

python - IO 错误 - 抓取网络时 - 代理问题?

python - 如何用Django模型数据库API实现Mysql操作UNHEX & HEX

python - Django mysql 表名未验证

unit-testing - 使用 phpunit 测试多个异常

c# - 带有 out 参数的最小起订量回调

python - 两个 pandas 数据框中的列中唯一值的数量

python - 编写一个名为index_of_smallest 的函数,该函数采用整数列表

python - Arduino 加速计到 Python 输出

python - 从单个 monorepo 引用多个 Django 项目