mirror of https://github.com/statping/statping
updates
parent
04a84a2488
commit
c28f4fdfe0
|
@ -6,14 +6,17 @@ import (
|
|||
_ "github.com/hunterlong/statping/notifiers"
|
||||
"github.com/hunterlong/statping/source"
|
||||
"github.com/hunterlong/statping/types/core"
|
||||
"github.com/hunterlong/statping/types/groups"
|
||||
"github.com/hunterlong/statping/types/services"
|
||||
"github.com/hunterlong/statping/types/users"
|
||||
"github.com/hunterlong/statping/utils"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
@ -85,6 +88,15 @@ func TestSetupRoutes(t *testing.T) {
|
|||
if !core.App.Setup {
|
||||
return errors.New("core has not been setup")
|
||||
}
|
||||
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")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}}
|
||||
|
@ -120,6 +132,8 @@ func TestMainApiRoutes(t *testing.T) {
|
|||
URL: "/api/renew",
|
||||
Method: "POST",
|
||||
ExpectedStatus: 200,
|
||||
BeforeTest: SetTestENV,
|
||||
SecureRoute: true,
|
||||
},
|
||||
{
|
||||
Name: "Statping Clear Cache",
|
||||
|
@ -132,18 +146,21 @@ func TestMainApiRoutes(t *testing.T) {
|
|||
}
|
||||
return nil
|
||||
},
|
||||
SecureRoute: true,
|
||||
},
|
||||
{
|
||||
Name: "404 Error Page",
|
||||
URL: "/api/missing_404_page",
|
||||
Method: "GET",
|
||||
ExpectedStatus: 404,
|
||||
AfterTest: UnsetTestENV,
|
||||
}}
|
||||
|
||||
for _, v := range tests {
|
||||
t.Run(v.Name, func(t *testing.T) {
|
||||
_, t, err := RunHTTPTest(v, t)
|
||||
require.Nil(t, err)
|
||||
res, t, err := RunHTTPTest(v, t)
|
||||
assert.Nil(t, err)
|
||||
t.Log(res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -161,11 +178,19 @@ type HTTPTest struct {
|
|||
HttpHeaders []string
|
||||
ExpectedFiles []string
|
||||
FuncTest HttpFuncTest
|
||||
BeforeTest HttpFuncTest
|
||||
AfterTest HttpFuncTest
|
||||
ResponseLen int
|
||||
SecureRoute bool
|
||||
}
|
||||
|
||||
// RunHTTPTest accepts a HTTPTest type to execute the HTTP request
|
||||
func RunHTTPTest(test HTTPTest, t *testing.T) (string, *testing.T, error) {
|
||||
if test.BeforeTest != nil {
|
||||
if err := test.BeforeTest(); err != nil {
|
||||
return "", t, err
|
||||
}
|
||||
}
|
||||
req, err := http.NewRequest(test.Method, serverDomain+test.URL, strings.NewReader(test.Body))
|
||||
if err != nil {
|
||||
assert.Nil(t, err)
|
||||
|
@ -188,6 +213,13 @@ func RunHTTPTest(test HTTPTest, t *testing.T) (string, *testing.T, error) {
|
|||
defer rr.Result().Body.Close()
|
||||
|
||||
stringBody := string(body)
|
||||
|
||||
if test.SecureRoute {
|
||||
test.SecureRoute = false
|
||||
str, tt, err := RunHTTPTest(test, t)
|
||||
return str, tt, err
|
||||
}
|
||||
|
||||
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)
|
||||
|
@ -212,5 +244,19 @@ func RunHTTPTest(test HTTPTest, t *testing.T) (string, *testing.T, error) {
|
|||
assert.Nil(t, err)
|
||||
assert.Equal(t, test.ResponseLen, len(respArray))
|
||||
}
|
||||
|
||||
if test.AfterTest != nil {
|
||||
if err := test.AfterTest(); err != nil {
|
||||
return "", t, err
|
||||
}
|
||||
}
|
||||
return stringBody, t, err
|
||||
}
|
||||
|
||||
func SetTestENV() error {
|
||||
return os.Setenv("GO_ENV", "test")
|
||||
}
|
||||
|
||||
func UnsetTestENV() error {
|
||||
return os.Setenv("GO_ENV", "production")
|
||||
}
|
||||
|
|
|
@ -12,29 +12,39 @@ func TestApiCheckinRoutes(t *testing.T) {
|
|||
URL: "/api/checkins",
|
||||
Method: "GET",
|
||||
ExpectedStatus: 200,
|
||||
BeforeTest: SetTestENV,
|
||||
}, {
|
||||
Name: "Statping Checkins",
|
||||
URL: "/api/checkins",
|
||||
Method: "GET",
|
||||
ExpectedStatus: 200,
|
||||
BeforeTest: UnsetTestENV,
|
||||
}, {
|
||||
Name: "Statping Create Checkin",
|
||||
URL: "/api/checkin",
|
||||
Method: "POST",
|
||||
Body: `{
|
||||
"service_id": 2,
|
||||
"name": "Server Checkin",
|
||||
"interval": 900,
|
||||
"grace": 60
|
||||
}`,
|
||||
"service_id": 2,
|
||||
"name": "Server Checkin",
|
||||
"interval": 900,
|
||||
"grace": 60
|
||||
}`,
|
||||
ExpectedStatus: 200,
|
||||
ExpectedContains: []string{`"status":"success","type":"checkin","method":"create"`},
|
||||
BeforeTest: SetTestENV,
|
||||
},
|
||||
{
|
||||
Name: "Statping Checkins",
|
||||
URL: "/api/checkins",
|
||||
Method: "GET",
|
||||
ExpectedStatus: 200,
|
||||
AfterTest: UnsetTestENV,
|
||||
}}
|
||||
|
||||
for _, v := range tests {
|
||||
t.Run(v.Name, func(t *testing.T) {
|
||||
_, t, err := RunHTTPTest(v, t)
|
||||
str, t, err := RunHTTPTest(v, t)
|
||||
t.Logf("Test %s: \n %v\n", v.Name, str)
|
||||
require.Nil(t, err)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -8,34 +8,72 @@ import (
|
|||
func TestGroupAPIRoutes(t *testing.T) {
|
||||
tests := []HTTPTest{
|
||||
{
|
||||
Name: "Statping Groups",
|
||||
Name: "Statping Public Groups",
|
||||
URL: "/api/groups",
|
||||
Method: "GET",
|
||||
ExpectedStatus: 200,
|
||||
ResponseLen: 3,
|
||||
BeforeTest: SetTestENV,
|
||||
AfterTest: UnsetTestENV,
|
||||
},
|
||||
{
|
||||
Name: "Statping View Group",
|
||||
Name: "Statping Public and Private Groups",
|
||||
URL: "/api/groups",
|
||||
Method: "GET",
|
||||
ExpectedStatus: 200,
|
||||
ResponseLen: 3,
|
||||
BeforeTest: UnsetTestENV,
|
||||
},
|
||||
{
|
||||
Name: "Statping View Public Group",
|
||||
URL: "/api/groups/1",
|
||||
Method: "GET",
|
||||
ExpectedStatus: 200,
|
||||
BeforeTest: SetTestENV,
|
||||
AfterTest: UnsetTestENV,
|
||||
},
|
||||
{
|
||||
Name: "Statping Create Group",
|
||||
Name: "Statping Create Public Group",
|
||||
URL: "/api/groups",
|
||||
HttpHeaders: []string{"Content-Type=application/json"},
|
||||
Body: `{
|
||||
"name": "New Group",
|
||||
"public": true
|
||||
}`,
|
||||
"name": "New Group",
|
||||
"public": true
|
||||
}`,
|
||||
Method: "POST",
|
||||
ExpectedStatus: 200,
|
||||
BeforeTest: SetTestENV,
|
||||
},
|
||||
{
|
||||
Name: "Statping Create Private Group",
|
||||
URL: "/api/groups",
|
||||
HttpHeaders: []string{"Content-Type=application/json"},
|
||||
Body: `{
|
||||
"name": "New Private Group",
|
||||
"public": false
|
||||
}`,
|
||||
Method: "POST",
|
||||
ExpectedStatus: 200,
|
||||
},
|
||||
{
|
||||
Name: "Statping View Private Group",
|
||||
URL: "/api/groups/2",
|
||||
Method: "GET",
|
||||
ExpectedStatus: 404,
|
||||
BeforeTest: UnsetTestENV,
|
||||
},
|
||||
{
|
||||
Name: "Statping View Unknown Group",
|
||||
URL: "/api/groups/8383883838",
|
||||
Method: "GET",
|
||||
ExpectedStatus: 404,
|
||||
},
|
||||
{
|
||||
Name: "Statping Delete Group",
|
||||
URL: "/api/groups/1",
|
||||
Method: "DELETE",
|
||||
ExpectedStatus: 200,
|
||||
AfterTest: UnsetTestENV,
|
||||
}}
|
||||
|
||||
for _, v := range tests {
|
||||
|
|
|
@ -150,9 +150,9 @@ func TestServiceAvgUptime(t *testing.T) {
|
|||
service2, err := Find(5)
|
||||
assert.Equal(t, "100", service2.AvgTime())
|
||||
service3, err := Find(13)
|
||||
assert.NotEqual(t, "0", service3.AvgUptime(since))
|
||||
assert.NotEqual(t, "0", service3.HitsSince(since).Avg())
|
||||
service4, err := Find(15)
|
||||
assert.NotEqual(t, "0", service4.AvgUptime(since))
|
||||
assert.NotEqual(t, "0", service4.HitsSince(since).Avg())
|
||||
}
|
||||
|
||||
func TestCreateService(t *testing.T) {
|
||||
|
|
|
@ -44,6 +44,7 @@ func TestFixedTime(t *testing.T) {
|
|||
}}
|
||||
|
||||
for _, e := range examples {
|
||||
t.Logf("Testing is %v time converts to %v duration\n", e.Time.String(), e.Duration.String())
|
||||
assert.Equal(t, e.Expected, FixedTime(e.Time, e.Duration), fmt.Sprintf("reformating for: %v %v", e.Time, e.Duration))
|
||||
}
|
||||
|
||||
|
|
|
@ -53,7 +53,7 @@ func (u *User) Update() error {
|
|||
func (u *User) Delete() error {
|
||||
db := DB().Delete(u)
|
||||
if db.Error() == nil {
|
||||
log.Warnf("User #%d (%s) has been deleted")
|
||||
log.Warnf("User #%d (%s) has been deleted", u.Id, u.Username)
|
||||
}
|
||||
return db.Error()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue