mirror of https://github.com/statping/statping
additional testing
parent
3210dfa226
commit
7355105c68
|
@ -50,7 +50,7 @@ func exportCli(args []string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err = configs.ConnectConfigs(config); err != nil {
|
if err = configs.ConnectConfigs(config, false); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if _, err := services.SelectAllServices(false); err != nil {
|
if _, err := services.SelectAllServices(false); err != nil {
|
||||||
|
@ -172,7 +172,7 @@ func importCli(args []string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err = configs.ConnectConfigs(config); err != nil {
|
if err = configs.ConnectConfigs(config, false); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if data, err = ExportSettings(); err != nil {
|
if data, err = ExportSettings(); err != nil {
|
||||||
|
@ -262,7 +262,7 @@ func runOnce() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "config.yml file not found")
|
return errors.Wrap(err, "config.yml file not found")
|
||||||
}
|
}
|
||||||
err = configs.ConnectConfigs(config)
|
err = configs.ConnectConfigs(config, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "issue connecting to database")
|
return errors.Wrap(err, "issue connecting to database")
|
||||||
}
|
}
|
||||||
|
|
|
@ -91,7 +91,7 @@ func start() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = configs.ConnectConfigs(confgs); err != nil {
|
if err = configs.ConnectConfigs(confgs, true); err != nil {
|
||||||
exit(err)
|
exit(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/getsentry/sentry-go"
|
"github.com/getsentry/sentry-go"
|
||||||
|
@ -18,7 +19,6 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
@ -55,6 +55,21 @@ func TestSetupRoutes(t *testing.T) {
|
||||||
form.Add("domain", "http://localhost:8080")
|
form.Add("domain", "http://localhost:8080")
|
||||||
form.Add("email", "info@statping.com")
|
form.Add("email", "info@statping.com")
|
||||||
|
|
||||||
|
badForm := url.Values{}
|
||||||
|
badForm.Add("db_host", "badconnection")
|
||||||
|
badForm.Add("db_user", utils.Params.GetString("DB_USER"))
|
||||||
|
badForm.Add("db_password", utils.Params.GetString("DB_PASS"))
|
||||||
|
badForm.Add("db_database", utils.Params.GetString("DB_DATABASE"))
|
||||||
|
badForm.Add("db_connection", "mysql")
|
||||||
|
badForm.Add("db_port", utils.Params.GetString("DB_PORT"))
|
||||||
|
badForm.Add("project", "Tester")
|
||||||
|
badForm.Add("username", "admin")
|
||||||
|
badForm.Add("password", "password123")
|
||||||
|
badForm.Add("sample_data", "on")
|
||||||
|
badForm.Add("description", "This is an awesome test")
|
||||||
|
badForm.Add("domain", "http://localhost:8080")
|
||||||
|
badForm.Add("email", "info@statping.com")
|
||||||
|
|
||||||
tests := []HTTPTest{
|
tests := []HTTPTest{
|
||||||
{
|
{
|
||||||
Name: "Statping Check",
|
Name: "Statping Check",
|
||||||
|
@ -69,13 +84,22 @@ func TestSetupRoutes(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "Statping Run Setup",
|
Name: "Statping Error Setup",
|
||||||
URL: "/api/setup",
|
URL: "/api/setup",
|
||||||
Method: "POST",
|
Method: "POST",
|
||||||
Body: form.Encode(),
|
Body: badForm.Encode(),
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: 500,
|
||||||
HttpHeaders: []string{"Content-Type=application/x-www-form-urlencoded"},
|
ExpectedContains: []string{BadJSONDatabase},
|
||||||
ExpectedFiles: []string{utils.Directory + "/config.yml"},
|
HttpHeaders: []string{"Content-Type=application/x-www-form-urlencoded"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Statping Run Setup",
|
||||||
|
URL: "/api/setup",
|
||||||
|
Method: "POST",
|
||||||
|
Body: form.Encode(),
|
||||||
|
//ExpectedStatus: 200,
|
||||||
|
HttpHeaders: []string{"Content-Type=application/x-www-form-urlencoded"},
|
||||||
|
ExpectedFiles: []string{utils.Directory + "/config.yml"},
|
||||||
FuncTest: func(t *testing.T) error {
|
FuncTest: func(t *testing.T) error {
|
||||||
if !core.App.Setup {
|
if !core.App.Setup {
|
||||||
return errors.New("core has not been setup")
|
return errors.New("core has not been setup")
|
||||||
|
@ -92,7 +116,8 @@ func TestSetupRoutes(t *testing.T) {
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
AfterTest: StopServices,
|
AfterTest: StopServices,
|
||||||
}}
|
},
|
||||||
|
}
|
||||||
|
|
||||||
for _, v := range tests {
|
for _, v := range tests {
|
||||||
t.Run(v.Name, func(t *testing.T) {
|
t.Run(v.Name, func(t *testing.T) {
|
||||||
|
@ -190,6 +215,14 @@ func TestMainApiRoutes(t *testing.T) {
|
||||||
GreaterThan: 20,
|
GreaterThan: 20,
|
||||||
ExpectedContains: []string{date},
|
ExpectedContains: []string{date},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "Logs endpoint",
|
||||||
|
URL: "/api/logs",
|
||||||
|
Method: "GET",
|
||||||
|
ExpectedStatus: 200,
|
||||||
|
GreaterThan: 20,
|
||||||
|
ExpectedContains: []string{date},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Name: "Logs Last Line endpoint",
|
Name: "Logs Last Line endpoint",
|
||||||
URL: "/api/logs/last",
|
URL: "/api/logs/last",
|
||||||
|
@ -273,6 +306,13 @@ func RunHTTPTest(test HTTPTest, t *testing.T) (string, *testing.T, error) {
|
||||||
}
|
}
|
||||||
defer rr.Result().Body.Close()
|
defer rr.Result().Body.Close()
|
||||||
|
|
||||||
|
if test.ExpectedStatus != 0 {
|
||||||
|
if test.ExpectedStatus != rr.Result().StatusCode {
|
||||||
|
assert.Equal(t, test.ExpectedStatus, rr.Result().StatusCode)
|
||||||
|
return "", t, fmt.Errorf("status code %v does not match %v", rr.Result().StatusCode, test.ExpectedStatus)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(rr.Result().Body)
|
body, err := ioutil.ReadAll(rr.Result().Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
@ -281,10 +321,6 @@ func RunHTTPTest(test HTTPTest, t *testing.T) (string, *testing.T, error) {
|
||||||
|
|
||||||
stringBody := string(body)
|
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 {
|
if len(test.ExpectedContains) != 0 {
|
||||||
for _, v := range test.ExpectedContains {
|
for _, v := range test.ExpectedContains {
|
||||||
assert.Contains(t, stringBody, v)
|
assert.Contains(t, stringBody, v)
|
||||||
|
@ -348,11 +384,13 @@ func Request(test HTTPTest) (*httptest.ResponseRecorder, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetTestENV(t *testing.T) error {
|
func SetTestENV(t *testing.T) error {
|
||||||
return os.Setenv("GO_ENV", "test")
|
utils.Params.Set("GO_ENV", "test")
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func UnsetTestENV(t *testing.T) error {
|
func UnsetTestENV(t *testing.T) error {
|
||||||
return os.Setenv("GO_ENV", "production")
|
utils.Params.Set("GO_ENV", "production")
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func StopServices(t *testing.T) error {
|
func StopServices(t *testing.T) error {
|
||||||
|
@ -362,6 +400,19 @@ func StopServices(t *testing.T) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func basicAuth(username, password string) string {
|
||||||
|
auth := username + ":" + password
|
||||||
|
return base64.StdEncoding.EncodeToString([]byte(auth))
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Success = `"status":"success"`
|
Success = `"status":"success"`
|
||||||
|
|
||||||
|
MethodCreate = `"method":"create"`
|
||||||
|
MethodUpdate = `"method":"update"`
|
||||||
|
MethodDelete = `"method":"delete"`
|
||||||
|
|
||||||
|
BadJSON = `{incorrect: JSON %%% formatting, [&]}`
|
||||||
|
BadJSONResponse = `{"error":"could not decode incoming JSON"}`
|
||||||
|
BadJSONDatabase = `{"error":"error connecting to database`
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/statping/statping/types/checkins"
|
"github.com/statping/statping/types/checkins"
|
||||||
"github.com/statping/statping/types/errors"
|
"github.com/statping/statping/types/errors"
|
||||||
|
@ -47,7 +46,7 @@ func checkinCreateHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
service, err := services.Find(checkin.ServiceId)
|
service, err := services.Find(checkin.ServiceId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sendErrorJson(fmt.Errorf("missing service_id field"), w, r)
|
sendErrorJson(err, w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
checkin.ServiceId = service.Id
|
checkin.ServiceId = service.Id
|
||||||
|
@ -59,9 +58,9 @@ func checkinCreateHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkinHitHandler(w http.ResponseWriter, r *http.Request) {
|
func checkinHitHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
checkin, id, err := findCheckin(r)
|
checkin, _, err := findCheckin(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sendErrorJson(fmt.Errorf("checkin %s was not found", id), w, r)
|
sendErrorJson(err, w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ip, _, _ := net.SplitHostPort(r.RemoteAddr)
|
ip, _, _ := net.SplitHostPort(r.RemoteAddr)
|
||||||
|
@ -75,7 +74,7 @@ func checkinHitHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
err = hit.Create()
|
err = hit.Create()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sendErrorJson(fmt.Errorf("checkin %v was not found", id), w, r)
|
sendErrorJson(err, w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
checkin.Failing = false
|
checkin.Failing = false
|
||||||
|
@ -84,9 +83,9 @@ func checkinHitHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkinDeleteHandler(w http.ResponseWriter, r *http.Request) {
|
func checkinDeleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
checkin, id, err := findCheckin(r)
|
checkin, _, err := findCheckin(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sendErrorJson(fmt.Errorf("checkin %v was not found", id), w, r)
|
sendErrorJson(err, w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,38 @@
|
||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"github.com/statping/statping/types/checkins"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestUnAuthenticatedCheckinRoutes(t *testing.T) {
|
||||||
|
tests := []HTTPTest{
|
||||||
|
{
|
||||||
|
Name: "No Authentication - New Checkin",
|
||||||
|
URL: "/api/checkins",
|
||||||
|
Method: "POST",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "No Authentication - Delete Checkin",
|
||||||
|
URL: "/api/checkins/1",
|
||||||
|
Method: "DELETE",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range tests {
|
||||||
|
t.Run(v.Name, func(t *testing.T) {
|
||||||
|
str, t, err := RunHTTPTest(v, t)
|
||||||
|
t.Logf("Test %s: \n %v\n", v.Name, str)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestApiCheckinRoutes(t *testing.T) {
|
func TestApiCheckinRoutes(t *testing.T) {
|
||||||
var apiToken string
|
|
||||||
tests := []HTTPTest{
|
tests := []HTTPTest{
|
||||||
{
|
{
|
||||||
Name: "Statping Create Checkins",
|
Name: "Statping Create Checkins",
|
||||||
|
@ -23,7 +46,8 @@ func TestApiCheckinRoutes(t *testing.T) {
|
||||||
"name": "Example Checkin",
|
"name": "Example Checkin",
|
||||||
"service_id": 1,
|
"service_id": 1,
|
||||||
"checkin_interval": 300,
|
"checkin_interval": 300,
|
||||||
"grace_period": 60
|
"grace_period": 60,
|
||||||
|
"api_key": "example"
|
||||||
}`,
|
}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -33,49 +57,26 @@ func TestApiCheckinRoutes(t *testing.T) {
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: 200,
|
||||||
ResponseLen: 3,
|
ResponseLen: 3,
|
||||||
BeforeTest: SetTestENV,
|
BeforeTest: SetTestENV,
|
||||||
ResponseFunc: func(t *testing.T, resp []byte) error {
|
|
||||||
var checkin []*checkins.Checkin
|
|
||||||
if err := json.Unmarshal(resp, &checkin); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
require.Len(t, checkin, 3)
|
|
||||||
last := checkin[len(checkin)-1]
|
|
||||||
apiToken = last.ApiKey
|
|
||||||
require.NotEmpty(t, apiToken)
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "Statping View Checkin",
|
Name: "Statping View Checkin",
|
||||||
URL: "/api/checkins/" + apiToken,
|
URL: "/api/checkins/example",
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: 200,
|
||||||
ExpectedContains: []string{Success, `"type":"checkin","method":"create"`},
|
BeforeTest: SetTestENV,
|
||||||
BeforeTest: SetTestENV,
|
SecureRoute: true,
|
||||||
SecureRoute: true,
|
|
||||||
Skip: true,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "Statping Trigger Checkin",
|
Name: "Statping Trigger Checkin",
|
||||||
URL: "/checkin/" + apiToken,
|
URL: "/checkin/example",
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: 200,
|
||||||
ExpectedContains: []string{Success, `"type":"checkin","method":"create"`},
|
SecureRoute: true,
|
||||||
SecureRoute: true,
|
BeforeTest: SetTestENV,
|
||||||
BeforeTest: SetTestENV,
|
|
||||||
Skip: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "Statping Delete Checkin",
|
|
||||||
URL: "/api/checkins/" + apiToken,
|
|
||||||
Method: "DELETE",
|
|
||||||
ExpectedContains: []string{Success, `"type":"checkin","method":"create"`},
|
|
||||||
BeforeTest: SetTestENV,
|
|
||||||
Skip: true,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "Statping Missing Trigger Checkin",
|
Name: "Statping Missing Trigger Checkin",
|
||||||
URL: "/checkin/" + apiToken,
|
URL: "/checkin/missing123",
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
BeforeTest: SetTestENV,
|
BeforeTest: SetTestENV,
|
||||||
ExpectedStatus: 404,
|
ExpectedStatus: 404,
|
||||||
|
@ -87,6 +88,22 @@ func TestApiCheckinRoutes(t *testing.T) {
|
||||||
BeforeTest: SetTestENV,
|
BeforeTest: SetTestENV,
|
||||||
ExpectedStatus: 404,
|
ExpectedStatus: 404,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "Statping Delete Checkin",
|
||||||
|
URL: "/api/checkins/example",
|
||||||
|
Method: "DELETE",
|
||||||
|
BeforeTest: SetTestENV,
|
||||||
|
ExpectedContains: []string{Success},
|
||||||
|
ExpectedStatus: 200,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Incorrect JSON POST",
|
||||||
|
URL: "/api/checkins",
|
||||||
|
Body: BadJSON,
|
||||||
|
ExpectedContains: []string{BadJSONResponse},
|
||||||
|
Method: "POST",
|
||||||
|
ExpectedStatus: 422,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range tests {
|
for _, v := range tests {
|
||||||
|
|
|
@ -1,10 +1,46 @@
|
||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/statping/statping/types/groups"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestUnAuthenticatedGroupRoutes(t *testing.T) {
|
||||||
|
tests := []HTTPTest{
|
||||||
|
{
|
||||||
|
Name: "No Authentication - New Group",
|
||||||
|
URL: "/api/groups",
|
||||||
|
Method: "POST",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "No Authentication - Update Group",
|
||||||
|
URL: "/api/groups/1",
|
||||||
|
Method: "POST",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "No Authentication - Delete Group",
|
||||||
|
URL: "/api/groups/1",
|
||||||
|
Method: "DELETE",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range tests {
|
||||||
|
t.Run(v.Name, func(t *testing.T) {
|
||||||
|
str, t, err := RunHTTPTest(v, t)
|
||||||
|
t.Logf("Test %s: \n %v\n", v.Name, str)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestGroupAPIRoutes(t *testing.T) {
|
func TestGroupAPIRoutes(t *testing.T) {
|
||||||
tests := []HTTPTest{
|
tests := []HTTPTest{
|
||||||
{
|
{
|
||||||
|
@ -47,6 +83,14 @@ func TestGroupAPIRoutes(t *testing.T) {
|
||||||
Method: "POST",
|
Method: "POST",
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: 200,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "Incorrect JSON POST",
|
||||||
|
URL: "/api/groups",
|
||||||
|
Body: BadJSON,
|
||||||
|
ExpectedContains: []string{BadJSONResponse},
|
||||||
|
Method: "POST",
|
||||||
|
ExpectedStatus: 422,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Name: "Statping Public and Private Groups",
|
Name: "Statping Public and Private Groups",
|
||||||
URL: "/api/groups",
|
URL: "/api/groups",
|
||||||
|
@ -87,13 +131,34 @@ func TestGroupAPIRoutes(t *testing.T) {
|
||||||
ExpectedStatus: 404,
|
ExpectedStatus: 404,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "Statping Delete Group",
|
Name: "Statping Update Group",
|
||||||
URL: "/api/groups/1",
|
URL: "/api/groups/1",
|
||||||
Method: "DELETE",
|
Method: "POST",
|
||||||
ExpectedStatus: 200,
|
Body: `{
|
||||||
AfterTest: UnsetTestENV,
|
"name": "Updated Group",
|
||||||
SecureRoute: true,
|
"public": false
|
||||||
}}
|
}`,
|
||||||
|
ExpectedStatus: 200,
|
||||||
|
ExpectedContains: []string{Success, MethodUpdate},
|
||||||
|
BeforeTest: SetTestENV,
|
||||||
|
SecureRoute: true,
|
||||||
|
AfterTest: func(t *testing.T) error {
|
||||||
|
g, err := groups.Find(1)
|
||||||
|
require.Nil(t, err)
|
||||||
|
assert.Equal(t, "Updated Group", g.Name)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Statping Delete Group",
|
||||||
|
URL: "/api/groups/1",
|
||||||
|
Method: "DELETE",
|
||||||
|
ExpectedStatus: 200,
|
||||||
|
ExpectedContains: []string{Success, MethodDelete},
|
||||||
|
AfterTest: UnsetTestENV,
|
||||||
|
SecureRoute: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
for _, v := range tests {
|
for _, v := range tests {
|
||||||
t.Run(v.Name, func(t *testing.T) {
|
t.Run(v.Name, func(t *testing.T) {
|
||||||
|
|
|
@ -10,7 +10,6 @@ import (
|
||||||
"github.com/statping/statping/types/errors"
|
"github.com/statping/statping/types/errors"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -110,7 +109,7 @@ func IsReadAuthenticated(r *http.Request) bool {
|
||||||
// IsFullAuthenticated returns true if the HTTP request is authenticated. You can set the environment variable GO_ENV=test
|
// IsFullAuthenticated returns true if the HTTP request is authenticated. You can set the environment variable GO_ENV=test
|
||||||
// to bypass the admin authenticate to the dashboard features.
|
// to bypass the admin authenticate to the dashboard features.
|
||||||
func IsFullAuthenticated(r *http.Request) bool {
|
func IsFullAuthenticated(r *http.Request) bool {
|
||||||
if os.Getenv("GO_ENV") == "test" {
|
if utils.Params.Get("GO_ENV") == "test" {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if core.App == nil {
|
if core.App == nil {
|
||||||
|
@ -187,7 +186,7 @@ func IsUser(r *http.Request) bool {
|
||||||
if !core.App.Setup {
|
if !core.App.Setup {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if os.Getenv("GO_ENV") == "test" {
|
if utils.Params.Get("GO_ENV") == "test" {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
tk, err := getJwtToken(r)
|
tk, err := getJwtToken(r)
|
||||||
|
@ -255,6 +254,11 @@ func returnJson(d interface{}, w http.ResponseWriter, r *http.Request) {
|
||||||
json.NewEncoder(w).Encode(e)
|
json.NewEncoder(w).Encode(e)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if e, ok := d.(error); ok {
|
||||||
|
w.WriteHeader(500)
|
||||||
|
json.NewEncoder(w).Encode(errors.New(e.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
json.NewEncoder(w).Encode(d)
|
json.NewEncoder(w).Encode(d)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,54 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestUnAuthenticatedIncidentRoutes(t *testing.T) {
|
||||||
|
tests := []HTTPTest{
|
||||||
|
{
|
||||||
|
Name: "No Authentication - New Incident",
|
||||||
|
URL: "/api/services/1/incidents",
|
||||||
|
Method: "POST",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "No Authentication - New Incident Update",
|
||||||
|
URL: "/api/incidents/updates",
|
||||||
|
Method: "POST",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "No Authentication - Update Incident",
|
||||||
|
URL: "/api/incidents/1",
|
||||||
|
Method: "POST",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "No Authentication - Delete Incident",
|
||||||
|
URL: "/api/incidents/1",
|
||||||
|
Method: "DELETE",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "No Authentication - Delete Incident Update",
|
||||||
|
URL: "/api/incidents/1/updates/1",
|
||||||
|
Method: "DELETE",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range tests {
|
||||||
|
t.Run(v.Name, func(t *testing.T) {
|
||||||
|
str, t, err := RunHTTPTest(v, t)
|
||||||
|
t.Logf("Test %s: \n %v\n", v.Name, str)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestIncidentsAPIRoutes(t *testing.T) {
|
func TestIncidentsAPIRoutes(t *testing.T) {
|
||||||
tests := []HTTPTest{
|
tests := []HTTPTest{
|
||||||
{
|
{
|
||||||
|
@ -63,6 +111,15 @@ func TestIncidentsAPIRoutes(t *testing.T) {
|
||||||
BeforeTest: SetTestENV,
|
BeforeTest: SetTestENV,
|
||||||
ExpectedContains: []string{Success},
|
ExpectedContains: []string{Success},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "Incorrect Checkin JSON POST",
|
||||||
|
URL: "/api/incidents/1/updates",
|
||||||
|
Body: BadJSON,
|
||||||
|
ExpectedContains: []string{BadJSONResponse},
|
||||||
|
BeforeTest: SetTestENV,
|
||||||
|
Method: "POST",
|
||||||
|
ExpectedStatus: 422,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Name: "Statping Delete Incident Update",
|
Name: "Statping Delete Incident Update",
|
||||||
URL: "/api/incidents/1/updates/1",
|
URL: "/api/incidents/1/updates/1",
|
||||||
|
@ -79,6 +136,14 @@ func TestIncidentsAPIRoutes(t *testing.T) {
|
||||||
BeforeTest: SetTestENV,
|
BeforeTest: SetTestENV,
|
||||||
ExpectedContains: []string{Success},
|
ExpectedContains: []string{Success},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "Incorrect JSON POST",
|
||||||
|
URL: "/api/services/1/incidents",
|
||||||
|
Body: BadJSON,
|
||||||
|
ExpectedContains: []string{BadJSONResponse},
|
||||||
|
Method: "POST",
|
||||||
|
ExpectedStatus: 422,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range tests {
|
for _, v := range tests {
|
||||||
|
|
|
@ -5,6 +5,40 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestUnAuthenticatedMessageRoutes(t *testing.T) {
|
||||||
|
tests := []HTTPTest{
|
||||||
|
{
|
||||||
|
Name: "No Authentication - New Message",
|
||||||
|
URL: "/api/messages",
|
||||||
|
Method: "POST",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "No Authentication - Update Message",
|
||||||
|
URL: "/api/messages/1",
|
||||||
|
Method: "POST",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "No Authentication - Delete Message",
|
||||||
|
URL: "/api/messages/1",
|
||||||
|
Method: "DELETE",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range tests {
|
||||||
|
t.Run(v.Name, func(t *testing.T) {
|
||||||
|
str, t, err := RunHTTPTest(v, t)
|
||||||
|
t.Logf("Test %s: \n %v\n", v.Name, str)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestMessagesApiRoutes(t *testing.T) {
|
func TestMessagesApiRoutes(t *testing.T) {
|
||||||
tests := []HTTPTest{
|
tests := []HTTPTest{
|
||||||
{
|
{
|
||||||
|
@ -57,7 +91,7 @@ func TestMessagesApiRoutes(t *testing.T) {
|
||||||
"notify_before_scale": "hour"
|
"notify_before_scale": "hour"
|
||||||
}`,
|
}`,
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: 200,
|
||||||
ExpectedContains: []string{`"status":"success"`, `"type":"message"`, `"method":"update"`},
|
ExpectedContains: []string{Success, `"type":"message"`, MethodUpdate},
|
||||||
BeforeTest: SetTestENV,
|
BeforeTest: SetTestENV,
|
||||||
SecureRoute: true,
|
SecureRoute: true,
|
||||||
},
|
},
|
||||||
|
@ -66,7 +100,7 @@ func TestMessagesApiRoutes(t *testing.T) {
|
||||||
URL: "/api/messages/1",
|
URL: "/api/messages/1",
|
||||||
Method: "DELETE",
|
Method: "DELETE",
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: 200,
|
||||||
ExpectedContains: []string{`"status":"success"`, `"method":"delete"`},
|
ExpectedContains: []string{Success, MethodDelete},
|
||||||
BeforeTest: SetTestENV,
|
BeforeTest: SetTestENV,
|
||||||
SecureRoute: true,
|
SecureRoute: true,
|
||||||
},
|
},
|
||||||
|
@ -76,6 +110,15 @@ func TestMessagesApiRoutes(t *testing.T) {
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
ExpectedStatus: 404,
|
ExpectedStatus: 404,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "Incorrect JSON POST",
|
||||||
|
URL: "/api/messages",
|
||||||
|
Body: BadJSON,
|
||||||
|
ExpectedContains: []string{BadJSONResponse},
|
||||||
|
BeforeTest: SetTestENV,
|
||||||
|
Method: "POST",
|
||||||
|
ExpectedStatus: 422,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range tests {
|
for _, v := range tests {
|
||||||
|
|
|
@ -11,6 +11,40 @@ func TestAttachment(t *testing.T) {
|
||||||
notifiers.InitNotifiers()
|
notifiers.InitNotifiers()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUnAuthenticatedNotifierRoutes(t *testing.T) {
|
||||||
|
tests := []HTTPTest{
|
||||||
|
{
|
||||||
|
Name: "No Authentication - View All Notifiers",
|
||||||
|
URL: "/api/notifiers",
|
||||||
|
Method: "GET",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "No Authentication - View Notifier",
|
||||||
|
URL: "/api/notifier/slack",
|
||||||
|
Method: "GET",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "No Authentication - Update Notifier",
|
||||||
|
URL: "/api/notifier/slack",
|
||||||
|
Method: "POST",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range tests {
|
||||||
|
t.Run(v.Name, func(t *testing.T) {
|
||||||
|
str, t, err := RunHTTPTest(v, t)
|
||||||
|
t.Logf("Test %s: \n %v\n", v.Name, str)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestApiNotifiersRoutes(t *testing.T) {
|
func TestApiNotifiersRoutes(t *testing.T) {
|
||||||
tests := []HTTPTest{
|
tests := []HTTPTest{
|
||||||
{
|
{
|
||||||
|
@ -49,7 +83,17 @@ func TestApiNotifiersRoutes(t *testing.T) {
|
||||||
ExpectedContains: []string{`"method":"slack"`},
|
ExpectedContains: []string{`"method":"slack"`},
|
||||||
BeforeTest: SetTestENV,
|
BeforeTest: SetTestENV,
|
||||||
SecureRoute: true,
|
SecureRoute: true,
|
||||||
}}
|
},
|
||||||
|
{
|
||||||
|
Name: "Incorrect JSON POST",
|
||||||
|
URL: "/api/notifier/slack",
|
||||||
|
Body: BadJSON,
|
||||||
|
ExpectedContains: []string{BadJSONResponse},
|
||||||
|
BeforeTest: SetTestENV,
|
||||||
|
Method: "POST",
|
||||||
|
ExpectedStatus: 422,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
for _, v := range tests {
|
for _, v := range tests {
|
||||||
t.Run(v.Name, func(t *testing.T) {
|
t.Run(v.Name, func(t *testing.T) {
|
||||||
|
|
|
@ -100,7 +100,6 @@ func Router() *mux.Router {
|
||||||
api.Handle("/api/services", authenticated(apiCreateServiceHandler, false)).Methods("POST")
|
api.Handle("/api/services", authenticated(apiCreateServiceHandler, false)).Methods("POST")
|
||||||
api.Handle("/api/services/{id}", scoped(apiServiceHandler)).Methods("GET")
|
api.Handle("/api/services/{id}", scoped(apiServiceHandler)).Methods("GET")
|
||||||
api.Handle("/api/reorder/services", authenticated(reorderServiceHandler, false)).Methods("POST")
|
api.Handle("/api/reorder/services", authenticated(reorderServiceHandler, false)).Methods("POST")
|
||||||
api.Handle("/api/services/{id}/running", authenticated(apiServiceRunningHandler, false)).Methods("POST")
|
|
||||||
api.Handle("/api/services/{id}", authenticated(apiServiceUpdateHandler, false)).Methods("POST")
|
api.Handle("/api/services/{id}", authenticated(apiServiceUpdateHandler, false)).Methods("POST")
|
||||||
api.Handle("/api/services/{id}", authenticated(apiServiceDeleteHandler, false)).Methods("DELETE")
|
api.Handle("/api/services/{id}", authenticated(apiServiceDeleteHandler, false)).Methods("DELETE")
|
||||||
api.Handle("/api/services/{id}/failures", scoped(apiServiceFailuresHandler)).Methods("GET")
|
api.Handle("/api/services/{id}/failures", scoped(apiServiceFailuresHandler)).Methods("GET")
|
||||||
|
|
|
@ -95,20 +95,6 @@ func apiServiceUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
sendJsonAction(service, "update", w, r)
|
sendJsonAction(service, "update", w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func apiServiceRunningHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
service, err := findService(r)
|
|
||||||
if err != nil {
|
|
||||||
sendErrorJson(err, w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if service.IsRunning() {
|
|
||||||
service.Close()
|
|
||||||
} else {
|
|
||||||
service.Start()
|
|
||||||
}
|
|
||||||
sendJsonAction(service, "running", w, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func apiServiceDataHandler(w http.ResponseWriter, r *http.Request) {
|
func apiServiceDataHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
service, err := findService(r)
|
service, err := findService(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -13,6 +13,40 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestUnAuthenticatedServicesRoutes(t *testing.T) {
|
||||||
|
tests := []HTTPTest{
|
||||||
|
{
|
||||||
|
Name: "No Authentication - New Service",
|
||||||
|
URL: "/api/services",
|
||||||
|
Method: "POST",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "No Authentication - Update Service",
|
||||||
|
URL: "/api/services/1",
|
||||||
|
Method: "POST",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "No Authentication - Delete Service",
|
||||||
|
URL: "/api/services/1",
|
||||||
|
Method: "DELETE",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range tests {
|
||||||
|
t.Run(v.Name, func(t *testing.T) {
|
||||||
|
str, t, err := RunHTTPTest(v, t)
|
||||||
|
t.Logf("Test %s: \n %v\n", v.Name, str)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestApiServiceRoutes(t *testing.T) {
|
func TestApiServiceRoutes(t *testing.T) {
|
||||||
since := utils.Now().Add(-30 * types.Day)
|
since := utils.Now().Add(-30 * types.Day)
|
||||||
end := utils.Now().Add(-30 * time.Minute)
|
end := utils.Now().Add(-30 * time.Minute)
|
||||||
|
@ -239,7 +273,7 @@ func TestApiServiceRoutes(t *testing.T) {
|
||||||
"order_id": 0
|
"order_id": 0
|
||||||
}`,
|
}`,
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: 200,
|
||||||
ExpectedContains: []string{Success, `"name":"Updated New Service"`, `"method":"update"`},
|
ExpectedContains: []string{Success, `"name":"Updated New Service"`, MethodUpdate},
|
||||||
FuncTest: func(t *testing.T) error {
|
FuncTest: func(t *testing.T) error {
|
||||||
item, err := services.Find(1)
|
item, err := services.Find(1)
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
|
@ -272,7 +306,7 @@ func TestApiServiceRoutes(t *testing.T) {
|
||||||
URL: "/api/services/1",
|
URL: "/api/services/1",
|
||||||
Method: "DELETE",
|
Method: "DELETE",
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: 200,
|
||||||
ExpectedContains: []string{Success, `"method":"delete"`},
|
ExpectedContains: []string{Success, MethodDelete},
|
||||||
FuncTest: func(t *testing.T) error {
|
FuncTest: func(t *testing.T) error {
|
||||||
count := len(services.Services())
|
count := len(services.Services())
|
||||||
if count != 6 {
|
if count != 6 {
|
||||||
|
@ -281,7 +315,17 @@ func TestApiServiceRoutes(t *testing.T) {
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
SecureRoute: true,
|
SecureRoute: true,
|
||||||
}}
|
},
|
||||||
|
{
|
||||||
|
Name: "Incorrect JSON POST",
|
||||||
|
URL: "/api/services",
|
||||||
|
Body: BadJSON,
|
||||||
|
ExpectedContains: []string{BadJSONResponse},
|
||||||
|
BeforeTest: SetTestENV,
|
||||||
|
Method: "POST",
|
||||||
|
ExpectedStatus: 422,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
for _, v := range tests {
|
for _, v := range tests {
|
||||||
t.Run(v.Name, func(t *testing.T) {
|
t.Run(v.Name, func(t *testing.T) {
|
||||||
|
|
|
@ -32,13 +32,8 @@ func processSetupHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
log.WithFields(utils.ToFields(core.App, confgs)).Debugln("new configs posted")
|
log.WithFields(utils.ToFields(core.App, confgs)).Debugln("new configs posted")
|
||||||
|
|
||||||
if err = configs.ConnectConfigs(confgs); err != nil {
|
if err = configs.ConnectConfigs(confgs, false); err != nil {
|
||||||
log.Errorln(err)
|
log.Errorln(err)
|
||||||
if err := confgs.Delete(); err != nil {
|
|
||||||
log.Errorln(err)
|
|
||||||
sendErrorJson(err, w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
sendErrorJson(err, w, r)
|
sendErrorJson(err, w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,47 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestUnAuthenticatedThemeRoutes(t *testing.T) {
|
||||||
|
tests := []HTTPTest{
|
||||||
|
{
|
||||||
|
Name: "No Authentication - Create Themes",
|
||||||
|
URL: "/api/theme/create",
|
||||||
|
Method: "GET",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "No Authentication - View Themes",
|
||||||
|
URL: "/api/theme",
|
||||||
|
Method: "GET",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "No Authentication - Update Themes",
|
||||||
|
URL: "/api/theme",
|
||||||
|
Method: "POST",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "No Authentication - Delete Themes",
|
||||||
|
URL: "/api/theme",
|
||||||
|
Method: "DELETE",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range tests {
|
||||||
|
t.Run(v.Name, func(t *testing.T) {
|
||||||
|
str, t, err := RunHTTPTest(v, t)
|
||||||
|
t.Logf("Test %s: \n %v\n", v.Name, str)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestThemeRoutes(t *testing.T) {
|
func TestThemeRoutes(t *testing.T) {
|
||||||
tests := []HTTPTest{
|
tests := []HTTPTest{
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,11 +1,54 @@
|
||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/statping/statping/utils"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"net/url"
|
"net/url"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestUnAuthenticatedUserRoutes(t *testing.T) {
|
||||||
|
tests := []HTTPTest{
|
||||||
|
{
|
||||||
|
Name: "No Authentication - New User",
|
||||||
|
URL: "/api/users",
|
||||||
|
Method: "POST",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "No Authentication - Update User",
|
||||||
|
URL: "/api/users/1",
|
||||||
|
Method: "POST",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "No Authentication - View User",
|
||||||
|
URL: "/api/users/1",
|
||||||
|
Method: "GET",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "No Authentication - Delete User",
|
||||||
|
URL: "/api/users/1",
|
||||||
|
Method: "DELETE",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range tests {
|
||||||
|
t.Run(v.Name, func(t *testing.T) {
|
||||||
|
str, t, err := RunHTTPTest(v, t)
|
||||||
|
t.Logf("Test %s: \n %v\n", v.Name, str)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestApiUsersRoutes(t *testing.T) {
|
func TestApiUsersRoutes(t *testing.T) {
|
||||||
form := url.Values{}
|
form := url.Values{}
|
||||||
form.Add("username", "adminupdated")
|
form.Add("username", "adminupdated")
|
||||||
|
@ -16,12 +59,29 @@ func TestApiUsersRoutes(t *testing.T) {
|
||||||
badForm.Add("password", "wrongpassword")
|
badForm.Add("password", "wrongpassword")
|
||||||
|
|
||||||
tests := []HTTPTest{
|
tests := []HTTPTest{
|
||||||
|
{
|
||||||
|
Name: "Check Basic Authentication",
|
||||||
|
URL: "/api",
|
||||||
|
Method: "GET",
|
||||||
|
ExpectedStatus: 401,
|
||||||
|
BeforeTest: func(t *testing.T) error {
|
||||||
|
utils.Params.Set("AUTH_USERNAME", "admin")
|
||||||
|
utils.Params.Set("AUTH_PASSWORD", "admin")
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
AfterTest: func(t *testing.T) error {
|
||||||
|
utils.Params.Set("AUTH_USERNAME", "")
|
||||||
|
utils.Params.Set("AUTH_PASSWORD", "")
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Name: "Statping All Users",
|
Name: "Statping All Users",
|
||||||
URL: "/api/users",
|
URL: "/api/users",
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: 200,
|
||||||
ResponseLen: 1,
|
ResponseLen: 1,
|
||||||
|
BeforeTest: SetTestENV,
|
||||||
}, {
|
}, {
|
||||||
Name: "Statping Create User",
|
Name: "Statping Create User",
|
||||||
URL: "/api/users",
|
URL: "/api/users",
|
||||||
|
@ -33,12 +93,18 @@ func TestApiUsersRoutes(t *testing.T) {
|
||||||
"password": "passsword123",
|
"password": "passsword123",
|
||||||
"admin": true
|
"admin": true
|
||||||
}`,
|
}`,
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: 200,
|
||||||
|
ExpectedContains: []string{Success, MethodCreate},
|
||||||
}, {
|
}, {
|
||||||
Name: "Statping View User",
|
Name: "Statping View User",
|
||||||
URL: "/api/users/1",
|
URL: "/api/users/1",
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: 200,
|
||||||
|
}, {
|
||||||
|
Name: "Statping Incorrect User ID",
|
||||||
|
URL: "/api/users/NOinteger",
|
||||||
|
Method: "GET",
|
||||||
|
ExpectedStatus: 422,
|
||||||
}, {
|
}, {
|
||||||
Name: "Statping Missing User",
|
Name: "Statping Missing User",
|
||||||
URL: "/api/users/9393939393",
|
URL: "/api/users/9393939393",
|
||||||
|
@ -54,12 +120,14 @@ func TestApiUsersRoutes(t *testing.T) {
|
||||||
"password": "password12345",
|
"password": "password12345",
|
||||||
"admin": true
|
"admin": true
|
||||||
}`,
|
}`,
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: 200,
|
||||||
|
ExpectedContains: []string{Success, MethodUpdate},
|
||||||
}, {
|
}, {
|
||||||
Name: "Statping Delete User",
|
Name: "Statping Delete User",
|
||||||
URL: "/api/users/2",
|
URL: "/api/users/2",
|
||||||
Method: "DELETE",
|
Method: "DELETE",
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: 200,
|
||||||
|
ExpectedContains: []string{Success, MethodDelete},
|
||||||
}, {
|
}, {
|
||||||
Name: "Statping Login User",
|
Name: "Statping Login User",
|
||||||
URL: "/api/login",
|
URL: "/api/login",
|
||||||
|
@ -81,7 +149,17 @@ func TestApiUsersRoutes(t *testing.T) {
|
||||||
URL: "/api/logout",
|
URL: "/api/logout",
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
ExpectedStatus: 303,
|
ExpectedStatus: 303,
|
||||||
}}
|
},
|
||||||
|
{
|
||||||
|
Name: "Incorrect JSON POST",
|
||||||
|
URL: "/api/users",
|
||||||
|
Body: BadJSON,
|
||||||
|
ExpectedContains: []string{BadJSONResponse},
|
||||||
|
BeforeTest: SetTestENV,
|
||||||
|
Method: "POST",
|
||||||
|
ExpectedStatus: 422,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
for _, v := range tests {
|
for _, v := range tests {
|
||||||
t.Run(v.Name, func(t *testing.T) {
|
t.Run(v.Name, func(t *testing.T) {
|
||||||
|
|
|
@ -23,6 +23,15 @@ func TestCore_UsingAssets(t *testing.T) {
|
||||||
assert.False(t, UsingAssets(dir))
|
assert.False(t, UsingAssets(dir))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCore_UsingAssetsTrue(t *testing.T) {
|
||||||
|
utils.Params.Set("USE_ASSETS", true)
|
||||||
|
assert.False(t, UsingAssets(dir))
|
||||||
|
assert.DirExists(t, dir+"/assets")
|
||||||
|
utils.Params.Set("USE_ASSETS", false)
|
||||||
|
err := utils.DeleteDirectory(dir + "/assets")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
func TestCreateAssets(t *testing.T) {
|
func TestCreateAssets(t *testing.T) {
|
||||||
assert.Nil(t, CreateAllAssets(dir))
|
assert.Nil(t, CreateAllAssets(dir))
|
||||||
assert.True(t, UsingAssets(dir))
|
assert.True(t, UsingAssets(dir))
|
||||||
|
|
|
@ -32,6 +32,8 @@ type CheckinHit struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Checkin) BeforeCreate() (err error) {
|
func (c *Checkin) BeforeCreate() (err error) {
|
||||||
c.ApiKey = utils.RandomString(7)
|
if c.ApiKey == "" {
|
||||||
|
c.ApiKey = utils.RandomString(7)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,8 +10,8 @@ import (
|
||||||
|
|
||||||
var log = utils.Log
|
var log = utils.Log
|
||||||
|
|
||||||
func ConnectConfigs(configs *DbConfig) error {
|
func ConnectConfigs(configs *DbConfig, retry bool) error {
|
||||||
err := Connect(configs, true)
|
err := Connect(configs, retry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "error connecting to database")
|
return errors.Wrap(err, "error connecting to database")
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,32 @@ func TestCreateLog(t *testing.T) {
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestToFields(t *testing.T) {
|
||||||
|
example := struct {
|
||||||
|
Id int64
|
||||||
|
Str string
|
||||||
|
}{
|
||||||
|
Id: 1,
|
||||||
|
Str: "example input",
|
||||||
|
}
|
||||||
|
fields := ToFields(example)
|
||||||
|
|
||||||
|
assert.Equal(t, "", fields)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetLastLine(t *testing.T) {
|
||||||
|
last := GetLastLine()
|
||||||
|
require.NotNil(t, last)
|
||||||
|
assert.Equal(t, "", last.lineAsString())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReplaceValue(t *testing.T) {
|
||||||
|
assert.Equal(t, true, replaceVal(true))
|
||||||
|
assert.Equal(t, 42, replaceVal(42))
|
||||||
|
assert.Equal(t, "hello world", replaceVal("hello world"))
|
||||||
|
assert.Equal(t, "5s", replaceVal(time.Duration(5*time.Second)))
|
||||||
|
}
|
||||||
|
|
||||||
func TestInitLogs(t *testing.T) {
|
func TestInitLogs(t *testing.T) {
|
||||||
assert.Nil(t, InitLogs())
|
assert.Nil(t, InitLogs())
|
||||||
Log.Infoln("this is a test")
|
Log.Infoln("this is a test")
|
||||||
|
|
Loading…
Reference in New Issue