-1
import pytest
import app

@pytest.yield_fixture
def client():
    application = app.App()
    client = application.create_app(flask_config_name='testing').test_client()
    yield client

def test_create_staff(client):
    rv = client.post('/api/v1/staff', json={
    "surname": "V",
    "name": "Vasu",
    "role": "administrator",
    "employ_id": "vasu@rossum.io",
    "password": "string",
    "phone": "9848664982",
    "email": "vinthavasu@gmail.com"
})
    if b'Successful' not in rv.data:
        raise AssertionError(rv.data)

@pytest.yield_fixture(scope="function")
def test_auth_login(client):
    rv = client.post('/api/v1/auth/login', json={
        "username": "vasu@rossum.io",
        "password": "string"})
    print(rv.cookie['_id'])
    if b'Vasu' not in rv.data:
        raise AssertionError(rv.data)


def test_list_companies(client): 
    rv = client.get('/api/v1/company')
    if b'[]' not in rv.data:
        raise AssertionError(rv.data)
    if rv.status_code != 200:
        raise AssertionError(rv.status_code)


platform linux -- Python 3.6.7, pytest-4.4.1, py-1.8.0, pluggy-0.9.0 -- /home/vasu/venv-sri/bin/python3
cachedir: .pytest_cache
rootdir: /home/vasu/Desktop/pytest
plugins: xdist-1.28.0, reorder-0.1.1, forked-1.0.2, cov-2.7.1
collected 3 items                                                                                                                            

app/api/tests/test_api.py::test_get_api [2019-05-06 09:45:51,743] INFO in __init__: Module created
[2019-05-06 09:45:51,753] INFO in __init__: Application started
PASSED
app/api/staff/tests/test_staff.py::test_create_staff [2019-05-06 09:45:51,794] INFO in __init__: Module created
[2019-05-06 09:45:51,797] INFO in __init__: Application started
[2019-05-06 09:45:52,426] INFO in controller: Creating Staff record : Successful
PASSED
app/api/company/tests/test_company.py::test_list_companies [2019-05-06 09:45:52,437] INFO in __init__: Module created
[2019-05-06 09:45:52,442] INFO in __init__: Application started
FAILED

====================FAILURES ===========
_________________________test_list_companies ________________________

client = <FlaskClient <Flask 'app'>>

    def test_list_companies(client):
        rv = client.get('/api/v1/company')
        if b'[]' not in rv.data:
>           raise AssertionError(rv.data)
E           AssertionError: b'{\n  "msg": "Missing JWT in headers or cookies (Missing cookie \\"access_token_cookie\\"; Missing Authorization Header)"\n}\n'

app/api/company/tests/test_company.py:4: AssertionError

我使用 jwt 身份验证在烧瓶 restplus 中编写了一个程序。因为试图做 pytest 来检查代码的功能。当我尝试它时,JWT 认证测试通过了,但是当我尝试访问由 jwt 认证保护的公司详细信息时,它说缺少认证标头/cookies。如果可以的话,请任何人建议我一个解决方案。

4

1 回答 1

0

您可以在 test_client 请求中设置标头。你可以在这里看到一个例子:https ://github.com/vimalloc/flask-jwt-extended/blob/master/tests/test_headers.py#L30

于 2019-05-06T04:59:16.740 回答