这是我要测试的功能
@jwt_required
def get_all_projects(self):
# implementation not included here
我从 pytest 类调用函数
def test_get_all_projects(db_session):
all_projects = ProjectController.get_all_projects()
与db_session
夹具
@pytest.fixture(scope='function')
def db_session(db, request):
"""Creates a new database session for a test."""
engine = create_engine(
DefaultConfig.SQLALCHEMY_DATABASE_URI,
connect_args={"options": "-c timezone=utc"})
DbSession = sessionmaker(bind=engine)
session = DbSession()
connection = engine.connect()
transaction = connection.begin()
options = dict(bind=connection, binds={})
session = db.create_scoped_session(options=options)
db.session = session
yield session
transaction.rollback()
connection.close()
session.remove()
这导致错误
> raise NoAuthorizationError("Missing {} Header".format(header_name))
E flask_jwt_extended.exceptions.NoAuthorizationError: Missing Authorization Header
../../.virtualenvs/my-app/lib/python3.6/site-packages/flask_jwt_extended/view_decorators.py:132: NoAuthorizationError
手动调用create_access_token
当我调用create_access_token
上面的夹具时,我仍然得到相同的结果
db.session = session
session._test_access_token = create_access_token(identity='pytest')
yield session
如何在测试期间伪造 JWT 令牌pytest
?