Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/opensourceways/app-cla-server/signing/domain/claservice"
"github.com/opensourceways/app-cla-server/signing/domain/emailcredential"
"github.com/opensourceways/app-cla-server/signing/domain/loginservice"
"github.com/opensourceways/app-cla-server/signing/domain/randomcode"
"github.com/opensourceways/app-cla-server/signing/domain/userservice"
"github.com/opensourceways/app-cla-server/signing/domain/vcservice"
"github.com/opensourceways/app-cla-server/signing/infrastructure/accesstokenimpl"
Expand Down Expand Up @@ -71,7 +72,13 @@ func initSigning(cfg *config.Config) error {
mongodb.DAO(cfg.Mongodb.Collections.VerificationCode),
),
limiterimpl.NewLimiterImpl(redisdb.DAO()),
randomcodeimpl.NewRandomCodeImpl(),
// 根据配置选择验证码实现
func() randomcode.RandomCode {
if cfg.Domain.Config.IsTestEnvironment {
return randomcode.NewTestRandomCode(cfg.Domain.Config.TestVerificationCode)
}
return randomcodeimpl.NewRandomCodeImpl()
}(),
)

models.RegisterCorpAdminAdatper(
Expand Down
2 changes: 2 additions & 0 deletions signing/domain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ func CommunityManagerLinkId() string {
}

type Config struct {
IsTestEnvironment bool `json:"is_test_environment"`
TestVerificationCode string `json:"test_verification_code"`
SourceOfCLAPDF []string `json:"source_of_cla_pdf"`
MaxSizeOfCLAContent int `json:"max_size_of_cla_content"`
FileTypeOfCLAContent string `json:"file_type_of_cla_content"`
Expand Down
23 changes: 23 additions & 0 deletions signing/domain/randomcode/test_random_code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package randomcode

// TestRandomCode 固定返回配置的验证码实现,用于测试环境
type TestRandomCode struct {
code string
}

func NewTestRandomCode(testCode string) RandomCode {
if testCode == "" {
testCode = "123456" // 默认值
}
return &TestRandomCode{
code: testCode,
}
}

func (t *TestRandomCode) New() (string, error) {
return t.code, nil
}

func (t *TestRandomCode) IsValid(code string) bool {
return code == t.code
}
43 changes: 43 additions & 0 deletions signing/domain/randomcode/test_random_code_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package randomcode

import (
"testing"
)

func TestTestRandomCode_New(t *testing.T) {
// 创建测试环境的验证码生成器
testCode := NewTestRandomCode("000000")

// 多次调用验证码生成,应该始终返回 "123456"
for i := 0; i < 5; i++ {
code, err := testCode.New()
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if code != "000000" {
t.Errorf("Expected '123456', got '%s'", code)
}
}
}

func TestTestRandomCode_IsValid(t *testing.T) {
testCode := NewTestRandomCode("000000")

// 测试有效验证码
if !testCode.IsValid("000000") {
t.Error("Expected '000000' to be valid")
}

// 测试无效验证码
invalidCodes := []string{"12345", "1234567", "000100", "abcdef", ""}
for _, code := range invalidCodes {
if testCode.IsValid(code) {
t.Errorf("Expected '%s' to be invalid", code)
}
}
}

func TestTestRandomCode_Interface(t *testing.T) {
// 确保 TestRandomCode 实现了 RandomCode 接口
var _ RandomCode = NewTestRandomCode("000000")
}
109 changes: 109 additions & 0 deletions signing/domain/vcservice/vcservice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package vcservice

import (
"testing"
"time"

"github.com/opensourceways/app-cla-server/signing/domain"
"github.com/opensourceways/app-cla-server/signing/domain/dp"
"github.com/opensourceways/app-cla-server/signing/domain/randomcode"
)

// mockVerificationCodeRepo 模拟验证码存储
type mockVerificationCodeRepo struct {
codes map[string]domain.VerificationCode // 注意:存储值而不是指针
}

func newMockVerificationCodeRepo() *mockVerificationCodeRepo {
return &mockVerificationCodeRepo{
codes: make(map[string]domain.VerificationCode),
}
}

func (m *mockVerificationCodeRepo) Add(vc *domain.VerificationCode) error {
key := vc.Purpose.Purpose()
m.codes[key] = *vc // 存储值的副本
return nil
}

// 修复:返回值而不是指针
func (m *mockVerificationCodeRepo) Find(key *domain.VerificationCodeKey) (domain.VerificationCode, error) {
k := key.Purpose.Purpose()
if vc, exists := m.codes[k]; exists {
return vc, nil // 返回值而不是指针
}
return domain.VerificationCode{}, domain.NewNotFoundDomainError("not_found")
}

// mockLimiter 模拟限流器
type mockLimiter struct{}

func (m *mockLimiter) IsAllowed(purpose string) (bool, error) {
return true, nil
}

func (m *mockLimiter) Add(purpose string, interval time.Duration) error {
return nil
}

func TestVCService_WithTestRandomCode(t *testing.T) {
// 创建测试环境的验证码服务
vcService := NewVCService(
newMockVerificationCodeRepo(),
&mockLimiter{},
randomcode.NewTestRandomCode("000000"),
)

// 创建测试用的 purpose
purpose, err := dp.NewPurpose("test, individual, test@example.com")
if err != nil {
t.Fatalf("Failed to create purpose: %v", err)
}

// 测试验证码生成
code, err := vcService.New(purpose)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}

if code != "000000" {
t.Errorf("Expected '000000', got '%s'", code)
}

// 测试验证码验证
key := domain.NewVerificationCodeKey("000000", purpose)
err = vcService.Verify(&key)
if err != nil {
t.Errorf("Expected valid verification, got error: %v", err)
}

// 测试错误验证码
wrongKey := domain.NewVerificationCodeKey("000100", purpose)
err = vcService.Verify(&wrongKey)
if err == nil {
t.Error("Expected error for wrong verification code")
}
}

func TestVCService_NewIfItCan_WithTestRandomCode(t *testing.T) {
vcService := NewVCService(
newMockVerificationCodeRepo(),
&mockLimiter{},
randomcode.NewTestRandomCode("000000"),
)

purpose, err := dp.NewPurpose("test, corp, test@example.com")
if err != nil {
t.Fatalf("Failed to create purpose: %v", err)
}

// 测试带限流的验证码生成
code, err := vcService.NewIfItCan(purpose, time.Minute)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}

if code != "000000" {
t.Errorf("Expected '000000', got '%s'", code)
}
}
Loading