From c28f4fdfe0ba9a0a49b5c784821c129c77c5160b Mon Sep 17 00:00:00 2001 From: Hunter Long Date: Sat, 7 Mar 2020 17:23:41 -0800 Subject: [PATCH] updates --- handlers/api_test.go | 52 +++++++++++++++++++++++++++++++-- handlers/checkins_test.go | 22 ++++++++++---- handlers/groups_test.go | 50 +++++++++++++++++++++++++++---- types/services/services_test.go | 4 +-- types/time_test.go | 1 + types/users/database.go | 2 +- 6 files changed, 113 insertions(+), 18 deletions(-) diff --git a/handlers/api_test.go b/handlers/api_test.go index 0c739572..98e1546b 100644 --- a/handlers/api_test.go +++ b/handlers/api_test.go @@ -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") +} diff --git a/handlers/checkins_test.go b/handlers/checkins_test.go index 8ccf416f..a6964a49 100644 --- a/handlers/checkins_test.go +++ b/handlers/checkins_test.go @@ -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) }) } diff --git a/handlers/groups_test.go b/handlers/groups_test.go index 2fef36f3..ebfc63fb 100644 --- a/handlers/groups_test.go +++ b/handlers/groups_test.go @@ -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 { diff --git a/types/services/services_test.go b/types/services/services_test.go index 89875b05..7006c383 100644 --- a/types/services/services_test.go +++ b/types/services/services_test.go @@ -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) { diff --git a/types/time_test.go b/types/time_test.go index 6e608933..2a835a49 100644 --- a/types/time_test.go +++ b/types/time_test.go @@ -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)) } diff --git a/types/users/database.go b/types/users/database.go index 8a83f37f..e650fc02 100644 --- a/types/users/database.go +++ b/types/users/database.go @@ -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() }