Files
backend/internal/service/verification_service_test.go
lan 4b4980820f
Some checks failed
SonarQube Analysis / sonarqube (push) Has been cancelled
Test / test (push) Has been cancelled
Test / lint (push) Has been cancelled
Test / build (push) Has been cancelled
chore: 初始化仓库,排除二进制文件和覆盖率文件
2025-11-28 23:30:49 +08:00

120 lines
2.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package service
import (
"testing"
"time"
)
// TestGenerateVerificationCode 测试生成验证码函数
func TestGenerateVerificationCode(t *testing.T) {
tests := []struct {
name string
wantLen int
wantErr bool
}{
{
name: "生成6位验证码",
wantLen: CodeLength,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
code, err := GenerateVerificationCode()
if (err != nil) != tt.wantErr {
t.Errorf("GenerateVerificationCode() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && len(code) != tt.wantLen {
t.Errorf("GenerateVerificationCode() code length = %v, want %v", len(code), tt.wantLen)
}
// 验证验证码只包含数字
for _, c := range code {
if c < '0' || c > '9' {
t.Errorf("GenerateVerificationCode() code contains non-digit: %c", c)
}
}
})
}
// 测试多次生成,验证码应该不同(概率上)
codes := make(map[string]bool)
for i := 0; i < 100; i++ {
code, err := GenerateVerificationCode()
if err != nil {
t.Fatalf("GenerateVerificationCode() failed: %v", err)
}
if codes[code] {
t.Logf("发现重复验证码这是正常的因为只有6位数字: %s", code)
}
codes[code] = true
}
}
// TestVerificationConstants 测试验证码相关常量
func TestVerificationConstants(t *testing.T) {
if CodeLength != 6 {
t.Errorf("CodeLength = %d, want 6", CodeLength)
}
if CodeExpiration != 10*time.Minute {
t.Errorf("CodeExpiration = %v, want 10 minutes", CodeExpiration)
}
if CodeRateLimit != 1*time.Minute {
t.Errorf("CodeRateLimit = %v, want 1 minute", CodeRateLimit)
}
// 验证验证码类型常量
types := []string{
VerificationTypeRegister,
VerificationTypeResetPassword,
VerificationTypeChangeEmail,
}
for _, vType := range types {
if vType == "" {
t.Error("验证码类型不能为空")
}
}
}
// TestVerificationCodeFormat 测试验证码格式
func TestVerificationCodeFormat(t *testing.T) {
code, err := GenerateVerificationCode()
if err != nil {
t.Fatalf("GenerateVerificationCode() failed: %v", err)
}
// 验证长度
if len(code) != 6 {
t.Errorf("验证码长度应为6位实际为%d位", len(code))
}
// 验证只包含数字
for i, c := range code {
if c < '0' || c > '9' {
t.Errorf("验证码第%d位包含非数字字符: %c", i+1, c)
}
}
}
// TestVerificationTypes 测试验证码类型
func TestVerificationTypes(t *testing.T) {
validTypes := map[string]bool{
VerificationTypeRegister: true,
VerificationTypeResetPassword: true,
VerificationTypeChangeEmail: true,
}
for vType, isValid := range validTypes {
if !isValid {
t.Errorf("验证码类型 %s 应该是有效的", vType)
}
if vType == "" {
t.Error("验证码类型不能为空字符串")
}
}
}