误解,来自文档 Pytest 授权的示例

时间:2021-01-23 15:25:06

标签: pytest pytest-django

我决定看Pytest,马上就误会了,文档中的一个例子,但是没有授权,测试崩溃,代码是301,有谁知道是什么原因?

def test_with_authenticated_client(client, django_user_model):
    username = "TestUser"
    password = "1234567"
    user = django_user_model.objects.create_user(username=username,
                                                 password=password)
    # Use this:
    client.force_login(user)
    response = client.get('/new')
    assert response.status_code == 200

def test_with_authenticated_client2(client):
    username = "user2"
    password = "bar"
    # Or this:
    client.login(username=username, password=password)
    response = client.get('/new')
    assert response.status_code == 200

使用未经授权的客户端,预期代码 301

def test_make_not_authorized_user(client):
    response = client.get('/new')
    assert response.status_code in (302, 301)

0 个答案:

没有答案
相关问题