65 lines
1.9 KiB
Go
65 lines
1.9 KiB
Go
package notify
|
|
|
|
import "strings"
|
|
import "testing"
|
|
import "time"
|
|
|
|
func TestParseRecipients(t *testing.T) {
|
|
var got []string
|
|
var cases = []struct {
|
|
in string
|
|
want []string
|
|
}{
|
|
{"a@x.com", []string{"a@x.com"}},
|
|
{"a@x.com, b@y.com", []string{"a@x.com", "b@y.com"}},
|
|
{" a@x.com ;b@y.com , ", []string{"a@x.com", "b@y.com"}},
|
|
{"", nil},
|
|
{" , ; ", nil},
|
|
}
|
|
var c struct {
|
|
in string
|
|
want []string
|
|
}
|
|
for _, c = range cases {
|
|
got = parseRecipients(c.in)
|
|
if len(got) != len(c.want) {
|
|
t.Fatalf("parseRecipients(%q) = %v, want %v", c.in, got, c.want)
|
|
}
|
|
var i int
|
|
for i = 0; i < len(got); i++ {
|
|
if got[i] != c.want[i] {
|
|
t.Fatalf("parseRecipients(%q)[%d] = %q, want %q", c.in, i, got[i], c.want[i])
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBuildEmailMessage(t *testing.T) {
|
|
var msg string
|
|
msg = string(buildEmailMessage("alerts@example.com", []string{"a@x.com", "b@y.com"}, "api.example.com is down", "connection refused", time.Unix(1700000000, 0).UTC()))
|
|
if !strings.Contains(msg, "From: alerts@example.com\r\n") {
|
|
t.Fatalf("missing From header: %q", msg)
|
|
}
|
|
if !strings.Contains(msg, "To: a@x.com, b@y.com\r\n") {
|
|
t.Fatalf("recipients not joined into To header: %q", msg)
|
|
}
|
|
if !strings.Contains(msg, "Subject: api.example.com is down\r\n") {
|
|
t.Fatalf("missing Subject: %q", msg)
|
|
}
|
|
if !strings.Contains(msg, "\r\n\r\nconnection refused") {
|
|
t.Fatalf("body not separated from headers: %q", msg)
|
|
}
|
|
}
|
|
|
|
func TestBuildEmailMessageSubjectNewlinesFlattened(t *testing.T) {
|
|
var msg string
|
|
// A newline in the subject would otherwise inject a header (SMTP smuggling).
|
|
msg = string(buildEmailMessage("f@x.com", []string{"a@x.com"}, "line1\nline2", "body", time.Unix(1700000000, 0).UTC()))
|
|
if strings.Contains(msg, "Subject: line1\nline2") {
|
|
t.Fatalf("subject newline not flattened: %q", msg)
|
|
}
|
|
if !strings.Contains(msg, "Subject: line1 line2\r\n") {
|
|
t.Fatalf("expected flattened subject: %q", msg)
|
|
}
|
|
}
|