loginLogic_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. package pub
  2. import (
  3. "context"
  4. "database/sql"
  5. "errors"
  6. "testing"
  7. "time"
  8. permModel "perms-system-server/internal/model/perm"
  9. productModel "perms-system-server/internal/model/product"
  10. productmemberModel "perms-system-server/internal/model/productmember"
  11. userModel "perms-system-server/internal/model/user"
  12. "perms-system-server/internal/response"
  13. "perms-system-server/internal/svc"
  14. "perms-system-server/internal/testutil"
  15. "perms-system-server/internal/types"
  16. "github.com/stretchr/testify/assert"
  17. "github.com/stretchr/testify/require"
  18. )
  19. func newTestSvcCtx() *svc.ServiceContext {
  20. return svc.NewServiceContext(testutil.GetTestConfig())
  21. }
  22. func insertTestUser(t *testing.T, ctx context.Context, svcCtx *svc.ServiceContext, username, password string, status, isSuperAdmin int64) (int64, func()) {
  23. t.Helper()
  24. conn := testutil.GetTestSqlConn()
  25. now := time.Now().Unix()
  26. hashed := testutil.HashPassword(password)
  27. res, err := svcCtx.SysUserModel.Insert(ctx, &userModel.SysUser{
  28. Username: username,
  29. Password: hashed,
  30. Nickname: username,
  31. Avatar: sql.NullString{},
  32. Email: username + "@test.com",
  33. Phone: "13800000000",
  34. Remark: "",
  35. DeptId: 0,
  36. IsSuperAdmin: isSuperAdmin,
  37. MustChangePassword: 2,
  38. Status: status,
  39. CreateTime: now,
  40. UpdateTime: now,
  41. })
  42. require.NoError(t, err)
  43. id, _ := res.LastInsertId()
  44. cleanup := func() {
  45. testutil.CleanTable(ctx, conn, "`sys_user`", id)
  46. }
  47. return id, cleanup
  48. }
  49. func insertTestProduct(t *testing.T, ctx context.Context, svcCtx *svc.ServiceContext, code, appKey, appSecret string) (int64, func()) {
  50. t.Helper()
  51. conn := testutil.GetTestSqlConn()
  52. now := time.Now().Unix()
  53. res, err := svcCtx.SysProductModel.Insert(ctx, &productModel.SysProduct{
  54. Code: code,
  55. Name: code,
  56. AppKey: appKey,
  57. AppSecret: appSecret,
  58. Status: 1,
  59. CreateTime: now,
  60. UpdateTime: now,
  61. })
  62. require.NoError(t, err)
  63. id, _ := res.LastInsertId()
  64. cleanup := func() {
  65. testutil.CleanTable(ctx, conn, "`sys_product`", id)
  66. }
  67. return id, cleanup
  68. }
  69. // TC-0001: 正常登录(普通用户+productCode)
  70. func TestLogin_NormalWithProductCodeBasic(t *testing.T) {
  71. ctx := context.Background()
  72. svcCtx := newTestSvcCtx()
  73. conn := testutil.GetTestSqlConn()
  74. username := testutil.UniqueId()
  75. password := "TestPass123"
  76. pc := testutil.UniqueId()
  77. now := time.Now().Unix()
  78. userId, cleanUser := insertTestUser(t, ctx, svcCtx, username, password, 1, 2)
  79. t.Cleanup(cleanUser)
  80. _, cleanProduct := insertTestProduct(t, ctx, svcCtx, pc, testutil.UniqueId(), "secret")
  81. t.Cleanup(cleanProduct)
  82. pmRes, err := svcCtx.SysProductMemberModel.Insert(ctx, &productmemberModel.SysProductMember{
  83. ProductCode: pc, UserId: userId, MemberType: "MEMBER", Status: 1, CreateTime: now, UpdateTime: now,
  84. })
  85. require.NoError(t, err)
  86. pmId, _ := pmRes.LastInsertId()
  87. t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_product_member`", pmId) })
  88. logic := NewLoginLogic(ctx, svcCtx)
  89. resp, err := logic.Login(&types.LoginReq{
  90. Username: username,
  91. Password: password,
  92. ProductCode: pc,
  93. })
  94. require.NoError(t, err)
  95. require.NotNil(t, resp)
  96. assert.NotEmpty(t, resp.AccessToken)
  97. assert.NotEmpty(t, resp.RefreshToken)
  98. assert.True(t, resp.Expires > time.Now().Unix(), "expires应为未来的unix时间戳")
  99. assert.Equal(t, username, resp.UserInfo.Username)
  100. assert.Equal(t, "MEMBER", resp.UserInfo.MemberType)
  101. }
  102. // TC-0002: 正常登录-带productCode
  103. func TestLogin_NormalWithProductCode(t *testing.T) {
  104. ctx := context.Background()
  105. svcCtx := newTestSvcCtx()
  106. conn := testutil.GetTestSqlConn()
  107. username := testutil.UniqueId()
  108. password := "TestPass123"
  109. pc := testutil.UniqueId()
  110. now := time.Now().Unix()
  111. userId, cleanUser := insertTestUser(t, ctx, svcCtx, username, password, 1, 2)
  112. t.Cleanup(cleanUser)
  113. _, cleanProduct := insertTestProduct(t, ctx, svcCtx, pc, testutil.UniqueId(), "secret")
  114. t.Cleanup(cleanProduct)
  115. pmRes, err := svcCtx.SysProductMemberModel.Insert(ctx, &productmemberModel.SysProductMember{
  116. ProductCode: pc, UserId: userId, MemberType: "ADMIN", Status: 1, CreateTime: now, UpdateTime: now,
  117. })
  118. require.NoError(t, err)
  119. pmId, _ := pmRes.LastInsertId()
  120. t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_product_member`", pmId) })
  121. permRes, err := svcCtx.SysPermModel.Insert(ctx, &permModel.SysPerm{
  122. ProductCode: pc, Name: "perm1", Code: testutil.UniqueId(), Status: 1, CreateTime: now, UpdateTime: now,
  123. })
  124. require.NoError(t, err)
  125. permId, _ := permRes.LastInsertId()
  126. t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_perm`", permId) })
  127. logic := NewLoginLogic(ctx, svcCtx)
  128. resp, err := logic.Login(&types.LoginReq{
  129. Username: username,
  130. Password: password,
  131. ProductCode: pc,
  132. })
  133. require.NoError(t, err)
  134. require.NotNil(t, resp)
  135. assert.Equal(t, "ADMIN", resp.UserInfo.MemberType)
  136. assert.NotEmpty(t, resp.UserInfo.Perms)
  137. }
  138. // TC-0003: 超管通过产品端登录被拒绝
  139. func TestLogin_SuperAdminRejected(t *testing.T) {
  140. ctx := context.Background()
  141. svcCtx := newTestSvcCtx()
  142. username := testutil.UniqueId()
  143. password := "TestPass123"
  144. pc := testutil.UniqueId()
  145. _, cleanUser := insertTestUser(t, ctx, svcCtx, username, password, 1, 1)
  146. t.Cleanup(cleanUser)
  147. _, cleanProduct := insertTestProduct(t, ctx, svcCtx, pc, testutil.UniqueId(), "secret")
  148. t.Cleanup(cleanProduct)
  149. logic := NewLoginLogic(ctx, svcCtx)
  150. resp, err := logic.Login(&types.LoginReq{
  151. Username: username,
  152. Password: password,
  153. ProductCode: pc,
  154. })
  155. require.Nil(t, resp)
  156. require.Error(t, err)
  157. var codeErr *response.CodeError
  158. require.True(t, errors.As(err, &codeErr))
  159. assert.Equal(t, 403, codeErr.Code())
  160. assert.Equal(t, "超级管理员不允许通过产品端登录,请使用管理后台", codeErr.Error())
  161. }
  162. // TC-0004: 超管无productCode被拒绝
  163. func TestLogin_SuperAdminWithoutProductCodeRejected(t *testing.T) {
  164. ctx := context.Background()
  165. svcCtx := newTestSvcCtx()
  166. username := testutil.UniqueId()
  167. password := "TestPass123"
  168. _, cleanUser := insertTestUser(t, ctx, svcCtx, username, password, 1, 1)
  169. t.Cleanup(cleanUser)
  170. logic := NewLoginLogic(ctx, svcCtx)
  171. resp, err := logic.Login(&types.LoginReq{
  172. Username: username,
  173. Password: password,
  174. })
  175. require.Nil(t, resp)
  176. require.Error(t, err)
  177. var codeErr *response.CodeError
  178. require.True(t, errors.As(err, &codeErr))
  179. assert.Equal(t, 403, codeErr.Code())
  180. assert.Equal(t, "超级管理员不允许通过产品端登录,请使用管理后台", codeErr.Error())
  181. }
  182. // TC-0005: 用户不存在
  183. func TestLogin_UserNotFound(t *testing.T) {
  184. ctx := context.Background()
  185. svcCtx := newTestSvcCtx()
  186. logic := NewLoginLogic(ctx, svcCtx)
  187. resp, err := logic.Login(&types.LoginReq{
  188. Username: "nonexistent_" + testutil.UniqueId(),
  189. Password: "whatever",
  190. })
  191. require.Nil(t, resp)
  192. require.Error(t, err)
  193. var codeErr *response.CodeError
  194. require.True(t, errors.As(err, &codeErr))
  195. assert.Equal(t, 401, codeErr.Code())
  196. assert.Equal(t, "用户名或密码错误", codeErr.Error())
  197. }
  198. // TC-0007: 密码错误
  199. func TestLogin_WrongPassword(t *testing.T) {
  200. ctx := context.Background()
  201. svcCtx := newTestSvcCtx()
  202. username := testutil.UniqueId()
  203. _, cleanUser := insertTestUser(t, ctx, svcCtx, username, "CorrectPass", 1, 2)
  204. t.Cleanup(cleanUser)
  205. logic := NewLoginLogic(ctx, svcCtx)
  206. resp, err := logic.Login(&types.LoginReq{
  207. Username: username,
  208. Password: "WrongPass",
  209. })
  210. require.Nil(t, resp)
  211. require.Error(t, err)
  212. var codeErr *response.CodeError
  213. require.True(t, errors.As(err, &codeErr))
  214. assert.Equal(t, 401, codeErr.Code())
  215. assert.Equal(t, "用户名或密码错误", codeErr.Error())
  216. }
  217. // TC-0008: 账号冻结
  218. func TestLogin_AccountFrozen(t *testing.T) {
  219. ctx := context.Background()
  220. svcCtx := newTestSvcCtx()
  221. username := testutil.UniqueId()
  222. password := "TestPass123"
  223. _, cleanUser := insertTestUser(t, ctx, svcCtx, username, password, 2, 2)
  224. t.Cleanup(cleanUser)
  225. logic := NewLoginLogic(ctx, svcCtx)
  226. resp, err := logic.Login(&types.LoginReq{
  227. Username: username,
  228. Password: password,
  229. })
  230. require.Nil(t, resp)
  231. require.Error(t, err)
  232. var codeErr *response.CodeError
  233. require.True(t, errors.As(err, &codeErr))
  234. assert.Equal(t, 403, codeErr.Code())
  235. assert.Equal(t, "账号已被冻结", codeErr.Error())
  236. }
  237. // TC-0009: 非产品成员
  238. func TestLogin_NonMemberWithProductCode(t *testing.T) {
  239. ctx := context.Background()
  240. svcCtx := newTestSvcCtx()
  241. username := testutil.UniqueId()
  242. password := "TestPass123"
  243. pc := testutil.UniqueId()
  244. _, cleanUser := insertTestUser(t, ctx, svcCtx, username, password, 1, 2)
  245. t.Cleanup(cleanUser)
  246. _, cleanProduct := insertTestProduct(t, ctx, svcCtx, pc, testutil.UniqueId(), "secret")
  247. t.Cleanup(cleanProduct)
  248. logic := NewLoginLogic(ctx, svcCtx)
  249. resp, err := logic.Login(&types.LoginReq{
  250. Username: username,
  251. Password: password,
  252. ProductCode: pc,
  253. })
  254. require.Nil(t, resp)
  255. require.Error(t, err)
  256. var codeErr *response.CodeError
  257. require.True(t, errors.As(err, &codeErr))
  258. assert.Equal(t, 403, codeErr.Code())
  259. assert.Equal(t, "您不是该产品的成员", codeErr.Error())
  260. }
  261. // TC-0010: DEVELOPER成员
  262. func TestLogin_DeveloperMember(t *testing.T) {
  263. ctx := context.Background()
  264. svcCtx := newTestSvcCtx()
  265. conn := testutil.GetTestSqlConn()
  266. username := testutil.UniqueId()
  267. password := "TestPass123"
  268. pc := testutil.UniqueId()
  269. now := time.Now().Unix()
  270. userId, cleanUser := insertTestUser(t, ctx, svcCtx, username, password, 1, 2)
  271. t.Cleanup(cleanUser)
  272. _, cleanProduct := insertTestProduct(t, ctx, svcCtx, pc, testutil.UniqueId(), "secret")
  273. t.Cleanup(cleanProduct)
  274. pmRes, err := svcCtx.SysProductMemberModel.Insert(ctx, &productmemberModel.SysProductMember{
  275. ProductCode: pc, UserId: userId, MemberType: "DEVELOPER", Status: 1, CreateTime: now, UpdateTime: now,
  276. })
  277. require.NoError(t, err)
  278. pmId, _ := pmRes.LastInsertId()
  279. t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_product_member`", pmId) })
  280. permCode := testutil.UniqueId()
  281. permRes, err := svcCtx.SysPermModel.Insert(ctx, &permModel.SysPerm{
  282. ProductCode: pc, Name: "dev_perm", Code: permCode, Status: 1, CreateTime: now, UpdateTime: now,
  283. })
  284. require.NoError(t, err)
  285. permId, _ := permRes.LastInsertId()
  286. t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_perm`", permId) })
  287. logic := NewLoginLogic(ctx, svcCtx)
  288. resp, err := logic.Login(&types.LoginReq{
  289. Username: username,
  290. Password: password,
  291. ProductCode: pc,
  292. })
  293. require.NoError(t, err)
  294. require.NotNil(t, resp)
  295. assert.Equal(t, "DEVELOPER", resp.UserInfo.MemberType)
  296. assert.Contains(t, resp.UserInfo.Perms, permCode)
  297. }
  298. // TC-0011: SQL注入
  299. func TestLogin_SQLInjection(t *testing.T) {
  300. ctx := context.Background()
  301. svcCtx := newTestSvcCtx()
  302. logic := NewLoginLogic(ctx, svcCtx)
  303. resp, err := logic.Login(&types.LoginReq{
  304. Username: "' OR 1=1 --",
  305. Password: "anything",
  306. })
  307. require.Nil(t, resp)
  308. require.Error(t, err)
  309. var codeErr *response.CodeError
  310. require.True(t, errors.As(err, &codeErr))
  311. assert.Equal(t, 401, codeErr.Code())
  312. assert.Equal(t, "用户名或密码错误", codeErr.Error())
  313. }