statping/handlers/services_test.go

256 lines
7.2 KiB
Go
Raw Normal View History

2020-03-04 10:29:00 +00:00
package handlers
import (
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"
)
func TestApiServiceRoutes(t *testing.T) {
2020-03-19 01:50:53 +00:00
since := utils.Now().Add(-30 * types.Day)
startEndQuery := fmt.Sprintf("?start=%d&end=%d", since.Unix(), utils.Now().Unix())
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,
ResponseLen: 5,
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-03-04 10:29:00 +00:00
if count != 5 {
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-03-29 01:21:32 +00:00
ResponseLen: 4,
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())
if count != 5 {
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
},
{
Name: "Statping Private Service 1",
2020-03-29 01:21:32 +00:00
URL: "/api/services/2",
2020-03-08 18:13:27 +00:00
Method: "GET",
2020-03-29 01:21:32 +00:00
ExpectedContains: []string{`"error":"not authenticated"`},
2020-03-08 18:13:27 +00:00
ExpectedStatus: 200,
2020-03-29 01:21:32 +00:00
BeforeTest: UnsetTestENV,
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",
URL: "/api/services/1/failures",
Method: "GET",
ResponseLen: 2,
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
{
Name: "Statping Service 1 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-03-19 01:50:53 +00:00
ResponseLen: 73,
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",
2020-03-19 01:50:53 +00:00
ResponseLen: 73,
2020-03-04 10:29:00 +00:00
ExpectedStatus: 200,
},
{
2020-03-19 01:50:53 +00:00
Name: "Statping Service 1 Failure Data - 12 Hour",
URL: "/api/services/1/failure_data" + startEndQuery + "&group=24h",
Method: "GET",
ResponseLen: 1,
ExpectedStatus: 200,
},
{
Name: "Statping Service 1 Failure Data - 12 Hour",
URL: "/api/services/1/failure_data" + startEndQuery + "&group=12h",
Method: "GET",
ResponseLen: 1,
ExpectedStatus: 200,
},
{
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
ResponseLen: 1,
ExpectedStatus: 200,
},
{
Name: "Statping Service 1 Failure Data - 15 Minute",
URL: "/api/services/1/failure_data" + startEndQuery + "&group=15m",
Method: "GET",
ResponseLen: 1,
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-03-19 01:50:53 +00:00
ResponseLen: 73,
2020-03-04 10:29:00 +00:00
ExpectedStatus: 200,
},
{
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-03-19 01:50:53 +00:00
ResponseLen: 1,
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-03-19 01:50:53 +00:00
ExpectedContains: []string{`"status":"success","type":"service","method":"create"`, `"public":false`, `"group_id":1`},
FuncTest: func(t *testing.T) error {
2020-03-04 14:20:47 +00:00
count := len(services.Services())
2020-03-04 10:29:00 +00:00
if count != 6 {
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,
ExpectedContains: []string{`"status":"success"`, `"name":"Updated New Service"`, `"method":"update"`},
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,
ExpectedContains: []string{`"status":"success"`, `"method":"delete_failures"`},
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,
ExpectedContains: []string{`"status":"success"`, `"method":"delete"`},
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-03-04 10:29:00 +00:00
if count != 5 {
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
}}
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
})
}
}