statping/notifiers/webhook_test.go

106 lines
2.9 KiB
Go
Raw Normal View History

2018-10-03 08:17:25 +00:00
// Statup
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statup
//
// 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/statup/core/notifier"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
var (
WEBHOOK_URL = "https://jsonplaceholder.typicode.com/posts"
2018-10-03 10:47:32 +00:00
webhookMessage = `{ "title": "%service.Id", "body": "%service.Name", "online": %service.Online, "userId": 19999 }`
2018-10-03 08:17:25 +00:00
fullMsg string
)
func init() {
webhook.Host = WEBHOOK_URL
webhook.Var1 = "POST"
}
func TestWebhookNotifier(t *testing.T) {
2018-10-05 05:46:49 +00:00
t.SkipNow()
2018-10-03 08:17:25 +00:00
t.Parallel()
currentCount = CountNotifiers()
2018-10-06 05:00:40 +00:00
t.Run("Load webhooker", func(t *testing.T) {
2018-10-03 08:17:25 +00:00
webhook.Host = WEBHOOK_URL
webhook.Delay = time.Duration(100 * time.Millisecond)
err := notifier.AddNotifier(webhook)
assert.Nil(t, err)
assert.Equal(t, "Hunter Long", webhook.Author)
assert.Equal(t, WEBHOOK_URL, webhook.Host)
})
2018-10-06 05:00:40 +00:00
t.Run("Load webhooker Notifier", func(t *testing.T) {
2018-10-03 08:17:25 +00:00
notifier.Load()
})
2018-10-06 05:00:40 +00:00
t.Run("webhooker Notifier Tester", func(t *testing.T) {
2018-10-03 08:17:25 +00:00
assert.True(t, webhook.CanTest())
})
2018-10-06 05:00:40 +00:00
t.Run("webhooker Replace Body Text", func(t *testing.T) {
2018-10-03 08:17:25 +00:00
fullMsg = replaceBodyText(webhookMessage, TestService, TestFailure)
2018-10-03 10:47:32 +00:00
assert.Equal(t, "{ \"title\": \"1\", \"body\": \"Interpol - All The Rage Back Home\", \"online\": false, \"userId\": 19999 }", fullMsg)
2018-10-03 08:17:25 +00:00
})
2018-10-06 05:00:40 +00:00
t.Run("webhooker Within Limits", func(t *testing.T) {
2018-10-03 08:17:25 +00:00
ok, err := webhook.WithinLimits()
assert.Nil(t, err)
assert.True(t, ok)
})
2018-10-06 05:00:40 +00:00
t.Run("webhooker OnFailure", func(t *testing.T) {
2018-10-03 08:17:25 +00:00
webhook.OnFailure(TestService, TestFailure)
assert.Len(t, webhook.Queue, 1)
})
2018-10-06 05:00:40 +00:00
t.Run("webhooker Check Offline", func(t *testing.T) {
2018-10-03 08:17:25 +00:00
assert.False(t, webhook.Online)
})
2018-10-06 05:00:40 +00:00
t.Run("webhooker OnSuccess", func(t *testing.T) {
2018-10-03 08:17:25 +00:00
webhook.OnSuccess(TestService)
assert.Len(t, webhook.Queue, 2)
})
2018-10-06 05:00:40 +00:00
t.Run("webhooker Check Back Online", func(t *testing.T) {
2018-10-03 08:17:25 +00:00
assert.True(t, webhook.Online)
})
2018-10-06 05:00:40 +00:00
t.Run("webhooker OnSuccess Again", func(t *testing.T) {
2018-10-03 08:17:25 +00:00
webhook.OnSuccess(TestService)
assert.Len(t, webhook.Queue, 2)
})
2018-10-06 05:00:40 +00:00
t.Run("webhooker Send", func(t *testing.T) {
2018-10-03 08:17:25 +00:00
err := webhook.Send(fullMsg)
assert.Nil(t, err)
assert.Len(t, webhook.Queue, 2)
})
2018-10-06 05:00:40 +00:00
t.Run("webhooker Queue", func(t *testing.T) {
2018-10-03 08:17:25 +00:00
go notifier.Queue(webhook)
2018-10-03 10:47:32 +00:00
time.Sleep(8 * time.Second)
2018-10-03 08:17:25 +00:00
assert.Equal(t, WEBHOOK_URL, webhook.Host)
assert.Equal(t, 1, len(webhook.Queue))
})
}