app-cloudep-member-server/pkg/usecase/generate_verify_code_utils_...

64 lines
1.3 KiB
Go
Raw Permalink Normal View History

2024-12-30 03:58:14 +00:00
package usecase
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGenerateVerifyCode(t *testing.T) {
tests := []struct {
name string
digits int
expectErr bool
expectedLen int
}{
{
name: "Generate 6-digit code (default)",
digits: 0, // 測試預設值
expectErr: false,
expectedLen: 6,
},
{
name: "Generate 4-digit code",
digits: 4,
expectErr: false,
expectedLen: 4,
},
{
name: "Generate 8-digit code",
digits: 8,
expectErr: false,
expectedLen: 8,
},
{
name: "Invalid digits (negative value)",
digits: -3, // 測試無效位數
expectErr: false,
expectedLen: 6, // 預設值為6位數
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
code, err := generateVerifyCode(tt.digits)
// 驗證錯誤是否符合預期
if tt.expectErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.NotEmpty(t, code)
// 驗證生成的代碼長度是否符合預期
assert.Equal(t, tt.expectedLen, len(code))
// 驗證代碼是否為純數字
for _, c := range code {
assert.True(t, c >= '0' && c <= '9', "Verify code should only contain digits")
}
}
})
}
}