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"
|
2019-12-30 00:40:20 +00:00
|
|
|
"github.com/hunterlong/statping/core"
|
2019-01-15 01:28:00 +00:00
|
|
|
_ "github.com/hunterlong/statping/notifiers"
|
|
|
|
"github.com/hunterlong/statping/source"
|
2019-12-30 00:40:20 +00:00
|
|
|
"github.com/hunterlong/statping/types"
|
2019-01-15 01:28:00 +00:00
|
|
|
"github.com/hunterlong/statping/utils"
|
2020-03-04 10:29:00 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-01-15 01:28:00 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-12-30 00:40:20 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-01-15 01:28:00 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"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 {
|
|
|
|
if core.CoreApp.Setup {
|
|
|
|
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-04 10:29:00 +00:00
|
|
|
ExpectedFiles: []string{dir + "/config.yml", dir + "/handlers/" + types.SqliteFilename},
|
|
|
|
FuncTest: func() error {
|
|
|
|
if !core.CoreApp.Setup {
|
|
|
|
return errors.New("core has not been setup")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
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-04 10:29:00 +00:00
|
|
|
ExpectedContains: []string{`"description":"This is an awesome test"`},
|
|
|
|
FuncTest: func() error {
|
|
|
|
if !core.CoreApp.Setup {
|
|
|
|
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,
|
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
|
|
|
|
},
|
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) {
|
2019-01-29 12:58:25 +00:00
|
|
|
_, t, err := RunHTTPTest(v, t)
|
2019-12-30 00:40:20 +00:00
|
|
|
require.Nil(t, err)
|
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
|
|
|
|
ResponseLen int
|
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) {
|
|
|
|
req, err := http.NewRequest(test.Method, serverDomain+test.URL, strings.NewReader(test.Body))
|
|
|
|
if err != nil {
|
|
|
|
assert.Nil(t, err)
|
|
|
|
return "", t, 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)
|
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)
|
|
|
|
return "", t, err
|
|
|
|
}
|
2020-03-04 10:29:00 +00:00
|
|
|
defer rr.Result().Body.Close()
|
|
|
|
|
2019-01-15 01:28:00 +00:00
|
|
|
stringBody := string(body)
|
|
|
|
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))
|
|
|
|
}
|
2019-01-15 01:28:00 +00:00
|
|
|
return stringBody, t, err
|
|
|
|
}
|