Files
user-system/internal/service/email_config_test.go

85 lines
2.5 KiB
Go
Raw Normal View History

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)
}
})
}