Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

my_config_project_test.go 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io"
  6. "net/http"
  7. "testing"
  8. "git.x2erp.com/qdy/go-base/model/request/configreq"
  9. )
  10. const (
  11. testProjectID = "test-project-001"
  12. testDescription = "测试项目描述"
  13. testMcpURL = "http://localhost:8080/mcp"
  14. baseURL = "http://localhost:8080"
  15. )
  16. // TestProjectCRUD 测试项目的增删改查
  17. func TestProjectCRUD(t *testing.T) {
  18. // 1. 创建项目
  19. t.Run("CreateProject", testCreateProject)
  20. // 2. 查询单个项目
  21. t.Run("GetProject", testGetProject)
  22. // 3. 查询项目列表
  23. t.Run("ListProjects", testListProjects)
  24. // 4. 更新项目
  25. t.Run("UpdateProject", testUpdateProject)
  26. // 5. 查询更新后的项目
  27. t.Run("GetUpdatedProject", testGetUpdatedProject)
  28. // 6. 删除项目
  29. t.Run("DeleteProject", testDeleteProject)
  30. // 7. 验证项目已删除
  31. t.Run("VerifyProjectDeleted", testVerifyProjectDeleted)
  32. }
  33. func testCreateProject(t *testing.T) {
  34. httpClient := &http.Client{}
  35. url := baseURL + "/api/create/config/project"
  36. reqBody := configreq.ProjectRequest{
  37. ProjectID: testProjectID,
  38. Description: testDescription,
  39. McpURL: testMcpURL,
  40. }
  41. jsonData, err := json.Marshal(reqBody)
  42. if err != nil {
  43. t.Fatalf("JSON序列化失败: %v", err)
  44. }
  45. req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
  46. if err != nil {
  47. t.Fatalf("创建请求失败: %v", err)
  48. }
  49. // 使用Basic认证
  50. req.SetBasicAuth("admin", "123")
  51. req.Header.Set("Content-Type", "application/json")
  52. resp, err := httpClient.Do(req)
  53. if err != nil {
  54. t.Fatalf("请求失败: %v", err)
  55. }
  56. defer resp.Body.Close()
  57. body, err := io.ReadAll(resp.Body)
  58. if err != nil {
  59. t.Fatalf("读取响应失败: %v", err)
  60. }
  61. var result map[string]interface{}
  62. if err := json.Unmarshal(body, &result); err != nil {
  63. t.Fatalf("JSON解析失败: %v", err)
  64. }
  65. if success, ok := result["success"].(bool); !ok || !success {
  66. t.Errorf("创建项目失败: %v", result)
  67. }
  68. t.Logf("创建项目成功: %s", string(body))
  69. }
  70. func testGetProject(t *testing.T) {
  71. httpClient := &http.Client{}
  72. url := baseURL + "/api/query/config/project/" + testProjectID
  73. req, err := http.NewRequest("POST", url, nil)
  74. if err != nil {
  75. t.Fatalf("创建请求失败: %v", err)
  76. }
  77. req.SetBasicAuth("admin", "123")
  78. req.Header.Set("Content-Type", "application/json")
  79. resp, err := httpClient.Do(req)
  80. if err != nil {
  81. t.Fatalf("请求失败: %v", err)
  82. }
  83. defer resp.Body.Close()
  84. body, err := io.ReadAll(resp.Body)
  85. if err != nil {
  86. t.Fatalf("读取响应失败: %v", err)
  87. }
  88. var result map[string]interface{}
  89. if err := json.Unmarshal(body, &result); err != nil {
  90. t.Fatalf("JSON解析失败: %v", err)
  91. }
  92. if success, ok := result["success"].(bool); !ok || !success {
  93. t.Errorf("查询项目失败: %v", result)
  94. }
  95. // 验证项目信息
  96. if data, ok := result["data"].(map[string]interface{}); ok {
  97. if projectID, ok := data["ProjectID"].(string); !ok || projectID != testProjectID {
  98. t.Errorf("项目ID不匹配: 期望 %s, 实际 %v", testProjectID, data["ProjectID"])
  99. }
  100. if description, ok := data["Description"].(string); !ok || description != testDescription {
  101. t.Errorf("项目描述不匹配: 期望 %s, 实际 %v", testDescription, data["Description"])
  102. }
  103. if mcpURL, ok := data["McpURL"].(string); !ok || mcpURL != testMcpURL {
  104. t.Errorf("MCP URL不匹配: 期望 %s, 实际 %v", testMcpURL, data["McpURL"])
  105. }
  106. }
  107. t.Logf("查询项目成功: %s", string(body))
  108. }
  109. func testListProjects(t *testing.T) {
  110. httpClient := &http.Client{}
  111. url := baseURL + "/api/query/config/projects"
  112. req, err := http.NewRequest("POST", url, nil)
  113. if err != nil {
  114. t.Fatalf("创建请求失败: %v", err)
  115. }
  116. req.SetBasicAuth("admin", "123")
  117. req.Header.Set("Content-Type", "application/json")
  118. resp, err := httpClient.Do(req)
  119. if err != nil {
  120. t.Fatalf("请求失败: %v", err)
  121. }
  122. defer resp.Body.Close()
  123. body, err := io.ReadAll(resp.Body)
  124. if err != nil {
  125. t.Fatalf("读取响应失败: %v", err)
  126. }
  127. var result map[string]interface{}
  128. if err := json.Unmarshal(body, &result); err != nil {
  129. t.Fatalf("JSON解析失败: %v", err)
  130. }
  131. if success, ok := result["success"].(bool); !ok || !success {
  132. t.Errorf("查询项目列表失败: %v", result)
  133. }
  134. // 验证返回的项目列表包含测试项目
  135. if count, ok := result["count"].(float64); !ok || count < 1 {
  136. t.Errorf("项目列表为空或计数错误: %v", result)
  137. }
  138. t.Logf("查询项目列表成功: %s", string(body))
  139. }
  140. func testUpdateProject(t *testing.T) {
  141. httpClient := &http.Client{}
  142. url := baseURL + "/api/update/config/project/" + testProjectID
  143. updatedReq := configreq.ProjectRequest{
  144. ProjectID: testProjectID,
  145. Description: "更新后的描述",
  146. McpURL: "http://localhost:9090/mcp",
  147. }
  148. jsonData, err := json.Marshal(updatedReq)
  149. if err != nil {
  150. t.Fatalf("JSON序列化失败: %v", err)
  151. }
  152. req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
  153. if err != nil {
  154. t.Fatalf("创建请求失败: %v", err)
  155. }
  156. req.SetBasicAuth("admin", "123")
  157. req.Header.Set("Content-Type", "application/json")
  158. resp, err := httpClient.Do(req)
  159. if err != nil {
  160. t.Fatalf("请求失败: %v", err)
  161. }
  162. defer resp.Body.Close()
  163. body, err := io.ReadAll(resp.Body)
  164. if err != nil {
  165. t.Fatalf("读取响应失败: %v", err)
  166. }
  167. var result map[string]interface{}
  168. if err := json.Unmarshal(body, &result); err != nil {
  169. t.Fatalf("JSON解析失败: %v", err)
  170. }
  171. if success, ok := result["success"].(bool); !ok || !success {
  172. t.Errorf("更新项目失败: %v", result)
  173. }
  174. t.Logf("更新项目成功: %s", string(body))
  175. }
  176. func testGetUpdatedProject(t *testing.T) {
  177. httpClient := &http.Client{}
  178. url := baseURL + "/api/query/config/project/" + testProjectID
  179. req, err := http.NewRequest("POST", url, nil)
  180. if err != nil {
  181. t.Fatalf("创建请求失败: %v", err)
  182. }
  183. req.SetBasicAuth("admin", "123")
  184. req.Header.Set("Content-Type", "application/json")
  185. resp, err := httpClient.Do(req)
  186. if err != nil {
  187. t.Fatalf("请求失败: %v", err)
  188. }
  189. defer resp.Body.Close()
  190. body, err := io.ReadAll(resp.Body)
  191. if err != nil {
  192. t.Fatalf("读取响应失败: %v", err)
  193. }
  194. var result map[string]interface{}
  195. if err := json.Unmarshal(body, &result); err != nil {
  196. t.Fatalf("JSON解析失败: %v", err)
  197. }
  198. if success, ok := result["success"].(bool); !ok || !success {
  199. t.Errorf("查询更新后的项目失败: %v", result)
  200. }
  201. // 验证更新后的信息
  202. if data, ok := result["data"].(map[string]interface{}); ok {
  203. if description, ok := data["Description"].(string); !ok || description != "更新后的描述" {
  204. t.Errorf("项目描述未更新: 期望 '更新后的描述', 实际 %v", data["Description"])
  205. }
  206. if mcpURL, ok := data["McpURL"].(string); !ok || mcpURL != "http://localhost:9090/mcp" {
  207. t.Errorf("MCP URL未更新: 期望 'http://localhost:9090/mcp', 实际 %v", data["McpURL"])
  208. }
  209. }
  210. t.Logf("查询更新后的项目成功: %s", string(body))
  211. }
  212. func testDeleteProject(t *testing.T) {
  213. httpClient := &http.Client{}
  214. url := baseURL + "/api/delete/config/project/" + testProjectID
  215. req, err := http.NewRequest("POST", url, nil)
  216. if err != nil {
  217. t.Fatalf("创建请求失败: %v", err)
  218. }
  219. // 删除操作使用Basic认证
  220. req.Header.Set("Authorization", "Basic YWRtaW46MTIz") // test:test
  221. req.Header.Set("Content-Type", "application/json")
  222. resp, err := httpClient.Do(req)
  223. if err != nil {
  224. t.Fatalf("请求失败: %v", err)
  225. }
  226. defer resp.Body.Close()
  227. body, err := io.ReadAll(resp.Body)
  228. if err != nil {
  229. t.Fatalf("读取响应失败: %v", err)
  230. }
  231. var result map[string]interface{}
  232. if err := json.Unmarshal(body, &result); err != nil {
  233. t.Fatalf("JSON解析失败: %v", err)
  234. }
  235. if success, ok := result["success"].(bool); !ok || !success {
  236. t.Errorf("删除项目失败: %v", result)
  237. }
  238. t.Logf("删除项目成功: %s", string(body))
  239. }
  240. func testVerifyProjectDeleted(t *testing.T) {
  241. httpClient := &http.Client{}
  242. url := baseURL + "/api/query/config/project/" + testProjectID
  243. req, err := http.NewRequest("POST", url, nil)
  244. if err != nil {
  245. t.Fatalf("创建请求失败: %v", err)
  246. }
  247. req.SetBasicAuth("admin", "123")
  248. req.Header.Set("Content-Type", "application/json")
  249. resp, err := httpClient.Do(req)
  250. if err != nil {
  251. t.Fatalf("请求失败: %v", err)
  252. }
  253. defer resp.Body.Close()
  254. body, err := io.ReadAll(resp.Body)
  255. if err != nil {
  256. t.Fatalf("读取响应失败: %v", err)
  257. }
  258. var result map[string]interface{}
  259. if err := json.Unmarshal(body, &result); err != nil {
  260. t.Fatalf("JSON解析失败: %v", err)
  261. }
  262. // 期望查询失败,因为项目已删除
  263. if success, ok := result["success"].(bool); ok && success {
  264. t.Errorf("项目删除验证失败: 项目仍存在")
  265. }
  266. t.Logf("项目删除验证成功: 项目已不存在")
  267. }