statping/notifiers/email_test.go

98 lines
2.5 KiB
Go
Raw Normal View History

2020-03-25 18:46:50 +00:00
package notifiers
import (
"github.com/statping/statping/database"
"github.com/statping/statping/types/core"
"github.com/statping/statping/types/failures"
2020-03-25 18:46:50 +00:00
"github.com/statping/statping/types/notifications"
"github.com/statping/statping/types/null"
"github.com/statping/statping/types/services"
2020-03-25 18:46:50 +00:00
"github.com/statping/statping/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
"time"
)
var (
EMAIL_HOST string
EMAIL_USER string
EMAIL_PASS string
EMAIL_OUTGOING string
EMAIL_SEND_TO string
EMAIL_PORT int64
)
2020-06-04 23:18:37 +00:00
func TestEmailNotifier(t *testing.T) {
err := utils.InitLogs()
require.Nil(t, err)
2020-03-25 18:46:50 +00:00
2020-04-16 09:57:00 +00:00
EMAIL_HOST = utils.Params.GetString("EMAIL_HOST")
EMAIL_USER = utils.Params.GetString("EMAIL_USER")
EMAIL_PASS = utils.Params.GetString("EMAIL_PASS")
EMAIL_OUTGOING = utils.Params.GetString("EMAIL_OUTGOING")
EMAIL_SEND_TO = utils.Params.GetString("EMAIL_SEND_TO")
EMAIL_PORT = utils.ToInt(utils.Params.GetString("EMAIL_PORT"))
2020-03-25 18:46:50 +00:00
db, err := database.OpenTester()
require.Nil(t, err)
db.AutoMigrate(&notifications.Notification{})
notifications.SetDB(db)
core.Example()
2020-03-25 18:46:50 +00:00
if EMAIL_HOST == "" || EMAIL_USER == "" || EMAIL_PASS == "" {
t.Log("email notifier testing skipped, missing EMAIL_ environment variables")
t.SkipNow()
}
t.Run("New email", func(t *testing.T) {
email.Host = EMAIL_HOST
email.Username = EMAIL_USER
email.Password = EMAIL_PASS
email.Var1 = EMAIL_OUTGOING
email.Var2 = EMAIL_SEND_TO
email.Port = int(EMAIL_PORT)
email.Delay = time.Duration(100 * time.Millisecond)
email.Enabled = null.NewNullBool(true)
Add(email)
assert.Equal(t, "Hunter Long", email.Author)
assert.Equal(t, EMAIL_HOST, email.Host)
})
t.Run("email Within Limits", func(t *testing.T) {
ok := email.CanSend()
assert.True(t, ok)
})
t.Run("email OnSave", func(t *testing.T) {
_, err := email.OnSave()
assert.Nil(t, err)
})
2020-03-25 18:46:50 +00:00
t.Run("email OnFailure", func(t *testing.T) {
_, err := email.OnFailure(services.Example(false), failures.Example())
2020-03-25 18:46:50 +00:00
assert.Nil(t, err)
})
t.Run("email OnSuccess", func(t *testing.T) {
_, err := email.OnSuccess(services.Example(false))
2020-03-25 18:46:50 +00:00
assert.Nil(t, err)
})
t.Run("email Check Back Online", func(t *testing.T) {
assert.True(t, services.Example(true).Online)
2020-03-25 18:46:50 +00:00
})
t.Run("email OnSuccess Again", func(t *testing.T) {
_, err := email.OnSuccess(services.Example(true))
2020-03-25 18:46:50 +00:00
assert.Nil(t, err)
})
t.Run("email Test", func(t *testing.T) {
2020-04-13 11:30:16 +00:00
_, err := email.OnTest()
2020-03-25 18:46:50 +00:00
assert.Nil(t, err)
})
}