100 lines
3.8 KiB
Python
100 lines
3.8 KiB
Python
|
|
import requests
|
|||
|
|
from bs4 import BeautifulSoup
|
|||
|
|
import time
|
|||
|
|
|
|||
|
|
def final_session_test():
|
|||
|
|
print("=== 最终会话测试 ===")
|
|||
|
|
|
|||
|
|
# 创建会话
|
|||
|
|
session = requests.Session()
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
# 1. 获取登录页面
|
|||
|
|
print("1. 获取登录页面...")
|
|||
|
|
login_page = session.get('http://localhost:5000/login')
|
|||
|
|
if login_page.status_code != 200:
|
|||
|
|
print(" ❌ 获取登录页面失败")
|
|||
|
|
return False
|
|||
|
|
print(" ✅ 获取登录页面成功")
|
|||
|
|
|
|||
|
|
# 2. 解析CSRF令牌
|
|||
|
|
soup = BeautifulSoup(login_page.text, 'html.parser')
|
|||
|
|
csrf_token_input = soup.find('input', {'name': 'csrf_token'})
|
|||
|
|
csrf_token = csrf_token_input['value'] if csrf_token_input else ''
|
|||
|
|
if not csrf_token:
|
|||
|
|
print(" ❌ 未找到CSRF令牌")
|
|||
|
|
return False
|
|||
|
|
print(" ✅ 获取CSRF令牌成功")
|
|||
|
|
|
|||
|
|
# 3. 测试登录
|
|||
|
|
print("2. 测试登录...")
|
|||
|
|
login_data = {
|
|||
|
|
'username': 'admin',
|
|||
|
|
'password': 'admin123',
|
|||
|
|
'csrf_token': csrf_token
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
headers = {
|
|||
|
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
|||
|
|
'Referer': 'http://localhost:5000/login'
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
login_response = session.post('http://localhost:5000/login', data=login_data, headers=headers)
|
|||
|
|
if login_response.status_code not in [200, 302]:
|
|||
|
|
print(f" ❌ 登录失败,状态码: {login_response.status_code}")
|
|||
|
|
return False
|
|||
|
|
print(" ✅ 登录请求发送成功")
|
|||
|
|
|
|||
|
|
# 4. 访问仪表板验证登录
|
|||
|
|
print("3. 验证登录状态...")
|
|||
|
|
dashboard_response = session.get('http://localhost:5000/dashboard', headers=headers)
|
|||
|
|
if dashboard_response.status_code != 200:
|
|||
|
|
print(f" ❌ 访问仪表板失败,状态码: {dashboard_response.status_code}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
# 检查是否真的在仪表板页面
|
|||
|
|
if '登录' in dashboard_response.text or 'login' in dashboard_response.text.lower():
|
|||
|
|
print(" ❌ 被重定向到登录页面,登录失败")
|
|||
|
|
return False
|
|||
|
|
print(" ✅ 登录成功,已进入仪表板")
|
|||
|
|
|
|||
|
|
# 5. 测试会话保持 - 等待较长时间
|
|||
|
|
print("4. 测试长时间会话保持...")
|
|||
|
|
print(" 等待10秒...")
|
|||
|
|
time.sleep(10)
|
|||
|
|
|
|||
|
|
dashboard_response2 = session.get('http://localhost:5000/dashboard', headers=headers)
|
|||
|
|
if dashboard_response2.status_code != 200:
|
|||
|
|
print(f" ❌ 长时间后访问仪表板失败,状态码: {dashboard_response2.status_code}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
# 再次检查是否还在仪表板页面
|
|||
|
|
if '登录' in dashboard_response2.text or 'login' in dashboard_response2.text.lower():
|
|||
|
|
print(" ❌ 长时间后被重定向到登录页面,会话已过期")
|
|||
|
|
return False
|
|||
|
|
print(" ✅ 长时间后会话仍然有效")
|
|||
|
|
|
|||
|
|
# 6. 测试API访问
|
|||
|
|
print("5. 测试API访问...")
|
|||
|
|
api_response = session.get('http://localhost:5000/api/v1/versions', headers=headers)
|
|||
|
|
if api_response.status_code != 200:
|
|||
|
|
print(f" ❌ API访问失败,状态码: {api_response.status_code}")
|
|||
|
|
return False
|
|||
|
|
print(" ✅ API访问成功")
|
|||
|
|
|
|||
|
|
print("\n🎉 所有测试通过!会话管理功能正常工作。")
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f" ❌ 测试过程中出现错误: {e}")
|
|||
|
|
import traceback
|
|||
|
|
traceback.print_exc()
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
success = final_session_test()
|
|||
|
|
if not success:
|
|||
|
|
print("\n❌ 会话测试失败!")
|
|||
|
|
exit(1)
|
|||
|
|
else:
|
|||
|
|
print("\n✅ 会话测试成功!")
|