diff --git a/README.md b/README.md index 5ae5810..b9d1043 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ func main() { } // Create template mailer. - templateMailer := MustStandardTemplateMailer(mandrillMailer, "/templates/*.tmpl") + templateMailer := MustNewStandardTemplateMailer(mandrillMailer, "/templates/*.tmpl") // Configure email for sending. template := &Template{ diff --git a/doc.go b/doc.go index 0f7dd46..5fc9ed7 100644 --- a/doc.go +++ b/doc.go @@ -9,7 +9,7 @@ Package mail provides an easy interface with interchangable backends. } // Create template mailer. - templateMailer := MustStandardTemplateMailer(mandrillMailer, "/templates/*.tmpl") + templateMailer := MustNewStandardTemplateMailer(mandrillMailer, "/templates/*.tmpl") // Configure email for sending. template := &Template{ diff --git a/mail.go b/mail.go index 7a5b221..d4c730c 100644 --- a/mail.go +++ b/mail.go @@ -2,6 +2,7 @@ package mail import ( "bytes" + "errors" "io" "net/http" "text/template" @@ -88,8 +89,8 @@ func NewStandardTemplateMailer(mailer Mailer, templatePath string) (*StandardTem }, nil } -// MustStandardTemplateMailer returns a new StandardTemplateMailer mailer or panics. -func MustStandardTemplateMailer(mailer Mailer, templatePath string) *StandardTemplateMailer { +// MustNewStandardTemplateMailer returns a new StandardTemplateMailer mailer or panics. +func MustNewStandardTemplateMailer(mailer Mailer, templatePath string) *StandardTemplateMailer { tpl, err := NewStandardTemplateMailer(mailer, templatePath) if err != nil { panic(err) @@ -100,6 +101,14 @@ func MustStandardTemplateMailer(mailer Mailer, templatePath string) *StandardTem // Send sends an email. func (m *StandardTemplateMailer) Send(template *Template, config *Config) error { + if config == nil { + return errors.New("config parameter is required") + } + + if template == nil { + return errors.New("template parameter is required") + } + textBuf := &bytes.Buffer{} if err := m.template.ExecuteTemplate(textBuf, template.TextPath, template.Data); err != nil { return err diff --git a/mail_test.go b/mail_test.go index 031a5e7..a781300 100644 --- a/mail_test.go +++ b/mail_test.go @@ -42,18 +42,40 @@ func TestNewStandardTemplateMailer(t *testing.T) { }) } -func TestMustStandardTemplateMailer(t *testing.T) { +func TestMustNewStandardTemplateMailer(t *testing.T) { mockMailer := &MockMailer{} assert.Panics(t, func() { - MustStandardTemplateMailer(mockMailer, "./invalid/*.tmpl") + MustNewStandardTemplateMailer(mockMailer, "./invalid/*.tmpl") }) } func TestStandardTemplateMailer_Send(t *testing.T) { + t.Run("config is required", func(t *testing.T) { + mockMailer := &MockMailer{} + templateMailer := MustNewStandardTemplateMailer(mockMailer, "./testdata/*.tmpl") + + template := &Template{} + err := templateMailer.Send(template, nil) + + require.Error(t, err) + assert.EqualError(t, err, "config parameter is required") + }) + + t.Run("template is required", func(t *testing.T) { + mockMailer := &MockMailer{} + templateMailer := MustNewStandardTemplateMailer(mockMailer, "./testdata/*.tmpl") + + config := &Config{} + err := templateMailer.Send(nil, config) + + require.Error(t, err) + assert.EqualError(t, err, "template parameter is required") + }) + t.Run("failed to execute plain text template", func(t *testing.T) { mockMailer := &MockMailer{} - templateMailer := MustStandardTemplateMailer(mockMailer, "./testdata/*.tmpl") + templateMailer := MustNewStandardTemplateMailer(mockMailer, "./testdata/*.tmpl") template := &Template{ TextPath: "foo_txt.tmpl", @@ -66,7 +88,7 @@ func TestStandardTemplateMailer_Send(t *testing.T) { t.Run("failed to execute HTML template", func(t *testing.T) { mockMailer := &MockMailer{} - templateMailer := MustStandardTemplateMailer(mockMailer, "./testdata/*.tmpl") + templateMailer := MustNewStandardTemplateMailer(mockMailer, "./testdata/*.tmpl") template := &Template{ TextPath: "foo_text.tmpl", @@ -80,7 +102,7 @@ func TestStandardTemplateMailer_Send(t *testing.T) { t.Run("failed to send email", func(t *testing.T) { mockMailer := &MockMailer{} - templateMailer := MustStandardTemplateMailer(mockMailer, "./testdata/*.tmpl") + templateMailer := MustNewStandardTemplateMailer(mockMailer, "./testdata/*.tmpl") errUnexpected := errors.New("unexpected error") mockMailer.On("Send", mock.Anything).Return(errUnexpected) @@ -97,7 +119,7 @@ func TestStandardTemplateMailer_Send(t *testing.T) { t.Run("success", func(t *testing.T) { mockMailer := &MockMailer{} - templateMailer := MustStandardTemplateMailer(mockMailer, "./testdata/*.tmpl") + templateMailer := MustNewStandardTemplateMailer(mockMailer, "./testdata/*.tmpl") config := &Config{ From: &Address{ @@ -121,6 +143,9 @@ func TestStandardTemplateMailer_Send(t *testing.T) { mockMailer.On("Send", eml).Return(nil) template := &Template{ + Data: map[string]interface{}{ + "FooVar": "foo", + }, TextPath: "foo_text.tmpl", HTMLPath: "foo_html.tmpl", } diff --git a/testdata/foo_html.tmpl b/testdata/foo_html.tmpl index bb2ca67..e2e8493 100644 --- a/testdata/foo_html.tmpl +++ b/testdata/foo_html.tmpl @@ -1 +1 @@ -foohtml +{{ .FooVar }}html diff --git a/testdata/foo_text.tmpl b/testdata/foo_text.tmpl index 8ae0a34..4fe7b22 100644 --- a/testdata/foo_text.tmpl +++ b/testdata/foo_text.tmpl @@ -1 +1 @@ -footext +{{ .FooVar }}text