fix(admin): 修复删除用户时没有级联删除

This commit is contained in:
sophie committed 2026-06-17 10:29:10 +08:00
1 parent 6f9c25bb34
commit b69d549b2c
3 files changed
+226 -3

No files matched your search

+37
View File
@@ -134,6 +134,43 @@ def test_user_list_pagination(client, admin_headers):
assert legacy.status_code == 200
assert isinstance(legacy.json, list)
def test_batch_delete_users_with_permissions(client, admin_headers):
"""批量删除用户前应清理 user_agent_permissions 等关联,避免 PostgreSQL 外键报错。"""
agent_res = client.post('/api/admin/users', json={
'username': 'permdelete',
'email': 'permdelete@example.com',
'role': 'user',
'password': 'password123',
'real_name': 'Perm Delete',
'employee_id': 'PD001',
'phone_number': '13800000099',
}, headers=admin_headers)
assert agent_res.status_code == 201
user_id = agent_res.json['id']
agent = client.post('/api/admin/agents', json={
'name': 'Delete Perm Agent',
'type': 'api',
'dify_app_id': 'del-app',
'dify_api_key': 'del-key',
}, headers=admin_headers)
assert agent.status_code == 201
agent_id = agent.json['id']
perm_res = client.post(f'/api/admin/users/{user_id}/permissions', json={
'agent_ids': [agent_id],
}, headers=admin_headers)
assert perm_res.status_code == 200
delete_res = client.post('/api/admin/users/batch-delete', headers=admin_headers, json={
'user_ids': [user_id],
})
assert delete_res.status_code == 200
assert delete_res.json['deleted_count'] == 1
users = client.get('/api/admin/users', headers=admin_headers).json
assert not any(u['id'] == user_id for u in users)
def test_agent_update_validation(client, admin_headers):
"""
Test validation when updating agents (especially API mode constraints)