#!/usr/bin/env python # -*- coding: utf-8 -*- """ 附件功能测试脚本 用于测试附件下载和上传功能 """ import sys from pathlib import Path # 添加项目根目录到Python路径 project_root = Path(__file__).parent sys.path.insert(0, str(project_root)) from src.config import ConfigManager from src.token_manager import TokenManager from src.attachment_handler import AttachmentHandler from src.tapd_api import TAPDApi def test_attachment_handler(): """测试附件处理器""" print("\n" + "=" * 60) print("测试1:附件处理器初始化") print("=" * 60) try: # 加载配置 config_manager = ConfigManager() attachment_config = config_manager.get_attachment_config() print(f"✓ 附件配置加载成功:") print(f" 临时目录: {attachment_config['temp_dir']}") print(f" 最大文件大小: {attachment_config['max_file_size']}MB") print(f" 下载超时: {attachment_config['download_timeout']}秒") print(f" 上传超时: {attachment_config['upload_timeout']}秒") print(f" 下载重试次数: {attachment_config['download_retry']}") print(f" 上传重试次数: {attachment_config['upload_retry']}") # 获取access_token token_manager = TokenManager() access_token = token_manager.get_token() print(f"\n✓ access_token获取成功") # 初始化附件处理器 handler = AttachmentHandler(access_token, attachment_config) print(f"\n✓ 附件处理器初始化成功") return True except Exception as e: print(f"\n✗ 测试失败: {e}") import traceback traceback.print_exc() return False def test_tapd_upload(): """测试TAPD附件上传功能""" print("\n" + "=" * 60) print("测试2:TAPD附件上传API") print("=" * 60) try: # 加载配置 config_manager = ConfigManager() tapd_config = config_manager.get_tapd_config() attachment_config = config_manager.get_attachment_config() # 初始化TAPD API tapd_api = TAPDApi(tapd_config['workspace_id'], test_mode=False) print(f"✓ TAPD API初始化成功") # 检查是否有测试文件 test_file = Path("test.png") if not test_file.exists(): print(f"\n⚠ 测试文件不存在: {test_file}") print(f" 请在项目根目录放置一个test.png文件用于测试") return False print(f"\n✓ 找到测试文件: {test_file}") print(f" 文件大小: {test_file.stat().st_size / 1024:.2f}KB") # 提示用户输入bug_id print(f"\n请输入一个已存在的bug ID用于测试附件上传:") print(f"(如果不想测试上传,直接按回车跳过)") bug_id = input("Bug ID: ").strip() if not bug_id: print(f"\n⚠ 跳过上传测试") return True # 测试上传 print(f"\n开始测试上传附件到bug {bug_id}...") result = tapd_api.upload_attachment( str(test_file), bug_id, max_size=attachment_config['max_file_size'] * 1024 * 1024, upload_timeout=attachment_config['upload_timeout'], max_retries=attachment_config['upload_retry'] ) if result['success']: print(f"\n✓ 上传测试成功!") print(f" 附件ID: {result['attachment_id']}") return True else: print(f"\n✗ 上传测试失败: {result['error_message']}") return False except Exception as e: print(f"\n✗ 测试失败: {e}") import traceback traceback.print_exc() return False def main(): """主函数""" print("=" * 60) print("附件功能测试") print("=" * 60) # 测试1:附件处理器 test1_passed = test_attachment_handler() # 测试2:TAPD上传 test2_passed = test_tapd_upload() # 显示测试结果 print("\n" + "=" * 60) print("测试结果汇总") print("=" * 60) print(f"测试1 - 附件处理器初始化: {'✓ 通过' if test1_passed else '✗ 失败'}") print(f"测试2 - TAPD附件上传API: {'✓ 通过' if test2_passed else '✗ 失败'}") print("=" * 60) if test1_passed and test2_passed: print("\n✓ 所有测试通过!") return 0 else: print("\n✗ 部分测试失败,请检查错误信息") return 1 if __name__ == "__main__": sys.exit(main())