70 lines
2.2 KiB
Go
70 lines
2.2 KiB
Go
package web
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"net/mail"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestBuildMailMessageCarriesTextPDFAndCalendar(t *testing.T) {
|
|
config := smtpConfig{
|
|
Host: "smtp.example.org",
|
|
Port: 587,
|
|
FromAddress: "plan@example.org",
|
|
FromName: "Pfarrei Sankt Michael",
|
|
}
|
|
pdf := []byte("%PDF-1.4\nfake dispatch packet\n%%EOF")
|
|
calendar := []byte("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nEND:VCALENDAR\r\n")
|
|
subject := "Gottesdienstplan März 2026 · Pfarrei"
|
|
body := "Hallo Anna"
|
|
|
|
raw := buildMailMessage(config, "anna@example.org", subject, body, []mailAttachment{
|
|
{Name: "Plan.pdf", ContentType: "application/pdf", Data: pdf},
|
|
{Name: "Kalender.ics", ContentType: "text/calendar; charset=utf-8; method=PUBLISH", Data: calendar},
|
|
})
|
|
|
|
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",
|
|
"Content-Type: text/calendar; charset=utf-8; method=PUBLISH",
|
|
`filename="Plan.pdf"`,
|
|
`filename="Kalender.ics"`,
|
|
"Content-Disposition: attachment",
|
|
} {
|
|
if !strings.Contains(text, want) {
|
|
t.Fatalf("message misses %q", want)
|
|
}
|
|
}
|
|
if !strings.Contains(text, base64.StdEncoding.EncodeToString(pdf)) {
|
|
t.Fatal("PDF attachment is not base64 encoded in the message")
|
|
}
|
|
if !strings.Contains(text, base64.StdEncoding.EncodeToString(calendar)) {
|
|
t.Fatal("calendar 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)
|
|
}
|
|
}
|