Files
meeting-room/backend/test_multi_identity.py

140 lines
4.9 KiB
Python
Raw Normal View History

#!/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()