From 51b1f6f8757ca45e851dc495fb8bee7f3547eaa4 Mon Sep 17 00:00:00 2001 From: JavaPythonAIForBAT <2770852170@qq.com> Date: Sun, 4 Jan 2026 20:22:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B5=8B=E8=AF=95=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=9B=BA=E5=AE=9A=E9=AA=8C=E8=AF=81=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signing.go | 9 +- signing/domain/config.go | 2 + signing/domain/randomcode/test_random_code.go | 23 ++++ .../randomcode/test_random_code_test.go | 43 +++++++ signing/domain/vcservice/vcservice_test.go | 109 ++++++++++++++++++ 5 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 signing/domain/randomcode/test_random_code.go create mode 100644 signing/domain/randomcode/test_random_code_test.go create mode 100644 signing/domain/vcservice/vcservice_test.go diff --git a/signing.go b/signing.go index 2e8eddb..4d1bcff 100644 --- a/signing.go +++ b/signing.go @@ -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" @@ -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( diff --git a/signing/domain/config.go b/signing/domain/config.go index aa677e2..28933c1 100644 --- a/signing/domain/config.go +++ b/signing/domain/config.go @@ -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"` diff --git a/signing/domain/randomcode/test_random_code.go b/signing/domain/randomcode/test_random_code.go new file mode 100644 index 0000000..87ee98a --- /dev/null +++ b/signing/domain/randomcode/test_random_code.go @@ -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 +} diff --git a/signing/domain/randomcode/test_random_code_test.go b/signing/domain/randomcode/test_random_code_test.go new file mode 100644 index 0000000..a0d693d --- /dev/null +++ b/signing/domain/randomcode/test_random_code_test.go @@ -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") +} diff --git a/signing/domain/vcservice/vcservice_test.go b/signing/domain/vcservice/vcservice_test.go new file mode 100644 index 0000000..f5cfb3f --- /dev/null +++ b/signing/domain/vcservice/vcservice_test.go @@ -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) + } +}