#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
创建登录模块和测试用例
"""

import requests
import json

BASE_URL = "http://localhost:8001/api"

# 1. 创建登录模块
print("创建登录模块...")
module_data = {
    "name": "登录模块",
    "description": "统一管理平台登录功能测试",
    "order": 1
}

response = requests.post(f"{BASE_URL}/modules", json=module_data)
module = response.json()
print(f"模块ID: {module['id']}")
print(f"模块名称: {module['name']}")

module_id = module['id']

# 2. 创建测试用例
test_cases = [
    {
        "name": "登录功能-页面访问验证",
        "description": "验证登录页面是否可以正常访问",
        "priority": "high",
        "steps": [
            {"order": 1, "name": "打开登录页面", "action": "navigate", "params": {"url": "https://192.168.5.44"}},
            {"order": 2, "name": "验证页面标题", "action": "assert", "params": {"type": "title"}, "expected": "统一管理平台"},
            {"order": 3, "name": "验证登录按钮存在", "action": "assert", "params": {"type": "visible", "selector": "button:has-text('登录')"}}
        ]
    },
    {
        "name": "登录功能-正常登录",
        "description": "使用正确的用户名和密码登录",
        "priority": "high",
        "steps": [
            {"order": 1, "name": "打开登录页面", "action": "navigate", "params": {"url": "https://192.168.5.44"}},
            {"order": 2, "name": "输入用户名", "action": "fill", "params": {"selector": "input[placeholder*='用户']", "value": "admin@xty"}},
            {"order": 3, "name": "输入密码", "action": "fill", "params": {"selector": "input[type='password']", "value": "Ubains@13579"}},
            {"order": 4, "name": "输入验证码", "action": "fill", "params": {"selector": "input[placeholder*='验证']", "value": "csba"}},
            {"order": 5, "name": "点击登录按钮", "action": "click", "params": {"selector": "button:has-text('登录')"}},
            {"order": 6, "name": "等待页面跳转", "action": "wait", "params": {"selector": "body", "timeout": 5000}},
            {"order": 7, "name": "验证登录成功", "action": "assert", "params": {"type": "url"}, "expected": "platform"}
        ]
    },
    {
        "name": "登录功能-用户名为空",
        "description": "用户名为空时点击登录",
        "priority": "medium",
        "steps": [
            {"order": 1, "name": "打开登录页面", "action": "navigate", "params": {"url": "https://192.168.5.44"}},
            {"order": 2, "name": "输入密码", "action": "fill", "params": {"selector": "input[type='password']", "value": "Ubains@13579"}},
            {"order": 3, "name": "点击登录按钮", "action": "click", "params": {"selector": "button:has-text('登录')"}},
            {"order": 4, "name": "验证错误提示", "action": "assert", "params": {"type": "visible", "selector": ".el-message--error"}}
        ]
    },
    {
        "name": "登录功能-密码为空",
        "description": "密码为空时点击登录",
        "priority": "medium",
        "steps": [
            {"order": 1, "name": "打开登录页面", "action": "navigate", "params": {"url": "https://192.168.5.44"}},
            {"order": 2, "name": "输入用户名", "action": "fill", "params": {"selector": "input[placeholder*='用户']", "value": "admin@xty"}},
            {"order": 3, "name": "点击登录按钮", "action": "click", "params": {"selector": "button:has-text('登录')"}},
            {"order": 4, "name": "验证错误提示", "action": "assert", "params": {"type": "visible", "selector": ".el-message--error"}}
        ]
    },
    {
        "name": "登录功能-错误密码",
        "description": "使用错误的密码登录",
        "priority": "medium",
        "steps": [
            {"order": 1, "name": "打开登录页面", "action": "navigate", "params": {"url": "https://192.168.5.44"}},
            {"order": 2, "name": "输入用户名", "action": "fill", "params": {"selector": "input[placeholder*='用户']", "value": "admin@xty"}},
            {"order": 3, "name": "输入错误密码", "action": "fill", "params": {"selector": "input[type='password']", "value": "WrongPassword"}},
            {"order": 4, "name": "输入验证码", "action": "fill", "params": {"selector": "input[placeholder*='验证']", "value": "csba"}},
            {"order": 5, "name": "点击登录按钮", "action": "click", "params": {"selector": "button:has-text('登录')"}},
            {"order": 6, "name": "验证错误提示", "action": "assert", "params": {"type": "visible", "selector": ".el-message--error"}}
        ]
    },
    {
        "name": "登录功能-记住密码勾选框",
        "description": "验证记住密码勾选框功能",
        "priority": "low",
        "steps": [
            {"order": 1, "name": "打开登录页面", "action": "navigate", "params": {"url": "https://192.168.5.44"}},
            {"order": 2, "name": "验证勾选框存在", "action": "assert", "params": {"type": "visible", "selector": "input[type='checkbox']"}},
            {"order": 3, "name": "勾选记住密码", "action": "check", "params": {"selector": "input[type='checkbox']"}}
        ]
    },
    {
        "name": "登录功能-忘记密码链接",
        "description": "验证忘记密码链接是否可用",
        "priority": "low",
        "steps": [
            {"order": 1, "name": "打开登录页面", "action": "navigate", "params": {"url": "https://192.168.5.44"}},
            {"order": 2, "name": "验证忘记密码链接存在", "action": "assert", "params": {"type": "visible", "selector": "text=忘记密码"}}
        ]
    },
    {
        "name": "登录功能-立即注册链接",
        "description": "验证立即注册链接是否可用",
        "priority": "low",
        "steps": [
            {"order": 1, "name": "打开登录页面", "action": "navigate", "params": {"url": "https://192.168.5.44"}},
            {"order": 2, "name": "验证立即注册链接存在", "action": "assert", "params": {"type": "visible", "selector": "text=立即注册"}}
        ]
    }
]

print(f"\n开始创建 {len(test_cases)} 个测试用例...")
created_cases = []

for case_data in test_cases:
    case_payload = {
        "module_id": module_id,
        "name": case_data["name"],
        "description": case_data["description"],
        "priority": case_data["priority"],
        "steps": case_data["steps"]
    }

    response = requests.post(f"{BASE_URL}/cases", json=case_payload)
    if response.status_code == 200:
        case = response.json()
        created_cases.append(case)
        print(f"[OK] {case_data['name']} (ID: {case['id']})")
    else:
        print(f"[FAIL] Created failed: {case_data['name']} - {response.text}")

print(f"\n[SUCCESS] Created {len(created_cases)} test cases")
print(f"Module ID: {module_id}")
print(f"Case IDs: {[c['id'] for c in created_cases]}")
