test_resource_storage.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/env python3
  2. """测试Resource存储系统"""
  3. import requests
  4. import base64
  5. import os
  6. BASE_URL = "http://localhost:8000"
  7. # 生成测试密钥
  8. test_key = base64.b64encode(os.urandom(32)).decode('ascii')
  9. print(f"测试密钥: {test_key}")
  10. print(f"请在.env中设置: ORG_KEYS=test:{test_key}")
  11. print()
  12. def test_submit_resource():
  13. """测试提交resource"""
  14. print("=== 测试1: 提交普通代码 ===")
  15. response = requests.post(f"{BASE_URL}/api/resource", json={
  16. "id": "test/code/example",
  17. "title": "示例代码",
  18. "body": "print('Hello World')",
  19. "content_type": "code",
  20. "metadata": {"language": "python"},
  21. "submitted_by": "test@example.com"
  22. })
  23. print(f"状态码: {response.status_code}")
  24. print(f"响应: {response.json()}")
  25. print()
  26. print("=== 测试2: 提交敏感凭证 ===")
  27. response = requests.post(f"{BASE_URL}/api/resource", json={
  28. "id": "test/credentials/website",
  29. "title": "网站登录凭证",
  30. "body": "使用方法:直接登录",
  31. "secure_body": "账号:user@example.com\n密码:SecurePass123",
  32. "content_type": "credential",
  33. "metadata": {"acquired_at": "2026-03-06T10:00:00Z"},
  34. "submitted_by": "test@example.com"
  35. })
  36. print(f"状态码: {response.status_code}")
  37. print(f"响应: {response.json()}")
  38. print()
  39. print("=== 测试3: 提交Cookie ===")
  40. response = requests.post(f"{BASE_URL}/api/resource", json={
  41. "id": "test/cookies/website",
  42. "title": "网站Cookie",
  43. "body": "适用于:已登录状态",
  44. "secure_body": "session_id=abc123; auth_token=xyz789",
  45. "content_type": "cookie",
  46. "metadata": {
  47. "acquired_at": "2026-03-06T10:00:00Z",
  48. "expires_at": "2026-03-07T10:00:00Z"
  49. },
  50. "submitted_by": "test@example.com"
  51. })
  52. print(f"状态码: {response.status_code}")
  53. print(f"响应: {response.json()}")
  54. print()
  55. def test_get_resource():
  56. """测试获取resource"""
  57. print("=== 测试4: 获取普通代码(无需密钥)===")
  58. response = requests.get(f"{BASE_URL}/api/resource/test/code/example")
  59. print(f"状态码: {response.status_code}")
  60. data = response.json()
  61. print(f"标题: {data['title']}")
  62. print(f"内容: {data['body']}")
  63. print(f"敏感内容: {data['secure_body']}")
  64. print()
  65. print("=== 测试5: 获取凭证(无密钥)===")
  66. response = requests.get(f"{BASE_URL}/api/resource/test/credentials/website")
  67. print(f"状态码: {response.status_code}")
  68. data = response.json()
  69. print(f"标题: {data['title']}")
  70. print(f"公开内容: {data['body']}")
  71. print(f"敏感内容: {data['secure_body']}") # 应该是 [ENCRYPTED]
  72. print()
  73. print("=== 测试6: 获取凭证(有密钥)===")
  74. response = requests.get(
  75. f"{BASE_URL}/api/resource/test/credentials/website",
  76. headers={"X-Org-Key": test_key}
  77. )
  78. print(f"状态码: {response.status_code}")
  79. data = response.json()
  80. print(f"标题: {data['title']}")
  81. print(f"公开内容: {data['body']}")
  82. print(f"敏感内容: {data['secure_body']}") # 应该解密
  83. print()
  84. def test_patch_resource():
  85. """测试更新resource"""
  86. print("=== 测试7: 更新resource ===")
  87. response = requests.patch(
  88. f"{BASE_URL}/api/resource/test/credentials/website",
  89. json={
  90. "title": "更新后的标题",
  91. "metadata": {"acquired_at": "2026-03-06T11:00:00Z"}
  92. }
  93. )
  94. print(f"状态码: {response.status_code}")
  95. print(f"响应: {response.json()}")
  96. print()
  97. def test_list_resources():
  98. """测试列出resource"""
  99. print("=== 测试8: 列出所有resource ===")
  100. response = requests.get(f"{BASE_URL}/api/resource")
  101. print(f"状态码: {response.status_code}")
  102. data = response.json()
  103. print(f"总数: {data['count']}")
  104. for item in data['results']:
  105. print(f" - {item['id']}: {item['title']} ({item['content_type']})")
  106. print()
  107. print("=== 测试9: 按类型过滤 ===")
  108. response = requests.get(f"{BASE_URL}/api/resource?content_type=credential")
  109. print(f"状态码: {response.status_code}")
  110. data = response.json()
  111. print(f"凭证数量: {data['count']}")
  112. for item in data['results']:
  113. print(f" - {item['id']}: {item['title']}")
  114. print()
  115. if __name__ == "__main__":
  116. print("请确保服务器正在运行: uvicorn knowhub.server:app --reload")
  117. print()
  118. try:
  119. test_submit_resource()
  120. test_get_resource()
  121. test_patch_resource()
  122. test_list_resources()
  123. print("✅ 所有测试完成")
  124. except Exception as e:
  125. print(f"❌ 测试失败: {e}")