2022-08-30 02:30:36 +00:00
|
|
|
package mailer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-09-03 09:24:28 +00:00
|
|
|
"os"
|
2022-08-30 02:30:36 +00:00
|
|
|
"testing"
|
|
|
|
|
2024-01-04 17:55:26 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-08-30 02:30:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
TestMailerConfig = "test-mailer.json"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetTestMailer() (*Mailer, error) {
|
|
|
|
// Read JSON File
|
2022-09-03 09:24:28 +00:00
|
|
|
bytes, err := os.ReadFile(TestMailerConfig)
|
2022-08-30 02:30:36 +00:00
|
|
|
|
|
|
|
mailer := &Mailer{}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal JSON
|
|
|
|
err = json.Unmarshal(bytes, mailer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return mailer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Mailer(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
mailer, err := GetTestMailer()
|
|
|
|
if err != nil {
|
|
|
|
t.Skip("Error Reading Test Mailer Config - Skipping")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !mailer.Ready() {
|
|
|
|
t.Skip("Mailer not ready - Skipping")
|
|
|
|
}
|
|
|
|
|
|
|
|
message, err := RenderWelcome()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mb := NewMessageBuilder().
|
|
|
|
SetBody(message).
|
|
|
|
SetSubject("Hello").
|
|
|
|
SetTo("John Doe", "john@doe.com").
|
|
|
|
SetFrom("Jane Doe", "jane@doe.com")
|
|
|
|
|
|
|
|
msg := mb.Build()
|
|
|
|
|
|
|
|
err = mailer.Send(msg)
|
|
|
|
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|