fix(auth): 修改密码当新密码与旧密码相同时弹出错误,不再退出登录

This commit is contained in:
sophie committed 2026-09-08 13:32:39 +08:00
1 parent 9570197378
commit 0f8b318458
18 files changed
+247 -18

No files matched your search

@@ -247,6 +247,35 @@ def test_admin_update_user_rolls_back_when_casdoor_password_fails(client, admin_
assert user.check_password('old-password')
def test_admin_update_user_rejects_unchanged_password(client, admin_headers, monkeypatch):
"""管理员提交与当前相同的密码应 400 AUTH.PASSWORD_UNCHANGED,且不调用 Casdoor。"""
user = _create_local_user('same_pw_user', 'same_pw_user@example.com')
client.application.config['CASDOOR_BASE_URL'] = 'http://casdoor.test'
client.application.config['CASDOOR_CLIENT_ID'] = 'client-id'
client.application.config['CASDOOR_CLIENT_SECRET'] = 'client-secret'
client.application.config['CASDOOR_ORGANIZATION'] = 'organization_HX'
casdoor_calls = {'set_password': 0}
def fake_set_password(_owner, _name, _old, _new):
casdoor_calls['set_password'] += 1
monkeypatch.setattr(casdoor_service, 'casdoor_set_password', fake_set_password)
res = client.put(
f'/api/admin/users/{user.id}',
json={'password': 'old-password'},
headers=admin_headers,
)
# 断言:相同密码按业务校验失败,不刷新 token / 不清会话。
assert res.status_code == 400
payload = res.get_json() or {}
error = payload.get('error') or {}
assert error.get('code') == 'AUTH.PASSWORD_UNCHANGED'
assert casdoor_calls['set_password'] == 0
db.session.refresh(user)
assert user.check_password('old-password')
def test_admin_update_user_provisions_missing_casdoor_user_with_password(client, admin_headers, monkeypatch):
"""
IdP 无账号且管理员改密时,应 set-password 失败后 fallback 至 add-user,不调用 update-user。
+27
View File
@@ -1,3 +1,10 @@
"""
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
@@ -34,6 +41,26 @@ def test_raise_casdoor_set_password_error_maps_policy(app):
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:
+28
View File
@@ -92,6 +92,34 @@ def test_change_password_local_mode(client):
def test_change_password_rejects_unchanged_password(client):
"""新密码与原密码相同时应 400,且不改哈希。"""
headers = _auth_headers(client)
res = client.post(
'/api/user/change-password',
headers=headers,
json={'current_password': 'old-password', 'new_password': 'old-password'},
)
assert res.status_code == 400
assert (res.get_json() or {}).get('error', {}).get('code') == 'AUTH.PASSWORD_UNCHANGED'
user = db.session.query(User).filter_by(username='testuser').first()
assert user.check_password('old-password')
def test_change_password_local_mode_syncs_casdoor(client, monkeypatch):
client.application.config['CASDOOR_BASE_URL'] = 'http://casdoor.test'