| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- package pub
- import (
- "context"
- "testing"
- "perms-system-server/internal/svc"
- "perms-system-server/internal/testutil"
- "perms-system-server/internal/types"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- )
- // TC-1208: 正常获取(默认宽高)
- func TestCaptcha_DefaultSize(t *testing.T) {
- svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
- logic := NewCaptchaLogic(context.Background(), svcCtx)
- resp, err := logic.Captcha(&types.CaptchaReq{})
- require.NoError(t, err)
- require.NotNil(t, resp)
- assert.NotEmpty(t, resp.Id)
- assert.NotEmpty(t, resp.Base64Image)
- }
- // TC-1209: 自定义宽高
- func TestCaptcha_CustomSize(t *testing.T) {
- svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
- logic := NewCaptchaLogic(context.Background(), svcCtx)
- resp, err := logic.Captcha(&types.CaptchaReq{Width: 300, Height: 100})
- require.NoError(t, err)
- require.NotNil(t, resp)
- assert.NotEmpty(t, resp.Id)
- assert.NotEmpty(t, resp.Base64Image)
- }
- // TC-1210: 宽高为 0 或负数,退化为默认值
- func TestCaptcha_ZeroOrNegativeSize(t *testing.T) {
- svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
- logic := NewCaptchaLogic(context.Background(), svcCtx)
- cases := []struct {
- name string
- width int
- height int
- }{
- {"zero_both", 0, 0},
- {"negative_width", -1, 80},
- {"negative_height", 240, -1},
- {"negative_both", -100, -100},
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- resp, err := logic.Captcha(&types.CaptchaReq{Width: tc.width, Height: tc.height})
- require.NoError(t, err)
- require.NotNil(t, resp)
- assert.NotEmpty(t, resp.Id)
- assert.NotEmpty(t, resp.Base64Image)
- })
- }
- }
- // TC-1252: VerifyCaptcha 验证:正确码消费后不可重用
- func TestVerifyCaptcha_CorrectCodeConsumes(t *testing.T) {
- id := "test_captcha_id_001"
- code := "1234"
- defaultCaptchaStore.Set(id, code)
- assert.True(t, VerifyCaptcha(id, code))
- assert.False(t, VerifyCaptcha(id, code), "should be consumed after first verification")
- }
- // TC-1253: VerifyCaptcha 验证:错误码不消费
- func TestVerifyCaptcha_WrongCode(t *testing.T) {
- id := "test_captcha_id_002"
- code := "5678"
- defaultCaptchaStore.Set(id, code)
- assert.False(t, VerifyCaptcha(id, "0000"))
- }
- // TC-1254: VerifyCaptcha 验证:不存在的 id
- func TestVerifyCaptcha_NonExistentId(t *testing.T) {
- assert.False(t, VerifyCaptcha("non_existent_id", "1234"))
- }
|