Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
117 lines
4.0 KiB
Go
117 lines
4.0 KiB
Go
package mail
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"mime"
|
|
"mime/multipart"
|
|
"net/mail"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestRender_documentPublished(t *testing.T) {
|
|
m, err := Render(KindDocumentPublished, "de", View{Tenant: "Acme <GmbH>", URL: "https://app/acme/documents",
|
|
D: map[string]any{"title": "CRA-Dossier", "description": "Q3 & <b>"}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if m.Subject != "Neues Dokument für Acme <GmbH>: CRA-Dossier" {
|
|
t.Fatalf("subject %q", m.Subject)
|
|
}
|
|
if !strings.Contains(m.HTML, "Acme <GmbH>") || strings.Contains(m.HTML, "<b>") {
|
|
t.Fatal("html not escaped")
|
|
}
|
|
if !strings.Contains(m.HTML, `href="https://app/acme/documents"`) || !strings.Contains(m.HTML, `lang="de"`) {
|
|
t.Fatalf("html: %s", m.HTML)
|
|
}
|
|
if !strings.Contains(m.Text, "https://app/acme/documents") || !strings.Contains(m.Text, "Q3 & <b>") {
|
|
t.Fatalf("text: %s", m.Text)
|
|
}
|
|
}
|
|
|
|
func TestRender_findingsEnglishAndDefaults(t *testing.T) {
|
|
m, err := Render(KindFindingsNew, "en", View{Tenant: "Acme", URL: "https://x", D: map[string]any{"target_name": "Pump", "new_count": 3, "critical": 1}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if m.Subject != "3 new findings in Pump" || !strings.Contains(m.Text, "Critical 1") || !strings.Contains(m.Text, "High 0") {
|
|
t.Fatalf("%q\n%s", m.Subject, m.Text)
|
|
}
|
|
if de, _ := Render(KindFindingsNew, "fr", View{D: map[string]any{"new_count": 1}}); !strings.Contains(de.Subject, "neue Befunde") {
|
|
t.Fatalf("fallback locale: %q", de.Subject)
|
|
}
|
|
if _, err := Render("nope", "de", View{}); err == nil {
|
|
t.Fatal("unknown kind rendered")
|
|
}
|
|
if m, _ := Render(KindDocumentPublished, "en", View{}); m.Subject == "" {
|
|
t.Fatal("nil data")
|
|
}
|
|
}
|
|
|
|
func TestBuild_isAValidMultipartMessage(t *testing.T) {
|
|
raw := Build(Message{To: "it@acme.de", Subject: "Prüfwerk: 3 neue Befunde", Text: "Hallo\nWelt ä", HTML: "<p>ä</p>"},
|
|
Config{From: "noreply@breakpilot.com", FromName: "BreakPilot"}, time.Unix(0, 0))
|
|
msg, err := mail.ReadMessage(strings.NewReader(string(raw)))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dec := new(mime.WordDecoder)
|
|
subj, _ := dec.DecodeHeader(msg.Header.Get("Subject"))
|
|
if subj != "Prüfwerk: 3 neue Befunde" || msg.Header.Get("To") != "it@acme.de" || !strings.Contains(msg.Header.Get("From"), "<noreply@breakpilot.com>") {
|
|
t.Fatalf("headers: %v %q", msg.Header, subj)
|
|
}
|
|
if !strings.HasSuffix(strings.Trim(msg.Header.Get("Message-ID"), "<>"), "@breakpilot.com") {
|
|
t.Fatalf("message id %q", msg.Header.Get("Message-ID"))
|
|
}
|
|
_, params, _ := mime.ParseMediaType(msg.Header.Get("Content-Type"))
|
|
mr := multipart.NewReader(msg.Body, params["boundary"])
|
|
var types []string
|
|
for {
|
|
p, err := mr.NextPart()
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
types = append(types, p.Header.Get("Content-Type"))
|
|
b, _ := io.ReadAll(p) // multipart decodes quoted-printable
|
|
if !strings.Contains(string(b), "ä") {
|
|
t.Fatalf("part not decoded: %q", b)
|
|
}
|
|
}
|
|
if len(types) != 2 || !strings.HasPrefix(types[0], "text/plain") || !strings.HasPrefix(types[1], "text/html") {
|
|
t.Fatalf("parts: %v", types)
|
|
}
|
|
// without a display name or an HTML part
|
|
raw = Build(Message{To: "a@b", Subject: "s", Text: "t"}, Config{From: "x"}, time.Now())
|
|
if !strings.Contains(string(raw), "From: x\r\n") || strings.Contains(string(raw), "text/html") || !strings.Contains(string(raw), "@breakpilot.com>") {
|
|
t.Fatalf("minimal: %s", raw)
|
|
}
|
|
}
|
|
|
|
func TestRecorderAndConfig(t *testing.T) {
|
|
r := &Recorder{}
|
|
_ = r.Send(context.Background(), Message{To: "a"})
|
|
if len(r.Messages()) != 1 {
|
|
t.Fatal("not recorded")
|
|
}
|
|
r.Fail = errors.New("x")
|
|
if r.Send(context.Background(), Message{}) == nil {
|
|
t.Fatal("fail ignored")
|
|
}
|
|
if (Config{}).Configured() || !(Config{Host: "h", From: "f"}).Configured() {
|
|
t.Fatal("Configured")
|
|
}
|
|
}
|
|
|
|
func TestSMTP_dialFailure(t *testing.T) {
|
|
s := &SMTP{Cfg: Config{Host: "127.0.0.1", Port: 1, From: "x"}, Timeout: time.Second}
|
|
if err := s.Send(context.Background(), Message{To: "a@b"}); err == nil || !strings.Contains(err.Error(), "dial") {
|
|
t.Fatalf("got %v", err)
|
|
}
|
|
}
|