additional testing

pull/755/head
hunterlong 2020-07-20 11:47:01 -07:00
parent b1255d7152
commit 6f9cb2141b
1 changed files with 76 additions and 2 deletions

View File

@ -3,6 +3,7 @@ package handlers
import (
"github.com/statping/statping/notifiers"
"github.com/statping/statping/types/services"
"github.com/statping/statping/utils"
"github.com/stretchr/testify/assert"
"testing"
)
@ -12,6 +13,8 @@ func TestAttachment(t *testing.T) {
}
func TestUnAuthenticatedNotifierRoutes(t *testing.T) {
slackWebhookUrl := utils.Params.GetString("SLACK_URL")
tests := []HTTPTest{
{
Name: "No Authentication - View All Notifiers",
@ -20,6 +23,13 @@ func TestUnAuthenticatedNotifierRoutes(t *testing.T) {
ExpectedStatus: 401,
BeforeTest: UnsetTestENV,
},
{
Name: "View All Notifiers",
URL: "/api/notifiers",
Method: "GET",
ExpectedStatus: 200,
BeforeTest: SetTestENV,
},
{
Name: "No Authentication - View Notifier",
URL: "/api/notifier/slack",
@ -28,12 +38,76 @@ func TestUnAuthenticatedNotifierRoutes(t *testing.T) {
BeforeTest: UnsetTestENV,
},
{
Name: "No Authentication - Update Notifier",
Name: "View Notifier",
URL: "/api/notifier/slack",
Method: "POST",
Method: "GET",
ExpectedStatus: 200,
BeforeTest: SetTestENV,
},
{
Name: "No Authentication - Update Notifier",
URL: "/api/notifier/slack",
Method: "POST",
Body: `{
"method": "slack",
"host": "` + slackWebhookUrl + `",
"enabled": true,
"limits": 55
}`,
ExpectedStatus: 401,
BeforeTest: UnsetTestENV,
},
{
Name: "Update Notifier",
URL: "/api/notifier/slack",
Method: "POST",
Body: `{
"method": "slack",
"host": "` + slackWebhookUrl + `",
"enabled": true,
"limits": 55
}`,
ExpectedStatus: 200,
BeforeTest: SetTestENV,
},
{
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}",
"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.DowntimeAgo}}\"\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}"
}
}`,
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}",
"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.DowntimeAgo}}\"\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}"
}
}`,
ExpectedStatus: 200,
ExpectedContains: []string{`"success":true`},
BeforeTest: SetTestENV,
},
}
for _, v := range tests {