- 添加UI测试场景,支持模拟各种认证场景 - 实现API测试脚本,覆盖登录、注册、验证码等接口 - 添加测试文档,说明测试用例和预期结果 - 支持自动化测试和手动测试验证
108 lines
3.8 KiB
Python
108 lines
3.8 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
简化版API接口测试脚本
|
|
|
|
快速验证API接口的基本连通性和功能
|
|
"""
|
|
|
|
import requests
|
|
import json
|
|
import time
|
|
from datetime import datetime
|
|
|
|
|
|
class SimpleAPITester:
|
|
def __init__(self, base_url="https://whaletownend.xinghangee.icu"):
|
|
self.base_url = base_url.rstrip('/')
|
|
self.session = requests.Session()
|
|
self.session.headers.update({
|
|
'Content-Type': 'application/json',
|
|
'User-Agent': 'whaleTown-Simple-Tester/1.0'
|
|
})
|
|
|
|
def log(self, message):
|
|
timestamp = datetime.now().strftime("%H:%M:%S")
|
|
print(f"[{timestamp}] {message}")
|
|
|
|
def test_endpoint(self, method, endpoint, data=None, description=""):
|
|
"""测试单个端点"""
|
|
url = f"{self.base_url}{endpoint}"
|
|
|
|
try:
|
|
if method.upper() == 'GET':
|
|
response = self.session.get(url, timeout=10)
|
|
elif method.upper() == 'POST':
|
|
response = self.session.post(url, json=data, timeout=10)
|
|
|
|
self.log(f"✅ {description}")
|
|
self.log(f" 状态码: {response.status_code}")
|
|
|
|
try:
|
|
result = response.json()
|
|
self.log(f" 响应: {json.dumps(result, ensure_ascii=False, indent=2)[:200]}...")
|
|
return result
|
|
except:
|
|
self.log(f" 响应: {response.text[:100]}...")
|
|
return {"status_code": response.status_code, "text": response.text}
|
|
|
|
except Exception as e:
|
|
self.log(f"❌ {description} - 错误: {str(e)}")
|
|
return None
|
|
|
|
def run_basic_tests(self):
|
|
"""运行基础测试"""
|
|
self.log("🚀 开始基础API测试...")
|
|
self.log(f"测试目标: {self.base_url}")
|
|
|
|
# 1. 测试应用状态
|
|
self.test_endpoint('GET', '/', description="应用状态检查")
|
|
|
|
# 2. 测试发送验证码
|
|
result = self.test_endpoint('POST', '/auth/send-email-verification',
|
|
{'email': 'test@example.com'},
|
|
"发送邮箱验证码")
|
|
|
|
# 3. 如果获取到验证码,测试注册
|
|
if result and 'data' in result and 'verification_code' in result['data']:
|
|
verification_code = result['data']['verification_code']
|
|
self.log(f"📧 获取到验证码: {verification_code}")
|
|
|
|
# 测试用户注册(需要验证码)
|
|
username = f"testuser_{int(time.time())}"
|
|
self.test_endpoint('POST', '/auth/register', {
|
|
'username': username,
|
|
'password': 'Test123456',
|
|
'nickname': '测试用户',
|
|
'email': 'test@example.com',
|
|
'email_verification_code': verification_code # 添加验证码
|
|
}, "用户注册")
|
|
|
|
# 测试用户登录
|
|
self.test_endpoint('POST', '/auth/login', {
|
|
'identifier': username,
|
|
'password': 'Test123456'
|
|
}, "用户登录")
|
|
|
|
# 4. 测试错误情况
|
|
self.test_endpoint('POST', '/auth/login', {
|
|
'identifier': 'nonexistent',
|
|
'password': 'wrongpassword'
|
|
}, "无效登录测试")
|
|
|
|
# 5. 测试管理员登录(可能失败)
|
|
self.test_endpoint('POST', '/admin/auth/login', {
|
|
'identifier': 'admin',
|
|
'password': 'Admin123456'
|
|
}, "管理员登录测试")
|
|
|
|
self.log("🎯 基础测试完成!")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
import sys
|
|
|
|
base_url = sys.argv[1] if len(sys.argv) > 1 else "https://whaletownend.xinghangee.icu"
|
|
|
|
tester = SimpleAPITester(base_url)
|
|
tester.run_basic_tests() |