pull/429/head
hunterlong 2020-03-08 14:32:26 -07:00
parent 6e8e74b60d
commit a14fb1f616
14 changed files with 43 additions and 30 deletions

View File

@ -1,3 +1,5 @@
// +build int
package handlers
import (

View File

@ -1,3 +1,5 @@
// +build int
package handlers
import (

View File

@ -53,6 +53,7 @@ func apiGroupHandler(w http.ResponseWriter, r *http.Request) {
func apiGroupUpdateHandler(w http.ResponseWriter, r *http.Request) {
group, err := selectGroup(r)
if err != nil {
w.WriteHeader(http.StatusNotFound)
sendErrorJson(errors.Wrap(err, "group not found"), w, r)
return
}

View File

@ -1,3 +1,5 @@
// +build int
package handlers
import (
@ -16,14 +18,6 @@ func TestGroupAPIRoutes(t *testing.T) {
BeforeTest: SetTestENV,
AfterTest: UnsetTestENV,
},
{
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",
@ -55,6 +49,14 @@ func TestGroupAPIRoutes(t *testing.T) {
Method: "POST",
ExpectedStatus: 200,
},
{
Name: "Statping Public and Private Groups",
URL: "/api/groups",
Method: "GET",
ExpectedStatus: 200,
ResponseLen: 2,
BeforeTest: UnsetTestENV,
},
{
Name: "Statping View Private Group",
URL: "/api/groups/2",

View File

@ -51,13 +51,12 @@ func apiMessageCreateHandler(w http.ResponseWriter, r *http.Request) {
sendJsonAction(message, "create", w, r)
}
func apiMessageGetHandler(w http.ResponseWriter, r *http.Request) {
func apiMessageGetHandler(r *http.Request) interface{} {
message, id, err := getMessageByID(r)
if err != nil {
sendErrorJson(fmt.Errorf("message #%d was not found", id), w, r)
return
return fmt.Errorf("message #%d was not found", id)
}
returnJson(message, w, r)
return message
}
func apiMessageDeleteHandler(w http.ResponseWriter, r *http.Request) {

View File

@ -1,3 +1,5 @@
// +build int
package handlers
import (

View File

@ -103,7 +103,6 @@ func scoped(handler func(r *http.Request) interface{}) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data := handler(r)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(scope{data: data, scope: ScopeName(r)})
})
}

View File

@ -1,10 +1,19 @@
// +build int
package handlers
import (
"github.com/hunterlong/statping/notifiers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)
func TestAttachment(t *testing.T) {
err := notifiers.AttachNotifiers()
require.Nil(t, err)
}
func TestApiNotifiersRoutes(t *testing.T) {
tests := []HTTPTest{
{

View File

@ -156,7 +156,7 @@ func Router() *mux.Router {
// API MESSAGES Routes
api.Handle("/api/messages", scoped(apiAllMessagesHandler)).Methods("GET")
api.Handle("/api/messages", authenticated(apiMessageCreateHandler, false)).Methods("POST")
api.Handle("/api/messages/{id}", readOnly(apiMessageGetHandler, false)).Methods("GET")
api.Handle("/api/messages/{id}", scoped(apiMessageGetHandler)).Methods("GET")
api.Handle("/api/messages/{id}", authenticated(apiMessageUpdateHandler, false)).Methods("POST")
api.Handle("/api/messages/{id}", authenticated(apiMessageDeleteHandler, false)).Methods("DELETE")

View File

@ -1,3 +1,5 @@
// +build int
package handlers
import (

View File

@ -1,3 +1,5 @@
// +build int
package handlers
import (

View File

@ -150,24 +150,18 @@ func AddIntegrations(inte ...integrations.Integrator) error {
// install will check the database for the notification, if its not inserted it will insert a new record for it
func install(i integrations.Integrator) error {
inDb := isInDatabase(i)
log.WithField("installed", inDb).
WithFields(utils.ToFields(i)).
Debugln(fmt.Sprintf("Checking if integrator '%v' is installed: %v", i.Get().Name, inDb))
if !inDb {
_, err := insertDatabase(i)
if err != nil {
log.Errorln(err)
return err
}
_, err := insertDatabase(i)
if err != nil {
log.Errorln(err)
return err
}
return nil
return err
}
// insertDatabase will create a new record into the database for the integrator
func insertDatabase(i integrations.Integrator) (string, error) {
integrator := i.Get()
query := db.Create(integrator)
query := db.FirstOrCreate(integrator)
if query.Error() != nil {
return "", query.Error()
}

View File

@ -26,7 +26,7 @@ func All() []*Notification {
}
func (n *Notification) Create() error {
db := DB().Create(n)
db := DB().FirstOrCreate(&n)
return db.Error()
}

View File

@ -174,11 +174,10 @@ func insertDatabase(n Notifier) (int64, error) {
noti := n.Select()
noti.Limits = 3
noti.name = noti.Name()
err := noti.Create()
if err != nil {
if err := noti.Create(); err != nil {
return 0, err
}
return noti.Id, err
return noti.Id, nil
}
// SelectNotifier returns the Notification struct from the database