adminLoginLogic_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package pub
  2. import (
  3. "context"
  4. "errors"
  5. "testing"
  6. "time"
  7. "perms-system-server/internal/response"
  8. "perms-system-server/internal/testutil"
  9. "perms-system-server/internal/types"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. )
  13. // TC-0500: 超管正常登录(管理后台)
  14. func TestAdminLogin_SuperAdmin(t *testing.T) {
  15. ctx := context.Background()
  16. svcCtx := newTestSvcCtx()
  17. username := testutil.UniqueId()
  18. password := "TestPass123"
  19. _, cleanUser := insertTestUser(t, ctx, svcCtx, username, password, 1, 1)
  20. t.Cleanup(cleanUser)
  21. logic := NewAdminLoginLogic(ctx, svcCtx)
  22. resp, err := logic.AdminLogin(&types.AdminLoginReq{
  23. Username: username,
  24. Password: password,
  25. ManagementKey: svcCtx.Config.Auth.ManagementKey,
  26. })
  27. require.NoError(t, err)
  28. require.NotNil(t, resp)
  29. assert.NotEmpty(t, resp.AccessToken)
  30. assert.NotEmpty(t, resp.RefreshToken)
  31. assert.True(t, resp.Expires > time.Now().Unix(), "expires应为未来的unix时间戳")
  32. assert.Equal(t, username, resp.UserInfo.Username)
  33. assert.Equal(t, int64(1), resp.UserInfo.IsSuperAdmin)
  34. assert.Nil(t, resp.UserInfo.Perms)
  35. assert.Equal(t, "SUPER_ADMIN", resp.UserInfo.MemberType)
  36. }
  37. // TC-0501: 普通用户正常登录(管理后台)
  38. func TestAdminLogin_NormalUser(t *testing.T) {
  39. ctx := context.Background()
  40. svcCtx := newTestSvcCtx()
  41. username := testutil.UniqueId()
  42. password := "TestPass123"
  43. _, cleanUser := insertTestUser(t, ctx, svcCtx, username, password, 1, 2)
  44. t.Cleanup(cleanUser)
  45. logic := NewAdminLoginLogic(ctx, svcCtx)
  46. resp, err := logic.AdminLogin(&types.AdminLoginReq{
  47. Username: username,
  48. Password: password,
  49. ManagementKey: svcCtx.Config.Auth.ManagementKey,
  50. })
  51. require.NoError(t, err)
  52. require.NotNil(t, resp)
  53. assert.NotEmpty(t, resp.AccessToken)
  54. assert.NotEmpty(t, resp.RefreshToken)
  55. assert.True(t, resp.Expires > time.Now().Unix(), "expires应为未来的unix时间戳")
  56. assert.Equal(t, username, resp.UserInfo.Username)
  57. assert.Nil(t, resp.UserInfo.Perms)
  58. assert.Empty(t, resp.UserInfo.MemberType)
  59. }
  60. // TC-0502: managementKey无效
  61. func TestAdminLogin_InvalidManagementKey(t *testing.T) {
  62. ctx := context.Background()
  63. svcCtx := newTestSvcCtx()
  64. logic := NewAdminLoginLogic(ctx, svcCtx)
  65. resp, err := logic.AdminLogin(&types.AdminLoginReq{
  66. Username: "anyone",
  67. Password: "pass",
  68. ManagementKey: "wrong-key",
  69. })
  70. require.Nil(t, resp)
  71. require.Error(t, err)
  72. var codeErr *response.CodeError
  73. require.True(t, errors.As(err, &codeErr))
  74. assert.Equal(t, 401, codeErr.Code())
  75. assert.Equal(t, "managementKey无效", codeErr.Error())
  76. }
  77. // TC-0503: managementKey为空
  78. func TestAdminLogin_EmptyManagementKey(t *testing.T) {
  79. ctx := context.Background()
  80. svcCtx := newTestSvcCtx()
  81. logic := NewAdminLoginLogic(ctx, svcCtx)
  82. resp, err := logic.AdminLogin(&types.AdminLoginReq{
  83. Username: "anyone",
  84. Password: "pass",
  85. ManagementKey: "",
  86. })
  87. require.Nil(t, resp)
  88. require.Error(t, err)
  89. var codeErr *response.CodeError
  90. require.True(t, errors.As(err, &codeErr))
  91. assert.Equal(t, 401, codeErr.Code())
  92. assert.Equal(t, "managementKey无效", codeErr.Error())
  93. }
  94. // TC-0504: 用户不存在
  95. func TestAdminLogin_UserNotFound(t *testing.T) {
  96. ctx := context.Background()
  97. svcCtx := newTestSvcCtx()
  98. logic := NewAdminLoginLogic(ctx, svcCtx)
  99. resp, err := logic.AdminLogin(&types.AdminLoginReq{
  100. Username: "nonexistent_" + testutil.UniqueId(),
  101. Password: "whatever",
  102. ManagementKey: svcCtx.Config.Auth.ManagementKey,
  103. })
  104. require.Nil(t, resp)
  105. require.Error(t, err)
  106. var codeErr *response.CodeError
  107. require.True(t, errors.As(err, &codeErr))
  108. assert.Equal(t, 401, codeErr.Code())
  109. assert.Equal(t, "用户名或密码错误", codeErr.Error())
  110. }
  111. // TC-0505: 密码错误
  112. func TestAdminLogin_WrongPassword(t *testing.T) {
  113. ctx := context.Background()
  114. svcCtx := newTestSvcCtx()
  115. username := testutil.UniqueId()
  116. _, cleanUser := insertTestUser(t, ctx, svcCtx, username, "CorrectPass", 1, 2)
  117. t.Cleanup(cleanUser)
  118. logic := NewAdminLoginLogic(ctx, svcCtx)
  119. resp, err := logic.AdminLogin(&types.AdminLoginReq{
  120. Username: username,
  121. Password: "WrongPass",
  122. ManagementKey: svcCtx.Config.Auth.ManagementKey,
  123. })
  124. require.Nil(t, resp)
  125. require.Error(t, err)
  126. var codeErr *response.CodeError
  127. require.True(t, errors.As(err, &codeErr))
  128. assert.Equal(t, 401, codeErr.Code())
  129. assert.Equal(t, "用户名或密码错误", codeErr.Error())
  130. }
  131. // TC-0506: 账号冻结
  132. func TestAdminLogin_AccountFrozen(t *testing.T) {
  133. ctx := context.Background()
  134. svcCtx := newTestSvcCtx()
  135. username := testutil.UniqueId()
  136. password := "TestPass123"
  137. _, cleanUser := insertTestUser(t, ctx, svcCtx, username, password, 2, 2)
  138. t.Cleanup(cleanUser)
  139. logic := NewAdminLoginLogic(ctx, svcCtx)
  140. resp, err := logic.AdminLogin(&types.AdminLoginReq{
  141. Username: username,
  142. Password: password,
  143. ManagementKey: svcCtx.Config.Auth.ManagementKey,
  144. })
  145. require.Nil(t, resp)
  146. require.Error(t, err)
  147. var codeErr *response.CodeError
  148. require.True(t, errors.As(err, &codeErr))
  149. assert.Equal(t, 403, codeErr.Code())
  150. assert.Equal(t, "账号已被冻结", codeErr.Error())
  151. }
  152. // TC-0507: 不带productCode时token无权限(perms为空)
  153. func TestAdminLogin_NoPermsWithoutProductCode(t *testing.T) {
  154. ctx := context.Background()
  155. svcCtx := newTestSvcCtx()
  156. username := testutil.UniqueId()
  157. password := "TestPass123"
  158. _, cleanUser := insertTestUser(t, ctx, svcCtx, username, password, 1, 1)
  159. t.Cleanup(cleanUser)
  160. logic := NewAdminLoginLogic(ctx, svcCtx)
  161. resp, err := logic.AdminLogin(&types.AdminLoginReq{
  162. Username: username,
  163. Password: password,
  164. ManagementKey: svcCtx.Config.Auth.ManagementKey,
  165. })
  166. require.NoError(t, err)
  167. require.NotNil(t, resp)
  168. assert.Nil(t, resp.UserInfo.Perms, "管理后台不传productCode,不应加载权限列表")
  169. assert.Equal(t, "SUPER_ADMIN", resp.UserInfo.MemberType, "超管即使不传productCode也会被标记SUPER_ADMIN")
  170. }
  171. // TC-0509: SQL注入username
  172. func TestAdminLogin_SQLInjection(t *testing.T) {
  173. ctx := context.Background()
  174. svcCtx := newTestSvcCtx()
  175. logic := NewAdminLoginLogic(ctx, svcCtx)
  176. resp, err := logic.AdminLogin(&types.AdminLoginReq{
  177. Username: "' OR 1=1 --",
  178. Password: "anything",
  179. ManagementKey: svcCtx.Config.Auth.ManagementKey,
  180. })
  181. require.Nil(t, resp)
  182. require.Error(t, err)
  183. var codeErr *response.CodeError
  184. require.True(t, errors.As(err, &codeErr))
  185. assert.Equal(t, 401, codeErr.Code())
  186. assert.Equal(t, "用户名或密码错误", codeErr.Error())
  187. }