2018-12-04 05:57:11 +00:00
|
|
|
// Statping
|
2018-10-03 08:17:25 +00:00
|
|
|
// Copyright (C) 2018. Hunter Long and the project contributors
|
|
|
|
// Written by Hunter Long <info@socialeck.com> and the project contributors
|
|
|
|
//
|
2018-12-04 04:17:29 +00:00
|
|
|
// https://github.com/hunterlong/statping
|
2018-10-03 08:17:25 +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 (
|
2018-12-04 04:17:29 +00:00
|
|
|
"github.com/hunterlong/statping/core/notifier"
|
2018-10-03 08:17:25 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-01-29 12:58:25 +00:00
|
|
|
webhookTestUrl = "https://statping.com"
|
2018-11-10 03:42:32 +00:00
|
|
|
webhookMessage = `{"id": "%service.Id","name": "%service.Name","online": "%service.Online","issue": "%failure.Issue"}`
|
2018-10-16 08:30:10 +00:00
|
|
|
apiKey = "application/json"
|
2018-10-03 08:17:25 +00:00
|
|
|
fullMsg string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-10-25 15:13:32 +00:00
|
|
|
Webhook.Host = webhookTestUrl
|
|
|
|
Webhook.Var1 = "POST"
|
|
|
|
Webhook.Var2 = webhookMessage
|
|
|
|
Webhook.ApiKey = "application/json"
|
2018-10-03 08:17:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebhookNotifier(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
currentCount = CountNotifiers()
|
|
|
|
|
2018-10-06 05:00:40 +00:00
|
|
|
t.Run("Load webhooker", func(t *testing.T) {
|
2019-10-25 15:13:32 +00:00
|
|
|
Webhook.Host = webhookTestUrl
|
|
|
|
Webhook.Delay = time.Duration(100 * time.Millisecond)
|
|
|
|
Webhook.ApiKey = apiKey
|
|
|
|
err := notifier.AddNotifiers(Webhook)
|
2018-10-03 08:17:25 +00:00
|
|
|
assert.Nil(t, err)
|
2019-10-25 15:13:32 +00:00
|
|
|
assert.Equal(t, "Hunter Long", Webhook.Author)
|
|
|
|
assert.Equal(t, webhookTestUrl, Webhook.Host)
|
|
|
|
assert.Equal(t, apiKey, Webhook.ApiKey)
|
2018-11-10 03:42:32 +00:00
|
|
|
})
|
|
|
|
|
2018-10-06 05:00:40 +00:00
|
|
|
t.Run("webhooker Notifier Tester", func(t *testing.T) {
|
2019-10-25 15:13:32 +00:00
|
|
|
assert.True(t, Webhook.CanTest())
|
2018-10-03 08:17:25 +00:00
|
|
|
})
|
|
|
|
|
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)
|
2019-09-03 21:10:13 +00:00
|
|
|
assert.Equal(t, `{"id": "1","name": "Interpol - All The Rage Back Home","online": "true","issue": "testing"}`, 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) {
|
2019-10-25 15:13:32 +00:00
|
|
|
ok, err := Webhook.WithinLimits()
|
2018-10-03 08:17:25 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, ok)
|
|
|
|
})
|
|
|
|
|
2018-10-06 05:00:40 +00:00
|
|
|
t.Run("webhooker OnFailure", func(t *testing.T) {
|
2019-10-25 15:13:32 +00:00
|
|
|
Webhook.OnFailure(TestService, TestFailure)
|
|
|
|
assert.Len(t, Webhook.Queue, 1)
|
2018-10-03 08:17:25 +00:00
|
|
|
})
|
|
|
|
|
2018-10-06 05:00:40 +00:00
|
|
|
t.Run("webhooker OnSuccess", func(t *testing.T) {
|
2019-10-25 15:13:32 +00:00
|
|
|
Webhook.OnSuccess(TestService)
|
|
|
|
assert.Equal(t, len(Webhook.Queue), 1)
|
2018-10-03 08:17:25 +00:00
|
|
|
})
|
|
|
|
|
2018-10-06 05:00:40 +00:00
|
|
|
t.Run("webhooker Check Back Online", func(t *testing.T) {
|
2019-08-15 23:26:36 +00:00
|
|
|
assert.True(t, TestService.Online)
|
2018-10-03 08:17:25 +00:00
|
|
|
})
|
|
|
|
|
2018-10-06 05:00:40 +00:00
|
|
|
t.Run("webhooker OnSuccess Again", func(t *testing.T) {
|
2019-10-25 15:13:32 +00:00
|
|
|
Webhook.OnSuccess(TestService)
|
|
|
|
assert.Equal(t, len(Webhook.Queue), 1)
|
2018-10-03 08:17:25 +00:00
|
|
|
})
|
|
|
|
|
2018-10-06 05:00:40 +00:00
|
|
|
t.Run("webhooker Send", func(t *testing.T) {
|
2019-10-25 15:13:32 +00:00
|
|
|
err := Webhook.Send(fullMsg)
|
2018-12-10 05:43:15 +00:00
|
|
|
assert.Nil(t, err)
|
2019-10-25 15:13:32 +00:00
|
|
|
assert.Equal(t, len(Webhook.Queue), 1)
|
2018-10-03 08:17:25 +00:00
|
|
|
})
|
|
|
|
|
2018-10-06 05:00:40 +00:00
|
|
|
t.Run("webhooker Queue", func(t *testing.T) {
|
2019-10-25 15:13:32 +00:00
|
|
|
go notifier.Queue(Webhook)
|
2018-10-03 10:47:32 +00:00
|
|
|
time.Sleep(8 * time.Second)
|
2019-10-25 15:13:32 +00:00
|
|
|
assert.Equal(t, webhookTestUrl, Webhook.Host)
|
|
|
|
assert.Equal(t, len(Webhook.Queue), 0)
|
2018-10-03 08:17:25 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|