2021-06-21 16:30:34 +02:00
|
|
|
package mailer
|
2021-06-23 20:48:08 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-09-29 14:47:31 +02:00
|
|
|
"go.uber.org/zap"
|
2021-06-23 22:55:53 +02:00
|
|
|
|
|
|
|
"github.com/go-mail/mail"
|
2021-06-23 20:48:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSetupAndPing(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2021-09-29 14:47:31 +02:00
|
|
|
log := zap.L()
|
|
|
|
|
|
|
|
mock, s := NewFakeServer(log)
|
2021-06-23 20:48:08 +02:00
|
|
|
assert.NotNil(mock)
|
|
|
|
// correct setup
|
|
|
|
err := s.Setup()
|
|
|
|
assert.NoError(err)
|
|
|
|
mock.Close()
|
|
|
|
|
|
|
|
s.SMTPPassword = "wrong"
|
2021-09-29 14:47:31 +02:00
|
|
|
mock, s = newFakeServer(s, log)
|
2021-06-23 20:48:08 +02:00
|
|
|
// wrong password
|
|
|
|
err = s.Setup()
|
|
|
|
assert.Error(err)
|
|
|
|
mock.Close()
|
|
|
|
}
|
2021-06-23 22:55:53 +02:00
|
|
|
|
|
|
|
func TestSend(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2021-09-29 14:47:31 +02:00
|
|
|
mock, s := NewFakeServer(zap.L())
|
2021-06-23 22:55:53 +02:00
|
|
|
assert.NotNil(mock)
|
|
|
|
// correct setup
|
|
|
|
err := s.Setup()
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
m := mail.NewMessage()
|
2021-06-25 12:22:19 +02:00
|
|
|
m.SetHeader("From", s.From)
|
2021-06-23 22:55:53 +02:00
|
|
|
m.SetHeader("To", "bob@example.com", "cora@example.com")
|
|
|
|
m.SetAddressHeader("Cc", "dan@example.com", "Dan")
|
|
|
|
m.SetHeader("Subject", "Hello!")
|
|
|
|
m.SetBody("text/plain", "Hello Bob and Cora!")
|
|
|
|
m.AddAlternative("text/html", "Hello <b>Bob</b> and <i>Cora</i>!")
|
|
|
|
|
|
|
|
err = s.Dailer.DialAndSend(m)
|
|
|
|
assert.NoError(err)
|
|
|
|
|
2021-06-25 12:22:19 +02:00
|
|
|
msg := <-mock.Mails
|
2021-06-23 22:55:53 +02:00
|
|
|
mock.Close()
|
2021-06-25 12:22:19 +02:00
|
|
|
assert.Equal(s.From, msg.Header["From"][0])
|
2021-06-23 22:55:53 +02:00
|
|
|
assert.Contains(msg.Body, "Bob and Cora!")
|
|
|
|
|
|
|
|
}
|