小编crh*_*des的帖子

Django使用错误的凭据登录返回200而不是401

这是一个非常直接的测试,但我似乎无法做到正确.

我想检查哪些用户可以登录并执行操作(它是更大的测试套件的一部分),但第一步导致一些问题.

class SuperUserTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.su = User.objects.create_superuser('super','','the_correct_password')
    def test_su_can_login(self):
        response = self.client.post(reverse('django.contrib.auth.views.login'),
            {'username': 'super', 'password': 'the_wrong_password'})
        self.assertEqual(response.status_code,401)

        # Success redirects to the homepage, so its 302 not 200
        response = self.client.post(reverse('django.contrib.auth.views.login'),
            {'username': 'super', 'password': 'the_correct_password'})
        self.assertEqual(response.status_code,302)
Run Code Online (Sandbox Code Playgroud)

当我运行测试时,我得到:

(my_app)00:20 ~/my_app (master)$ ./manage.py test my_app.SuperUserTest
Creating test database for alias 'default'...
F
======================================================================
FAIL: test_su_can_login (my_app.SuperUserTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./my_app/tests.py", line 341, in test_su_can_login
    self.assertEqual(response.status_code,401)
AssertionError: 200 != 401

----------------------------------------------------------------------
Ran 1 test …
Run Code Online (Sandbox Code Playgroud)

python django http-status-code-401

3
推荐指数
1
解决办法
1577
查看次数

标签 统计

django ×1

http-status-code-401 ×1

python ×1