statping/handlers/notifiers_test.go

105 lines
2.5 KiB
Go
Raw Normal View History

2020-03-04 10:29:00 +00:00
package handlers
import (
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/notifiers"
"github.com/statping/statping/types/services"
2020-03-08 18:13:27 +00:00
"github.com/stretchr/testify/assert"
2020-03-04 10:29:00 +00:00
"testing"
)
2020-03-08 21:32:26 +00:00
func TestAttachment(t *testing.T) {
2020-03-15 21:36:55 +00:00
notifiers.InitNotifiers()
2020-03-08 21:32:26 +00:00
}
2020-04-19 08:03:50 +00:00
func TestUnAuthenticatedNotifierRoutes(t *testing.T) {
tests := []HTTPTest{
{
Name: "No Authentication - View All Notifiers",
URL: "/api/notifiers",
Method: "GET",
ExpectedStatus: 401,
BeforeTest: UnsetTestENV,
},
{
Name: "No Authentication - View Notifier",
URL: "/api/notifier/slack",
Method: "GET",
ExpectedStatus: 401,
BeforeTest: UnsetTestENV,
},
{
Name: "No Authentication - Update Notifier",
URL: "/api/notifier/slack",
Method: "POST",
ExpectedStatus: 401,
BeforeTest: UnsetTestENV,
},
}
for _, v := range tests {
t.Run(v.Name, func(t *testing.T) {
str, t, err := RunHTTPTest(v, t)
t.Logf("Test %s: \n %v\n", v.Name, str)
assert.Nil(t, err)
})
}
}
2020-03-04 10:29:00 +00:00
func TestApiNotifiersRoutes(t *testing.T) {
tests := []HTTPTest{
{
Name: "Statping Notifiers",
URL: "/api/notifiers",
Method: "GET",
ExpectedStatus: 200,
ResponseLen: len(services.AllNotifiers()),
2020-03-08 18:13:27 +00:00
BeforeTest: SetTestENV,
2020-03-19 01:50:53 +00:00
SecureRoute: true,
2020-03-04 10:29:00 +00:00
}, {
Name: "Statping Slack Notifier",
URL: "/api/notifier/slack",
2020-03-04 10:29:00 +00:00
Method: "GET",
ExpectedStatus: 200,
2020-03-08 18:13:27 +00:00
BeforeTest: SetTestENV,
2020-03-19 01:50:53 +00:00
SecureRoute: true,
2020-03-04 10:29:00 +00:00
}, {
Name: "Statping Update Notifier",
URL: "/api/notifier/slack",
2020-03-04 10:29:00 +00:00
Method: "POST",
Body: `{
"method": "slack",
"host": "https://slack.api/example/12345",
2020-03-08 18:13:27 +00:00
"enabled": true,
"limits": 55
}`,
2020-03-04 10:29:00 +00:00
ExpectedStatus: 200,
2020-03-08 18:13:27 +00:00
BeforeTest: SetTestENV,
2020-03-19 01:50:53 +00:00
SecureRoute: true,
}, {
Name: "Statping Slack Notifier",
URL: "/api/notifier/slack",
2020-03-19 01:50:53 +00:00
Method: "GET",
ExpectedStatus: 200,
ExpectedContains: []string{`"method":"slack"`},
2020-03-19 01:50:53 +00:00
BeforeTest: SetTestENV,
SecureRoute: true,
2020-04-19 08:03:50 +00:00
},
{
Name: "Incorrect JSON POST",
URL: "/api/notifier/slack",
Body: BadJSON,
ExpectedContains: []string{BadJSONResponse},
BeforeTest: SetTestENV,
Method: "POST",
ExpectedStatus: 422,
},
}
2020-03-04 10:29:00 +00:00
for _, v := range tests {
t.Run(v.Name, func(t *testing.T) {
_, t, err := RunHTTPTest(v, t)
2020-03-08 18:13:27 +00:00
assert.Nil(t, err)
2020-03-04 10:29:00 +00:00
})
}
}