Skip to content

Commit

Permalink
Refactor: auth in notifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Castell committed Jan 15, 2019
1 parent 7dd22e5 commit 979138b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
11 changes: 8 additions & 3 deletions dkron/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,19 @@ func (n *Notifier) sendExecutionEmail() {
}

serverAddr := fmt.Sprintf("%s:%d", n.Config.MailHost, n.Config.MailPort)
if err := e.Send(serverAddr, n.auth()); err != nil {
log.WithError(err).Error("notifier: Error sending email")
}
}

func (n *Notifier) auth() smtp.Auth {
var auth smtp.Auth

if n.Config.MailUsername != "" && n.Config.MailPassword != "" {
auth = smtp.PlainAuth("", n.Config.MailUsername, n.Config.MailPassword, n.Config.MailHost)
}
if err := e.Send(serverAddr, auth); err != nil {
log.WithError(err).Error("notifier: Error sending email")
}

return auth
}

func (n *Notifier) callExecutionWebhook() {
Expand Down
25 changes: 24 additions & 1 deletion dkron/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"net/http/httptest"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestNotifier_callExecutionWebhook(t *testing.T) {
Expand Down Expand Up @@ -60,10 +62,31 @@ func TestNotifier_sendExecutionEmail(t *testing.T) {
}

n := Notification(c, ex1, exg, job)

n.Send()
}

func Test_auth(t *testing.T) {
n1 := &Notifier{
Config: &Config{
MailHost: "localhost",
MailPort: 25,
MailUsername: "username",
MailPassword: "password",
}}

a1 := n1.auth()
assert.NotNil(t, a1)

n2 := &Notifier{
Config: &Config{
MailHost: "localhost",
MailPort: 25,
}}

a2 := n2.auth()
assert.Nil(t, a2)
}

func TestNotifier_buildTemplate(t *testing.T) {
c := &Config{
NodeName: "test-node",
Expand Down

0 comments on commit 979138b

Please sign in to comment.