Skip to content

Commit

Permalink
Tight up template mailer implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
perajovic committed Jan 27, 2020
1 parent 2d2dcbe commit 5a39eaa
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
13 changes: 11 additions & 2 deletions mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mail

import (
"bytes"
"errors"
"io"
"net/http"
"text/template"
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
37 changes: 31 additions & 6 deletions mail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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)
Expand All @@ -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{
Expand All @@ -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",
}
Expand Down
2 changes: 1 addition & 1 deletion testdata/foo_html.tmpl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
foohtml
{{ .FooVar }}html
2 changes: 1 addition & 1 deletion testdata/foo_text.tmpl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
footext
{{ .FooVar }}text

0 comments on commit 5a39eaa

Please sign in to comment.