"""预置数据空间测试数据""" import sys import os import time from pathlib import Path sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from playwright.sync_api import sync_playwright from tests.utils import APIClient from config.settings import config from scripts.shared_data import AUTH_STATE_FILE, TEST_FILES, TEST_FOLDERS, get_token def main(): print("=== 开始预置测试数据 ===") with sync_playwright() as p: browser = p.chromium.launch(headless=True) context = browser.new_context( storage_state=str(AUTH_STATE_FILE), viewport={"width": 1280, "height": 720}, ) page = context.new_page() # 导航到数据空间 print("1. 导航到数据空间...") page.goto( f"{config.BASE_URL.rstrip('/')}/d-cloud/data-space/list?tab=personal", wait_until="domcontentloaded", timeout=60000, ) # 等待空间卡片 page.wait_for_selector(".ant-card.ant-card-hoverable", timeout=30000) # 获取 API client(from_page 内建 token 提取,降级使用 get_token) api_client = APIClient.from_page(page) token = get_token() if token and not api_client.headers.get("Authorization"): api_client.headers["Authorization"] = token print(f" Token: {token[:30]}..." if token else " No token!") # 获取空间列表 print("\n2. 获取空间列表...") resp = api_client.post( "/api/cloudDiskApi/spaces/list", json={"page": 1, "page_size": 100} ) data = resp.json() spaces = data.get("data", {}).get("list", []) personal_spaces = [] for space in spaces: name = space.get("name", "") space_id = space.get("id") space_type = space.get("space_type") print(f' - ID={space_id}, name="{name}", type={space_type}') if space_type is None: personal_spaces.append({"id": space_id, "name": name}) print(f"\n 个人空间数量: {len(personal_spaces)}") if not personal_spaces: print(" 错误: 没有找到个人空间!") browser.close() return # 选择第一个个人空间 target_space = personal_spaces[0] space_id = target_space["id"] space_name = target_space["name"] print(f'\n3. 使用空间: "{space_name}" (ID={space_id})') # 检查现有文件 resp = api_client.post( f"/api/cloudDiskApi/files/{space_id}", json={"path": "", "page": 1, "page_size": 100}, ) existing_files = resp.json().get("data", {}).get("files", []) existing_names = [f.get("name") for f in existing_files] print(f" 现有文件数: {len(existing_files)}") # 上传测试文件 print("\n4. 上传测试文件...") for file_info in TEST_FILES: file_name = file_info["name"] content = file_info["content"] if file_name in existing_names: print(f" - {file_name}: 已存在,跳过") continue # 上传文件 if isinstance(content, bytes): file_bytes = content else: file_bytes = content.encode("utf-8") resp = api_client.upload_form( f"/api/cloudDiskApi/files/{space_id}/upload", file_name=file_name, file_bytes=file_bytes, form_data={"path": ""}, ) result = resp.json() if result.get("status") == 0: print(f" - {file_name}: 上传成功") else: print(f" - {file_name}: 上传失败 - {result}") # 创建测试文件夹 print("\n5. 创建测试文件夹...") for folder_name in TEST_FOLDERS: if folder_name in existing_names: print(f" - {folder_name}: 已存在,跳过") continue resp = api_client.post( f"/api/cloudDiskApi/files/{space_id}/folder", json={"name": folder_name, "path": ""}, ) result = resp.json() if result.get("status") == 0: print(f" - {folder_name}: 创建成功") else: print(f" - {folder_name}: 创建失败 - {result}") # 验证 print("\n6. 验证文件列表...") resp = api_client.post( f"/api/cloudDiskApi/files/{space_id}", json={"path": "", "page": 1, "page_size": 100}, ) final_files = resp.json().get("data", {}).get("files", []) print(f" 最终文件数: {len(final_files)}") for f in final_files: is_dir = "📁" if f.get("is_dir") else "📄" print(f" {is_dir} {f.get('name')}") # 如果需要第二个空间 if len(personal_spaces) < 2: print("\n7. 创建第二个测试空间...") resp = api_client.post( "/api/cloudDiskApi/spaces", json={ "name": f"test_space_{int(time.time())}", "space_type": None, "is_public": False, }, ) result = resp.json() if result.get("status") == 0: new_space_id = result.get("data", {}).get("id") print(f" 创建成功: ID={new_space_id}") else: print(f" 创建失败: {result}") browser.close() print("\n=== 测试数据预置完成 ===") if __name__ == "__main__": main()