137 lines
4.1 KiB
Python
137 lines
4.1 KiB
Python
|
|
import requests
|
|||
|
|
from bs4 import BeautifulSoup
|
|||
|
|
|
|||
|
|
# 创建会话以保持登录状态
|
|||
|
|
session = requests.Session()
|
|||
|
|
|
|||
|
|
def get_csrf_token():
|
|||
|
|
"""从登录页面获取CSRF令牌"""
|
|||
|
|
try:
|
|||
|
|
response = session.get("http://localhost:5000/login")
|
|||
|
|
soup = BeautifulSoup(response.text, 'html.parser')
|
|||
|
|
csrf_input = soup.find('input', {'name': 'csrf_token'})
|
|||
|
|
if csrf_input:
|
|||
|
|
# 直接获取value属性
|
|||
|
|
try:
|
|||
|
|
# 忽略类型检查错误
|
|||
|
|
csrf_token = csrf_input.get('value') # type: ignore
|
|||
|
|
if not csrf_token:
|
|||
|
|
csrf_token = csrf_input['value'] # type: ignore
|
|||
|
|
if csrf_token:
|
|||
|
|
return csrf_token
|
|||
|
|
except:
|
|||
|
|
pass
|
|||
|
|
print("未找到CSRF令牌输入字段")
|
|||
|
|
return None
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"获取CSRF令牌失败: {e}")
|
|||
|
|
return None
|
|||
|
|
|
|||
|
|
def login():
|
|||
|
|
"""登录系统"""
|
|||
|
|
try:
|
|||
|
|
# 获取CSRF令牌
|
|||
|
|
csrf_token = get_csrf_token()
|
|||
|
|
if not csrf_token:
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
# 准备登录数据
|
|||
|
|
login_data = {
|
|||
|
|
'username': 'admin',
|
|||
|
|
'password': 'admin123',
|
|||
|
|
'csrf_token': csrf_token
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# 发送登录请求
|
|||
|
|
response = session.post("http://localhost:5000/login", data=login_data)
|
|||
|
|
|
|||
|
|
# 检查是否登录成功(通过重定向到dashboard来判断)
|
|||
|
|
if response.url and 'dashboard' in response.url:
|
|||
|
|
print("登录成功")
|
|||
|
|
return True
|
|||
|
|
else:
|
|||
|
|
print(f"登录失败,状态码: {response.status_code}")
|
|||
|
|
print(f"响应URL: {response.url}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"登录过程中出现错误: {e}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
def test_create_product():
|
|||
|
|
"""测试创建产品"""
|
|||
|
|
try:
|
|||
|
|
# 准备产品数据
|
|||
|
|
product_data = {
|
|||
|
|
'product_name': '测试产品',
|
|||
|
|
'description': '这是一个测试产品'
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# 发送创建产品请求
|
|||
|
|
response = session.post(
|
|||
|
|
"http://localhost:5000/api/v1/products",
|
|||
|
|
json=product_data,
|
|||
|
|
headers={'Content-Type': 'application/json'}
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
print(f"创建产品状态码: {response.status_code}")
|
|||
|
|
print(f"创建产品响应: {response.text}")
|
|||
|
|
return response.status_code == 200
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"创建产品时出现错误: {e}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
def test_get_logs():
|
|||
|
|
"""测试获取操作日志"""
|
|||
|
|
try:
|
|||
|
|
# 发送获取日志请求
|
|||
|
|
response = session.get("http://localhost:5000/api/v1/logs")
|
|||
|
|
|
|||
|
|
print(f"获取日志状态码: {response.status_code}")
|
|||
|
|
print(f"获取日志响应: {response.text}")
|
|||
|
|
return response.status_code == 200
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"获取日志时出现错误: {e}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
def test_view_logs_page():
|
|||
|
|
"""测试访问日志页面"""
|
|||
|
|
try:
|
|||
|
|
# 访问日志管理页面
|
|||
|
|
response = session.get("http://localhost:5000/logs")
|
|||
|
|
|
|||
|
|
print(f"访问日志页面状态码: {response.status_code}")
|
|||
|
|
if response.status_code == 200:
|
|||
|
|
print("成功访问日志管理页面")
|
|||
|
|
return True
|
|||
|
|
else:
|
|||
|
|
print(f"访问日志页面失败: {response.text}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"访问日志页面时出现错误: {e}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
print("开始测试日志功能...")
|
|||
|
|
|
|||
|
|
# 登录
|
|||
|
|
if login():
|
|||
|
|
print("=== 登录成功 ===")
|
|||
|
|
|
|||
|
|
# 测试创建产品(这会生成操作日志)
|
|||
|
|
print("\n=== 测试创建产品 ===")
|
|||
|
|
test_create_product()
|
|||
|
|
|
|||
|
|
# 测试获取操作日志
|
|||
|
|
print("\n=== 测试获取操作日志 ===")
|
|||
|
|
test_get_logs()
|
|||
|
|
|
|||
|
|
# 测试访问日志页面
|
|||
|
|
print("\n=== 测试访问日志页面 ===")
|
|||
|
|
test_view_logs_page()
|
|||
|
|
|
|||
|
|
else:
|
|||
|
|
print("登录失败,无法继续测试")
|