-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//go:generate mockgen -package mail -destination mail_mock.go -source mail.go Client | ||
package mail | ||
|
||
import ( | ||
"gopkg.in/gomail.v2" | ||
) | ||
|
||
const ( | ||
ContentTypePlain = "text/plain" | ||
ContentTypeHTML = "text/html" | ||
) | ||
|
||
// Stubbed out for tests. | ||
var newDialerSender = func(config Config) dialerSender { | ||
return gomail.NewDialer(config.Host, config.Port, config.User, config.Password) | ||
} | ||
|
||
type ( | ||
Client interface { | ||
Send(to []string, subject, contentType, body string) error | ||
} | ||
|
||
Config struct { | ||
Host string | ||
Port int | ||
User string | ||
Password string | ||
} | ||
|
||
defaultClient struct { | ||
config Config | ||
} | ||
|
||
dialerSender interface { | ||
DialAndSend(m ...*gomail.Message) error | ||
} | ||
) | ||
|
||
func NewClient(config Config) Client { | ||
return &defaultClient{ | ||
config: config, | ||
} | ||
} | ||
|
||
func (c *defaultClient) Send(to []string, subject, contentType, body string) error { | ||
m := gomail.NewMessage() | ||
m.SetHeader("From", c.config.User) | ||
m.SetHeader("To", to...) | ||
m.SetHeader("Subject", subject) | ||
m.SetBody(contentType, body) | ||
|
||
return newDialerSender(c.config).DialAndSend(m) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package mail | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/prashantv/gostub" | ||
"github.com/stretchr/testify/assert" | ||
"gopkg.in/gomail.v2" | ||
) | ||
|
||
type ( | ||
dialerSenderMock struct { | ||
send func(...*gomail.Message) error | ||
} | ||
) | ||
|
||
func (d dialerSenderMock) DialAndSend(m ...*gomail.Message) error { | ||
return d.send(m...) | ||
} | ||
|
||
func TestNewClient(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
config Config | ||
to []string | ||
subject string | ||
contentType string | ||
body string | ||
err error | ||
}{{ | ||
name: "to:1", | ||
config: Config{User: "u"}, | ||
to: []string{"1"}, | ||
subject: "subject", | ||
contentType: ContentTypePlain, | ||
body: "body", | ||
}, { | ||
name: "to:2", | ||
config: Config{User: "u"}, | ||
to: []string{"1"}, | ||
subject: "subject", | ||
contentType: ContentTypeHTML, | ||
body: "body", | ||
}, { | ||
name: "error", | ||
config: Config{User: "u"}, | ||
to: []string{"1"}, | ||
subject: "subject", | ||
contentType: ContentTypePlain, | ||
body: "body", | ||
err: errors.New("send error"), | ||
}} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
ast := assert.New(t) | ||
stubs := gostub.StubFunc(&newDialerSender, dialerSenderMock{ | ||
send: func(msg ...*gomail.Message) error { | ||
if ast.Len(msg, 1) { | ||
ast.Equal([]string{test.config.User}, msg[0].GetHeader("From")) | ||
ast.Equal(test.to, msg[0].GetHeader("To")) | ||
ast.Equal([]string{test.subject}, msg[0].GetHeader("Subject")) | ||
} | ||
return test.err | ||
}, | ||
}) | ||
defer stubs.Reset() | ||
c := NewClient(test.config) | ||
err := c.Send(test.to, test.subject, test.contentType, test.body) | ||
ast.Equal(test.err, err) | ||
}) | ||
} | ||
} | ||
|
||
func Test_newDialerSender(t *testing.T) { | ||
d := newDialerSender(Config{}) | ||
assert.IsType(t, (*gomail.Dialer)(nil), d) | ||
} |