86 lines
2.5 KiB
Go
86 lines
2.5 KiB
Go
package email
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/smtp"
|
|
"testing"
|
|
|
|
"github.com/agiledragon/gomonkey/v2"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// Use gomonkey to patch smtp functions for sendViaClient testing
|
|
func TestSMTPSender_SendViaClient_Patched_Cov15(t *testing.T) {
|
|
patches := gomonkey.NewPatches()
|
|
defer patches.Reset()
|
|
|
|
// Patch smtp.Client.Mail to return error
|
|
var client smtp.Client
|
|
patches.ApplyMethodFunc(&client, "Mail", func(from string) error {
|
|
return fmt.Errorf("patched: MAIL FROM failed")
|
|
})
|
|
|
|
s := &SMTPSender{}
|
|
_, err := s.sendViaClient(&client, "from@test.com", "to@test.com", []byte("test"))
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestSMTPSender_SendViaClient_RcptError_Cov15(t *testing.T) {
|
|
patches := gomonkey.NewPatches()
|
|
defer patches.Reset()
|
|
|
|
var client smtp.Client
|
|
patches.ApplyMethodFunc(&client, "Mail", func(from string) error { return nil })
|
|
patches.ApplyMethodFunc(&client, "Rcpt", func(to string) error {
|
|
return fmt.Errorf("patched: RCPT TO failed")
|
|
})
|
|
|
|
s := &SMTPSender{}
|
|
_, err := s.sendViaClient(&client, "from@test.com", "to@test.com", []byte("test"))
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestSMTPSender_SendViaClient_DataError_Cov15(t *testing.T) {
|
|
patches := gomonkey.NewPatches()
|
|
defer patches.Reset()
|
|
|
|
var client smtp.Client
|
|
patches.ApplyMethodFunc(&client, "Mail", func(from string) error { return nil })
|
|
patches.ApplyMethodFunc(&client, "Rcpt", func(to string) error { return nil })
|
|
patches.ApplyMethodFunc(&client, "Data", func() (io.WriteCloser, error) {
|
|
return nil, fmt.Errorf("patched: DATA failed")
|
|
})
|
|
|
|
s := &SMTPSender{}
|
|
_, err := s.sendViaClient(&client, "from@test.com", "to@test.com", []byte("test"))
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestSMTPSender_ValidatePlain_Success_Cov15(t *testing.T) {
|
|
patches := gomonkey.NewPatches()
|
|
defer patches.Reset()
|
|
|
|
// Patch smtp.Dial to return nil error (simulating success at dial level)
|
|
patches.ApplyFunc(smtp.Dial, func(addr string) (*smtp.Client, error) {
|
|
return nil, fmt.Errorf("patched: connection refused")
|
|
})
|
|
|
|
s := &SMTPSender{}
|
|
err := s.validatePlain("smtp.example.com:587", "user", "pass")
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestSMTPSender_SendTLS_Patched_Cov15(t *testing.T) {
|
|
patches := gomonkey.NewPatches()
|
|
defer patches.Reset()
|
|
|
|
patches.ApplyFunc(smtp.Dial, func(addr string) (*smtp.Client, error) {
|
|
return nil, fmt.Errorf("patched: connection refused")
|
|
})
|
|
|
|
s := &SMTPSender{}
|
|
_, err := s.sendTLS("smtp.example.com:465", "user", "pass", "from@ex.com", "to@ex.com", []byte("test"))
|
|
require.Error(t, err)
|
|
}
|