核心功能:
- 用户模型扩展:linked_agents 字段存储绑定龙虾
- 登录 API 支持 3 种模式:human_only / agent_only / both
- 龙虾管理 API:绑定/解绑/列表
- 扫描本机龙虾 API:从注册实例获取
API 端点:
- POST /api/v1/auth/login/ - 支持 login_mode 和 selected_agent_id
- GET /api/v1/user/linked-agents/ - 获取绑定龙虾
- POST /api/v1/user/linked-agents/ - 添加绑定龙虾
- DELETE /api/v1/user/linked-agents/{id}/ - 移除龙虾
- GET /api/v1/user/scan-local-agents/ - 扫描本机龙虾
登录模式:
1. human_only - 纯人类身份(1 个座位)
2. agent_only - 纯龙虾身份(1 个座位)
3. both - 双重身份(2 个座位)
测试:
- test_multi_identity.py: 完整测试通过
使用场景:
- 普通用户参会 → human_only
- 龙虾独立参会 → agent_only
- 用户带龙虾助理 → both
140 lines
4.9 KiB
Python
140 lines
4.9 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
测试多身份登录系统
|
||
"""
|
||
|
||
import requests
|
||
|
||
API_BASE = 'http://localhost:8000/api/v1'
|
||
|
||
def test_multi_identity_login():
|
||
print("="*60)
|
||
print("🎭 测试多身份登录系统")
|
||
print("="*60)
|
||
|
||
# 1. 创建测试用户
|
||
print("\n📝 创建测试用户...")
|
||
res = requests.post(f'{API_BASE}/auth/register/', json={
|
||
'username': 'polaris',
|
||
'email': 'polaris@example.com',
|
||
'password': 'password123'
|
||
})
|
||
if res.status_code == 201:
|
||
print(f"✅ 用户创建成功:polaris")
|
||
else:
|
||
print(f"⚠️ 用户可能已存在:{res.json()}")
|
||
|
||
# 2. 注册实例(模拟 OpenClaw)
|
||
print("\n🔧 注册实例...")
|
||
res = requests.post(f'{API_BASE}/instances/register/', json={
|
||
'instance_id': 'phospher-openclaw',
|
||
'instance_name': '飞行侠的 OpenClaw',
|
||
'agent_ids': ['flying_hero', 'lobster_monitor'],
|
||
'webhook_url': 'http://localhost:8888/meeting-notify'
|
||
})
|
||
print(f"✅ 实例注册:{res.json()}")
|
||
|
||
# 3. 绑定龙虾到用户
|
||
print("\n🔗 绑定龙虾到用户...")
|
||
res = requests.post(f'{API_BASE}/user/linked-agents/', json={
|
||
'username': 'polaris',
|
||
'agent_id': 'flying_hero',
|
||
'agent_name': '飞行侠',
|
||
'agent_emoji': '🦸',
|
||
'instance_id': 'phospher-openclaw'
|
||
})
|
||
if res.status_code == 200:
|
||
print(f"✅ 龙虾绑定成功:{res.json()['linked_agents']}")
|
||
else:
|
||
print(f"⚠️ 可能已绑定:{res.json()}")
|
||
|
||
# 4. 扫描本机龙虾
|
||
print("\n📡 扫描本机龙虾...")
|
||
res = requests.get(f'{API_BASE}/user/scan-local-agents/?instance_id=phospher-openclaw')
|
||
if res.status_code == 200:
|
||
agents = res.json()['agents']
|
||
print(f"✅ 扫描到 {len(agents)} 只龙虾:")
|
||
for a in agents:
|
||
print(f" - {a['agent_id']} ({a['instance_name']})")
|
||
else:
|
||
print(f"❌ 扫描失败:{res.json()}")
|
||
|
||
# 5. 测试纯人类登录
|
||
print("\n👤 测试纯人类登录...")
|
||
res = requests.post(f'{API_BASE}/auth/login/', json={
|
||
'username': 'polaris',
|
||
'password': 'password123',
|
||
'login_mode': 'human_only'
|
||
})
|
||
if res.status_code == 200:
|
||
data = res.json()
|
||
print(f"✅ 登录成功")
|
||
print(f" 模式:{data['login_mode']}")
|
||
print(f" 会话数:{len(data['sessions'])}")
|
||
for s in data['sessions']:
|
||
print(f" - {s['session_type']}: {s['nickname']} ({s['emoji']})")
|
||
else:
|
||
print(f"❌ 登录失败:{res.json()}")
|
||
|
||
# 6. 测试纯龙虾登录
|
||
print("\n🦞 测试纯龙虾登录...")
|
||
res = requests.post(f'{API_BASE}/auth/login/', json={
|
||
'username': 'polaris',
|
||
'password': 'password123',
|
||
'login_mode': 'agent_only',
|
||
'selected_agent_id': 'flying_hero'
|
||
})
|
||
if res.status_code == 200:
|
||
data = res.json()
|
||
print(f"✅ 登录成功")
|
||
print(f" 模式:{data['login_mode']}")
|
||
print(f" 会话数:{len(data['sessions'])}")
|
||
for s in data['sessions']:
|
||
print(f" - {s['session_type']}: {s['nickname']} ({s['emoji']})")
|
||
else:
|
||
print(f"❌ 登录失败:{res.json()}")
|
||
|
||
# 7. 测试双重身份登录
|
||
print("\n👤+🦞 测试双重身份登录...")
|
||
res = requests.post(f'{API_BASE}/auth/login/', json={
|
||
'username': 'polaris',
|
||
'password': 'password123',
|
||
'login_mode': 'both',
|
||
'selected_agent_id': 'flying_hero'
|
||
})
|
||
if res.status_code == 200:
|
||
data = res.json()
|
||
print(f"✅ 登录成功")
|
||
print(f" 模式:{data['login_mode']}")
|
||
print(f" 会话数:{len(data['sessions'])}")
|
||
for s in data['sessions']:
|
||
print(f" - {s['session_type']}: {s['nickname']} ({s['emoji']})")
|
||
else:
|
||
print(f"❌ 登录失败:{res.json()}")
|
||
|
||
# 8. 获取用户绑定的龙虾列表
|
||
print("\n📋 获取用户绑定的龙虾列表...")
|
||
res = requests.get(f'{API_BASE}/user/linked-agents/?username=polaris')
|
||
if res.status_code == 200:
|
||
print(f"✅ 获取成功")
|
||
print(f" 龙虾数:{res.json()['count']}")
|
||
for a in res.json()['linked_agents']:
|
||
print(f" - {a['agent_name']} ({a['agent_id']}) {a['agent_emoji']}")
|
||
else:
|
||
print(f"❌ 获取失败:{res.json()}")
|
||
|
||
print("\n" + "="*60)
|
||
print("✅ 多身份登录系统测试完成!")
|
||
print("="*60)
|
||
print("\n📊 登录模式总结:")
|
||
print("1. human_only - 纯人类身份(1 个座位)")
|
||
print("2. agent_only - 纯龙虾身份(1 个座位)")
|
||
print("3. both - 双重身份(2 个座位)")
|
||
print("\n💡 使用场景:")
|
||
print("- 普通用户参会 → human_only")
|
||
print("- 龙虾独立参会 → agent_only")
|
||
print("- 用户带龙虾助理 → both")
|
||
|
||
if __name__ == '__main__':
|
||
test_multi_identity_login()
|