statping/notifiers/slack_test.go

71 lines
1.7 KiB
Go
Raw Permalink 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-04-16 09:57:00 +00:00
"github.com/statping/statping/utils"
2020-03-25 18:46:50 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
"time"
)
var (
SLACK_URL string
)
func TestSlackNotifier(t *testing.T) {
2020-06-04 22:59:31 +00:00
err := utils.InitLogs()
require.Nil(t, err)
2020-07-27 00:19:26 +00:00
t.Parallel()
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
2020-04-16 09:57:00 +00:00
SLACK_URL = utils.Params.GetString("SLACK_URL")
2020-03-25 18:46:50 +00:00
if SLACK_URL == "" {
t.Log("slack notifier testing skipped, missing SLACK_URL environment variable")
t.SkipNow()
}
slacker.Host = null.NewNullString(SLACK_URL)
slacker.Enabled = null.NewNullBool(true)
2020-03-25 18:46:50 +00:00
t.Run("Load slack", func(t *testing.T) {
slacker.Host = null.NewNullString(SLACK_URL)
slacker.Delay = 100 * time.Millisecond
2020-03-25 18:46:50 +00:00
slacker.Limits = 3
Add(slacker)
assert.Equal(t, "Hunter Long", slacker.Author)
assert.Equal(t, SLACK_URL, slacker.Host.String)
2020-03-25 18:46:50 +00:00
})
t.Run("slack Within Limits", func(t *testing.T) {
ok := slacker.CanSend()
assert.True(t, ok)
})
t.Run("slack OnSave", func(t *testing.T) {
_, err := slacker.OnSave()
assert.Nil(t, err)
})
2020-03-25 18:46:50 +00:00
t.Run("slack OnFailure", func(t *testing.T) {
_, err := slacker.OnFailure(services.Example(false), failures.Example())
2020-03-25 18:46:50 +00:00
assert.Nil(t, err)
})
t.Run("slack OnSuccess", func(t *testing.T) {
_, err := slacker.OnSuccess(services.Example(true))
2020-03-25 18:46:50 +00:00
assert.Nil(t, err)
})
}