194 lines
5.3 KiB
Python
194 lines
5.3 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
简化版文件API测试脚本
|
|
"""
|
|
|
|
import requests
|
|
import json
|
|
|
|
# API基础URL
|
|
BASE_URL = "http://localhost:8000/api/v1"
|
|
|
|
# 固定用户ID
|
|
USER_ID = 8
|
|
|
|
def test_file_upload():
|
|
"""测试文件上传"""
|
|
test_content = "Hello World! 这是测试文件内容。"
|
|
|
|
try:
|
|
files = {
|
|
"file": ("test.txt", test_content.encode("utf-8"), "text/plain")
|
|
}
|
|
data = {
|
|
"user_id": USER_ID,
|
|
"description": "测试文件上传",
|
|
"tags": "test,upload",
|
|
"is_public": "false"
|
|
}
|
|
|
|
response = requests.post(
|
|
f"{BASE_URL}/files/upload",
|
|
files=files,
|
|
data=data
|
|
)
|
|
|
|
print(f"上传状态码: {response.status_code}")
|
|
print(f"上传响应: {response.text}")
|
|
|
|
if response.status_code == 201:
|
|
result = response.json()
|
|
if result.get("success"):
|
|
file_info = result["data"]["file"]
|
|
print(f"上传成功! 文件ID: {file_info['id']}")
|
|
return file_info['id']
|
|
|
|
return None
|
|
except Exception as e:
|
|
print(f"上传出错: {e}")
|
|
return None
|
|
|
|
def test_file_list():
|
|
"""测试获取文件列表"""
|
|
try:
|
|
data = {
|
|
"user_id": USER_ID,
|
|
"page": 1,
|
|
"size": 20
|
|
}
|
|
|
|
response = requests.post(
|
|
f"{BASE_URL}/files/list",
|
|
json=data
|
|
)
|
|
|
|
print(f"列表状态码: {response.status_code}")
|
|
|
|
if response.status_code == 200:
|
|
result = response.json()
|
|
if result.get("success"):
|
|
files = result["data"]["files"]
|
|
print(f"文件数量: {len(files)}")
|
|
for f in files:
|
|
print(f" - {f['original_filename']} (ID: {f['id']}, 大小: {f['file_size']} bytes)")
|
|
return files
|
|
|
|
except Exception as e:
|
|
print(f"获取列表出错: {e}")
|
|
return []
|
|
|
|
def test_storage_info():
|
|
"""测试存储信息"""
|
|
try:
|
|
data = {
|
|
"user_id": USER_ID
|
|
}
|
|
|
|
response = requests.post(
|
|
f"{BASE_URL}/files/storage/info",
|
|
json=data
|
|
)
|
|
|
|
print(f"存储信息状态码: {response.status_code}")
|
|
|
|
if response.status_code == 200:
|
|
result = response.json()
|
|
if result.get("success"):
|
|
info = result["data"]
|
|
print(f"存储信息:")
|
|
print(f" 总配额: {info['total_quota']} bytes")
|
|
print(f" 已使用: {info['used_space']} bytes")
|
|
print(f" 可用空间: {info['available_space']} bytes")
|
|
print(f" 使用百分比: {info['usage_percentage']:.2f}%")
|
|
print(f" 文件数量: {info['file_count']}")
|
|
|
|
except Exception as e:
|
|
print(f"获取存储信息出错: {e}")
|
|
|
|
def test_file_info(file_id):
|
|
"""测试获取文件信息"""
|
|
try:
|
|
data = {
|
|
"user_id": USER_ID,
|
|
"file_id": file_id
|
|
}
|
|
|
|
response = requests.post(
|
|
f"{BASE_URL}/files/info",
|
|
json=data
|
|
)
|
|
|
|
print(f"文件信息状态码: {response.status_code}")
|
|
|
|
if response.status_code == 200:
|
|
result = response.json()
|
|
if result.get("success"):
|
|
info = result["data"]
|
|
print(f"文件信息:")
|
|
print(f" 文件名: {info['original_filename']}")
|
|
print(f" 大小: {info['file_size']} bytes")
|
|
print(f" MIME类型: {info['mime_type']}")
|
|
print(f" 是否图片: {info['is_image']}")
|
|
print(f" 是否文档: {info['is_document']}")
|
|
print(f" 格式化大小: {info['size_formatted']}")
|
|
|
|
except Exception as e:
|
|
print(f"获取文件信息出错: {e}")
|
|
|
|
def test_file_delete(file_id):
|
|
"""测试删除文件"""
|
|
try:
|
|
data = {
|
|
"user_id": USER_ID,
|
|
"file_id": file_id
|
|
}
|
|
|
|
response = requests.post(
|
|
f"{BASE_URL}/files/delete",
|
|
json=data
|
|
)
|
|
|
|
print(f"删除状态码: {response.status_code}")
|
|
|
|
if response.status_code == 200:
|
|
result = response.json()
|
|
if result.get("success"):
|
|
print("文件删除成功")
|
|
else:
|
|
print(f"删除失败: {result}")
|
|
else:
|
|
print(f"删除请求失败: {response.text}")
|
|
|
|
except Exception as e:
|
|
print(f"删除文件出错: {e}")
|
|
|
|
if __name__ == "__main__":
|
|
print(f"开始测试文件API (用户ID: {USER_ID})...")
|
|
|
|
# 1. 测试存储信息
|
|
print("\n=== 测试存储信息 ===")
|
|
test_storage_info()
|
|
|
|
# 2. 上传文件
|
|
print("\n=== 测试文件上传 ===")
|
|
file_id = test_file_upload()
|
|
|
|
# 3. 获取文件列表
|
|
print("\n=== 测试文件列表 ===")
|
|
files = test_file_list()
|
|
|
|
# 4. 获取文件信息
|
|
if file_id:
|
|
print("\n=== 测试文件信息 ===")
|
|
test_file_info(file_id)
|
|
|
|
# 5. 删除文件
|
|
print("\n=== 测试文件删除 ===")
|
|
test_file_delete(file_id)
|
|
|
|
# 6. 再次检查文件列表和存储信息
|
|
print("\n=== 最终检查 ===")
|
|
test_file_list()
|
|
test_storage_info()
|
|
|
|
print("\n测试完成!") |