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

print("=" * 60)
print("完整登录流程测试")
print("=" * 60)

base_url = "http://localhost:8000"

# 1. 测试登录
print("\n1. 测试登录...")
print("-" * 60)

login_response = requests.post(
    f"{base_url}/api/v1/auth/login",
    json={
        "username": "admin",
        "password": "Admin123456"
    }
)

print(f"状态码: {login_response.status_code}")
login_data = login_response.json()

if login_data.get("code") == 0:
    print("✅ 登录成功!")
    access_token = login_data["data"]["access_token"]
    refresh_token = login_data["data"]["refresh_token"]
    print(f"Access Token: {access_token[:50]}...")
    print(f"Refresh Token: {refresh_token[:50]}...")
else:
    print(f"❌ 登录失败: {login_data.get('message')}")
    exit(1)

# 2. 测试获取用户信息
print("\n2. 测试获取用户信息...")
print("-" * 60)

userinfo_response = requests.get(
    f"{base_url}/api/v1/auth/userinfo",
    headers={
        "Authorization": f"Bearer {access_token}"
    }
)

print(f"状态码: {userinfo_response.status_code}")
userinfo_data = userinfo_response.json()

if userinfo_data.get("code") == 0:
    print("✅ 获取用户信息成功!")
    user = userinfo_data["data"]
    print(f"用户ID: {user.get('id')}")
    print(f"用户名: {user.get('username')}")
    print(f"邮箱: {user.get('email')}")
    print(f"是否激活: {user.get('is_active')}")
else:
    print(f"❌ 获取用户信息失败: {userinfo_data.get('message')}")

# 3. 测试刷新令牌
print("\n3. 测试刷新令牌...")
print("-" * 60)

refresh_response = requests.post(
    f"{base_url}/api/v1/auth/refresh",
    json={
        "refresh_token": refresh_token
    }
)

print(f"状态码: {refresh_response.status_code}")
refresh_data = refresh_response.json()

if refresh_data.get("code") == 0:
    print("✅ 刷新令牌成功!")
    new_access_token = refresh_data["data"]["access_token"]
    print(f"新 Access Token: {new_access_token[:50]}...")
else:
    print(f"❌ 刷新令牌失败: {refresh_data.get('message')}")

print("\n" + "=" * 60)
print("✅ 所有测试通过！登录功能正常工作。")
print("=" * 60)
