"""
测试登录API
"""
import requests

# 测试登录
url = "http://localhost:8000/api/users/login"

# OAuth2PasswordRequestForm 格式需要使用 form-data
data = {
    'username': 'admin',
    'password': '123456'
}

print("测试登录API...")
print(f"URL: {url}")
print(f"数据: {data}")
print("\n" + "="*60)

try:
    response = requests.post(url, data=data)
    print(f"状态码: {response.status_code}")
    print(f"响应: {response.json()}")
    
    if response.status_code == 200:
        print("\n✓ 登录成功！")
        token = response.json()['access_token']
        print(f"Token: {token[:50]}...")
        
        # 测试获取用户信息
        print("\n" + "="*60)
        print("测试获取用户信息...")
        me_url = "http://localhost:8000/api/users/me"
        headers = {'Authorization': f'Bearer {token}'}
        me_response = requests.get(me_url, headers=headers)
        print(f"状态码: {me_response.status_code}")
        print(f"用户信息: {me_response.json()}")
    else:
        print("\n✗ 登录失败")
        
except requests.exceptions.ConnectionError:
    print("✗ 无法连接到服务器，请确保后端服务正在运行")
    print("运行命令: cd backend && python main.py")
except Exception as e:
    print(f"✗ 错误: {e}")
