statping/handlers/services_test.go

337 lines
9.2 KiB
Go
Raw Normal View History

2020-03-04 10:29:00 +00:00
package handlers
import (
2020-04-16 17:32:54 +00:00
"encoding/json"
2020-03-19 01:50:53 +00:00
"fmt"
2020-03-04 10:29:00 +00:00
"github.com/pkg/errors"
2020-03-19 01:50:53 +00:00
"github.com/statping/statping/types"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/types/services"
2020-03-19 01:50:53 +00:00
"github.com/statping/statping/utils"
2020-03-08 18:13:27 +00:00
"github.com/stretchr/testify/assert"
2020-03-19 01:50:53 +00:00
"github.com/stretchr/testify/require"
2020-03-04 10:29:00 +00:00
"testing"
2020-04-16 17:32:54 +00:00
"time"
2020-03-04 10:29:00 +00:00
)
2020-04-19 08:03:50 +00:00
func TestUnAuthenticatedServicesRoutes(t *testing.T) {
tests := []HTTPTest{
{
Name: "No Authentication - New Service",
URL: "/api/services",
Method: "POST",
ExpectedStatus: 401,
BeforeTest: UnsetTestENV,
},
{
Name: "No Authentication - Update Service",
URL: "/api/services/1",
Method: "POST",
ExpectedStatus: 401,
BeforeTest: UnsetTestENV,
},
{
Name: "No Authentication - Delete Service",
URL: "/api/services/1",
Method: "DELETE",
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 TestApiServiceRoutes(t *testing.T) {
2020-03-19 01:50:53 +00:00
since := utils.Now().Add(-30 * types.Day)
2020-04-16 17:32:54 +00:00
end := utils.Now().Add(-30 * time.Minute)
startEndQuery := fmt.Sprintf("?start=%d&end=%d", since.Unix(), end.Unix()+15)
2020-03-19 01:50:53 +00:00
2020-03-04 10:29:00 +00:00
tests := []HTTPTest{
{
2020-03-08 18:13:27 +00:00
Name: "Statping All Public and Private Services",
2020-03-04 10:29:00 +00:00
URL: "/api/services",
Method: "GET",
ExpectedContains: []string{`"name":"Google"`},
ExpectedStatus: 200,
2020-04-09 02:10:43 +00:00
ResponseLen: 6,
2020-03-08 18:13:27 +00:00
BeforeTest: SetTestENV,
2020-03-19 01:50:53 +00:00
FuncTest: func(t *testing.T) error {
2020-03-04 14:20:47 +00:00
count := len(services.Services())
2020-04-09 02:10:43 +00:00
if count != 6 {
2020-03-04 10:29:00 +00:00
return errors.Errorf("incorrect services count: %d", count)
}
return nil
},
},
{
2020-03-08 18:13:27 +00:00
Name: "Statping All Public Services",
URL: "/api/services",
Method: "GET",
ExpectedContains: []string{`"name":"Google"`},
ExpectedStatus: 200,
2020-04-09 02:10:43 +00:00
ResponseLen: 5,
2020-03-08 18:13:27 +00:00
BeforeTest: UnsetTestENV,
2020-03-19 01:50:53 +00:00
FuncTest: func(t *testing.T) error {
2020-03-08 18:13:27 +00:00
count := len(services.Services())
2020-04-09 02:10:43 +00:00
if count != 6 {
2020-03-08 18:13:27 +00:00
return errors.Errorf("incorrect services count: %d", count)
}
return nil
},
},
{
Name: "Statping Public Service 1",
URL: "/api/services/1",
Method: "GET",
ExpectedContains: []string{`"name":"Google"`},
ExpectedStatus: 200,
2020-03-29 01:21:32 +00:00
BeforeTest: UnsetTestENV,
2020-03-08 18:13:27 +00:00
},
{
2020-04-17 03:21:17 +00:00
Name: "Statping Private Service 6",
2020-04-09 02:10:43 +00:00
URL: "/api/services/6",
2020-03-08 18:13:27 +00:00
Method: "GET",
2020-04-17 03:21:17 +00:00
ExpectedContains: []string{`"error":"user not authenticated"`},
ExpectedStatus: 401,
2020-03-29 01:21:32 +00:00
BeforeTest: UnsetTestENV,
2020-03-08 18:13:27 +00:00
},
2020-04-17 03:21:17 +00:00
{
Name: "Statping Authenticated Private Service 6",
URL: "/api/services/6",
Method: "GET",
ExpectedStatus: 200,
BeforeTest: SetTestENV,
},
2020-03-08 18:13:27 +00:00
{
Name: "Statping Service 1 with Private responses",
2020-03-04 10:29:00 +00:00
URL: "/api/services/1",
Method: "GET",
ExpectedContains: []string{`"name":"Google"`},
ExpectedStatus: 200,
2020-03-08 18:13:27 +00:00
BeforeTest: SetTestENV,
2020-03-04 10:29:00 +00:00
},
2020-03-19 01:50:53 +00:00
{
Name: "Statping Service Failures",
2020-04-16 17:32:54 +00:00
URL: "/api/services/1/failures" + startEndQuery,
2020-03-19 01:50:53 +00:00
Method: "GET",
2020-04-16 17:32:54 +00:00
GreaterThan: 120,
ExpectedStatus: 200,
},
{
Name: "Statping Service 1 Hits",
URL: "/api/services/1/hits" + startEndQuery,
Method: "GET",
GreaterThan: 8580,
ExpectedStatus: 200,
},
{
Name: "Statping Service 2 Hits",
URL: "/api/services/2/hits" + startEndQuery,
Method: "GET",
GreaterThan: 8580,
2020-03-19 01:50:53 +00:00
ExpectedStatus: 200,
},
{
Name: "Statping Service Failures Limited",
URL: "/api/services/1/failures?limit=1",
Method: "GET",
ResponseLen: 1,
ExpectedStatus: 200,
},
2020-03-04 10:29:00 +00:00
{
2020-04-16 17:32:54 +00:00
Name: "Statping Service 1 Hits Data",
2020-03-19 01:50:53 +00:00
URL: "/api/services/1/hits_data" + startEndQuery,
2020-03-04 10:29:00 +00:00
Method: "GET",
2020-04-16 17:32:54 +00:00
GreaterThan: 70,
2020-03-04 10:29:00 +00:00
ExpectedStatus: 200,
},
{
Name: "Statping Service 1 Ping Data",
2020-03-19 01:50:53 +00:00
URL: "/api/services/1/ping_data" + startEndQuery,
2020-03-04 10:29:00 +00:00
Method: "GET",
ExpectedStatus: 200,
2020-04-16 17:32:54 +00:00
GreaterThan: 70,
2020-03-04 10:29:00 +00:00
},
{
2020-03-31 20:38:43 +00:00
Name: "Statping Service 1 Failure Data - 24 Hour",
2020-03-19 01:50:53 +00:00
URL: "/api/services/1/failure_data" + startEndQuery + "&group=24h",
Method: "GET",
ExpectedStatus: 200,
2020-04-17 03:21:17 +00:00
GreaterThan: 3,
2020-03-19 01:50:53 +00:00
},
{
Name: "Statping Service 1 Failure Data - 12 Hour",
URL: "/api/services/1/failure_data" + startEndQuery + "&group=12h",
Method: "GET",
ExpectedStatus: 200,
2020-04-17 03:21:17 +00:00
GreaterThan: 6,
2020-03-19 01:50:53 +00:00
},
{
Name: "Statping Service 1 Failure Data - 1 Hour",
URL: "/api/services/1/failure_data" + startEndQuery + "&group=1h",
2020-03-04 10:29:00 +00:00
Method: "GET",
2020-03-19 01:50:53 +00:00
ExpectedStatus: 200,
2020-04-16 17:32:54 +00:00
GreaterThan: 70,
2020-03-19 01:50:53 +00:00
},
{
Name: "Statping Service 1 Failure Data - 15 Minute",
URL: "/api/services/1/failure_data" + startEndQuery + "&group=15m",
Method: "GET",
2020-04-16 17:32:54 +00:00
GreaterThan: 120,
2020-03-04 10:29:00 +00:00
ExpectedStatus: 200,
},
{
Name: "Statping Service 1 Hits",
2020-03-19 01:50:53 +00:00
URL: "/api/services/1/hits_data" + startEndQuery,
2020-03-04 10:29:00 +00:00
Method: "GET",
2020-04-16 17:32:54 +00:00
GreaterThan: 70,
ExpectedStatus: 200,
},
{
Name: "Statping Service 1 Uptime",
URL: "/api/services/1/uptime_data" + startEndQuery,
Method: "GET",
2020-03-04 10:29:00 +00:00
ExpectedStatus: 200,
2020-04-16 17:32:54 +00:00
ResponseFunc: func(t *testing.T, resp []byte) error {
var uptime *services.UptimeSeries
if err := json.Unmarshal(resp, &uptime); err != nil {
return err
}
2020-04-17 03:21:17 +00:00
assert.GreaterOrEqual(t, uptime.Uptime, int64(200000000))
2020-04-16 17:32:54 +00:00
return nil
},
2020-03-04 10:29:00 +00:00
},
{
2020-03-19 01:50:53 +00:00
Name: "Statping Service 1 Failure Data",
URL: "/api/services/1/failure_data" + startEndQuery,
2020-03-04 10:29:00 +00:00
Method: "GET",
2020-04-16 17:32:54 +00:00
GreaterThan: 70,
2020-03-04 10:29:00 +00:00
ExpectedStatus: 200,
},
{
Name: "Statping Reorder Services",
2020-03-19 01:50:53 +00:00
URL: "/api/reorder/services",
2020-03-04 10:29:00 +00:00
Method: "POST",
2020-03-13 04:06:06 +00:00
Body: `[{"service":1,"order":1},{"service":4,"order":2},{"service":2,"order":3},{"service":3,"order":4}]`,
2020-03-04 10:29:00 +00:00
ExpectedStatus: 200,
HttpHeaders: []string{"Content-Type=application/json"},
2020-03-19 01:50:53 +00:00
SecureRoute: true,
2020-03-04 10:29:00 +00:00
},
{
Name: "Statping Create Service",
URL: "/api/services",
HttpHeaders: []string{"Content-Type=application/json"},
Method: "POST",
Body: `{
2020-03-19 01:50:53 +00:00
"name": "New Private Service",
2020-03-13 04:06:06 +00:00
"domain": "https://statping.com",
"expected": "",
"expected_status": 200,
"check_interval": 30,
"type": "http",
2020-03-19 01:50:53 +00:00
"public": false,
"group_id": 1,
2020-03-13 04:06:06 +00:00
"method": "GET",
"post_data": "",
"port": 0,
"timeout": 30,
"order_id": 0
}`,
2020-03-04 10:29:00 +00:00
ExpectedStatus: 200,
2020-04-16 17:32:54 +00:00
ExpectedContains: []string{Success, `"type":"service","method":"create"`, `"public":false`, `"group_id":1`},
2020-03-19 01:50:53 +00:00
FuncTest: func(t *testing.T) error {
2020-03-04 14:20:47 +00:00
count := len(services.Services())
2020-04-09 02:10:43 +00:00
if count != 7 {
2020-03-04 10:29:00 +00:00
return errors.Errorf("incorrect services count: %d", count)
}
return nil
},
2020-03-19 01:50:53 +00:00
SecureRoute: true,
2020-03-04 10:29:00 +00:00
},
{
Name: "Statping Update Service",
URL: "/api/services/1",
HttpHeaders: []string{"Content-Type=application/json"},
Method: "POST",
Body: `{
2020-03-13 04:06:06 +00:00
"name": "Updated New Service",
"domain": "https://google.com",
"expected": "",
"expected_status": 200,
"check_interval": 60,
"type": "http",
"method": "GET",
"post_data": "",
"port": 0,
"timeout": 10,
"order_id": 0
}`,
2020-03-04 10:29:00 +00:00
ExpectedStatus: 200,
2020-04-19 08:03:50 +00:00
ExpectedContains: []string{Success, `"name":"Updated New Service"`, MethodUpdate},
2020-03-19 01:50:53 +00:00
FuncTest: func(t *testing.T) error {
item, err := services.Find(1)
require.Nil(t, err)
if item.Interval != 60 {
return errors.Errorf("incorrect service check interval: %d", item.Interval)
}
return nil
},
SecureRoute: true,
},
{
Name: "Statping Delete Failures",
URL: "/api/services/1/failures",
Method: "DELETE",
ExpectedStatus: 200,
2020-04-16 17:32:54 +00:00
ExpectedContains: []string{Success, `"method":"delete_failures"`},
2020-03-19 01:50:53 +00:00
FuncTest: func(t *testing.T) error {
item, err := services.Find(1)
require.Nil(t, err)
fails := item.AllFailures().Count()
if fails != 0 {
return errors.Errorf("incorrect service failures count: %d", fails)
}
return nil
},
SecureRoute: true,
2020-03-04 10:29:00 +00:00
},
{
Name: "Statping Delete Service",
URL: "/api/services/1",
Method: "DELETE",
ExpectedStatus: 200,
2020-04-19 08:03:50 +00:00
ExpectedContains: []string{Success, MethodDelete},
2020-03-19 01:50:53 +00:00
FuncTest: func(t *testing.T) error {
2020-03-04 14:20:47 +00:00
count := len(services.Services())
2020-04-09 02:10:43 +00:00
if count != 6 {
2020-03-04 10:29:00 +00:00
return errors.Errorf("incorrect services count: %d", count)
}
return nil
},
2020-03-19 01:50:53 +00:00
SecureRoute: true,
2020-04-19 08:03:50 +00:00
},
{
Name: "Incorrect JSON POST",
URL: "/api/services",
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
})
}
}