pull/429/head
Hunter Long 2020-03-07 17:23:41 -08:00
parent 04a84a2488
commit c28f4fdfe0
6 changed files with 113 additions and 18 deletions

View File

@ -6,14 +6,17 @@ import (
_ "github.com/hunterlong/statping/notifiers" _ "github.com/hunterlong/statping/notifiers"
"github.com/hunterlong/statping/source" "github.com/hunterlong/statping/source"
"github.com/hunterlong/statping/types/core" "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/hunterlong/statping/utils"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"os"
"strings" "strings"
"testing" "testing"
) )
@ -85,6 +88,15 @@ func TestSetupRoutes(t *testing.T) {
if !core.App.Setup { if !core.App.Setup {
return errors.New("core has not been 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 return nil
}, },
}} }}
@ -120,6 +132,8 @@ func TestMainApiRoutes(t *testing.T) {
URL: "/api/renew", URL: "/api/renew",
Method: "POST", Method: "POST",
ExpectedStatus: 200, ExpectedStatus: 200,
BeforeTest: SetTestENV,
SecureRoute: true,
}, },
{ {
Name: "Statping Clear Cache", Name: "Statping Clear Cache",
@ -132,18 +146,21 @@ func TestMainApiRoutes(t *testing.T) {
} }
return nil return nil
}, },
SecureRoute: true,
}, },
{ {
Name: "404 Error Page", Name: "404 Error Page",
URL: "/api/missing_404_page", URL: "/api/missing_404_page",
Method: "GET", Method: "GET",
ExpectedStatus: 404, ExpectedStatus: 404,
AfterTest: UnsetTestENV,
}} }}
for _, v := range tests { for _, v := range tests {
t.Run(v.Name, func(t *testing.T) { t.Run(v.Name, func(t *testing.T) {
_, t, err := RunHTTPTest(v, t) res, t, err := RunHTTPTest(v, t)
require.Nil(t, err) assert.Nil(t, err)
t.Log(res)
}) })
} }
} }
@ -161,11 +178,19 @@ type HTTPTest struct {
HttpHeaders []string HttpHeaders []string
ExpectedFiles []string ExpectedFiles []string
FuncTest HttpFuncTest FuncTest HttpFuncTest
BeforeTest HttpFuncTest
AfterTest HttpFuncTest
ResponseLen int ResponseLen int
SecureRoute bool
} }
// RunHTTPTest accepts a HTTPTest type to execute the HTTP request // RunHTTPTest accepts a HTTPTest type to execute the HTTP request
func RunHTTPTest(test HTTPTest, t *testing.T) (string, *testing.T, error) { 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)) req, err := http.NewRequest(test.Method, serverDomain+test.URL, strings.NewReader(test.Body))
if err != nil { if err != nil {
assert.Nil(t, err) assert.Nil(t, err)
@ -188,6 +213,13 @@ func RunHTTPTest(test HTTPTest, t *testing.T) (string, *testing.T, error) {
defer rr.Result().Body.Close() defer rr.Result().Body.Close()
stringBody := string(body) 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 { if test.ExpectedStatus != rr.Result().StatusCode {
assert.Equal(t, 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) 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.Nil(t, err)
assert.Equal(t, test.ResponseLen, len(respArray)) assert.Equal(t, test.ResponseLen, len(respArray))
} }
if test.AfterTest != nil {
if err := test.AfterTest(); err != nil {
return "", t, err
}
}
return stringBody, t, err return stringBody, t, err
} }
func SetTestENV() error {
return os.Setenv("GO_ENV", "test")
}
func UnsetTestENV() error {
return os.Setenv("GO_ENV", "production")
}

View File

@ -12,6 +12,13 @@ func TestApiCheckinRoutes(t *testing.T) {
URL: "/api/checkins", URL: "/api/checkins",
Method: "GET", Method: "GET",
ExpectedStatus: 200, ExpectedStatus: 200,
BeforeTest: SetTestENV,
}, {
Name: "Statping Checkins",
URL: "/api/checkins",
Method: "GET",
ExpectedStatus: 200,
BeforeTest: UnsetTestENV,
}, { }, {
Name: "Statping Create Checkin", Name: "Statping Create Checkin",
URL: "/api/checkin", URL: "/api/checkin",
@ -24,17 +31,20 @@ func TestApiCheckinRoutes(t *testing.T) {
}`, }`,
ExpectedStatus: 200, ExpectedStatus: 200,
ExpectedContains: []string{`"status":"success","type":"checkin","method":"create"`}, ExpectedContains: []string{`"status":"success","type":"checkin","method":"create"`},
BeforeTest: SetTestENV,
}, },
{ {
Name: "Statping Checkins", Name: "Statping Checkins",
URL: "/api/checkins", URL: "/api/checkins",
Method: "GET", Method: "GET",
ExpectedStatus: 200, ExpectedStatus: 200,
AfterTest: UnsetTestENV,
}} }}
for _, v := range tests { for _, v := range tests {
t.Run(v.Name, func(t *testing.T) { 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) require.Nil(t, err)
}) })
} }

