//go:build testing package email import ( "os" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestRenderAlert(t *testing.T) { html, err := RenderAlert(Alert{ Title: "CPU above threshold on web-1", Message: "CPU averaged 91.00% for the previous 10 minutes.", Link: "https://beszel.example.com/system/abc", LinkText: "View web-1", SettingsLink: "https://beszel.example.com/settings/notifications", }) require.NoError(t, err) assert.Contains(t, html, "CPU above threshold on web-1") assert.Contains(t, html, "CPU averaged 91.00% for the previous 10 minutes.") assert.Contains(t, html, `href="https://beszel.example.com/system/abc"`) assert.Contains(t, html, ">View web-1") assert.Contains(t, html, `href="https://beszel.example.com/settings/notifications"`) if dir := os.Getenv("BESZEL_EMAIL_PREVIEW_DIR"); dir != "" { preview, _ := RenderAlert(Alert{ Title: "Container api is unhealthy on web-1", Message: "Container api became unhealthy.\nPrevious state: healthy\n\napi logs:\n```\nERROR connection refused\nretrying in 5s\n```", Link: "https://beszel.example.com/system/abc", LinkText: "View web-1", SettingsLink: "https://beszel.example.com/settings/notifications", }) require.NoError(t, os.WriteFile(dir+"/alert_email.html", []byte(preview), 0o644)) } } func TestRenderAlertEscapesContent(t *testing.T) { html, err := RenderAlert(Alert{ Title: "title", Message: "```\n\n```", Link: "javascript:alert(1)", }) require.NoError(t, err) assert.NotContains(t, html, "