大佬教程收集整理的这篇文章主要介绍了测试 FastAPI OAuth2PasswordRequestForm Got 400 状态码,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试测试将检查用户凭据的路由。
这是我的单元测试
@pytest.mark.parametrize(
"payload,status_code",[
[{"username": "john","password": "secret"},status.http_200_OK],[{"username": "john"},status.http_401_UNAUTHORIZED],[{"password": "secret"},[{},status.http_401_UNAUTHORIZED]
]
)
def test_user_credentials(test_app: TESTClIEnt,monkeypatch,payload,status_code):
async def mock_check_credentials(credentials:User):
if "email" in payload and "password" in payload:
return True
return false
monkeypatch.setattr(user_repository,"check_credentials",mock_check_credentials)
response = test_app.post("/users/credentials",data=Json.dumps(payload),headers={"content-type": "multipart/form-data"})
assert response.status_code == status_code
这是我的路线
@router.post("/credentials",status_code=status.http_200_OK)
async def check_user_credentials(request: OAuth2passwordrequestForm = Depends()):
valID_credentials = await user_repository.check_credentials(request)
if not valID_credentials:
raise httpException(
status_code=status.http_401_UNAUTHORIZED,detail="Incorrect username or password",headers={"WWW-Authenticate": "Bearer"},)
return True
这是我的存储库方法:
async def check_credentials(user: OAuth2passwordrequestForm):
query = Users.SELEct().where(Users.c.email == user.userName)
exists = await database.fetch_one(query=query)
if not exists:
return false
password_ok = hashing.verify(exists.get("password"),user.password)
if not password_ok:
return false
return True
但是我得到的状态代码是 400,我期望第一次测试是 200,其他测试是 401。
test_app = <starlette.TESTClIEnt.TESTClIEnt object at 0x7f75199fb9a0>,monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x7f7519c369a0>,payload = {},status_code = 401
@pytest.mark.parametrize(
"payload,[
[{"username": "john",status.http_401_UNAUTHORIZED]
]
)
def test_user_credentials(test_app: TESTClIEnt,status_code):
async def mock_check_credentials(credentials:User):
if "email" in payload and "password" in payload:
return True
return false
monkeypatch.setattr(user_repository,mock_check_credentials)
response = test_app.post("/users/credentials",headers={"content-type": "multipart/form-data"})
> assert response.status_code == status_code
E assert 400 == 401
E + where 400 = <Response @R_197_11240@0]>.status_code
tests/test_user.py:75: AssertionError
==================================================================================================================================================================================================== short test sumMary info =====================================================================================================================================================================================================
Failed tests/test_user.py::test_user_credentials[payload0-200] - assert 400 == 200
Failed tests/test_user.py::test_user_credentials[payload1-401] - assert 400 == 401
Failed tests/test_user.py::test_user_credentials[payload2-401] - assert 400 == 401
Failed tests/test_user.py::test_user_credentials[payload3-401] - assert 400 == 401
================================================================================================================================================================================================== 4 Failed,8 passed in 0.58s ===================================================================================================================================================================================================
ERROR: 1
在手动测试中(使用 Swagger UI),这条路线完美运行。
我无法面对我在这里缺少的东西
编辑:我找到了 this thread,但它没有帮助我:我的requirement.txt 中有python-multipart>=0.0.5
并且我已经安装了它
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
以上是大佬教程为你收集整理的测试 FastAPI OAuth2PasswordRequestForm Got 400 状态码全部内容,希望文章能够帮你解决测试 FastAPI OAuth2PasswordRequestForm Got 400 状态码所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。