@@ -0,0 +1,64 @@
|
||||
package mailer
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFileModeAppendsMessages(t *testing.T) {
|
||||
filePath := filepath.Join(t.TempDir(), "mails.log")
|
||||
m, err := New(Config{
|
||||
Mode: ModeFile,
|
||||
Sender: "gardomatic@example.com",
|
||||
FilePath: filePath,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("New() returned an error: %v", err)
|
||||
}
|
||||
|
||||
data := map[string]any{
|
||||
"userID": 42,
|
||||
"activationToken": "test-token",
|
||||
}
|
||||
for _, recipient := range []string{"alice@example.com", "bob@example.com"} {
|
||||
if err = m.Send(recipient, "user_welcome.tmpl", data); err != nil {
|
||||
t.Fatalf("Send() returned an error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
content, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
t.Fatalf("reading mail output: %v", err)
|
||||
}
|
||||
output := string(content)
|
||||
|
||||
if got := strings.Count(output, "=== gardomatic mail ==="); got != 2 {
|
||||
t.Errorf("message count = %d, want 2", got)
|
||||
}
|
||||
for _, expected := range []string{
|
||||
"alice@example.com",
|
||||
"bob@example.com",
|
||||
"Subject: Welcome to Gardomatic!",
|
||||
"test-token",
|
||||
} {
|
||||
if !strings.Contains(output, expected) {
|
||||
t.Errorf("output does not contain %q", expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileModeRequiresPath(t *testing.T) {
|
||||
_, err := New(Config{Mode: ModeFile, Sender: "gardomatic@example.com"})
|
||||
if err == nil {
|
||||
t.Fatal("New() returned no error without a file path")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewRejectsUnknownMode(t *testing.T) {
|
||||
_, err := New(Config{Mode: "unknown", Sender: "gardomatic@example.com"})
|
||||
if err == nil {
|
||||
t.Fatal("New() returned no error for an unknown mode")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user