| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- package main
-
- import (
- "bytes"
- "encoding/json"
- "io"
- "net/http"
- "testing"
-
- "git.x2erp.com/qdy/go-base/model/request/configreq"
- )
-
- const (
- testRoleID = "test-role-002"
- testRoleName = "测试角色"
- testRoleDescription = "这是一个测试角色描述"
- roleBaseURL = "http://localhost:8080"
- )
-
- // TestRoleCRUD 测试角色的增删改查
- func TestRoleCRUD(t *testing.T) {
- // 1. 创建角色
- t.Run("CreateRole", testCreateRole)
-
- // 2. 查询单个角色
- t.Run("GetRole", testGetRole)
-
- // 3. 查询角色列表
- t.Run("ListRoles", testListRoles)
-
- // 4. 更新角色
- t.Run("UpdateRole", testUpdateRole)
-
- // 5. 查询更新后的角色
- t.Run("GetUpdatedRole", testGetUpdatedRole)
-
- // 6. 删除角色
- t.Run("DeleteRole", testDeleteRole)
-
- // 7. 验证角色已删除
- t.Run("VerifyRoleDeleted", testVerifyRoleDeleted)
- }
-
- func testCreateRole(t *testing.T) {
- httpClient := &http.Client{}
- url := roleBaseURL + "/api/create/config/role"
-
- reqBody := configreq.RoleRequest{
- RoleID: testRoleID,
- Name: testRoleName,
- Description: testRoleDescription,
- }
-
- jsonData, err := json.Marshal(reqBody)
- if err != nil {
- t.Fatalf("JSON序列化失败: %v", err)
- }
-
- req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
- if err != nil {
- t.Fatalf("创建请求失败: %v", err)
- }
-
- // 使用Basic认证
- req.SetBasicAuth("admin", "123")
- req.Header.Set("Content-Type", "application/json")
-
- resp, err := httpClient.Do(req)
- if err != nil {
- t.Fatalf("请求失败: %v", err)
- }
- defer resp.Body.Close()
-
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- t.Fatalf("读取响应失败: %v", err)
- }
-
- var result map[string]interface{}
- if err := json.Unmarshal(body, &result); err != nil {
- t.Fatalf("JSON解析失败: %v", err)
- }
-
- if success, ok := result["success"].(bool); !ok || !success {
- t.Errorf("创建角色失败: %v", result)
- }
-
- t.Logf("创建角色成功: %s", string(body))
- }
-
- func testGetRole(t *testing.T) {
- httpClient := &http.Client{}
- url := roleBaseURL + "/api/query/config/role/" + testRoleID
-
- req, err := http.NewRequest("POST", url, nil)
- if err != nil {
- t.Fatalf("创建请求失败: %v", err)
- }
-
- req.SetBasicAuth("admin", "123")
- req.Header.Set("Content-Type", "application/json")
-
- resp, err := httpClient.Do(req)
- if err != nil {
- t.Fatalf("请求失败: %v", err)
- }
- defer resp.Body.Close()
-
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- t.Fatalf("读取响应失败: %v", err)
- }
-
- var result map[string]interface{}
- if err := json.Unmarshal(body, &result); err != nil {
- t.Fatalf("JSON解析失败: %v", err)
- }
-
- if success, ok := result["success"].(bool); !ok || !success {
- t.Errorf("查询角色失败: %v", result)
- }
-
- // 验证角色信息
- if data, ok := result["data"].(map[string]interface{}); ok {
- if roleID, ok := data["RoleID"].(string); !ok || roleID != testRoleID {
- t.Errorf("角色ID不匹配, 期望: %s, 实际: %v", testRoleID, roleID)
- }
- if name, ok := data["Name"].(string); !ok || name != testRoleName {
- t.Errorf("角色名称不匹配, 期望: %s, 实际: %v", testRoleName, name)
- }
- if description, ok := data["Description"].(string); !ok || description != testRoleDescription {
- t.Errorf("角色描述不匹配, 期望: %s, 实际: %v", testRoleDescription, description)
- }
- }
-
- t.Logf("查询角色成功: %s", string(body))
- }
-
- func testListRoles(t *testing.T) {
- httpClient := &http.Client{}
- url := roleBaseURL + "/api/query/config/roles"
-
- req, err := http.NewRequest("POST", url, nil)
- if err != nil {
- t.Fatalf("创建请求失败: %v", err)
- }
-
- req.SetBasicAuth("admin", "123")
- req.Header.Set("Content-Type", "application/json")
-
- resp, err := httpClient.Do(req)
- if err != nil {
- t.Fatalf("请求失败: %v", err)
- }
- defer resp.Body.Close()
-
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- t.Fatalf("读取响应失败: %v", err)
- }
-
- var result map[string]interface{}
- if err := json.Unmarshal(body, &result); err != nil {
- t.Fatalf("JSON解析失败: %v", err)
- }
-
- if success, ok := result["success"].(bool); !ok || !success {
- t.Errorf("查询角色列表失败: %v", result)
- }
-
- t.Logf("查询角色列表成功: %s", string(body))
- }
-
- func testUpdateRole(t *testing.T) {
- httpClient := &http.Client{}
- url := roleBaseURL + "/api/update/config/role/" + testRoleID
-
- updatedReq := configreq.RoleRequest{
- RoleID: testRoleID,
- Name: "更新后的角色名称",
- Description: "更新后的角色描述",
- }
-
- jsonData, err := json.Marshal(updatedReq)
- if err != nil {
- t.Fatalf("JSON序列化失败: %v", err)
- }
-
- req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
- if err != nil {
- t.Fatalf("创建请求失败: %v", err)
- }
-
- req.SetBasicAuth("admin", "123")
- req.Header.Set("Content-Type", "application/json")
-
- resp, err := httpClient.Do(req)
- if err != nil {
- t.Fatalf("请求失败: %v", err)
- }
- defer resp.Body.Close()
-
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- t.Fatalf("读取响应失败: %v", err)
- }
-
- var result map[string]interface{}
- if err := json.Unmarshal(body, &result); err != nil {
- t.Fatalf("JSON解析失败: %v", err)
- }
-
- if success, ok := result["success"].(bool); !ok || !success {
- t.Errorf("更新角色失败: %v", result)
- }
-
- t.Logf("更新角色成功: %s", string(body))
- }
-
- func testGetUpdatedRole(t *testing.T) {
- httpClient := &http.Client{}
- url := roleBaseURL + "/api/query/config/role/" + testRoleID
-
- req, err := http.NewRequest("POST", url, nil)
- if err != nil {
- t.Fatalf("创建请求失败: %v", err)
- }
-
- req.SetBasicAuth("admin", "123")
- req.Header.Set("Content-Type", "application/json")
-
- resp, err := httpClient.Do(req)
- if err != nil {
- t.Fatalf("请求失败: %v", err)
- }
- defer resp.Body.Close()
-
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- t.Fatalf("读取响应失败: %v", err)
- }
-
- var result map[string]interface{}
- if err := json.Unmarshal(body, &result); err != nil {
- t.Fatalf("JSON解析失败: %v", err)
- }
-
- if success, ok := result["success"].(bool); !ok || !success {
- t.Errorf("查询更新后的角色失败: %v", result)
- }
-
- // 验证角色信息已更新
- if data, ok := result["data"].(map[string]interface{}); ok {
- if name, ok := data["Name"].(string); !ok || name != "更新后的角色名称" {
- t.Errorf("角色名称未更新, 期望: %s, 实际: %v", "更新后的角色名称", name)
- }
- if description, ok := data["Description"].(string); !ok || description != "更新后的角色描述" {
- t.Errorf("角色描述未更新, 期望: %s, 实际: %v", "更新后的角色描述", description)
- }
- }
-
- t.Logf("查询更新后的角色成功: %s", string(body))
- }
-
- func testDeleteRole(t *testing.T) {
- httpClient := &http.Client{}
- url := roleBaseURL + "/api/delete/config/role/" + testRoleID
-
- req, err := http.NewRequest("POST", url, nil)
- if err != nil {
- t.Fatalf("创建请求失败: %v", err)
- }
-
- req.SetBasicAuth("admin", "123")
- req.Header.Set("Content-Type", "application/json")
-
- resp, err := httpClient.Do(req)
- if err != nil {
- t.Fatalf("请求失败: %v", err)
- }
- defer resp.Body.Close()
-
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- t.Fatalf("读取响应失败: %v", err)
- }
-
- var result map[string]interface{}
- if err := json.Unmarshal(body, &result); err != nil {
- t.Fatalf("JSON解析失败: %v", err)
- }
-
- if success, ok := result["success"].(bool); !ok || !success {
- t.Errorf("删除角色失败: %v", result)
- }
-
- t.Logf("删除角色成功: %s", string(body))
- }
-
- func testVerifyRoleDeleted(t *testing.T) {
- httpClient := &http.Client{}
- url := roleBaseURL + "/api/query/config/role/" + testRoleID
-
- req, err := http.NewRequest("POST", url, nil)
- if err != nil {
- t.Fatalf("创建请求失败: %v", err)
- }
-
- req.SetBasicAuth("admin", "123")
- req.Header.Set("Content-Type", "application/json")
-
- resp, err := httpClient.Do(req)
- if err != nil {
- t.Fatalf("请求失败: %v", err)
- }
- defer resp.Body.Close()
-
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- t.Fatalf("读取响应失败: %v", err)
- }
-
- var result map[string]interface{}
- if err := json.Unmarshal(body, &result); err != nil {
- t.Fatalf("JSON解析失败: %v", err)
- }
-
- // 角色已删除,应该返回失败
- if success, ok := result["success"].(bool); ok && success {
- t.Errorf("角色应该已删除但查询成功: %v", result)
- }
-
- t.Logf("验证角色已删除成功: %s", string(body))
- }
|