Initial cloud-services repo - gateway service + pkg modules
This commit is contained in:
32
pkg/smtpclient/mock_smtp.go
Normal file
32
pkg/smtpclient/mock_smtp.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package smtpclient
|
||||
|
||||
import (
|
||||
"net/smtp"
|
||||
)
|
||||
|
||||
type MockSMTPInterface interface {
|
||||
Auth(username string, password string)
|
||||
Send(from string, to []string, subject string, body string) error
|
||||
Close()
|
||||
}
|
||||
|
||||
type MockSMTP struct {
|
||||
Host string
|
||||
Port int
|
||||
Username string
|
||||
auth smtp.Auth
|
||||
}
|
||||
|
||||
func (s *MockSMTP) Auth(username string, password string) {
|
||||
s.auth = smtp.PlainAuth("", username, password, s.Host)
|
||||
}
|
||||
|
||||
func (s *MockSMTP) Send(from string, to []string, subject string, body string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MockSMTP) Close() {
|
||||
s.Host = ""
|
||||
s.Username = ""
|
||||
s.auth = nil
|
||||
}
|
||||
61
pkg/smtpclient/smtp.go
Normal file
61
pkg/smtpclient/smtp.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package smtpclient
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/smtp"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var ErrAuthRequired = errors.New("auth required")
|
||||
|
||||
const CRLF = "\r\n"
|
||||
const FromEmail = "no-reply@fiskerinc.com"
|
||||
|
||||
type SMTPInterface interface {
|
||||
Auth(username string, password string)
|
||||
Send(from string, to []string, subject string, body string) error
|
||||
Close()
|
||||
}
|
||||
|
||||
func NewSMTP(host string, port int) SMTPInterface {
|
||||
return &SMTP{
|
||||
Host: host,
|
||||
Port: port,
|
||||
}
|
||||
}
|
||||
|
||||
type SMTP struct {
|
||||
Host string
|
||||
Port int
|
||||
Username string
|
||||
auth smtp.Auth
|
||||
}
|
||||
|
||||
func (s *SMTP) Auth(username string, password string) {
|
||||
s.auth = smtp.PlainAuth("", username, password, s.Host)
|
||||
}
|
||||
|
||||
func (s *SMTP) Send(from string, to []string, subject string, body string) error {
|
||||
if s.auth == nil {
|
||||
return ErrAuthRequired
|
||||
}
|
||||
|
||||
msg := []string{
|
||||
fmt.Sprintf("From: %s", from),
|
||||
fmt.Sprintf("To: %s", strings.Join(to, ", ")),
|
||||
fmt.Sprintf("Subject: %s", subject),
|
||||
"",
|
||||
fmt.Sprintf("%s%s", body, CRLF),
|
||||
}
|
||||
addr := fmt.Sprintf("%s:%d", s.Host, s.Port)
|
||||
|
||||
return smtp.SendMail(addr, s.auth, from, to, []byte(strings.Join(msg, CRLF)))
|
||||
}
|
||||
|
||||
func (s *SMTP) Close() {
|
||||
s.Host = ""
|
||||
s.Username = ""
|
||||
s.auth = nil
|
||||
}
|
||||
28
pkg/smtpclient/smtp_test.go
Normal file
28
pkg/smtpclient/smtp_test.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package smtpclient_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"fiskerinc.com/modules/smtpclient"
|
||||
"fiskerinc.com/modules/testhelper"
|
||||
)
|
||||
|
||||
func TestSMTP(t *testing.T) {
|
||||
testEmails := []string{"test@fiskerinc.com"}
|
||||
expected := "535 Authentication Credentials Invalid"
|
||||
host := "email-smtp.us-west-2.amazonaws.com"
|
||||
smtp := smtpclient.NewSMTP(host, 587)
|
||||
defer smtp.Close()
|
||||
err := smtp.Send(smtpclient.FromEmail, testEmails, "test", "this is a test")
|
||||
if err != smtpclient.ErrAuthRequired {
|
||||
t.Errorf(testhelper.TestErrorTemplate, "No auth", smtpclient.ErrAuthRequired, err)
|
||||
}
|
||||
|
||||
smtp.Auth("fakeuser", "fakepassword")
|
||||
err = smtp.Send(smtpclient.FromEmail, testEmails, "test", "this is a test")
|
||||
if err == nil {
|
||||
t.Errorf(testhelper.TestErrorTemplate, "auth error", "error", err)
|
||||
} else if err.Error() != expected {
|
||||
t.Errorf(testhelper.TestErrorTemplate, "auth error", expected, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user