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 package handlers
import ( import (

View File

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

View File

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

View File

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

View File

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

View File

@ -1,3 +1,5 @@
// +build int
package handlers package handlers
import ( 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) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data := handler(r) data := handler(r)
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(scope{data: data, scope: ScopeName(r)}) json.NewEncoder(w).Encode(scope{data: data, scope: ScopeName(r)})
}) })
} }

View File

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

View File

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

View File

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

View File

@ -1,3 +1,5 @@
// +build int
package handlers package handlers
import ( 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 // 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 { func install(i integrations.Integrator) error {
inDb := isInDatabase(i) _, err := insertDatabase(i)
log.WithField("installed", inDb). if err != nil {
WithFields(utils.ToFields(i)). log.Errorln(err)
Debugln(fmt.Sprintf("Checking if integrator '%v' is installed: %v", i.Get().Name, inDb)) return err
if !inDb {
_, 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 // insertDatabase will create a new record into the database for the integrator
func insertDatabase(i integrations.Integrator) (string, error) { func insertDatabase(i integrations.Integrator) (string, error) {
integrator := i.Get() integrator := i.Get()
query := db.Create(integrator) query := db.FirstOrCreate(integrator)
if query.Error() != nil { if query.Error() != nil {
return "", query.Error() return "", query.Error()
} }

View File

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

View File

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