loginLogic_captcha_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package pub
  2. import (
  3. "context"
  4. "errors"
  5. "testing"
  6. "time"
  7. "perms-system-server/internal/config"
  8. productmemberModel "perms-system-server/internal/model/productmember"
  9. "perms-system-server/internal/response"
  10. "perms-system-server/internal/svc"
  11. "perms-system-server/internal/testutil"
  12. "perms-system-server/internal/types"
  13. "github.com/stretchr/testify/assert"
  14. "github.com/stretchr/testify/require"
  15. )
  16. func newCaptchaDisabledSvcCtx() *svc.ServiceContext {
  17. cfg := testutil.GetTestConfig()
  18. cfg.Capjs = config.CapjsConf{Enable: 0}
  19. return svc.NewServiceContext(cfg)
  20. }
  21. // TC-1213: cap.js 未启用 + 验证码为空
  22. func TestLogin_CaptchaDisabled_EmptyCaptcha(t *testing.T) {
  23. svcCtx := newCaptchaDisabledSvcCtx()
  24. logic := NewLoginLogic(context.Background(), svcCtx)
  25. resp, err := logic.Login(&types.LoginReq{
  26. Username: "testuser",
  27. Password: "pass123",
  28. ProductCode: "pc",
  29. CaptchaId: "",
  30. CaptchaCode: "",
  31. })
  32. require.Nil(t, resp)
  33. require.Error(t, err)
  34. var codeErr *response.CodeError
  35. require.True(t, errors.As(err, &codeErr))
  36. assert.Equal(t, 400, codeErr.Code())
  37. assert.Contains(t, codeErr.Error(), "验证码不能为空")
  38. }
  39. // TC-1214: cap.js 未启用 + 验证码错误/过期
  40. func TestLogin_CaptchaDisabled_WrongCaptcha(t *testing.T) {
  41. svcCtx := newCaptchaDisabledSvcCtx()
  42. logic := NewLoginLogic(context.Background(), svcCtx)
  43. resp, err := logic.Login(&types.LoginReq{
  44. Username: "testuser",
  45. Password: "pass123",
  46. ProductCode: "pc",
  47. CaptchaId: "non_existent_captcha",
  48. CaptchaCode: "0000",
  49. })
  50. require.Nil(t, resp)
  51. require.Error(t, err)
  52. var codeErr *response.CodeError
  53. require.True(t, errors.As(err, &codeErr))
  54. assert.Equal(t, 400, codeErr.Code())
  55. assert.Contains(t, codeErr.Error(), "验证码错误或已过期")
  56. }
  57. // TC-1215: cap.js 未启用 + 验证码正确 → 正常登录
  58. func TestLogin_CaptchaDisabled_CorrectCaptcha(t *testing.T) {
  59. ctx := context.Background()
  60. svcCtx := newCaptchaDisabledSvcCtx()
  61. conn := testutil.GetTestSqlConn()
  62. username := testutil.UniqueId()
  63. password := "TestPass123"
  64. pc := testutil.UniqueId()
  65. now := time.Now().Unix()
  66. userId, cleanUser := insertTestUser(t, ctx, svcCtx, username, password, 1, 2)
  67. t.Cleanup(cleanUser)
  68. _, cleanProduct := insertTestProduct(t, ctx, svcCtx, pc, testutil.UniqueId(), "secret")
  69. t.Cleanup(cleanProduct)
  70. pmRes, err := svcCtx.SysProductMemberModel.Insert(ctx, &productmemberModel.SysProductMember{
  71. ProductCode: pc, UserId: userId, MemberType: "MEMBER", Status: 1, CreateTime: now, UpdateTime: now,
  72. })
  73. require.NoError(t, err)
  74. pmId, _ := pmRes.LastInsertId()
  75. t.Cleanup(func() { testutil.CleanTable(ctx, conn, "`sys_product_member`", pmId) })
  76. captchaId := "test_login_captcha_" + testutil.UniqueId()
  77. captchaCode := "9876"
  78. defaultCaptchaStore.Set(captchaId, captchaCode)
  79. logic := NewLoginLogic(ctx, svcCtx)
  80. resp, err := logic.Login(&types.LoginReq{
  81. Username: username,
  82. Password: password,
  83. ProductCode: pc,
  84. CaptchaId: captchaId,
  85. CaptchaCode: captchaCode,
  86. })
  87. require.NoError(t, err)
  88. require.NotNil(t, resp)
  89. assert.NotEmpty(t, resp.AccessToken)
  90. assert.NotEmpty(t, resp.RefreshToken)
  91. assert.Equal(t, username, resp.UserInfo.Username)
  92. }
  93. // TC-1250: 验证 cap.js 已启用时传统登录接口被拒绝
  94. func TestLogin_CapEnabled_Rejected(t *testing.T) {
  95. ctx := context.Background()
  96. svcCtx := newTestSvcCtx() // Capjs.Enable=1
  97. logic := NewLoginLogic(ctx, svcCtx)
  98. resp, err := logic.Login(&types.LoginReq{
  99. Username: "user",
  100. Password: "pass",
  101. ProductCode: "pc",
  102. })
  103. require.Nil(t, resp)
  104. require.Error(t, err)
  105. var codeErr *response.CodeError
  106. require.True(t, errors.As(err, &codeErr))
  107. assert.Equal(t, 400, codeErr.Code())
  108. assert.Contains(t, codeErr.Error(), "当前已启用人机验证")
  109. }