Files
syops/backend/tests/test_casdoor_errors.py
T

82 lines
3.5 KiB
Python

"""
tests/test_casdoor_errors.py
用途:校验 Casdoor 登录/改密错误文本到稳定 AppError 的映射。
依赖:app.api.user 兼容入口中的 _raise_casdoor_* 辅助函数。
逻辑:覆盖 OAuth 错误、set-password 策略/缺失用户/相同密码/原密码错误。
"""
import pytest
from app.api import user as user_api
@pytest.mark.parametrize(
('payload', 'context', 'expected_code'),
[
({'error': 'invalid_grant'}, 'password', 'CASDOOR_INVALID_CREDENTIALS'),
({'error': 'invalid_grant'}, 'code', 'CASDOOR_CODE_EXPIRED'),
({'error': 'invalid_client'}, 'password', 'CASDOOR_CONFIG_ERROR'),
({'error': 'access_denied'}, 'code', 'CASDOOR_ACCESS_DENIED'),
],
)
def test_raise_casdoor_auth_error_maps_oauth_errors(app, payload, context, expected_code):
with app.app_context():
with pytest.raises(user_api.AppError) as exc:
user_api._raise_casdoor_auth_error(payload, context=context)
assert exc.value.code == expected_code
def test_raise_casdoor_set_password_error_maps_user_missing(app):
with app.app_context():
with pytest.raises(user_api.AppError) as exc:
user_api._raise_casdoor_set_password_error({'status': 'error', 'msg': "The user: organization_HX/26050001 doesn't exist"})
assert exc.value.code == 'CASDOOR_USER_NOT_IN_IDP'
assert exc.value.http_status == 409
def test_raise_casdoor_set_password_error_maps_policy(app):
with app.app_context():
with pytest.raises(user_api.AppError) as exc:
user_api._raise_casdoor_set_password_error({'status': 'error', 'msg': 'password is too short'})
assert exc.value.code == 'CASDOOR_PASSWORD_POLICY'
def test_raise_casdoor_set_password_error_maps_same_password(app):
"""Casdoor “新密码不能与旧密码相同”不得映射为 401,以免前端清会话。"""
with app.app_context():
with pytest.raises(user_api.AppError) as exc:
user_api._raise_casdoor_set_password_error(
{'status': 'error', 'msg': 'The new password cannot be the same as the old password'}
)
assert exc.value.code == 'AUTH.PASSWORD_UNCHANGED'
assert exc.value.http_status == 400
def test_raise_casdoor_set_password_error_maps_incorrect_current_as_business(app):
"""原密码错误走业务码 400,禁止 CASDOOR_INVALID_CREDENTIALS 401。"""
with app.app_context():
with pytest.raises(user_api.AppError) as exc:
user_api._raise_casdoor_set_password_error({'status': 'error', 'msg': 'incorrect current password'})
assert exc.value.code == 'AUTH.INCORRECT_CURRENT_PASSWORD'
assert exc.value.http_status == 400
def test_raise_casdoor_add_user_error_maps_policy(app):
with app.app_context():
with pytest.raises(user_api.AppError) as exc:
user_api._raise_casdoor_add_user_error({'status': 'error', 'msg': 'password is too short'})
assert exc.value.code == 'CASDOOR_PASSWORD_POLICY'
def test_raise_casdoor_update_user_error_maps_missing(app):
with app.app_context():
with pytest.raises(user_api.AppError) as exc:
user_api._raise_casdoor_update_user_error({'status': 'error', 'msg': "The user: organization_HX/26050001 doesn't exist"})
assert exc.value.code == 'CASDOOR_USER_NOT_IN_IDP'
def test_extract_casdoor_error_payload_from_runtime_message():
raw = "Casdoor password login failed: {'error': 'invalid_grant', 'error_description': 'wrong password'}"
payload = user_api._extract_casdoor_error_payload(raw)
assert payload.get('error') == 'invalid_grant'