pull/471/head v0.90.19
Hunter Long 2020-03-28 18:21:32 -07:00
parent 30d70cb6ac
commit ff887d9e51
13 changed files with 67 additions and 107 deletions

View File

@ -1,6 +1,9 @@
# 0.90.19
- Fixed private Services from showing in API (/api/services and /api/services/{id})
- Removed unused code
# 0.90.18
- Added service type gRPC, you can now check on gRPC services. (limited)
-
# 0.90.17
- Fixed notification fields for frontend

View File

@ -238,6 +238,8 @@
delete s.online_24_hours
s.check_interval = parseInt(s.check_interval)
window.console.log(s)
if (s.id) {
await this.updateService(s)
} else {

View File

@ -23,10 +23,6 @@ import (
"testing"
)
const (
serverDomain = "http://localhost:18888"
)
var (
dir string
)
@ -180,13 +176,14 @@ func TestMainApiRoutes(t *testing.T) {
// Name: "Prometheus Export Metrics",
// URL: "/metrics",
// Method: "GET",
// BeforeTest: SetTestENV,
// ExpectedStatus: 200,
// ExpectedContains: []string{
// `Statping Totals`,
// `total_failures`,
// `Golang Metrics`,
// },
//}
//},
}
for _, v := range tests {
@ -198,6 +195,26 @@ func TestMainApiRoutes(t *testing.T) {
}
}
//func TestExportSettings(t *testing.T) {
// data, err := ExportSettings()
// require.Nil(t, err)
// assert.Len(t, data, 50)
//
// var exportData ExportData
// err = json.Unmarshal(data, &exportData)
// require.Nil(t, err)
//
// assert.Len(t, exportData.Services, 4)
// assert.Len(t, exportData.Messages, 4)
// assert.Len(t, exportData.Checkins, 2)
// assert.Len(t, exportData.Groups, 1)
//
// assert.Equal(t, "Updated Core", exportData.Core.Name)
// assert.True(t, exportData.Core.Setup)
// assert.NotEmpty(t, exportData.Core.ApiKey)
// assert.NotEmpty(t, exportData.Core.ApiSecret)
//}
type HttpFuncTest func(*testing.T) error
// HTTPTest contains all the parameters for a HTTP Unit Test

View File

@ -180,7 +180,7 @@ func removeJwtToken(w http.ResponseWriter) {
http.SetCookie(w, &http.Cookie{
Name: cookieKey,
Value: "",
Expires: time.Now().UTC(),
Expires: utils.Now(),
})
}

View File

@ -1,47 +0,0 @@
package handlers
import (
"encoding/json"
"github.com/pkg/errors"
"net/http"
)
type Error struct {
err error
code int
}
func (e Error) Error() string {
return e.err.Error()
}
var (
NewError = func(e error) Error {
return Error{
err: e,
code: http.StatusInternalServerError,
}
}
NotFound = func(err error) Error {
return Error{
err: errors.Wrap(err, "not found"),
code: http.StatusNotFound,
}
}
Unauthorized = func(e error) Error {
return Error{
err: e,
code: http.StatusUnauthorized,
}
}
)
func RespondError(w http.ResponseWriter, err Error) {
output := apiResponse{
Status: "error",
Error: err.Error(),
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(err.code)
json.NewEncoder(w).Encode(output)
}

View File

@ -1,7 +1,6 @@
package handlers
import (
"encoding/json"
"github.com/statping/statping/source"
"github.com/statping/statping/types/core"
"github.com/statping/statping/utils"
@ -14,56 +13,11 @@ var (
basePath = "/"
)
type CustomResponseWriter struct {
body []byte
statusCode int
header http.Header
}
func NewCustomResponseWriter() *CustomResponseWriter {
return &CustomResponseWriter{
header: http.Header{},
}
}
func (w *CustomResponseWriter) Header() http.Header {
return w.header
}
func (w *CustomResponseWriter) Write(b []byte) (int, error) {
w.body = b
// implement it as per your requirement
return 0, nil
}
func (w *CustomResponseWriter) WriteHeader(statusCode int) {
w.statusCode = statusCode
}
func parseForm(r *http.Request) url.Values {
r.ParseForm()
return r.PostForm
}
func parseGet(r *http.Request) url.Values {
r.ParseForm()
return r.Form
}
func decodeRequest(r *http.Request, object interface{}) error {
decoder := json.NewDecoder(r.Body)
defer r.Body.Close()
return decoder.Decode(&object)
}
type parsedObject struct {
Error Error
}
func serviceFromID(r *http.Request, object interface{}) error {
return nil
}
var handlerFuncs = func(w http.ResponseWriter, r *http.Request) template.FuncMap {
return template.FuncMap{
"VERSION": func() string {

View File

@ -205,10 +205,13 @@ func IsUser(r *http.Request) bool {
if os.Getenv("GO_ENV") == "test" {
return true
}
_, err := getJwtToken(r)
tk, err := getJwtToken(r)
if err != nil {
return false
}
if err := tk.Valid(); err != nil {
return false
}
return true
}

View File

@ -93,6 +93,11 @@ func sendLog(next http.Handler) http.Handler {
func scoped(handler func(r *http.Request) interface{}) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data := handler(r)
err, ok := data.(error)
if ok {
sendErrorJson(err, w, r)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(scope{data: data, scope: ScopeName(r)})
})

View File

@ -16,6 +16,7 @@
package handlers
import (
"fmt"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/statping/statping/database"
@ -66,6 +67,10 @@ func apiServiceHandler(r *http.Request) interface{} {
if err != nil {
return err
}
user := IsUser(r)
if !srv.Public.Bool && !user {
return errors.New("not authenticated")
}
srv = srv.UpdateStats()
return *srv
}
@ -203,7 +208,16 @@ func apiServiceDeleteHandler(w http.ResponseWriter, r *http.Request) {
}
func apiAllServicesHandler(r *http.Request) interface{} {
return services.AllInOrder()
user := IsUser(r)
fmt.Println("user: ", user)
var srvs []services.Service
for _, v := range services.AllInOrder() {
if !v.Public.Bool && !user {
continue
}
srvs = append(srvs, v)
}
return srvs
}
func servicesDeleteFailuresHandler(w http.ResponseWriter, r *http.Request) {

View File

@ -39,7 +39,7 @@ func TestApiServiceRoutes(t *testing.T) {
Method: "GET",
ExpectedContains: []string{`"name":"Google"`},
ExpectedStatus: 200,
ResponseLen: 5,
ResponseLen: 4,
BeforeTest: UnsetTestENV,
FuncTest: func(t *testing.T) error {
count := len(services.Services())
@ -55,14 +55,15 @@ func TestApiServiceRoutes(t *testing.T) {
Method: "GET",
ExpectedContains: []string{`"name":"Google"`},
ExpectedStatus: 200,
BeforeTest: UnsetTestENV,
},
{
Name: "Statping Private Service 1",
URL: "/api/services/1",
URL: "/api/services/2",
Method: "GET",
ExpectedContains: []string{`"name":"Google"`},
ExpectedContains: []string{`"error":"not authenticated"`},
ExpectedStatus: 200,
BeforeTest: SetTestENV,
BeforeTest: UnsetTestENV,
},
{
Name: "Statping Service 1 with Private responses",

View File

@ -18,6 +18,7 @@ func Samples() error {
Timeout: 10,
Order: 1,
GroupId: 1,
Public: null.NewNullBool(true),
Permalink: null.NewNullString("google"),
VerifySSL: null.NewNullBool(true),
NotifyAfter: 3,
@ -36,6 +37,7 @@ func Samples() error {
Method: "GET",
Timeout: 20,
Order: 2,
Public: null.NewNullBool(false),
Permalink: null.NewNullString("statping_github"),
VerifySSL: null.NewNullBool(true),
NotifyAfter: 1,

View File

@ -91,6 +91,12 @@ func TestSaveFile(t *testing.T) {
assert.Nil(t, SaveFile(Directory+"/test.txt", []byte("testing saving a file")))
}
func TestOpenFile(t *testing.T) {
f, err := OpenFile(Directory + "/test.txt")
require.Nil(t, err)
assert.Equal(t, "testing saving a file", f)
}
func TestFileExists(t *testing.T) {
assert.True(t, FileExists(Directory+"/test.txt"))
assert.False(t, FileExists(Directory+"fake.txt"))

View File

@ -1 +1 @@
0.90.18
0.90.19