Files
LektorenPlan/web/web/mailer_test.go
T
2026-11-14 09:33:17 +01:00

61 lines
1.7 KiB
Go

package web
import (
"encoding/base64"
"net/mail"
"strings"
"testing"
)
func TestBuildMailMessageCarriesTextAndPDF(t *testing.T) {
config := smtpConfig{
Host: "smtp.example.org",
Port: 587,
FromAddress: "plan@example.org",
FromName: "Pfarrei Sankt Michael",
}
attachment := []byte("%PDF-1.4\nfake dispatch packet\n%%EOF")
subject := "Gottesdienstplan März 2026 · Pfarrei"
body := "Hallo Anna"
raw := buildMailMessage(config, "anna@example.org", subject, body, attachment, "Plan.pdf")
message, err := mail.ReadMessage(strings.NewReader(string(raw)))
if err != nil {
t.Fatalf("message is not valid mail: %v", err)
}
if got := message.Header.Get("To"); got != "anna@example.org" {
t.Fatalf("To = %q", got)
}
if got := message.Header.Get("MIME-Version"); got != "1.0" {
t.Fatalf("MIME-Version = %q", got)
}
if contentType := message.Header.Get("Content-Type"); !strings.HasPrefix(contentType, "multipart/mixed;") {
t.Fatalf("Content-Type = %q", contentType)
}
text := string(raw)
for _, want := range []string{
"Content-Type: text/plain; charset=utf-8",
"Content-Type: application/pdf",
`filename="Plan.pdf"`,
"Content-Disposition: attachment",
} {
if !strings.Contains(text, want) {
t.Fatalf("message misses %q", want)
}
}
if !strings.Contains(text, base64.StdEncoding.EncodeToString(attachment)) {
t.Fatal("PDF attachment is not base64 encoded in the message")
}
if !strings.Contains(text, base64.StdEncoding.EncodeToString([]byte(body))) {
t.Fatal("mail body is not base64 encoded in the message")
}
}
func TestSanitizeHeaderStripsLineBreaks(t *testing.T) {
if got := sanitizeHeader("Pfarrei\r\nBcc: evil@example.org"); got != "PfarreiBcc: evil@example.org" {
t.Fatalf("sanitizeHeader = %q", got)
}
}