statping/handlers/api_test.go

368 lines
8.8 KiB
Go
Raw Normal View History

2019-01-15 01:28:00 +00:00
package handlers
import (
2020-03-04 10:29:00 +00:00
"encoding/json"
2019-01-15 01:28:00 +00:00
"fmt"
2020-03-08 18:13:27 +00:00
"github.com/getsentry/sentry-go"
2020-03-04 10:29:00 +00:00
"github.com/pkg/errors"
2020-03-09 18:17:55 +00:00
_ "github.com/statping/statping/notifiers"
"github.com/statping/statping/source"
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/core"
"github.com/statping/statping/types/groups"
"github.com/statping/statping/types/services"
"github.com/statping/statping/types/users"
"github.com/statping/statping/utils"
2019-01-15 01:28:00 +00:00
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
2020-03-08 01:23:41 +00:00
"os"
2019-01-15 01:28:00 +00:00
"strings"
"testing"
)
var (
dir string
)
func init() {
source.Assets()
utils.InitLogs()
dir = utils.Directory
2020-03-22 07:57:17 +00:00
core.New("test")
2019-01-15 01:28:00 +00:00
}
func TestFailedHTTPServer(t *testing.T) {
err := RunHTTPServer("missinghost", 0)
assert.Error(t, err)
}
func TestSetupRoutes(t *testing.T) {
form := url.Values{}
2020-04-16 09:57:00 +00:00
form.Add("db_host", utils.Params.GetString("DB_HOST"))
form.Add("db_user", utils.Params.GetString("DB_USER"))
form.Add("db_password", utils.Params.GetString("DB_PASS"))
form.Add("db_database", utils.Params.GetString("DB_DATABASE"))
form.Add("db_connection", utils.Params.GetString("DB_CONN"))
form.Add("db_port", utils.Params.GetString("DB_PORT"))
2019-01-15 01:28:00 +00:00
form.Add("project", "Tester")
form.Add("username", "admin")
form.Add("password", "password123")
form.Add("sample_data", "on")
form.Add("description", "This is an awesome test")
form.Add("domain", "http://localhost:8080")
form.Add("email", "info@statping.com")
tests := []HTTPTest{
{
2020-02-19 04:07:22 +00:00
Name: "Statping Check",
URL: "/api",
2019-01-15 01:28:00 +00:00
Method: "GET",
2020-02-19 04:07:22 +00:00
ExpectedStatus: 200,
2020-03-19 01:50:53 +00:00
FuncTest: func(t *testing.T) error {
2020-03-04 14:20:47 +00:00
if core.App.Setup {
2020-03-04 10:29:00 +00:00
return errors.New("core has already been setup")
}
return nil
},
2019-01-15 01:28:00 +00:00
},
{
Name: "Statping Run Setup",
2020-02-19 04:07:22 +00:00
URL: "/api/setup",
2019-01-15 01:28:00 +00:00
Method: "POST",
Body: form.Encode(),
2020-02-19 04:07:22 +00:00
ExpectedStatus: 200,
2019-01-15 01:28:00 +00:00
HttpHeaders: []string{"Content-Type=application/x-www-form-urlencoded"},
2020-04-16 09:57:00 +00:00
ExpectedFiles: []string{utils.Directory + "/config.yml"},
2020-03-19 01:50:53 +00:00
FuncTest: func(t *testing.T) error {
2020-03-04 14:20:47 +00:00
if !core.App.Setup {
2020-03-04 10:29:00 +00:00
return errors.New("core has not been setup")
}
2020-03-08 01:23:41 +00:00
if len(services.AllInOrder()) == 0 {
return errors.New("no services where found")
}
if len(users.All()) == 0 {
return errors.New("no users where found")
}
if len(groups.All()) == 0 {
return errors.New("no groups where found")
}
2020-03-04 10:29:00 +00:00
return nil
},
2020-03-13 04:06:06 +00:00
AfterTest: StopServices,
2019-01-15 01:28:00 +00:00
}}
for _, v := range tests {
t.Run(v.Name, func(t *testing.T) {
2019-01-29 12:58:25 +00:00
_, t, err := RunHTTPTest(v, t)
2019-01-15 01:28:00 +00:00
assert.Nil(t, err)
if err != nil {
t.FailNow()
}
})
}
}
func TestMainApiRoutes(t *testing.T) {
2020-04-16 17:32:54 +00:00
date := utils.Now().Format("2006-01-02")
2019-01-15 01:28:00 +00:00
tests := []HTTPTest{
{
Name: "Statping Details",
URL: "/api",
Method: "GET",
ExpectedStatus: 200,
2020-03-31 20:38:43 +00:00
ExpectedContains: []string{`"description":"This is an awesome test"`},
2020-03-19 01:50:53 +00:00
FuncTest: func(t *testing.T) error {
2020-03-04 14:20:47 +00:00
if !core.App.Setup {
2020-03-04 10:29:00 +00:00
return errors.New("database is not setup")
}
return nil
},
2019-02-06 18:51:30 +00:00
},
{
Name: "Statping Renew API Keys",
URL: "/api/renew",
Method: "POST",
2020-02-19 04:07:22 +00:00
ExpectedStatus: 200,
2020-03-08 01:23:41 +00:00
BeforeTest: SetTestENV,
SecureRoute: true,
2019-02-06 18:51:30 +00:00
},
2020-04-16 17:32:54 +00:00
{
Name: "Statping View Cache",
URL: "/api/cache",
Method: "GET",
ExpectedStatus: 200,
BeforeTest: SetTestENV,
SecureRoute: true,
ResponseLen: 0,
},
2019-02-06 18:51:30 +00:00
{
Name: "Statping Clear Cache",
URL: "/api/clear_cache",
Method: "POST",
2020-02-19 04:07:22 +00:00
ExpectedStatus: 200,
2020-03-19 01:50:53 +00:00
SecureRoute: true,
BeforeTest: func(t *testing.T) error {
CacheStorage.Set("test", []byte("data here"), types.Day)
list := CacheStorage.List()
assert.Len(t, list, 1)
return nil
},
AfterTest: func(t *testing.T) error {
list := CacheStorage.List()
assert.Len(t, list, 0)
2020-03-04 10:29:00 +00:00
return nil
},
2019-03-05 20:13:25 +00:00
},
2020-03-19 04:55:32 +00:00
{
Name: "Update Core",
URL: "/api/core",
Method: "POST",
ExpectedStatus: 200,
Body: `{
"name": "Updated Core"
}`,
AfterTest: func(t *testing.T) error {
assert.Equal(t, "Updated Core", core.App.Name)
return nil
},
},
2019-03-05 20:13:25 +00:00
{
Name: "404 Error Page",
URL: "/api/missing_404_page",
Method: "GET",
ExpectedStatus: 404,
2020-03-19 04:55:32 +00:00
},
2020-04-16 17:32:54 +00:00
{
Name: "Health Check endpoint",
URL: "/health",
Method: "GET",
ExpectedStatus: 200,
ExpectedContains: []string{`"online":true`, `"setup":true`},
},
{
Name: "Logs endpoint",
URL: "/api/logs",
Method: "GET",
ExpectedStatus: 200,
GreaterThan: 50,
ExpectedContains: []string{date},
},
{
Name: "Logs Last Line endpoint",
URL: "/api/logs/last",
Method: "GET",
ExpectedStatus: 200,
ExpectedContains: []string{date},
},
{
Name: "Prometheus Export Metrics",
URL: "/metrics",
Method: "GET",
BeforeTest: SetTestENV,
ExpectedStatus: 200,
ExpectedContains: []string{
`Statping Totals`,
`total_failures`,
`Golang Metrics`,
},
},
2020-03-19 04:55:32 +00:00
}
2019-01-15 01:28:00 +00:00
for _, v := range tests {
t.Run(v.Name, func(t *testing.T) {
2020-03-08 01:23:41 +00:00
res, t, err := RunHTTPTest(v, t)
assert.Nil(t, err)
t.Log(res)
2019-01-15 01:28:00 +00:00
})
}
}
2020-03-19 01:50:53 +00:00
type HttpFuncTest func(*testing.T) error
2019-01-15 01:28:00 +00:00
2020-04-16 17:32:54 +00:00
type ResponseFunc func(*testing.T, []byte) error
2019-01-15 01:28:00 +00:00
// HTTPTest contains all the parameters for a HTTP Unit Test
type HTTPTest struct {
2020-03-19 01:50:53 +00:00
Name string
URL string
Method string
Body string
ExpectedStatus int
ExpectedContains []string
ExpectedNotContains []string
HttpHeaders []string
ExpectedFiles []string
FuncTest HttpFuncTest
BeforeTest HttpFuncTest
AfterTest HttpFuncTest
2020-04-16 17:32:54 +00:00
ResponseFunc ResponseFunc
2020-03-19 01:50:53 +00:00
ResponseLen int
2020-04-16 17:32:54 +00:00
GreaterThan int
2020-03-19 01:50:53 +00:00
SecureRoute bool
2020-04-16 17:32:54 +00:00
Skip bool
2019-01-15 01:28:00 +00:00
}
2020-03-08 18:13:27 +00:00
func logTest(t *testing.T, err error) error {
e := sentry.NewEvent()
e.Environment = "testing"
e.Timestamp = utils.Now().Unix()
e.Message = fmt.Sprintf("failed test %s", t.Name())
e.Transaction = t.Name()
sentry.CaptureEvent(e)
sentry.CaptureException(err)
return err
}
2019-01-15 01:28:00 +00:00
// RunHTTPTest accepts a HTTPTest type to execute the HTTP request
func RunHTTPTest(test HTTPTest, t *testing.T) (string, *testing.T, error) {
2020-04-16 17:32:54 +00:00
if test.Skip {
t.SkipNow()
}
2020-03-08 01:23:41 +00:00
if test.BeforeTest != nil {
2020-03-19 01:50:53 +00:00
if err := test.BeforeTest(t); err != nil {
2020-03-08 18:13:27 +00:00
return "", t, logTest(t, err)
2020-03-08 01:23:41 +00:00
}
}
2020-03-08 18:13:27 +00:00
rr, err := Request(test)
2019-01-15 01:28:00 +00:00
if err != nil {
2020-03-08 18:13:27 +00:00
return "", t, logTest(t, err)
2019-01-15 01:28:00 +00:00
}
2020-03-08 18:13:27 +00:00
defer rr.Result().Body.Close()
2019-12-30 00:40:20 +00:00
2019-01-15 01:28:00 +00:00
body, err := ioutil.ReadAll(rr.Result().Body)
if err != nil {
assert.Nil(t, err)
2020-03-08 18:13:27 +00:00
return "", t, logTest(t, err)
2019-01-15 01:28:00 +00:00
}
2020-03-04 10:29:00 +00:00
2019-01-15 01:28:00 +00:00
stringBody := string(body)
2020-03-08 01:23:41 +00:00
2019-01-15 01:28:00 +00:00
if test.ExpectedStatus != rr.Result().StatusCode {
assert.Equal(t, test.ExpectedStatus, rr.Result().StatusCode)
return stringBody, t, fmt.Errorf("status code %v does not match %v", rr.Result().StatusCode, test.ExpectedStatus)
}
if len(test.ExpectedContains) != 0 {
for _, v := range test.ExpectedContains {
assert.Contains(t, stringBody, v)
}
}
2020-03-19 01:50:53 +00:00
if len(test.ExpectedNotContains) != 0 {
for _, v := range test.ExpectedNotContains {
assert.NotContains(t, stringBody, v)
}
}
2019-01-15 01:28:00 +00:00
if len(test.ExpectedFiles) != 0 {
for _, v := range test.ExpectedFiles {
assert.FileExists(t, v)
}
}
2020-03-04 10:29:00 +00:00
if test.FuncTest != nil {
2020-03-19 01:50:53 +00:00
err := test.FuncTest(t)
2020-03-04 10:29:00 +00:00
assert.Nil(t, err)
}
2020-04-16 17:32:54 +00:00
if test.ResponseFunc != nil {
err := test.ResponseFunc(t, body)
assert.Nil(t, err)
}
2020-03-04 10:29:00 +00:00
if test.ResponseLen != 0 {
var respArray []interface{}
err := json.Unmarshal(body, &respArray)
assert.Nil(t, err)
assert.Equal(t, test.ResponseLen, len(respArray))
}
2020-04-16 17:32:54 +00:00
if test.GreaterThan != 0 {
var respArray []interface{}
err := json.Unmarshal(body, &respArray)
assert.Nil(t, err)
assert.GreaterOrEqual(t, len(respArray), test.GreaterThan)
}
2020-03-08 18:13:27 +00:00
2020-03-08 01:23:41 +00:00
if test.AfterTest != nil {
2020-03-19 01:50:53 +00:00
if err := test.AfterTest(t); err != nil {
2020-03-08 18:13:27 +00:00
return "", t, logTest(t, err)
2020-03-08 01:23:41 +00:00
}
}
2020-03-08 18:13:27 +00:00
return stringBody, t, logTest(t, err)
}
func Request(test HTTPTest) (*httptest.ResponseRecorder, error) {
2020-03-19 03:13:50 +00:00
req, err := http.NewRequest(test.Method, test.URL, strings.NewReader(test.Body))
2020-03-08 18:13:27 +00:00
if err != nil {
return nil, err
}
if len(test.HttpHeaders) != 0 {
for _, v := range test.HttpHeaders {
splits := strings.Split(v, "=")
req.Header.Set(splits[0], splits[1])
}
}
rr := httptest.NewRecorder()
Router().ServeHTTP(rr, req)
return rr, err
2019-01-15 01:28:00 +00:00
}
2020-03-08 01:23:41 +00:00
2020-03-19 01:50:53 +00:00
func SetTestENV(t *testing.T) error {
2020-03-08 01:23:41 +00:00
return os.Setenv("GO_ENV", "test")
}
2020-03-19 01:50:53 +00:00
func UnsetTestENV(t *testing.T) error {
2020-03-08 01:23:41 +00:00
return os.Setenv("GO_ENV", "production")
}
2020-03-13 04:06:06 +00:00
2020-03-19 01:50:53 +00:00
func StopServices(t *testing.T) error {
2020-03-13 04:06:06 +00:00
for _, s := range services.All() {
s.Close()
}
return nil
}
2020-04-16 17:32:54 +00:00
var (
Success = `"status":"success"`
)