statping/handlers/api_test.go

295 lines
6.7 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"
"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"
)
const (
serverDomain = "http://localhost:8080"
)
var (
dir string
)
func init() {
source.Assets()
utils.InitLogs()
dir = utils.Directory
}
2020-03-04 10:29:00 +00:00
//func TestResetDatabase(t *testing.T) {
// err := core.TmpRecords("handlers.db")
// t.Log(err)
// require.Nil(t, err)
// require.NotNil(t, core.CoreApp)
//}
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{}
form.Add("db_host", "")
form.Add("db_user", "")
form.Add("db_password", "")
form.Add("db_database", "")
form.Add("db_connection", "sqlite")
form.Add("db_port", "")
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-04 10:29:00 +00:00
FuncTest: func() 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-03-05 05:38:56 +00:00
ExpectedFiles: []string{dir + "/config.yml", dir + "/" + "statping.db"},
2020-03-04 10:29:00 +00:00
FuncTest: func() 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) {
tests := []HTTPTest{
{
Name: "Statping Details",
URL: "/api",
Method: "GET",
ExpectedStatus: 200,
2020-03-05 05:38:56 +00:00
ExpectedContains: []string{`"description":"This data is only used to testing"`},
2020-03-04 10:29:00 +00:00
FuncTest: func() 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
},
{
Name: "Statping Clear Cache",
URL: "/api/clear_cache",
Method: "POST",
2020-02-19 04:07:22 +00:00
ExpectedStatus: 200,
2020-03-04 10:29:00 +00:00
FuncTest: func() error {
if len(CacheStorage.List()) != 0 {
return errors.New("cache was not reset")
}
return nil
},
2020-03-08 01:23:41 +00:00
SecureRoute: true,
2020-03-08 18:13:27 +00:00
BeforeTest: SetTestENV,
2019-03-05 20:13:25 +00:00
},
{
Name: "404 Error Page",
URL: "/api/missing_404_page",
Method: "GET",
ExpectedStatus: 404,
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-04 10:29:00 +00:00
type HttpFuncTest func() error
2019-01-15 01:28:00 +00:00
// HTTPTest contains all the parameters for a HTTP Unit Test
type HTTPTest struct {
Name string
URL string
Method string
Body string
ExpectedStatus int
ExpectedContains []string
HttpHeaders []string
ExpectedFiles []string
2020-03-04 10:29:00 +00:00
FuncTest HttpFuncTest
2020-03-08 01:23:41 +00:00
BeforeTest HttpFuncTest
AfterTest HttpFuncTest
2020-03-04 10:29:00 +00:00
ResponseLen int
2020-03-08 01:23:41 +00:00
SecureRoute 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-03-08 01:23:41 +00:00
if test.BeforeTest != nil {
if err := test.BeforeTest(); 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)
}
}
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 {
err := test.FuncTest()
assert.Nil(t, err)
}
if test.ResponseLen != 0 {
var respArray []interface{}
err := json.Unmarshal(body, &respArray)
assert.Nil(t, err)
assert.Equal(t, test.ResponseLen, len(respArray))
}
2020-03-08 01:23:41 +00:00
2020-03-08 18:13:27 +00:00
if test.SecureRoute {
UnsetTestENV()
rec, err := Request(test)
if err != nil {
return "", t, logTest(t, err)
}
defer rec.Result().Body.Close()
assert.Equal(t, http.StatusUnauthorized, rec.Result().StatusCode)
}
2020-03-08 01:23:41 +00:00
if test.AfterTest != nil {
if err := test.AfterTest(); 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) {
req, err := http.NewRequest(test.Method, serverDomain+test.URL, strings.NewReader(test.Body))
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
func SetTestENV() error {
return os.Setenv("GO_ENV", "test")
}
func UnsetTestENV() error {
return os.Setenv("GO_ENV", "production")
}
2020-03-13 04:06:06 +00:00
func StopServices() error {
for _, s := range services.All() {
s.Close()
}
return nil
}