statping/notifiers/webhook_test.go

76 lines
1.9 KiB
Go
Raw Normal View History

2020-03-25 18:46:50 +00:00
package notifiers
import (
2020-10-12 22:30:21 +00:00
"testing"
2020-03-25 18:46:50 +00:00
"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"
)
var (
2020-03-25 19:23:50 +00:00
webhookTestUrl = "https://statping.com"
2020-03-25 18:46:50 +00:00
webhookMessage = `{"id": {{.Service.Id}},"name": "{{.Service.Name}}","online": {{.Service.Online}},"issue": "{{.Failure.Issue}}"}`
apiKey = "application/json"
fullMsg string
)
func TestWebhookNotifier(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()
t.SkipNow()
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 webhooker", func(t *testing.T) {
Webhook.Host = null.NewNullString(webhookTestUrl)
Webhook.Var1 = null.NewNullString("POST")
Webhook.Var2 = null.NewNullString(webhookMessage)
Webhook.ApiKey = null.NewNullString("application/json")
2020-03-25 18:46:50 +00:00
Webhook.Enabled = null.NewNullBool(true)
Add(Webhook)
assert.Equal(t, "Hunter Long", Webhook.Author)
assert.Equal(t, webhookTestUrl, Webhook.Host)
assert.Equal(t, apiKey, Webhook.ApiKey)
})
t.Run("webhooker Notifier Tester", func(t *testing.T) {
assert.True(t, Webhook.CanSend())
})
t.Run("webhooker OnSave", func(t *testing.T) {
_, err := Webhook.OnSave()
assert.Nil(t, err)
})
2020-03-25 18:46:50 +00:00
t.Run("webhooker OnFailure", func(t *testing.T) {
_, err := Webhook.OnFailure(services.Example(false), failures.Example())
2020-03-25 18:46:50 +00:00
assert.Nil(t, err)
})
t.Run("webhooker OnSuccess", func(t *testing.T) {
_, err := Webhook.OnSuccess(services.Example(true))
2020-03-25 18:46:50 +00:00
assert.Nil(t, err)
})
t.Run("webhooker Send", func(t *testing.T) {
err := Webhook.Send(fullMsg)
assert.Nil(t, err)
})
}