statping/notifiers/email_test.go

145 lines
3.7 KiB
Go
Raw Normal View History

2018-12-04 05:57:11 +00:00
// Statping
2018-09-18 22:02:27 +00:00
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
2018-12-04 04:17:29 +00:00
// https://github.com/hunterlong/statping
2018-09-18 22:02:27 +00:00
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package notifiers
import (
"fmt"
2018-12-04 04:17:29 +00:00
"github.com/hunterlong/statping/core/notifier"
"github.com/hunterlong/statping/utils"
"github.com/stretchr/testify/assert"
"os"
"testing"
"time"
)
var (
2018-11-25 03:56:09 +00:00
EMAIL_HOST string
EMAIL_USER string
EMAIL_PASS string
EMAIL_OUTGOING string
EMAIL_SEND_TO string
EMAIL_PORT int64
)
2018-10-06 05:03:10 +00:00
var testEmail *emailOutgoing
func init() {
2018-09-17 22:13:42 +00:00
EMAIL_HOST = os.Getenv("EMAIL_HOST")
EMAIL_USER = os.Getenv("EMAIL_USER")
EMAIL_PASS = os.Getenv("EMAIL_PASS")
EMAIL_OUTGOING = os.Getenv("EMAIL_OUTGOING")
EMAIL_SEND_TO = os.Getenv("EMAIL_SEND_TO")
2018-11-25 03:56:09 +00:00
EMAIL_PORT = utils.ToInt(os.Getenv("EMAIL_PORT"))
2018-09-17 22:13:42 +00:00
emailer.Host = EMAIL_HOST
emailer.Username = EMAIL_USER
emailer.Password = EMAIL_PASS
emailer.Var1 = EMAIL_OUTGOING
emailer.Var2 = EMAIL_SEND_TO
emailer.Port = int(EMAIL_PORT)
}
func TestEmailNotifier(t *testing.T) {
t.Parallel()
if EMAIL_HOST == "" || EMAIL_USER == "" || EMAIL_PASS == "" {
2018-10-06 05:00:40 +00:00
t.Log("email notifier testing skipped, missing EMAIL_ environment variables")
t.SkipNow()
}
currentCount = CountNotifiers()
t.Run("New Emailer", func(t *testing.T) {
emailer.Host = EMAIL_HOST
emailer.Username = EMAIL_USER
emailer.Password = EMAIL_PASS
emailer.Var1 = EMAIL_OUTGOING
emailer.Var2 = EMAIL_SEND_TO
emailer.Port = int(EMAIL_PORT)
emailer.Delay = time.Duration(100 * time.Millisecond)
2018-10-06 05:03:10 +00:00
testEmail = &emailOutgoing{
To: emailer.GetValue("var2"),
Subject: fmt.Sprintf("Service %v is Failing", TestService.Name),
2018-10-06 05:05:50 +00:00
Template: mainEmailTemplate,
2018-09-28 06:57:03 +00:00
Data: TestService,
From: emailer.GetValue("var1"),
}
})
2018-10-06 05:00:40 +00:00
t.Run("Add email Notifier", func(t *testing.T) {
err := notifier.AddNotifier(emailer)
assert.Nil(t, err)
assert.Equal(t, "Hunter Long", emailer.Author)
assert.Equal(t, EMAIL_HOST, emailer.Host)
})
t.Run("Emailer Load", func(t *testing.T) {
notifier.Load()
})
2018-10-06 05:00:40 +00:00
t.Run("email Within Limits", func(t *testing.T) {
ok, err := emailer.WithinLimits()
assert.Nil(t, err)
assert.True(t, ok)
})
2018-10-06 05:00:40 +00:00
t.Run("email Test Source", func(t *testing.T) {
emailSource(testEmail)
assert.NotEmpty(t, testEmail.Source)
})
2018-10-06 05:00:40 +00:00
t.Run("email OnFailure", func(t *testing.T) {
emailer.OnFailure(TestService, TestFailure)
2018-10-27 09:27:09 +00:00
assert.Equal(t, 1, len(emailer.Queue))
})
2018-10-06 05:00:40 +00:00
t.Run("email Check Offline", func(t *testing.T) {
assert.False(t, emailer.Online)
})
2018-10-06 05:00:40 +00:00
t.Run("email OnSuccess", func(t *testing.T) {
emailer.OnSuccess(TestService)
2018-10-27 09:27:09 +00:00
assert.Equal(t, 1, len(emailer.Queue))
})
2018-10-06 05:00:40 +00:00
t.Run("email Check Back Online", func(t *testing.T) {
assert.True(t, emailer.Online)
})
2018-10-06 05:00:40 +00:00
t.Run("email OnSuccess Again", func(t *testing.T) {
emailer.OnSuccess(TestService)
2018-10-27 09:27:09 +00:00
assert.Equal(t, 1, len(emailer.Queue))
})
2018-10-06 05:00:40 +00:00
t.Run("email Send", func(t *testing.T) {
err := emailer.Send(testEmail)
assert.Nil(t, err)
})
2018-10-07 06:39:57 +00:00
t.Run("emailer Test", func(t *testing.T) {
2018-10-07 08:53:11 +00:00
t.SkipNow()
2018-10-07 06:39:57 +00:00
err := emailer.OnTest()
assert.Nil(t, err)
})
2018-10-06 05:00:40 +00:00
t.Run("email Run Queue", func(t *testing.T) {
go notifier.Queue(emailer)
time.Sleep(5 * time.Second)
assert.Equal(t, EMAIL_HOST, emailer.Host)
assert.Equal(t, 0, len(emailer.Queue))
})
}