more tests

pull/508/head
hunterlong 2020-04-16 10:55:47 -07:00
parent cead240508
commit 38601e7652
10 changed files with 77 additions and 66 deletions

View File

@ -3,6 +3,7 @@
- Added postman (newman) API testing
- Added Viper and Cobra config/env parsing package
- Added more golang tests
- Modified handlers to use a more generic find method
# 0.90.27 (04-15-2020)
- Fixed postgres database table creation process

View File

@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/statping/statping/handlers"
"github.com/statping/statping/source"
"github.com/statping/statping/types/checkins"
"github.com/statping/statping/types/configs"
@ -121,7 +120,7 @@ func importCli(args []string) error {
if err = configs.ConnectConfigs(config); err != nil {
return err
}
if data, err = handlers.ExportSettings(); err != nil {
if data, err = ExportSettings(); err != nil {
return fmt.Errorf("could not export settings: %v", err.Error())
}

View File

@ -1,7 +1,7 @@
package handlers
import (
"encoding/json"
"errors"
"fmt"
"github.com/gorilla/mux"
"github.com/statping/statping/types/checkins"
@ -11,16 +11,27 @@ import (
"net/http"
)
func findCheckin(r *http.Request) (*checkins.Checkin, string, error) {
vars := mux.Vars(r)
if vars["api"] == "" {
return nil, "", errors.New("missing checkin API in URL")
}
checkin, err := checkins.FindByAPI(vars["api"])
if err != nil {
return nil, vars["api"], err
}
return checkin, vars["api"], nil
}
func apiAllCheckinsHandler(w http.ResponseWriter, r *http.Request) {
chks := checkins.All()
returnJson(chks, w, r)
}
func apiCheckinHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
checkin, err := checkins.FindByAPI(vars["api"])
checkin, id, err := findCheckin(r)
if err != nil {
sendErrorJson(fmt.Errorf("checkin %v was not found", vars["api"]), w, r)
sendErrorJson(fmt.Errorf("checkin %v was not found", id), w, r)
return
}
returnJson(checkin, w, r)
@ -28,8 +39,7 @@ func apiCheckinHandler(w http.ResponseWriter, r *http.Request) {
func checkinCreateHandler(w http.ResponseWriter, r *http.Request) {
var checkin *checkins.Checkin
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&checkin)
err := DecodeJSON(r, &checkin)
if err != nil {
sendErrorJson(err, w, r)
return
@ -48,10 +58,9 @@ func checkinCreateHandler(w http.ResponseWriter, r *http.Request) {
}
func checkinHitHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
checkin, err := checkins.FindByAPI(vars["api"])
checkin, id, err := findCheckin(r)
if err != nil {
sendErrorJson(fmt.Errorf("checkin %s was not found", vars["api"]), w, r)
sendErrorJson(fmt.Errorf("checkin %s was not found", id), w, r)
return
}
ip, _, _ := net.SplitHostPort(r.RemoteAddr)
@ -65,7 +74,7 @@ func checkinHitHandler(w http.ResponseWriter, r *http.Request) {
err = hit.Create()
if err != nil {
sendErrorJson(fmt.Errorf("checkin %v was not found", vars["api"]), w, r)
sendErrorJson(fmt.Errorf("checkin %v was not found", id), w, r)
return
}
checkin.Failing = false
@ -74,10 +83,9 @@ func checkinHitHandler(w http.ResponseWriter, r *http.Request) {
}
func checkinDeleteHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
checkin, err := checkins.FindByAPI(vars["api"])
checkin, id, err := findCheckin(r)
if err != nil {
sendErrorJson(fmt.Errorf("checkin %v was not found", vars["api"]), w, r)
sendErrorJson(fmt.Errorf("checkin %v was not found", id), w, r)
return
}

View File

@ -8,9 +8,12 @@ import (
"net/http"
)
func selectGroup(r *http.Request) (*groups.Group, error) {
func findGroup(r *http.Request) (*groups.Group, error) {
vars := mux.Vars(r)
id := utils.ToInt(vars["id"])
if id == 0 {
return nil, errors.New("missing group id")
}
g, err := groups.Find(id)
if err != nil {
return nil, err
@ -26,7 +29,7 @@ func apiAllGroupHandler(r *http.Request) interface{} {
// apiGroupHandler will show a single group
func apiGroupHandler(w http.ResponseWriter, r *http.Request) {
group, err := selectGroup(r)
group, err := findGroup(r)
if err != nil {
sendErrorJson(errors.Wrap(err, "group not found"), w, r, http.StatusNotFound)
return
@ -36,7 +39,7 @@ func apiGroupHandler(w http.ResponseWriter, r *http.Request) {
// apiGroupUpdateHandler will update a group
func apiGroupUpdateHandler(w http.ResponseWriter, r *http.Request) {
group, err := selectGroup(r)
group, err := findGroup(r)
if err != nil {
w.WriteHeader(http.StatusNotFound)
sendErrorJson(errors.Wrap(err, "group not found"), w, r)
@ -74,7 +77,7 @@ func apiCreateGroupHandler(w http.ResponseWriter, r *http.Request) {
// apiGroupDeleteHandler accepts a DELETE method to delete groups
func apiGroupDeleteHandler(w http.ResponseWriter, r *http.Request) {
group, err := selectGroup(r)
group, err := findGroup(r)
if err != nil {
sendErrorJson(errors.Wrap(err, "group not found"), w, r)
return

View File

@ -1,14 +1,26 @@
package handlers
import (
"encoding/json"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/statping/statping/types/incidents"
"github.com/statping/statping/types/services"
"github.com/statping/statping/utils"
"net/http"
)
func findIncident(r *http.Request) (*incidents.Incident, int64, error) {
vars := mux.Vars(r)
id := utils.ToInt(vars["id"])
if id == 0 {
return nil, id, errors.New("missing checkin API in URL")
}
checkin, err := incidents.Find(id)
if err != nil {
return nil, id, err
}
return checkin, id, nil
}
func apiServiceIncidentsHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
incids := incidents.FindByService(utils.ToInt(vars["id"]))
@ -16,8 +28,7 @@ func apiServiceIncidentsHandler(w http.ResponseWriter, r *http.Request) {
}
func apiIncidentUpdatesHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
incid, err := incidents.Find(utils.ToInt(vars["id"]))
incid, _, err := findIncident(r)
if err != nil {
sendErrorJson(err, w, r)
return
@ -26,16 +37,19 @@ func apiIncidentUpdatesHandler(w http.ResponseWriter, r *http.Request) {
}
func apiCreateIncidentUpdateHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
var update *incidents.IncidentUpdate
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&update)
incid, _, err := findIncident(r)
if err != nil {
sendErrorJson(err, w, r)
return
}
update.IncidentId = utils.ToInt(vars["id"])
var update *incidents.IncidentUpdate
if err := DecodeJSON(r, &update); err != nil {
sendErrorJson(err, w, r)
return
}
update.IncidentId = incid.Id
err = update.Create()
if err != nil {
@ -46,16 +60,13 @@ func apiCreateIncidentUpdateHandler(w http.ResponseWriter, r *http.Request) {
}
func apiCreateIncidentHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
service, err := services.Find(utils.ToInt(vars["id"]))
service, err := findService(r)
if err != nil {
sendErrorJson(err, w, r)
return
}
var incident *incidents.Incident
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&incident); err != nil {
if err := DecodeJSON(r, &incident); err != nil {
sendErrorJson(err, w, r)
return
}
@ -69,16 +80,12 @@ func apiCreateIncidentHandler(w http.ResponseWriter, r *http.Request) {
}
func apiIncidentUpdateHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
incident, err := incidents.Find(utils.ToInt(vars["id"]))
incident, _, err := findIncident(r)
if err != nil {
sendErrorJson(err, w, r)
return
}
decoder := json.NewDecoder(r.Body)
err = decoder.Decode(&incident)
if err != nil {
if err := DecodeJSON(r, &incident); err != nil {
sendErrorJson(err, w, r)
return
}
@ -88,8 +95,7 @@ func apiIncidentUpdateHandler(w http.ResponseWriter, r *http.Request) {
}
func apiDeleteIncidentHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
incident, err := incidents.Find(utils.ToInt(vars["id"]))
incident, _, err := findIncident(r)
if err != nil {
sendErrorJson(err, w, r)
return

View File

@ -8,7 +8,7 @@ import (
"net/http"
)
func getMessageByID(r *http.Request) (*messages.Message, int64, error) {
func findMessage(r *http.Request) (*messages.Message, int64, error) {
vars := mux.Vars(r)
num := utils.ToInt(vars["id"])
message, err := messages.Find(num)
@ -37,7 +37,7 @@ func apiMessageCreateHandler(w http.ResponseWriter, r *http.Request) {
}
func apiMessageGetHandler(r *http.Request) interface{} {
message, id, err := getMessageByID(r)
message, id, err := findMessage(r)
if err != nil {
return fmt.Errorf("message #%d was not found", id)
}
@ -45,7 +45,7 @@ func apiMessageGetHandler(r *http.Request) interface{} {
}
func apiMessageDeleteHandler(w http.ResponseWriter, r *http.Request) {
message, id, err := getMessageByID(r)
message, id, err := findMessage(r)
if err != nil {
sendErrorJson(fmt.Errorf("message #%d was not found", id), w, r)
return
@ -59,7 +59,7 @@ func apiMessageDeleteHandler(w http.ResponseWriter, r *http.Request) {
}
func apiMessageUpdateHandler(w http.ResponseWriter, r *http.Request) {
message, id, err := getMessageByID(r)
message, id, err := findMessage(r)
if err != nil {
sendErrorJson(fmt.Errorf("message #%d was not found", id), w, r)
return

View File

@ -1,7 +1,6 @@
package handlers
import (
"encoding/json"
"github.com/gorilla/mux"
"github.com/statping/statping/types/notifications"
"github.com/statping/statping/types/services"
@ -41,12 +40,11 @@ func apiNotifierUpdateHandler(w http.ResponseWriter, r *http.Request) {
return
}
decoder := json.NewDecoder(r.Body)
err = decoder.Decode(&notifer)
if err != nil {
if err := DecodeJSON(r, &notifer); err != nil {
sendErrorJson(err, w, r)
return
}
err = notifer.Update()
if err != nil {
sendErrorJson(err, w, r)
@ -64,9 +62,7 @@ func testNotificationHandler(w http.ResponseWriter, r *http.Request) {
return
}
decoder := json.NewDecoder(r.Body)
err = decoder.Decode(&notifer)
if err != nil {
if err := DecodeJSON(r, &notifer); err != nil {
sendErrorJson(err, w, r)
return
}

View File

@ -16,7 +16,7 @@ type serviceOrder struct {
Order int `json:"order"`
}
func serviceByID(r *http.Request) (*services.Service, error) {
func findService(r *http.Request) (*services.Service, error) {
vars := mux.Vars(r)
id := utils.ToInt(vars["id"])
servicer, err := services.Find(id)
@ -47,7 +47,7 @@ func reorderServiceHandler(w http.ResponseWriter, r *http.Request) {
}
func apiServiceHandler(r *http.Request) interface{} {
srv, err := serviceByID(r)
srv, err := findService(r)
if err != nil {
return err
}
@ -76,7 +76,7 @@ func apiCreateServiceHandler(w http.ResponseWriter, r *http.Request) {
}
func apiServiceUpdateHandler(w http.ResponseWriter, r *http.Request) {
service, err := serviceByID(r)
service, err := findService(r)
if err != nil {
sendErrorJson(err, w, r, http.StatusNotFound)
return
@ -96,7 +96,7 @@ func apiServiceUpdateHandler(w http.ResponseWriter, r *http.Request) {
}
func apiServiceRunningHandler(w http.ResponseWriter, r *http.Request) {
service, err := serviceByID(r)
service, err := findService(r)
if err != nil {
sendErrorJson(err, w, r)
return
@ -155,7 +155,7 @@ func apiServiceFailureDataHandler(w http.ResponseWriter, r *http.Request) {
}
func apiServicePingDataHandler(w http.ResponseWriter, r *http.Request) {
service, err := serviceByID(r)
service, err := findService(r)
if err != nil {
sendErrorJson(errors.New("service data not found"), w, r)
return
@ -177,7 +177,7 @@ func apiServicePingDataHandler(w http.ResponseWriter, r *http.Request) {
}
func apiServiceTimeDataHandler(w http.ResponseWriter, r *http.Request) {
service, err := serviceByID(r)
service, err := findService(r)
if err != nil {
sendErrorJson(errors.New("service data not found"), w, r)
return
@ -217,7 +217,7 @@ func apiServiceTimeDataHandler(w http.ResponseWriter, r *http.Request) {
}
func apiServiceDeleteHandler(w http.ResponseWriter, r *http.Request) {
service, err := serviceByID(r)
service, err := findService(r)
if err != nil {
sendErrorJson(err, w, r)
return
@ -244,7 +244,7 @@ func apiAllServicesHandler(r *http.Request) interface{} {
}
func servicesDeleteFailuresHandler(w http.ResponseWriter, r *http.Request) {
service, err := serviceByID(r)
service, err := findService(r)
if err != nil {
sendErrorJson(err, w, r)
return

View File

@ -9,7 +9,7 @@ import (
"net/http"
)
func getUser(r *http.Request) (*users.User, int64, error) {
func findUser(r *http.Request) (*users.User, int64, error) {
vars := mux.Vars(r)
num := utils.ToInt(vars["id"])
user, err := users.Find(num)
@ -20,7 +20,7 @@ func getUser(r *http.Request) (*users.User, int64, error) {
}
func apiUserHandler(w http.ResponseWriter, r *http.Request) {
user, _, err := getUser(r)
user, _, err := findUser(r)
if err != nil {
sendErrorJson(err, w, r, http.StatusNotFound)
return
@ -30,7 +30,7 @@ func apiUserHandler(w http.ResponseWriter, r *http.Request) {
}
func apiUserUpdateHandler(w http.ResponseWriter, r *http.Request) {
user, id, err := getUser(r)
user, id, err := findUser(r)
if err != nil {
sendErrorJson(fmt.Errorf("user #%d was not found", id), w, r)
return
@ -60,7 +60,7 @@ func apiUserDeleteHandler(w http.ResponseWriter, r *http.Request) {
sendErrorJson(errors.New("cannot delete the last user"), w, r)
return
}
user, _, err := getUser(r)
user, _, err := findUser(r)
if err != nil {
sendErrorJson(err, w, r)
return

View File

@ -102,12 +102,10 @@ func (d *DbConfig) CreateDatabase() error {
log.Infoln("Creating Database Tables...")
for _, table := range DbModels {
log.Infof("Creating table '%T'", table)
if err := d.Db.CreateTable(table); err.Error() != nil {
return errors.Wrap(err.Error(), fmt.Sprintf("error creating '%T' table", table))
}
}
log.Infof("Creating table 'core'")
if err := d.Db.Table("core").CreateTable(&core.Core{}); err.Error() != nil {
return errors.Wrap(err.Error(), fmt.Sprintf("error creating 'core' table"))
}