2025-12-24 16:03:46 +08:00
|
|
|
|
package email
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
|
|
"carrotskin/pkg/config"
|
|
|
|
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-06-15 16:40:36 +08:00
|
|
|
|
// 全局单例访问器(Init/GetService/MustGetService)已在 DI 迁移中移除。
|
|
|
|
|
|
// 本测试改为直接使用构造函数 NewService。
|
2025-12-24 16:03:46 +08:00
|
|
|
|
|
|
|
|
|
|
func TestEmailManager_Disabled(t *testing.T) {
|
|
|
|
|
|
cfg := config.EmailConfig{Enabled: false}
|
2026-06-15 16:40:36 +08:00
|
|
|
|
svc := NewService(cfg, zap.NewNop())
|
2025-12-24 16:03:46 +08:00
|
|
|
|
if err := svc.SendVerificationCode("to@test.com", "123456", "email_verification"); err == nil {
|
|
|
|
|
|
t.Fatalf("expected error when disabled")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestEmailManager_SendFailsWithInvalidSMTP(t *testing.T) {
|
|
|
|
|
|
cfg := config.EmailConfig{
|
|
|
|
|
|
Enabled: true,
|
|
|
|
|
|
SMTPHost: "127.0.0.1",
|
|
|
|
|
|
SMTPPort: 1, // invalid/closed port to trigger error quickly
|
|
|
|
|
|
Username: "user",
|
|
|
|
|
|
Password: "pwd",
|
|
|
|
|
|
FromName: "name",
|
|
|
|
|
|
}
|
2026-06-15 16:40:36 +08:00
|
|
|
|
svc := NewService(cfg, zap.NewNop())
|
2025-12-24 16:03:46 +08:00
|
|
|
|
if err := svc.SendVerificationCode("to@test.com", "123456", "reset_password"); err == nil {
|
|
|
|
|
|
t.Fatalf("expected send error with invalid smtp")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestEmailManager_SubjectAndBody(t *testing.T) {
|
|
|
|
|
|
svc := &Service{cfg: config.EmailConfig{FromName: "name", Username: "user"}, logger: zap.NewNop()}
|
|
|
|
|
|
if subj := svc.getSubject("email_verification"); subj == "" {
|
|
|
|
|
|
t.Fatalf("subject empty")
|
|
|
|
|
|
}
|
|
|
|
|
|
body := svc.getBody("123456", "change_email")
|
|
|
|
|
|
if !strings.Contains(body, "123456") || !strings.Contains(body, "更换邮箱") {
|
|
|
|
|
|
t.Fatalf("body content mismatch")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|