Няма описание
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. testRoleID = "test-role-002"
  12. testRoleName = "测试角色"
  13. testRoleDescription = "这是一个测试角色描述"
  14. roleBaseURL = "http://localhost:8080"
  15. )
  16. // TestRoleCRUD 测试角色的增删改查
  17. func TestRoleCRUD(t *testing.T) {
  18. // 1. 创建角色
  19. t.Run("CreateRole", testCreateRole)
  20. // 2. 查询单个角色
  21. t.Run("GetRole", testGetRole)
  22. // 3. 查询角色列表
  23. t.Run("ListRoles", testListRoles)
  24. // 4. 更新角色
  25. t.Run("UpdateRole", testUpdateRole)
  26. // 5. 查询更新后的角色
  27. t.Run("GetUpdatedRole", testGetUpdatedRole)
  28. // 6. 删除角色
  29. t.Run("DeleteRole", testDeleteRole)
  30. // 7. 验证角色已删除
  31. t.Run("VerifyRoleDeleted", testVerifyRoleDeleted)
  32. }
  33. func testCreateRole(t *testing.T) {
  34. httpClient := &http.Client{}
  35. url := roleBaseURL + "/api/create/config/role"
  36. reqBody := configreq.RoleRequest{
  37. RoleID: testRoleID,
  38. Name: testRoleName,
  39. Description: testRoleDescription,
  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 testGetRole(t *testing.T) {
  71. httpClient := &http.Client{}
  72. url := roleBaseURL + "/api/query/config/role/" + testRoleID
  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 roleID, ok := data["RoleID"].(string); !ok || roleID != testRoleID {
  98. t.Errorf("角色ID不匹配, 期望: %s, 实际: %v", testRoleID, roleID)
  99. }
  100. if name, ok := data["Name"].(string); !ok || name != testRoleName {
  101. t.Errorf("角色名称不匹配, 期望: %s, 实际: %v", testRoleName, name)
  102. }
  103. if description, ok := data["Description"].(string); !ok || description != testRoleDescription {
  104. t.Errorf("角色描述不匹配, 期望: %s, 实际: %v", testRoleDescription, description)
  105. }
  106. }
  107. t.Logf("查询角色成功: %s", string(body))
  108. }
  109. func testListRoles(t *testing.T) {
  110. httpClient := &http.Client{}
  111. url := roleBaseURL + "/api/query/config/roles"
  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. t.Logf("查询角色列表成功: %s", string(body))
  135. }
  136. func testUpdateRole(t *testing.T) {
  137. httpClient := &http.Client{}
  138. url := roleBaseURL + "/api/update/config/role/" + testRoleID
  139. updatedReq := configreq.RoleRequest{
  140. RoleID: testRoleID,
  141. Name: "更新后的角色名称",
  142. Description: "更新后的角色描述",
  143. }
  144. jsonData, err := json.Marshal(updatedReq)
  145. if err != nil {
  146. t.Fatalf("JSON序列化失败: %v", err)
  147. }
  148. req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
  149. if err != nil {
  150. t.Fatalf("创建请求失败: %v", err)
  151. }
  152. req.SetBasicAuth("admin", "123")
  153. req.Header.Set("Content-Type", "application/json")
  154. resp, err := httpClient.Do(req)
  155. if err != nil {
  156. t.Fatalf("请求失败: %v", err)
  157. }
  158. defer resp.Body.Close()
  159. body, err := io.ReadAll(resp.Body)
  160. if err != nil {
  161. t.Fatalf("读取响应失败: %v", err)
  162. }
  163. var result map[string]interface{}
  164. if err := json.Unmarshal(body, &result); err != nil {
  165. t.Fatalf("JSON解析失败: %v", err)
  166. }
  167. if success, ok := result["success"].(bool); !ok || !success {
  168. t.Errorf("更新角色失败: %v", result)
  169. }
  170. t.Logf("更新角色成功: %s", string(body))
  171. }
  172. func testGetUpdatedRole(t *testing.T) {
  173. httpClient := &http.Client{}
  174. url := roleBaseURL + "/api/query/config/role/" + testRoleID
  175. req, err := http.NewRequest("POST", url, nil)
  176. if err != nil {
  177. t.Fatalf("创建请求失败: %v", err)
  178. }
  179. req.SetBasicAuth("admin", "123")
  180. req.Header.Set("Content-Type", "application/json")
  181. resp, err := httpClient.Do(req)
  182. if err != nil {
  183. t.Fatalf("请求失败: %v", err)
  184. }
  185. defer resp.Body.Close()
  186. body, err := io.ReadAll(resp.Body)
  187. if err != nil {
  188. t.Fatalf("读取响应失败: %v", err)
  189. }
  190. var result map[string]interface{}
  191. if err := json.Unmarshal(body, &result); err != nil {
  192. t.Fatalf("JSON解析失败: %v", err)
  193. }
  194. if success, ok := result["success"].(bool); !ok || !success {
  195. t.Errorf("查询更新后的角色失败: %v", result)
  196. }
  197. // 验证角色信息已更新
  198. if data, ok := result["data"].(map[string]interface{}); ok {
  199. if name, ok := data["Name"].(string); !ok || name != "更新后的角色名称" {
  200. t.Errorf("角色名称未更新, 期望: %s, 实际: %v", "更新后的角色名称", name)
  201. }
  202. if description, ok := data["Description"].(string); !ok || description != "更新后的角色描述" {
  203. t.Errorf("角色描述未更新, 期望: %s, 实际: %v", "更新后的角色描述", description)
  204. }
  205. }
  206. t.Logf("查询更新后的角色成功: %s", string(body))
  207. }
  208. func testDeleteRole(t *testing.T) {
  209. httpClient := &http.Client{}
  210. url := roleBaseURL + "/api/delete/config/role/" + testRoleID
  211. req, err := http.NewRequest("POST", url, nil)
  212. if err != nil {
  213. t.Fatalf("创建请求失败: %v", err)
  214. }
  215. req.SetBasicAuth("admin", "123")
  216. req.Header.Set("Content-Type", "application/json")
  217. resp, err := httpClient.Do(req)
  218. if err != nil {
  219. t.Fatalf("请求失败: %v", err)
  220. }
  221. defer resp.Body.Close()
  222. body, err := io.ReadAll(resp.Body)
  223. if err != nil {
  224. t.Fatalf("读取响应失败: %v", err)
  225. }
  226. var result map[string]interface{}
  227. if err := json.Unmarshal(body, &result); err != nil {
  228. t.Fatalf("JSON解析失败: %v", err)
  229. }
  230. if success, ok := result["success"].(bool); !ok || !success {
  231. t.Errorf("删除角色失败: %v", result)
  232. }
  233. t.Logf("删除角色成功: %s", string(body))
  234. }
  235. func testVerifyRoleDeleted(t *testing.T) {
  236. httpClient := &http.Client{}
  237. url := roleBaseURL + "/api/query/config/role/" + testRoleID
  238. req, err := http.NewRequest("POST", url, nil)
  239. if err != nil {
  240. t.Fatalf("创建请求失败: %v", err)
  241. }
  242. req.SetBasicAuth("admin", "123")
  243. req.Header.Set("Content-Type", "application/json")
  244. resp, err := httpClient.Do(req)
  245. if err != nil {
  246. t.Fatalf("请求失败: %v", err)
  247. }
  248. defer resp.Body.Close()
  249. body, err := io.ReadAll(resp.Body)
  250. if err != nil {
  251. t.Fatalf("读取响应失败: %v", err)
  252. }
  253. var result map[string]interface{}
  254. if err := json.Unmarshal(body, &result); err != nil {
  255. t.Fatalf("JSON解析失败: %v", err)
  256. }
  257. // 角色已删除,应该返回失败
  258. if success, ok := result["success"].(bool); ok && success {
  259. t.Errorf("角色应该已删除但查询成功: %v", result)
  260. }
  261. t.Logf("验证角色已删除成功: %s", string(body))
  262. }