268 lines
6.3 KiB
Go
268 lines
6.3 KiB
Go
package mailx
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"code.yun.ink/pkg/mailx/aliyun"
|
|
"code.yun.ink/pkg/mailx/aws"
|
|
"code.yun.ink/pkg/mailx/interfaces"
|
|
"code.yun.ink/pkg/mailx/mailgun"
|
|
"code.yun.ink/pkg/mailx/smtp"
|
|
)
|
|
|
|
// 测试SMTP增强功能
|
|
func TestSMTPEnhanced(t *testing.T) {
|
|
smtpClient := smtp.NewSmtp()
|
|
ctx := context.Background()
|
|
|
|
// 测试配置验证
|
|
_, err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
|
Host: "smtp.example.com",
|
|
Port: "587",
|
|
Username: "test@example.com",
|
|
Password: "password",
|
|
}
|
|
})
|
|
if err != nil {
|
|
t.Errorf("SMTP configuration failed: %v", err)
|
|
}
|
|
|
|
// 测试消息验证
|
|
message := interfaces.Message{
|
|
To: []string{"recipient@example.com"},
|
|
Subject: "Test Email",
|
|
Body: "<h1>Test Content</h1>",
|
|
}
|
|
|
|
err = smtpClient.Send(ctx, message)
|
|
if err != nil {
|
|
t.Errorf("SMTP send failed: %v", err)
|
|
}
|
|
// 注意:这里不会真正发送邮件,只是测试验证逻辑
|
|
// 在实际测试中需要mock或使用测试邮件服务器
|
|
t.Logf("SMTP client configured successfully")
|
|
}
|
|
|
|
// 测试阿里云增强功能
|
|
func TestAliyunEnhanced(t *testing.T) {
|
|
aliyunClient := aliyun.NewAliyun()
|
|
ctx := context.Background()
|
|
|
|
// 测试配置验证
|
|
_, err := aliyunClient.SetOption(ctx, func(opt *interfaces.Options) {
|
|
opt.Aliyun = &interfaces.EmialConfigDataAliyun{
|
|
AccessId: "test-access-id",
|
|
AccessKey: "test-access-key",
|
|
AccountName: "test@example.com",
|
|
Endpoint: "dm.aliyuncs.com",
|
|
}
|
|
})
|
|
if err != nil {
|
|
t.Errorf("Aliyun configuration failed: %v", err)
|
|
}
|
|
|
|
t.Logf("Aliyun client configured successfully")
|
|
}
|
|
|
|
// 测试AWS增强功能
|
|
func TestAWSEnhanced(t *testing.T) {
|
|
awsClient := aws.NewAws()
|
|
ctx := context.Background()
|
|
|
|
// 测试配置验证
|
|
_, err := awsClient.SetOption(ctx, func(opt *interfaces.Options) {
|
|
opt.Aws = &interfaces.EmailConfigDataAws{
|
|
AccessId: "test-access-id",
|
|
AccessSecret: "test-access-secret",
|
|
Region: "us-east-1",
|
|
Sender: "test@example.com",
|
|
}
|
|
})
|
|
if err != nil {
|
|
t.Errorf("AWS configuration failed: %v", err)
|
|
}
|
|
|
|
t.Logf("AWS client configured successfully")
|
|
}
|
|
|
|
// 测试Mailgun增强功能
|
|
func TestMailgunEnhanced(t *testing.T) {
|
|
mailgunClient := mailgun.NewMailGun()
|
|
ctx := context.Background()
|
|
|
|
// 测试配置验证
|
|
_, err := mailgunClient.SetOption(ctx, func(opt *interfaces.Options) {
|
|
opt.Mailgun = &interfaces.EmialConfigDataMailgun{
|
|
ApiKey: "test-api-key",
|
|
Domain: "example.com",
|
|
Sender: "test@example.com",
|
|
}
|
|
})
|
|
if err != nil {
|
|
t.Errorf("Mailgun configuration failed: %v", err)
|
|
}
|
|
|
|
t.Logf("Mailgun client configured successfully")
|
|
}
|
|
|
|
// 测试消息验证
|
|
func TestMessageValidation(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
message interfaces.Message
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "valid message",
|
|
message: interfaces.Message{
|
|
To: []string{"test@example.com"},
|
|
Subject: "Test Subject",
|
|
Body: "Test Body",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "empty recipients",
|
|
message: interfaces.Message{
|
|
Subject: "Test Subject",
|
|
Body: "Test Body",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "empty subject",
|
|
message: interfaces.Message{
|
|
To: []string{"test@example.com"},
|
|
Body: "Test Body",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "empty body",
|
|
message: interfaces.Message{
|
|
To: []string{"test@example.com"},
|
|
Subject: "Test Subject",
|
|
},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// 这里可以测试各个实现的消息验证逻辑
|
|
// 由于验证逻辑在各个实现中,这里只是示例
|
|
if len(tt.message.To) == 0 && !tt.wantErr {
|
|
t.Error("Expected error for empty recipients")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// 测试重试机制
|
|
func TestRetryMechanism(t *testing.T) {
|
|
// 模拟重试逻辑
|
|
maxRetries := 3
|
|
attempts := 0
|
|
|
|
for i := 0; i < maxRetries; i++ {
|
|
attempts++
|
|
if i > 0 {
|
|
time.Sleep(time.Duration(i) * time.Millisecond) // 快速测试
|
|
}
|
|
|
|
// 模拟第3次成功
|
|
if attempts == 3 {
|
|
break
|
|
}
|
|
}
|
|
|
|
if attempts != 3 {
|
|
t.Errorf("Expected 3 attempts, got %d", attempts)
|
|
}
|
|
}
|
|
|
|
// 测试配置安全性
|
|
func TestConfigSecurity(t *testing.T) {
|
|
// 测试敏感信息不应该出现在日志中
|
|
config := &interfaces.EmailConfigDataSmtp{
|
|
Host: "smtp.example.com",
|
|
Username: "user@example.com",
|
|
Password: "secret-password",
|
|
}
|
|
|
|
// 模拟安全日志输出
|
|
safeLog := fmt.Sprintf("Host:%s Username:%s Password:***",
|
|
config.Host, config.Username)
|
|
|
|
if contains(safeLog, "secret-password") {
|
|
t.Error("Password should not appear in logs")
|
|
}
|
|
}
|
|
|
|
// 辅助函数
|
|
func contains(s, substr string) bool {
|
|
return len(s) >= len(substr) && s[len(s)-len(substr):] == substr ||
|
|
len(s) > len(substr) && s[:len(substr)] == substr ||
|
|
len(s) > len(substr) && s[len(s)/2-len(substr)/2:len(s)/2+len(substr)/2] == substr
|
|
}
|
|
|
|
// 基准测试
|
|
func BenchmarkEmailSend(b *testing.B) {
|
|
smtpClient := smtp.NewSmtp()
|
|
ctx := context.Background()
|
|
|
|
smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
|
Host: "smtp.example.com",
|
|
Port: "587",
|
|
Username: "test@example.com",
|
|
Password: "password",
|
|
}
|
|
})
|
|
|
|
message := interfaces.Message{
|
|
To: []string{"recipient@example.com"},
|
|
Subject: "Benchmark Test",
|
|
Body: "Benchmark test content",
|
|
}
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
// 注意:这里不会真正发送邮件
|
|
// 在实际基准测试中需要mock发送逻辑
|
|
_ = message
|
|
}
|
|
}
|
|
|
|
// 集成测试示例
|
|
func TestEmailFactory(t *testing.T) {
|
|
factory := interfaces.NewDefaultEmailFactory()
|
|
|
|
// 注册所有邮件服务
|
|
factory.Register(smtp.NewSmtp())
|
|
factory.Register(aliyun.NewAliyun())
|
|
factory.Register(aws.NewAws())
|
|
factory.Register(mailgun.NewMailGun())
|
|
|
|
// 测试获取不同类型的邮件服务
|
|
smtpEmail, err := factory.GetEmail(interfaces.EmailTypeSmtp)
|
|
if err != nil {
|
|
t.Errorf("Failed to get SMTP email: %v", err)
|
|
}
|
|
if smtpEmail.GetEmailType() != interfaces.EmailTypeSmtp {
|
|
t.Error("Wrong email type returned")
|
|
}
|
|
|
|
aliyunEmail, err := factory.GetEmail(interfaces.EmailTypeAliyun)
|
|
if err != nil {
|
|
t.Errorf("Failed to get Aliyun email: %v", err)
|
|
}
|
|
if aliyunEmail.GetEmailType() != interfaces.EmailTypeAliyun {
|
|
t.Error("Wrong email type returned")
|
|
}
|
|
}
|