statping/handlers/notifiers_test.go

199 lines
9.0 KiB
Go
Raw Permalink Normal View History

2020-03-04 10:29:00 +00:00
package handlers
import (
2020-10-12 22:30:21 +00:00
"testing"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/notifiers"
"github.com/statping/statping/types/services"
2020-07-20 18:47:01 +00:00
"github.com/statping/statping/utils"
2020-03-08 18:13:27 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2020-03-04 10:29:00 +00:00
)
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
}
func TestAuthenticatedNotifierRoutes(t *testing.T) {
2020-07-20 18:47:01 +00:00
slackWebhookUrl := utils.Params.GetString("SLACK_URL")
2020-04-19 08:03:50 +00:00
tests := []HTTPTest{
{
Name: "No Authentication - View All Notifiers",
URL: "/api/notifiers",
Method: "GET",
ExpectedStatus: 401,
BeforeTest: UnsetTestENV,
},
2020-07-20 18:47:01 +00:00
{
Name: "View All Notifiers",
URL: "/api/notifiers",
Method: "GET",
ExpectedStatus: 200,
BeforeTest: SetTestENV,
},
2020-04-19 08:03:50 +00:00
{
Name: "No Authentication - View Notifier",
URL: "/api/notifier/slack",
Method: "GET",
ExpectedStatus: 401,
BeforeTest: UnsetTestENV,
},
{
2020-07-20 18:47:01 +00:00
Name: "View Notifier",
2020-04-19 08:03:50 +00:00
URL: "/api/notifier/slack",
2020-07-20 18:47:01 +00:00
Method: "GET",
ExpectedStatus: 200,
BeforeTest: func(t *testing.T) error {
notif := services.FindNotifier("slack")
require.NotNil(t, notif)
assert.Equal(t, "slack", notif.Method)
return SetTestENV(t)
},
2020-07-20 18:47:01 +00:00
},
{
Name: "No Authentication - Update Notifier",
URL: "/api/notifier/slack",
Method: "POST",
Body: `{
"method": "slack",
"host": "` + slackWebhookUrl + `",
"enabled": true,
"limits": 55
}`,
2020-04-19 08:03:50 +00:00
ExpectedStatus: 401,
BeforeTest: UnsetTestENV,
},
2020-07-20 18:47:01 +00:00
{
Name: "Update Notifier",
URL: "/api/notifier/slack",
Method: "POST",
Body: `{
"method": "slack",
"host": "` + slackWebhookUrl + `",
"enabled": true,
"limits": 55
}`,
ExpectedStatus: 200,
BeforeTest: func(t *testing.T) error {
notif := services.FindNotifier("slack")
require.NotNil(t, notif)
assert.Equal(t, "slack", notif.Method)
assert.False(t, notif.Enabled.Bool)
return SetTestENV(t)
},
AfterTest: func(t *testing.T) error {
notif := services.FindNotifier("slack")
require.NotNil(t, notif)
assert.Equal(t, "slack", notif.Method)
assert.True(t, notif.Enabled.Bool)
return UnsetTestENV(t)
},
2020-07-20 18:47:01 +00:00
},
{
Name: "Test Notifier (OnSuccess)",
URL: "/api/notifier/slack/test",
Method: "POST",
Body: `{
"method": "success",
"notifier": {
"enabled": false,
"limits": 60,
"method": "slack",
"host": "` + slackWebhookUrl + `",
"success_data": "{\n \"blocks\": [{\n \"type\": \"section\",\n \"text\": {\n \"type\": \"mrkdwn\",\n \"text\": \"The service {{.Service.Name}} is back online.\"\n }\n }, {\n \"type\": \"actions\",\n \"elements\": [{\n \"type\": \"button\",\n \"text\": {\n \"type\": \"plain_text\",\n \"text\": \"View Service\",\n \"emoji\": true\n },\n \"style\": \"primary\",\n \"url\": \"{{.Core.Domain}}/service/{{.Service.Id}}\"\n }, {\n \"type\": \"button\",\n \"text\": {\n \"type\": \"plain_text\",\n \"text\": \"Go to Statping\",\n \"emoji\": true\n },\n \"url\": \"{{.Core.Domain}}\"\n }]\n }]\n}",
2020-07-30 23:14:41 +00:00
"failure_data": "{\n \"blocks\": [{\n \"type\": \"section\",\n \"text\": {\n \"type\": \"mrkdwn\",\n \"text\": \":warning: The service {{.Service.Name}} is currently offline! :warning:\"\n }\n }, {\n \"type\": \"divider\"\n }, {\n \"type\": \"section\",\n \"fields\": [{\n \"type\": \"mrkdwn\",\n \"text\": \"*Service:*\\n{{.Service.Name}}\"\n }, {\n \"type\": \"mrkdwn\",\n \"text\": \"*URL:*\\n{{.Service.Domain}}\"\n }, {\n \"type\": \"mrkdwn\",\n \"text\": \"*Status Code:*\\n{{.Service.LastStatusCode}}\"\n }, {\n \"type\": \"mrkdwn\",\n \"text\": \"*When:*\\n{{.Failure.CreatedAt}}\"\n }, {\n \"type\": \"mrkdwn\",\n \"text\": \"*Downtime:*\\n{{.Service.Downtime.Human}}\"\n }, {\n \"type\": \"plain_text\",\n \"text\": \"*Error:*\\n{{.Failure.Issue}}\"\n }]\n }, {\n \"type\": \"divider\"\n }, {\n \"type\": \"actions\",\n \"elements\": [{\n \"type\": \"button\",\n \"text\": {\n \"type\": \"plain_text\",\n \"text\": \"View Offline Service\",\n \"emoji\": true\n },\n \"style\": \"danger\",\n \"url\": \"{{.Core.Domain}}/service/{{.Service.Id}}\"\n }, {\n \"type\": \"button\",\n \"text\": {\n \"type\": \"plain_text\",\n \"text\": \"Go to Statping\",\n \"emoji\": true\n },\n \"url\": \"{{.Core.Domain}}\"\n }]\n }]\n}"
2020-07-20 18:47:01 +00:00
}
}`,
ExpectedStatus: 200,
ExpectedContains: []string{`"success":true`},
BeforeTest: SetTestENV,
},
{
Name: "Test Notifier (OnFailure)",
URL: "/api/notifier/slack/test",
Method: "POST",
Body: `{
"method": "failure",
"notifier": {
"enabled": false,
"limits": 60,
"method": "slack",
"host": "` + slackWebhookUrl + `",
"success_data": "{\n \"blocks\": [{\n \"type\": \"section\",\n \"text\": {\n \"type\": \"mrkdwn\",\n \"text\": \"The service {{.Service.Name}} is back online.\"\n }\n }, {\n \"type\": \"actions\",\n \"elements\": [{\n \"type\": \"button\",\n \"text\": {\n \"type\": \"plain_text\",\n \"text\": \"View Service\",\n \"emoji\": true\n },\n \"style\": \"primary\",\n \"url\": \"{{.Core.Domain}}/service/{{.Service.Id}}\"\n }, {\n \"type\": \"button\",\n \"text\": {\n \"type\": \"plain_text\",\n \"text\": \"Go to Statping\",\n \"emoji\": true\n },\n \"url\": \"{{.Core.Domain}}\"\n }]\n }]\n}",
2020-07-30 23:14:41 +00:00
"failure_data": "{\n \"blocks\": [{\n \"type\": \"section\",\n \"text\": {\n \"type\": \"mrkdwn\",\n \"text\": \":warning: The service {{.Service.Name}} is currently offline! :warning:\"\n }\n }, {\n \"type\": \"divider\"\n }, {\n \"type\": \"section\",\n \"fields\": [{\n \"type\": \"mrkdwn\",\n \"text\": \"*Service:*\\n{{.Service.Name}}\"\n }, {\n \"type\": \"mrkdwn\",\n \"text\": \"*URL:*\\n{{.Service.Domain}}\"\n }, {\n \"type\": \"mrkdwn\",\n \"text\": \"*Status Code:*\\n{{.Service.LastStatusCode}}\"\n }, {\n \"type\": \"mrkdwn\",\n \"text\": \"*When:*\\n{{.Failure.CreatedAt}}\"\n }, {\n \"type\": \"mrkdwn\",\n \"text\": \"*Downtime:*\\n{{.Service.Downtime.Human}}\"\n }, {\n \"type\": \"plain_text\",\n \"text\": \"*Error:*\\n{{.Failure.Issue}}\"\n }]\n }, {\n \"type\": \"divider\"\n }, {\n \"type\": \"actions\",\n \"elements\": [{\n \"type\": \"button\",\n \"text\": {\n \"type\": \"plain_text\",\n \"text\": \"View Offline Service\",\n \"emoji\": true\n },\n \"style\": \"danger\",\n \"url\": \"{{.Core.Domain}}/service/{{.Service.Id}}\"\n }, {\n \"type\": \"button\",\n \"text\": {\n \"type\": \"plain_text\",\n \"text\": \"Go to Statping\",\n \"emoji\": true\n },\n \"url\": \"{{.Core.Domain}}\"\n }]\n }]\n}"
2020-07-20 18:47:01 +00:00
}
}`,
ExpectedStatus: 200,
ExpectedContains: []string{`"success":true`},
BeforeTest: SetTestENV,
},
2020-04-19 08:03:50 +00:00
}
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",
2020-08-20 08:52:16 +00:00
"host": "https://hooks.slack.com/services/TTJ1B49DP/XBNU09O9M/9uI2123SUnYBuGcxLopZomz9H",
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,
2020-08-20 08:52:16 +00:00
ExpectedContains: []string{`"method":"slack"`, `"host":"https://hooks.slack.com/services/TTJ1B49DP/XBNU09O9M/9uI2123SUnYBuGcxLopZomz9H"`},
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
})
}
}