diff --git a/handlers/api_test.go b/handlers/api_test.go index 9d3e2bc1..bc3f336c 100644 --- a/handlers/api_test.go +++ b/handlers/api_test.go @@ -1,3 +1,5 @@ +// +build int + package handlers import ( diff --git a/handlers/checkins_test.go b/handlers/checkins_test.go index e2e09925..975b7ce8 100644 --- a/handlers/checkins_test.go +++ b/handlers/checkins_test.go @@ -1,3 +1,5 @@ +// +build int + package handlers import ( diff --git a/handlers/groups.go b/handlers/groups.go index df473489..93088675 100644 --- a/handlers/groups.go +++ b/handlers/groups.go @@ -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 } diff --git a/handlers/groups_test.go b/handlers/groups_test.go index 7298d5ee..00b79def 100644 --- a/handlers/groups_test.go +++ b/handlers/groups_test.go @@ -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", diff --git a/handlers/messages.go b/handlers/messages.go index 8fa0dda0..13efd0f2 100644 --- a/handlers/messages.go +++ b/handlers/messages.go @@ -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) { diff --git a/handlers/messages_test.go b/handlers/messages_test.go index a51e2260..babc62d2 100644 --- a/handlers/messages_test.go +++ b/handlers/messages_test.go @@ -1,3 +1,5 @@ +// +build int + package handlers import ( diff --git a/handlers/middleware.go b/handlers/middleware.go index b6895e77..8a15e39e 100644 --- a/handlers/middleware.go +++ b/handlers/middleware.go @@ -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)}) }) } diff --git a/handlers/notifiers_test.go b/handlers/notifiers_test.go index 7d130cfb..d59cb991 100644 --- a/handlers/notifiers_test.go +++ b/handlers/notifiers_test.go @@ -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{ { diff --git a/handlers/routes.go b/handlers/routes.go index 4ec2e3a6..0e3b8276 100644 --- a/handlers/routes.go +++ b/handlers/routes.go @@ -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") diff --git a/handlers/services_test.go b/handlers/services_test.go index 342f0e93..862e2010 100644 --- a/handlers/services_test.go +++ b/handlers/services_test.go @@ -1,3 +1,5 @@ +// +build int + package handlers import ( diff --git a/handlers/users_test.go b/handlers/users_test.go index 7b31ee7e..fe7d299e 100644 --- a/handlers/users_test.go +++ b/handlers/users_test.go @@ -1,3 +1,5 @@ +// +build int + package handlers import ( diff --git a/integrators/integrations.go b/integrators/integrations.go index 7c27edb2..eb6b3c72 100644 --- a/integrators/integrations.go +++ b/integrators/integrations.go @@ -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() } diff --git a/types/notifications/database.go b/types/notifications/database.go index ba406363..74d15315 100644 --- a/types/notifications/database.go +++ b/types/notifications/database.go @@ -26,7 +26,7 @@ func All() []*Notification { } func (n *Notification) Create() error { - db := DB().Create(n) + db := DB().FirstOrCreate(&n) return db.Error() } diff --git a/types/notifications/struct.go b/types/notifications/struct.go index c94346c3..23301c75 100644 --- a/types/notifications/struct.go +++ b/types/notifications/struct.go @@ -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