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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "testing"
  9. "time"
  10. )
  11. // TestAuthValidate 测试token验证API
  12. func TestAuthValidate(t *testing.T) {
  13. // 获取svc-code服务地址
  14. svcCodeURL := "http://localhost:8020"
  15. // 检查服务是否运行
  16. if !checkServiceRunningForValidateTest(t, svcCodeURL) {
  17. t.Skipf("svc-code服务未运行在 %s,跳过测试", svcCodeURL)
  18. }
  19. // 1. 用户登录获取token
  20. token, err := loginForValidateTest(t, svcCodeURL)
  21. if err != nil {
  22. t.Fatalf("登录失败: %v", err)
  23. }
  24. t.Logf("获取到Token: %s...", token[:minValidateInt(20, len(token))])
  25. // 2. 验证token有效性
  26. userInfo, err := validateToken(t, svcCodeURL, token)
  27. if err != nil {
  28. t.Fatalf("token验证失败: %v", err)
  29. }
  30. // 3. 验证用户信息
  31. t.Logf("token验证成功,用户信息: %v", userInfo)
  32. if userInfo["user_id"] != "test-user-001" {
  33. t.Errorf("预期user_id为'test-user-001',得到 %v", userInfo["user_id"])
  34. }
  35. if userInfo["authenticated"] != true {
  36. t.Error("authenticated字段应为true")
  37. }
  38. // 4. 测试无效token
  39. t.Run("InvalidToken", func(t *testing.T) {
  40. testInvalidToken(t, svcCodeURL)
  41. })
  42. // 5. 测试缺失token
  43. t.Run("MissingToken", func(t *testing.T) {
  44. testMissingToken(t, svcCodeURL)
  45. })
  46. }
  47. // validateToken 验证token并返回用户信息
  48. func validateToken(t *testing.T, svcCodeURL, token string) (map[string]interface{}, error) {
  49. url := svcCodeURL + "/api/auth/validate"
  50. req, err := http.NewRequest("POST", url, nil)
  51. if err != nil {
  52. return nil, fmt.Errorf("创建请求失败: %w", err)
  53. }
  54. req.Header.Set("Authorization", "Bearer "+token)
  55. req.Header.Set("Content-Type", "application/json")
  56. client := &http.Client{Timeout: 10 * time.Second}
  57. resp, err := client.Do(req)
  58. if err != nil {
  59. return nil, fmt.Errorf("HTTP请求失败: %w", err)
  60. }
  61. defer resp.Body.Close()
  62. if resp.StatusCode != http.StatusOK {
  63. bodyBytes, _ := io.ReadAll(resp.Body)
  64. return nil, fmt.Errorf("token验证失败 (状态码 %d): %s", resp.StatusCode, string(bodyBytes))
  65. }
  66. var result struct {
  67. Success bool `json:"success"`
  68. Data map[string]interface{} `json:"data"`
  69. Message string `json:"message"`
  70. }
  71. bodyBytes, _ := io.ReadAll(resp.Body)
  72. if err := json.Unmarshal(bodyBytes, &result); err != nil {
  73. return nil, fmt.Errorf("解析响应失败: %w", err)
  74. }
  75. if !result.Success {
  76. return nil, fmt.Errorf("token验证失败: %s", result.Message)
  77. }
  78. return result.Data, nil
  79. }
  80. // testInvalidToken 测试无效token
  81. func testInvalidToken(t *testing.T, svcCodeURL string) {
  82. invalidToken := "invalid.token.here"
  83. _, err := validateToken(t, svcCodeURL, invalidToken)
  84. if err == nil {
  85. t.Error("无效token应该验证失败,但成功了")
  86. } else {
  87. t.Logf("无效token验证失败(预期): %v", err)
  88. }
  89. }
  90. // testMissingToken 测试缺失token
  91. func testMissingToken(t *testing.T, svcCodeURL string) {
  92. url := svcCodeURL + "/api/auth/validate"
  93. req, err := http.NewRequest("POST", url, nil)
  94. if err != nil {
  95. t.Fatalf("创建请求失败: %v", err)
  96. }
  97. req.Header.Set("Content-Type", "application/json")
  98. // 不设置Authorization头
  99. client := &http.Client{Timeout: 10 * time.Second}
  100. resp, err := client.Do(req)
  101. if err != nil {
  102. t.Fatalf("HTTP请求失败: %v", err)
  103. }
  104. defer resp.Body.Close()
  105. // 缺少token应该返回401或400
  106. if resp.StatusCode == http.StatusOK {
  107. // 如果是200,检查响应内容
  108. bodyBytes, _ := io.ReadAll(resp.Body)
  109. var result map[string]interface{}
  110. if err := json.Unmarshal(bodyBytes, &result); err == nil {
  111. if success, ok := result["success"].(bool); ok && success {
  112. t.Error("缺失token应该验证失败,但成功了")
  113. } else {
  114. t.Logf("缺失token验证失败(预期): %v", result)
  115. }
  116. }
  117. } else {
  118. t.Logf("缺失token返回状态码 %d(预期)", resp.StatusCode)
  119. }
  120. }
  121. // loginForValidateTest 登录获取token(验证测试专用)
  122. func loginForValidateTest(t *testing.T, svcCodeURL string) (string, error) {
  123. loginURL := svcCodeURL + "/api/auth/login"
  124. loginData := map[string]string{
  125. "user_id": "test-user-001",
  126. "password": "password123",
  127. }
  128. jsonData, _ := json.Marshal(loginData)
  129. resp, err := http.Post(loginURL, "application/json", bytes.NewBuffer(jsonData))
  130. if err != nil {
  131. return "", fmt.Errorf("登录请求失败: %v", err)
  132. }
  133. defer resp.Body.Close()
  134. if resp.StatusCode != http.StatusOK {
  135. bodyBytes, _ := io.ReadAll(resp.Body)
  136. return "", fmt.Errorf("登录失败 (状态码 %d): %s", resp.StatusCode, string(bodyBytes))
  137. }
  138. var result struct {
  139. Success bool `json:"success"`
  140. Data string `json:"data"`
  141. Message string `json:"message"`
  142. }
  143. bodyBytes, _ := io.ReadAll(resp.Body)
  144. if err := json.Unmarshal(bodyBytes, &result); err != nil {
  145. return "", fmt.Errorf("解析响应失败: %v", err)
  146. }
  147. if !result.Success {
  148. return "", fmt.Errorf("登录失败: %s", result.Message)
  149. }
  150. return result.Data, nil
  151. }
  152. // checkServiceRunningForValidateTest 检查服务是否运行(验证测试专用)
  153. func checkServiceRunningForValidateTest(t *testing.T, url string) bool {
  154. client := &http.Client{Timeout: 3 * time.Second}
  155. resp, err := client.Get(url + "/api/health")
  156. if err != nil {
  157. // 尝试其他端点
  158. resp, err = client.Get(url)
  159. if err != nil {
  160. return false
  161. }
  162. }
  163. defer resp.Body.Close()
  164. return resp.StatusCode == http.StatusOK || resp.StatusCode == 404
  165. }
  166. // minValidateInt 返回两个整数的最小值(验证测试专用)
  167. func minValidateInt(a, b int) int {
  168. if a < b {
  169. return a
  170. }
  171. return b
  172. }