statping/notifiers/telegram_test.go

82 lines
2.1 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-06-04 22:59:31 +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 (
telegramToken string
telegramChannel string
)
func TestTelegramNotifier(t *testing.T) {
2020-07-27 00:19:26 +00:00
err := utils.InitLogs()
require.Nil(t, err)
t.Parallel()
telegramToken = utils.Params.GetString("TELEGRAM_TOKEN")
telegramChannel = utils.Params.GetString("TELEGRAM_CHANNEL")
if telegramToken == "" || telegramChannel == "" {
t.Log("Telegram notifier testing skipped, missing TELEGRAM_TOKEN and TELEGRAM_CHANNEL environment variable")
t.SkipNow()
}
Telegram.ApiSecret = null.NewNullString(telegramToken)
Telegram.Var1 = null.NewNullString(telegramChannel)
2020-06-04 23:18:37 +00:00
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
t.Run("Load Telegram", func(t *testing.T) {
Telegram.ApiSecret = null.NewNullString(telegramToken)
Telegram.Var1 = null.NewNullString(telegramChannel)
2020-03-25 18:46:50 +00:00
Telegram.Delay = time.Duration(1 * time.Second)
Telegram.Enabled = null.NewNullBool(true)
Add(Telegram)
assert.Equal(t, "Hunter Long", Telegram.Author)
assert.Equal(t, telegramToken, Telegram.ApiSecret.String)
assert.Equal(t, telegramChannel, Telegram.Var1.String)
2020-03-25 18:46:50 +00:00
})
t.Run("Telegram Within Limits", func(t *testing.T) {
assert.True(t, Telegram.CanSend())
})
t.Run("Telegram OnSave", func(t *testing.T) {
_, err := Telegram.OnSave()
assert.Nil(t, err)
})
2020-03-25 18:46:50 +00:00
t.Run("Telegram OnFailure", func(t *testing.T) {
_, err := Telegram.OnFailure(services.Example(false), failures.Example())
2020-03-25 18:46:50 +00:00
assert.Nil(t, err)
})
t.Run("Telegram OnSuccess", func(t *testing.T) {
_, err := Telegram.OnSuccess(services.Example(true))
2020-03-25 18:46:50 +00:00
assert.Nil(t, err)
})
t.Run("Telegram Test", func(t *testing.T) {
2020-04-13 11:30:16 +00:00
_, err := Telegram.OnTest()
2020-03-25 18:46:50 +00:00
assert.Nil(t, err)
})
}