On APP_ENV=onprem the registry's mails (new findings, published documents)
no longer carry our company signature, address, register entry, contact
address or legal pages. They say the installation sent them, name the
customer's contact (MAIL_CONTACT) and link the customer's imprint and
privacy notice (LEGAL_IMPRINT_URL, LEGAL_PRIVACY_URL), each only when set.
mail.View gets a Site; the zero value is the cloud, so cloud mails stay as
they were (checked byte for byte against the previous templates) apart from
the spelling "Breakpilot", also in the default sender name. The legal line
moved from every text template into signature.tmpl.
On-prem the registry also refuses to start when MAIL_FROM, PORTAL_URL,
INVITE_REDIRECT_URI or LEGAL_* point at breakpilot.com.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 6058a94254)
275 lines
11 KiB
Go
275 lines
11 KiB
Go
package mail
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/base64"
|
|
"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 ") {
|
|
t.Fatalf("%q\n%s", m.Subject, m.Text)
|
|
}
|
|
if !strings.Contains(m.HTML, "Needs review") || strings.Contains(m.HTML, "AI assessment") {
|
|
t.Fatalf("html: %s", m.HTML)
|
|
}
|
|
// JSON numbers arrive as float64: one finding still reads in the singular.
|
|
if de, _ := Render(KindFindingsNew, "fr", View{D: map[string]any{"new_count": float64(1), "target_name": "Presse"}}); de.Subject != "1 neuer Befund in Presse" || !strings.Contains(de.Text, "1 Befund ergeben, den es vorher nicht gab") {
|
|
t.Fatalf("fallback locale singular: %q\n%s", de.Subject, de.Text)
|
|
}
|
|
if en, _ := Render(KindFindingsNew, "en", View{D: map[string]any{"new_count": float64(1), "target_name": "Press"}}); en.Subject != "1 new finding in Press" {
|
|
t.Fatalf("english singular: %q", en.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)
|
|
}
|
|
}
|
|
|
|
// Every mail carries the company signature for the no-reply sender, in the
|
|
// mail's language, in both bodies.
|
|
func TestRender_signature(t *testing.T) {
|
|
for _, kind := range []string{KindDocumentPublished, KindFindingsNew} {
|
|
for _, c := range []struct{ lang, greeting, team string }{
|
|
{"de", "Mit freundlichen Grüßen", "Ihr Breakpilot-Team"},
|
|
{"en", "Kind regards", "Your Breakpilot team"},
|
|
} {
|
|
m, err := Render(kind, c.lang, View{Tenant: "acme", URL: "https://app/acme",
|
|
D: map[string]any{"title": "T", "target_name": "fw", "new_count": 2}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, body := range []string{m.Text, m.HTML} {
|
|
for _, want := range []string{c.greeting, c.team, "Breakpilot GmbH", "78351 Bodman-Ludwigshafen",
|
|
"noreply@breakpilot.com", "kontakt@breakpilot.com", "DE464808363", "HRB 736403", "Sharang Deepak Parnerkar"} {
|
|
if !strings.Contains(body, want) {
|
|
t.Errorf("%s/%s: %q missing", kind, c.lang, want)
|
|
}
|
|
}
|
|
}
|
|
// The signature comes before the "why you get this" footer.
|
|
if strings.Index(m.Text, c.team) > strings.Index(m.Text, "\n--\n") {
|
|
t.Errorf("%s/%s: signature after the footer", kind, c.lang)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// The cloud's legal line: our imprint and privacy notice, overridable.
|
|
func TestRender_cloudLegalLinks(t *testing.T) {
|
|
m, _ := Render(KindFindingsNew, "de", View{URL: "https://x", D: map[string]any{"target_name": "fw", "new_count": 2}})
|
|
if !strings.Contains(m.Text, "Breakpilot GmbH · "+CloudImprint) || !strings.Contains(m.HTML, `href="`+CloudImprint+`"`) || !strings.Contains(m.HTML, `href="`+CloudPrivacy+`"`) {
|
|
t.Fatalf("cloud defaults:\n%s", m.Text)
|
|
}
|
|
m, _ = Render(KindTrialReceived, "de", View{Site: Site{Imprint: "https://i.test", Privacy: "https://p.test"}})
|
|
if !strings.Contains(m.Text, "Breakpilot GmbH · https://i.test · https://p.test") || strings.Contains(m.HTML, CloudImprint) {
|
|
t.Fatalf("override:\n%s", m.Text)
|
|
}
|
|
}
|
|
|
|
// On a customer's installation no mail carries our company data, address,
|
|
// contact or legal pages; it names their pages and contact when given.
|
|
func TestRender_onPrem(t *testing.T) {
|
|
d := map[string]any{"title": "T", "target_name": "fw", "new_count": 2}
|
|
ours := []string{"breakpilot.com", "Breakpilot GmbH", "Bodman", "DE464808363", "HRB 736403", "Parnerkar", "Mit freundlichen", "Kind regards"}
|
|
for _, kind := range []string{KindDocumentPublished, KindFindingsNew} {
|
|
for _, c := range []struct{ lang, note, ask, imprint, privacy string }{
|
|
{"de", "die Installation in Ihrem", "Fragen", "Impressum", "Datenschutz"},
|
|
{"en", "the installation in your company", "Questions", "Imprint", "Privacy"},
|
|
} {
|
|
full, err := Render(kind, c.lang, View{Tenant: "werk", URL: "https://bp.onprem.test/werk",
|
|
D: d, Site: Site{OnPrem: true, Imprint: "https://intranet.onprem.test/impressum", Privacy: "https://intranet.onprem.test/datenschutz", Contact: "IT <it@onprem.test>"}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, body := range []string{full.Text, full.HTML} {
|
|
for _, bad := range ours {
|
|
if strings.Contains(body, bad) {
|
|
t.Errorf("%s/%s: %q in an on-prem mail", kind, c.lang, bad)
|
|
}
|
|
}
|
|
for _, want := range []string{c.note, c.ask, "https://intranet.onprem.test/impressum", "https://intranet.onprem.test/datenschutz"} {
|
|
if !strings.Contains(body, want) {
|
|
t.Errorf("%s/%s: %q missing", kind, c.lang, want)
|
|
}
|
|
}
|
|
}
|
|
if !strings.Contains(full.Text, "IT <it@onprem.test>") || !strings.Contains(full.HTML, "IT <it@onprem.test>") {
|
|
t.Errorf("%s/%s: contact not shown (escaped in HTML)", kind, c.lang)
|
|
}
|
|
if strings.Index(full.Text, c.note) > strings.Index(full.Text, "\n--\n") {
|
|
t.Errorf("%s/%s: note after the footer", kind, c.lang)
|
|
}
|
|
// Nothing named: no legal line, no contact, no empty link.
|
|
bare, _ := Render(kind, c.lang, View{Tenant: "werk", URL: "https://bp.onprem.test/werk", D: d, Site: Site{OnPrem: true}})
|
|
for _, body := range []string{bare.Text, bare.HTML} {
|
|
for _, bad := range append(ours, c.imprint+":", c.ask+":", `href=""`) {
|
|
if strings.Contains(body, bad) {
|
|
t.Errorf("%s/%s bare: %q", kind, c.lang, bad)
|
|
}
|
|
}
|
|
}
|
|
if !strings.HasSuffix(bare.Text, "werk verwalten.\n") && !strings.HasSuffix(bare.Text, "einsehen dürfen.\n") &&
|
|
!strings.HasSuffix(bare.Text, ".\n") {
|
|
t.Errorf("%s/%s bare: text ends %q", kind, c.lang, bare.Text[max(0, len(bare.Text)-40):])
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBuild_attachmentsAreAMixedMultipart(t *testing.T) {
|
|
pdf := bytes.Repeat([]byte("%PDF-1.7 Testbedingungen \x00\xff"), 20)
|
|
raw := Build(Message{To: "a@b.de", Subject: "Aktiviert", Text: "Text ä", HTML: "<p>ä</p>",
|
|
Attachments: []Attachment{{Name: "Breakpilot_Testbedingungen_v1.0_2026-10-01.pdf", ContentType: "application/pdf", Data: pdf}}},
|
|
Config{From: "noreply@breakpilot.com"}, time.Unix(0, 0))
|
|
msg, err := mail.ReadMessage(bytes.NewReader(raw))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mt, params, _ := mime.ParseMediaType(msg.Header.Get("Content-Type"))
|
|
if mt != "multipart/mixed" {
|
|
t.Fatalf("top type %q", mt)
|
|
}
|
|
mr := multipart.NewReader(msg.Body, params["boundary"])
|
|
alt, err := mr.NextPart()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
amt, aparams, _ := mime.ParseMediaType(alt.Header.Get("Content-Type"))
|
|
if amt != "multipart/alternative" {
|
|
t.Fatalf("first part %q", amt)
|
|
}
|
|
inner := multipart.NewReader(alt, aparams["boundary"])
|
|
n := 0
|
|
for {
|
|
p, err := inner.NextPart()
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, _ := io.ReadAll(p)
|
|
if !strings.Contains(string(b), "ä") {
|
|
t.Fatalf("inner part %q", b)
|
|
}
|
|
n++
|
|
}
|
|
if n != 2 {
|
|
t.Fatalf("inner parts %d", n)
|
|
}
|
|
att, err := mr.NextPart()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if att.FileName() != "Breakpilot_Testbedingungen_v1.0_2026-10-01.pdf" || !strings.HasPrefix(att.Header.Get("Content-Type"), "application/pdf") {
|
|
t.Fatalf("attachment header %v", att.Header)
|
|
}
|
|
enc, _ := io.ReadAll(att)
|
|
got, err := base64.StdEncoding.DecodeString(strings.ReplaceAll(string(enc), "\r\n", ""))
|
|
if err != nil || !bytes.Equal(got, pdf) {
|
|
t.Fatalf("attachment bytes differ (err %v)", err)
|
|
}
|
|
if _, err := mr.NextPart(); err != io.EOF {
|
|
t.Fatalf("extra part: %v", err)
|
|
}
|
|
}
|