48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
package email
|
||
|
||
import (
|
||
"strings"
|
||
"testing"
|
||
|
||
"carrotskin/pkg/config"
|
||
|
||
"go.uber.org/zap"
|
||
)
|
||
|
||
// 全局单例访问器(Init/GetService/MustGetService)已在 DI 迁移中移除。
|
||
// 本测试改为直接使用构造函数 NewService。
|
||
|
||
func TestEmailManager_Disabled(t *testing.T) {
|
||
cfg := config.EmailConfig{Enabled: false}
|
||
svc := NewService(cfg, zap.NewNop())
|
||
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",
|
||
}
|
||
svc := NewService(cfg, zap.NewNop())
|
||
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")
|
||
}
|
||
}
|