#!/usr/bin/env python # -*- coding: utf-8 -*- """ 测试卡密格式更新功能 """ import sys import os # 添加项目路径 sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) # 导入应用和模型 from app import create_app from app.models.license import License from app.utils.validators import LicenseValidator def test_license_generation(): """测试卡密生成""" print("=== 测试卡密生成 ===") # 生成新的卡密(不依赖数据库查询) # 直接测试格式化逻辑 import secrets import string # 模拟生成卡密的逻辑 chars = string.ascii_uppercase + string.digits random_chars = ''.join(secrets.choice(chars) for _ in range(32)) # 格式化为XXXX-XXXX-XXXX-XXXX格式 formatted_key = '-'.join([ random_chars[i:i+8] for i in range(0, len(random_chars), 8) ]) print(f"生成的卡密: {formatted_key}") # 验证格式 validator = LicenseValidator() is_valid = validator.validate_license_key(formatted_key) print(f"格式验证结果: {'通过' if is_valid else '失败'}") # 检查格式是否正确 parts = formatted_key.split('-') if len(parts) == 4 and all(len(part) == 8 for part in parts): print("格式检查: 正确 (XXXX-XXXX-XXXX-XXXX)") else: print("格式检查: 错误") return formatted_key def test_license_formatting(): """测试卡密格式化""" print("\n=== 测试卡密格式化 ===") validator = LicenseValidator() # 测试不同输入格式 test_cases = [ "ABCDEFGHIJKLMNOPQRSTUVWXZY12345678", "ABCD-EFGH-IJKL-MNOP", "ABCDEF123456", "ABCDEFGHIJKLMNOPQRSTUVWXZY12345678901234567890" ] for test_case in test_cases: formatted = validator.format_license_key(test_case) print(f"原始: {test_case}") print(f"格式化后: {formatted}") is_valid = validator.validate_license_key(formatted) print(f"验证结果: {'通过' if is_valid else '失败'}") print("-" * 50) def test_old_format_compatibility(): """测试旧格式兼容性""" print("\n=== 测试旧格式兼容性 ===") validator = LicenseValidator() # 测试旧格式卡密 old_license = "ABCDEFGHIJKLMNOPQRSTUVWXZY123456" # 30位 is_valid = validator.validate_license_key(old_license) print(f"旧格式卡密: {old_license}") print(f"验证结果: {'通过' if is_valid else '失败'}") # 格式化旧格式卡密 formatted = validator.format_license_key(old_license) print(f"格式化后: {formatted}") # 验证格式化后的卡密 is_valid_new = validator.validate_license_key(formatted) print(f"格式化后验证结果: {'通过' if is_valid_new else '失败'}") def test_new_license_key_validation(): """测试新格式卡密验证""" print("\n=== 测试新格式卡密验证 ===") validator = LicenseValidator() # 测试正确的新格式 new_format = "ABCD1234-EFGH5678-IJKL9012-MNOP3456" is_valid = validator.validate_license_key(new_format) print(f"新格式卡密: {new_format}") print(f"验证结果: {'通过' if is_valid else '失败'}") # 测试错误格式 wrong_formats = [ "ABCD123-EFGH5678-IJKL9012-MNOP3456", # 第一部分只有7位 "ABCD12345-EFGH5678-IJKL9012-MNOP3456", # 第一部分有9位 "ABCD1234-EFGH567-IJKL9012-MNOP3456", # 第二部分只有7位 "ABCD1234-EFGH5678-IJKL9012-MNOP345", # 第四部分只有7位 "ABCD1234_EFGH5678_IJKL9012_MNOP3456" # 使用下划线而不是连字符 ] for wrong_format in wrong_formats: is_valid = validator.validate_license_key(wrong_format) print(f"错误格式: {wrong_format}") print(f"验证结果: {'通过' if is_valid else '失败'}") if __name__ == "__main__": print("开始测试卡密格式更新功能...") # 测试卡密生成 generated_key = test_license_generation() # 测试卡密格式化 test_license_formatting() # 测试旧格式兼容性 test_old_format_compatibility() # 测试新格式验证 test_new_license_key_validation() print("\n测试完成!")