Files
syops/backend/tests/test_change_password.py
T

403 lines
7.9 KiB
Python

import pytest
from app import create_app, db
from app.models import User
from app.api import user as user_api
@pytest.fixture
def client():
app = create_app('testing')
app.config['AUTH_LOGIN_MODE'] = 'password'
with app.test_client() as client:
with app.app_context():
db.create_all()
user = User(username='testuser', role='user', email='test@example.com')
user.set_password('old-password')
db.session.add(user)
db.session.commit()
yield client
db.session.remove()
db.drop_all()
def _auth_headers(client):
res = client.post('/api/user/login', json={'username': 'testuser', 'password': 'old-password'})
data = res.get_json()
return {
'X-User-Id': str(data['id']),
'X-Username': data['username'],
'X-Role': data['role'],
}
def test_change_password_local_mode(client):
headers = _auth_headers(client)
res = client.post(
'/api/user/change-password',
headers=headers,
json={'current_password': 'old-password', 'new_password': 'new-password'},
)
assert res.status_code == 200
user = db.session.query(User).filter_by(username='testuser').first()
assert user.check_password('new-password')
assert not user.check_password('old-password')
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'
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(user_owner, user_name, old_password, new_password):
casdoor_calls['set_password'] += 1
assert user_owner == 'organization_HX'
assert user_name == 'testuser'
assert old_password == 'old-password'
assert new_password == 'new-password'
monkeypatch.setattr(user_api, '_casdoor_set_password', fake_set_password)
headers = _auth_headers(client)
res = client.post(
'/api/user/change-password',
headers=headers,
json={'current_password': 'old-password', 'new_password': 'new-password'},
)
assert res.status_code == 200
assert casdoor_calls == {'set_password': 1}
def test_change_password_local_mode_wrong_current(client):
headers = _auth_headers(client)
res = client.post(
'/api/user/change-password',
headers=headers,
json={'current_password': 'wrong-password', 'new_password': 'new-password'},
)
assert res.status_code == 403
def test_change_password_casdoor_mode(client, monkeypatch):
client.application.config['AUTH_LOGIN_MODE'] = 'casdoor'
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(user_owner, user_name, old_password, new_password):
casdoor_calls['set_password'] += 1
assert user_owner == 'organization_HX'
assert user_name == 'testuser'
assert old_password == 'old-password'
assert new_password == 'new-password'
monkeypatch.setattr(user_api, '_casdoor_set_password', fake_set_password)
headers = _auth_headers(client)
res = client.post(
'/api/user/change-password',
headers=headers,
json={'current_password': 'old-password', 'new_password': 'new-password'},
)
assert res.status_code == 200
assert casdoor_calls == {'set_password': 1}
user = db.session.query(User).filter_by(username='testuser').first()
assert user.check_password('new-password')
def test_change_password_accepts_casdoor_password_when_local_out_of_sync(client, monkeypatch):
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'
user = db.session.query(User).filter_by(username='testuser').first()
user.set_password('stale-local-password')
db.session.commit()
headers = {
'X-User-Id': str(user.id),
'X-Username': user.username,
'X-Role': user.role,
}
def fake_verify(username, password):
assert username == 'testuser'
assert password == 'casdoor-password'
def fake_set_password(user_owner, user_name, old_password, new_password):
assert old_password == 'casdoor-password'
assert new_password == 'new-password'
monkeypatch.setattr(user_api, '_verify_casdoor_password', fake_verify)
monkeypatch.setattr(user_api, '_casdoor_set_password', fake_set_password)
res = client.post(
'/api/user/change-password',
headers=headers,
json={'current_password': 'casdoor-password', 'new_password': 'new-password'},
)
assert res.status_code == 200
user = db.session.query(User).filter_by(username='testuser').first()
assert user.check_password('new-password')
def test_change_password_casdoor_mode_wrong_current(client, monkeypatch):
client.application.config['AUTH_LOGIN_MODE'] = 'casdoor'
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'
def fake_verify(username, password):
raise RuntimeError('Casdoor password login failed: invalid_grant')
monkeypatch.setattr(user_api, '_verify_casdoor_password', fake_verify)
headers = _auth_headers(client)
res = client.post(
'/api/user/change-password',
headers=headers,
json={'current_password': 'wrong-password', 'new_password': 'new-password'},
)
assert res.status_code == 403
def test_change_password_casdoor_admin_override_when_old_password_mismatch(client, monkeypatch):
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 = []
def fake_set_password(user_owner, user_name, old_password, new_password):
casdoor_calls.append((old_password, new_password))
if old_password:
raise RuntimeError('Casdoor set-password failed: old password is incorrect')
monkeypatch.setattr(user_api, '_casdoor_set_password', fake_set_password)
headers = _auth_headers(client)
res = client.post(
'/api/user/change-password',
headers=headers,
json={'current_password': 'old-password', 'new_password': 'new-password'},
)
assert res.status_code == 200
assert casdoor_calls == [('old-password', 'new-password'), ('', 'new-password')]