Files
user-system/internal/service/email_config_test.go
long-agent 582ad7a069 test: add comprehensive test coverage and improve code quality
- Add new test files for auth, service, and handler modules
- Improve test organization and coverage
- Refactor code for better maintainability
- Add captcha, settings, stats, and theme handler tests
- Add auth module tests (CAS, OAuth, password, SSO, state)
- Add service layer tests for auth, export, permissions, roles
- All Go tests pass (exit code 0)
- All frontend tests pass (325 tests in 59 files)
2026-04-17 20:43:50 +08:00

85 lines
2.5 KiB
Go

package service
import (
"context"
"testing"
"time"
"github.com/user-management-system/internal/cache"
)
// =============================================================================
// Email Configuration Tests
// =============================================================================
func TestDefaultEmailCodeConfig(t *testing.T) {
cfg := DefaultEmailCodeConfig()
if cfg.CodeTTL != 5*time.Minute {
t.Errorf("CodeTTL = %v, want %v", cfg.CodeTTL, 5*time.Minute)
}
if cfg.ResendCooldown != time.Minute {
t.Errorf("ResendCooldown = %v, want %v", cfg.ResendCooldown, time.Minute)
}
if cfg.MaxDailyLimit != 10 {
t.Errorf("MaxDailyLimit = %d, want 10", cfg.MaxDailyLimit)
}
if cfg.SiteURL != "http://localhost:8080" {
t.Errorf("SiteURL = %q, want %q", cfg.SiteURL, "http://localhost:8080")
}
if cfg.SiteName != "User Management System" {
t.Errorf("SiteName = %q, want %q", cfg.SiteName, "User Management System")
}
}
// =============================================================================
// Email Code Service Tests
// =============================================================================
func TestNewEmailCodeService(t *testing.T) {
t.Run("with default config", func(t *testing.T) {
l1Cache := cache.NewL1Cache()
l2Cache := cache.NewRedisCache(false)
cacheManager := cache.NewCacheManager(l1Cache, l2Cache)
provider := &MockEmailProvider{}
svc := NewEmailCodeService(provider, cacheManager, EmailCodeConfig{})
if svc == nil {
t.Error("Expected service to be created")
}
})
t.Run("with custom config", func(t *testing.T) {
l1Cache := cache.NewL1Cache()
l2Cache := cache.NewRedisCache(false)
cacheManager := cache.NewCacheManager(l1Cache, l2Cache)
provider := &MockEmailProvider{}
cfg := EmailCodeConfig{
CodeTTL: 10 * time.Minute,
ResendCooldown: 2 * time.Minute,
MaxDailyLimit: 20,
}
svc := NewEmailCodeService(provider, cacheManager, cfg)
if svc == nil {
t.Error("Expected service to be created")
}
})
}
func TestEmailCodeService_SendEmailCode(t *testing.T) {
t.Run("with valid email", func(t *testing.T) {
l1Cache := cache.NewL1Cache()
l2Cache := cache.NewRedisCache(false)
cacheManager := cache.NewCacheManager(l1Cache, l2Cache)
provider := &MockEmailProvider{}
svc := NewEmailCodeService(provider, cacheManager, DefaultEmailCodeConfig())
err := svc.SendEmailCode(context.Background(), "test@example.com", "login")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
})
}