-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail_test.go
157 lines (124 loc) · 3.75 KB
/
mail_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package mail
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
type MockMailer struct {
mock.Mock
}
func (m *MockMailer) Send(eml *Mail) error {
args := m.Called(eml)
return args.Error(0)
}
func (m *MockMailer) Ping() error {
args := m.Called()
return args.Error(0)
}
func TestNewStandardTemplateMailer(t *testing.T) {
t.Run("invalid template path", func(t *testing.T) {
mockMailer := &MockMailer{}
_, err := NewStandardTemplateMailer(mockMailer, "./invalid/*.tmpl")
require.Error(t, err)
assert.EqualError(t, err, "template: pattern matches no files: `./invalid/*.tmpl`")
})
t.Run("success", func(t *testing.T) {
mockMailer := &MockMailer{}
_, err := NewStandardTemplateMailer(mockMailer, "./testdata/*.tmpl")
require.NoError(t, err)
})
}
func TestMustNewStandardTemplateMailer(t *testing.T) {
mockMailer := &MockMailer{}
assert.Panics(t, func() {
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 := MustNewStandardTemplateMailer(mockMailer, "./testdata/*.tmpl")
template := &Template{
TextPath: "foo_txt.tmpl",
}
err := templateMailer.Send(template, &Config{})
require.Error(t, err)
assert.Contains(t, err.Error(), `template: no template "foo_txt.tmpl"`)
})
t.Run("failed to execute HTML template", func(t *testing.T) {
mockMailer := &MockMailer{}
templateMailer := MustNewStandardTemplateMailer(mockMailer, "./testdata/*.tmpl")
template := &Template{
TextPath: "foo_text.tmpl",
HTMLPath: "foo_htm.tmpl",
}
err := templateMailer.Send(template, &Config{})
require.Error(t, err)
assert.Contains(t, err.Error(), `template: no template "foo_htm.tmpl"`)
})
t.Run("failed to send email", func(t *testing.T) {
mockMailer := &MockMailer{}
templateMailer := MustNewStandardTemplateMailer(mockMailer, "./testdata/*.tmpl")
errUnexpected := errors.New("unexpected error")
mockMailer.On("Send", mock.Anything).Return(errUnexpected)
template := &Template{
TextPath: "foo_text.tmpl",
HTMLPath: "foo_html.tmpl",
}
err := templateMailer.Send(template, &Config{})
mockMailer.AssertExpectations(t)
assert.Equal(t, errUnexpected, err)
})
t.Run("success", func(t *testing.T) {
mockMailer := &MockMailer{}
templateMailer := MustNewStandardTemplateMailer(mockMailer, "./testdata/*.tmpl")
config := &Config{
From: &Address{
Name: "joe",
Email: "[email protected]",
},
To: []Address{
{
Name: "jane",
Email: "[email protected]",
},
},
Subject: "hello",
Options: nil,
}
eml := &Mail{
Config: *config,
HTML: "foohtml\n",
Text: "footext\n",
}
mockMailer.On("Send", eml).Return(nil)
template := &Template{
Data: map[string]interface{}{
"FooVar": "foo",
},
TextPath: "foo_text.tmpl",
HTMLPath: "foo_html.tmpl",
}
err := templateMailer.Send(template, config)
mockMailer.AssertExpectations(t)
require.NoError(t, err)
})
}