statping/notifiers/telegram_test.go

109 lines
3.1 KiB
Go
Raw Normal View History

2018-12-10 05:43:15 +00:00
// Statup
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
2019-01-09 04:20:43 +00:00
// https://github.com/hunterlong/statping
2018-12-10 05:43:15 +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 (
"github.com/hunterlong/statping/core/notifier"
"github.com/stretchr/testify/assert"
"os"
"testing"
"time"
)
var (
telegramToken string
telegramChannel string
telegramMessage = "The Telegram notifier on Statping has been tested!"
)
func init() {
telegramToken = os.Getenv("TELEGRAM_TOKEN")
telegramChannel = os.Getenv("TELEGRAM_CHANNEL")
telegramNotifier.ApiSecret = telegramToken
telegramNotifier.Var1 = telegramChannel
}
func TestTelegramNotifier(t *testing.T) {
t.Parallel()
if telegramToken == "" || telegramChannel == "" {
t.Log("Telegram notifier testing skipped, missing TELEGRAM_TOKEN and TELEGRAM_CHANNEL environment variable")
t.SkipNow()
}
currentCount = CountNotifiers()
t.Run("Load Telegram", func(t *testing.T) {
telegramNotifier.ApiSecret = telegramToken
telegramNotifier.Var1 = telegramChannel
telegramNotifier.Delay = time.Duration(1 * time.Second)
err := notifier.AddNotifier(telegramNotifier)
assert.Nil(t, err)
assert.Equal(t, "Hunter Long", telegramNotifier.Author)
assert.Equal(t, telegramToken, telegramNotifier.ApiSecret)
assert.Equal(t, telegramChannel, telegramNotifier.Var1)
})
t.Run("Load Telegram Notifier", func(t *testing.T) {
notifier.Load()
})
t.Run("Telegram Within Limits", func(t *testing.T) {
ok, err := telegramNotifier.WithinLimits()
assert.Nil(t, err)
assert.True(t, ok)
})
t.Run("Telegram OnFailure", func(t *testing.T) {
telegramNotifier.OnFailure(TestService, TestFailure)
assert.Equal(t, 1, len(telegramNotifier.Queue))
})
t.Run("Telegram Check Offline", func(t *testing.T) {
assert.False(t, telegramNotifier.Online)
})
t.Run("Telegram OnSuccess", func(t *testing.T) {
telegramNotifier.OnSuccess(TestService)
assert.Equal(t, 1, len(telegramNotifier.Queue))
})
t.Run("Telegram Check Back Online", func(t *testing.T) {
assert.True(t, telegramNotifier.Online)
})
t.Run("Telegram OnSuccess Again", func(t *testing.T) {
telegramNotifier.OnSuccess(TestService)
assert.Equal(t, 1, len(telegramNotifier.Queue))
})
t.Run("Telegram Send", func(t *testing.T) {
err := telegramNotifier.Send(telegramMessage)
assert.Nil(t, err)
})
t.Run("Telegram Test", func(t *testing.T) {
err := telegramNotifier.OnTest()
assert.Nil(t, err)
})
t.Run("Telegram Queue", func(t *testing.T) {
go notifier.Queue(telegramNotifier)
time.Sleep(3 * time.Second)
assert.Equal(t, telegramToken, telegramNotifier.ApiSecret)
assert.Equal(t, 0, len(telegramNotifier.Queue))
})
}