93 lines
2.6 KiB
Go
93 lines
2.6 KiB
Go
package email
|
|
|
|
import (
|
|
"fmt"
|
|
"net/smtp"
|
|
"testing"
|
|
|
|
"github.com/agiledragon/gomonkey/v2"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// Use gomonkey to patch smtp.Dial so we can test validatePlain/sendPlain
|
|
// without actual network calls.
|
|
func TestSMTPSender_ValidatePlain_Patched_Cov14(t *testing.T) {
|
|
patches := gomonkey.NewPatches()
|
|
defer patches.Reset()
|
|
|
|
// Patch smtp.Dial to return an error
|
|
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)
|
|
require.Contains(t, err.Error(), "SMTP connection failed")
|
|
}
|
|
|
|
func TestSMTPSender_ValidateTLS_Patched_Cov14(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.validateTLS("smtp.example.com:465", "user", "pass")
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestSMTPSender_ValidateSTARTTLS_Patched_Cov14(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.validateSTARTTLS("smtp.example.com:587", "user", "pass")
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestSMTPSender_SendPlain_Patched_Cov14(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.sendPlain("smtp.example.com:587", "user", "pass", "from@ex.com", "to@ex.com", []byte("test"))
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestSMTPSender_SendTLS_Patched_Cov14(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)
|
|
}
|
|
|
|
func TestSMTPSender_SendSTARTTLS_Patched_Cov14(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.sendSTARTTLS("smtp.example.com:587", "user", "pass", "from@ex.com", "to@ex.com", []byte("test"))
|
|
require.Error(t, err)
|
|
}
|