#!/usr/bin/env python3
"""测试登录功能"""

import requests

def test_login():
    """测试登录"""
    base_url = "http://localhost:8001/api/v1"
    
    # 测试登录
    print("测试登录功能:")
    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:
            data = response.json()
            print(f"登录成功!")
            print(f"访问令牌: {data.get('access_token', 'N/A')[:50]}...")
            print(f"令牌类型: {data.get('token_type', 'N/A')}")
            
            # 测试使用令牌访问受保护的API
            if data.get('access_token'):
                headers = {"Authorization": f"Bearer {data['access_token']}"}
                user_response = requests.get(f"{base_url}/users/me", headers=headers)
                print(f"\n测试用户信息API:")
                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')}")
                else:
                    print(f"失败: {user_response.text}")
        else:
            print(f"登录失败: {response.text}")
    except Exception as e:
        print(f"错误: {e}")

if __name__ == "__main__":
    test_login()