View File

@ -8,20 +8,32 @@ import (
func TestGroupAPIRoutes(t *testing.T) { func TestGroupAPIRoutes(t *testing.T) {
tests := []HTTPTest{ tests := []HTTPTest{
{ {
Name: "Statping Groups", Name: "Statping Public Groups",
URL: "/api/groups", URL: "/api/groups",
Method: "GET", Method: "GET",
ExpectedStatus: 200, ExpectedStatus: 200,
ResponseLen: 3, 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", URL: "/api/groups/1",
Method: "GET", Method: "GET",
ExpectedStatus: 200, ExpectedStatus: 200,
BeforeTest: SetTestENV,
AfterTest: UnsetTestENV,
}, },
{ {
Name: "Statping Create Group", Name: "Statping Create Public Group",
URL: "/api/groups", URL: "/api/groups",
HttpHeaders: []string{"Content-Type=application/json"}, HttpHeaders: []string{"Content-Type=application/json"},
Body: `{ Body: `{
@ -30,12 +42,38 @@ func TestGroupAPIRoutes(t *testing.T) {
}`, }`,
Method: "POST", Method: "POST",
ExpectedStatus: 200, 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", Name: "Statping Delete Group",
URL: "/api/groups/1", URL: "/api/groups/1",
Method: "DELETE", Method: "DELETE",
ExpectedStatus: 200, ExpectedStatus: 200,
AfterTest: UnsetTestENV,
}} }}
for _, v := range tests { for _, v := range tests {

View File

@ -150,9 +150,9 @@ func TestServiceAvgUptime(t *testing.T) {
service2, err := Find(5) service2, err := Find(5)
assert.Equal(t, "100", service2.AvgTime()) assert.Equal(t, "100", service2.AvgTime())
service3, err := Find(13) service3, err := Find(13)
assert.NotEqual(t, "0", service3.AvgUptime(since)) assert.NotEqual(t, "0", service3.HitsSince(since).Avg())
service4, err := Find(15) service4, err := Find(15)
assert.NotEqual(t, "0", service4.AvgUptime(since)) assert.NotEqual(t, "0", service4.HitsSince(since).Avg())
} }
func TestCreateService(t *testing.T) { func TestCreateService(t *testing.T) {

View File

@ -44,6 +44,7 @@ func TestFixedTime(t *testing.T) {
}} }}
for _, e := range examples { 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)) assert.Equal(t, e.Expected, FixedTime(e.Time, e.Duration), fmt.Sprintf("reformating for: %v %v", e.Time, e.Duration))
} }

View File

@ -53,7 +53,7 @@ func (u *User) Update() error {
func (u *User) Delete() error { func (u *User) Delete() error {
db := DB().Delete(u) db := DB().Delete(u)
if db.Error() == nil { 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() return db.Error()
} }