#!/usr/bin/env python3
"""
测试登录接口
"""
import requests
import json

# 测试登录
def test_login():
    url = "http://localhost:8037/api/v1/auth/login"
    
    # 测试不同的密码
    test_cases = [
        {"username": "admin", "password": "123456"},
        {"username": "admin", "password": "admin"},
    ]
    
    for case in test_cases:
        print(f"\n🔍 测试登录: username={case['username']}, password={case['password']}")
        try:
            response = requests.post(
                url,
                data=case,
                headers={"Content-Type": "application/x-www-form-urlencoded"},
                timeout=5
            )
            print(f"状态码: {response.status_code}")
            if response.status_code == 200:
                data = response.json()
                print(f"✅ 登录成功!")
                print(f"Token: {data.get('access_token', '')[:50]}...")
                return True
            else:
                print(f"❌ 登录失败: {response.text}")
        except requests.exceptions.Timeout:
            print("❌ 请求超时 - 后端服务可能未响应")
        except requests.exceptions.ConnectionError:
            print("❌ 连接失败 - 后端服务可能未运行")
        except Exception as e:
            print(f"❌ 错误: {e}")
    
    return False

if __name__ == "__main__":
    print("=" * 50)
    print("登录接口测试")
    print("=" * 50)
    test_login()
