Kamixitong/comprehensive_test.py
2025-11-11 21:39:12 +08:00

135 lines
4.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
综合测试修复后的账号创建API
"""
import json
from app import create_app
from app.api.admin import validate_admin_data
def test_backend_logic():
"""测试后端逻辑"""
print("=== 测试后端逻辑 ===")
app = create_app()
with app.app_context():
# 模拟API接收到的数据
# 情况1: 没有password字段前端密码为空时不会发送
data1 = {
"username": "test_user1",
"email": "test1@example.com",
"role": 0,
"status": 1
# 注意没有password字段
}
print("\n情况1: 没有password字段")
print(f"接收到的数据: {json.dumps(data1, ensure_ascii=False)}")
# 模拟后端处理逻辑(修复后的版本)
password1 = data1.get('password', '')
print(f"提取的密码: '{password1}'")
print(f"'password'键是否存在: {'password' in data1}")
print(f"密码是否为空: {not password1}")
if 'password' not in data1 or not password1:
print("❌ 验证失败: 密码不能为空")
else:
print("✅ 验证通过")
# 情况2: 空密码字段
data2 = {
"username": "test_user2",
"email": "test2@example.com",
"password": "", # 空密码
"role": 0,
"status": 1
}
print("\n情况2: 空密码字段")
print(f"接收到的数据: {json.dumps(data2, ensure_ascii=False)}")
# 模拟后端处理逻辑
password2 = data2.get('password', '')
print(f"提取的密码: '{password2}'")
print(f"'password'键是否存在: {'password' in data2}")
print(f"密码是否为空: {not password2}")
if 'password' not in data2 or not password2:
print("❌ 验证失败: 密码不能为空")
else:
print("✅ 验证通过")
# 情况3: 有效密码
data3 = {
"username": "test_user3",
"email": "test3@example.com",
"password": "ValidPass123!", # 有效密码
"role": 0,
"status": 1
}
print("\n情况3: 有效密码")
print(f"接收到的数据: {json.dumps(data3, ensure_ascii=False)}")
# 模拟后端处理逻辑
password3 = data3.get('password', '')
print(f"提取的密码: '{password3}'")
print(f"'password'键是否存在: {'password' in data3}")
print(f"密码是否为空: {not password3}")
if 'password' not in data3 or not password3:
print("❌ 验证失败: 密码不能为空")
else:
print("✅ 验证通过")
def test_validate_function():
"""测试验证函数"""
print("\n=== 测试验证函数 ===")
app = create_app()
with app.app_context():
# 测试数据验证函数
data1 = {
"username": "test_user1",
"email": "test1@example.com",
"role": 0,
"status": 1
}
print("\n测试数据验证函数 - 情况1:")
is_valid, message = validate_admin_data(data1, is_create=True)
print(f"数据: {json.dumps(data1, ensure_ascii=False)}")
print(f"验证结果: {is_valid}, 消息: {message}")
data2 = {
"username": "test_user2",
"email": "test2@example.com",
"password": "", # 空密码
"role": 0,
"status": 1
}
print("\n测试数据验证函数 - 情况2:")
is_valid, message = validate_admin_data(data2, is_create=True)
print(f"数据: {json.dumps(data2, ensure_ascii=False)}")
print(f"验证结果: {is_valid}, 消息: {message}")
data3 = {
"username": "test_user3",
"email": "test3@example.com",
"password": "ValidPass123!", # 有效密码
"role": 0,
"status": 1
}
print("\n测试数据验证函数 - 情况3:")
is_valid, message = validate_admin_data(data3, is_create=True)
print(f"数据: {json.dumps(data3, ensure_ascii=False)}")
print(f"验证结果: {is_valid}, 消息: {message}")
if __name__ == "__main__":
test_backend_logic()
test_validate_function()