#!/usr/bin/env python3
"""测试完整的登录流程"""

import requests

def test_full_login_flow():
    """测试完整的登录流程"""
    base_url = "http://localhost:8001/api/v1"
    
    # 步骤1: 登录
    print("步骤1: 登录")
    login_data = {
        "username": "admin",
        "password": "admin123"
    }
    
    try:
        response = requests.post(f"{base_url}/users/login", json=login_data)
        print(f"状态码: {response.status_code}")
        
        if response.status_code != 200:
            print(f"登录失败: {response.text}")
            return
        
        data = response.json()
        access_token = data.get('access_token')
        print(f"登录成功!")
        print(f"Token: {access_token[:50]}...")
        
        # 步骤2: 使用token获取用户信息
        print("\n步骤2: 获取用户信息")
        headers = {"Authorization": f"Bearer {access_token}"}
        user_response = requests.get(f"{base_url}/users/me", headers=headers)
        print(f"状态码: {user_response.status_code}")
        
        if user_response.status_code == 200:
            user_data = user_response.json()
            print(f"用户名: {user_data.get('username', 'N/A')}")
            print(f"邮箱: {user_data.get('email', 'N/A')}")
            print(f"角色: {user_data.get('role_name', 'N/A')}")
        else:
            print(f"获取用户信息失败: {user_response.text}")
            
    except Exception as e:
        print(f"错误: {e}")

if __name__ == "__main__":
    test_full_login_flow()