From ec6c9f67020492edde2d686f0a7cae7b43b7e66d Mon Sep 17 00:00:00 2001 From: hunterlong Date: Sun, 9 Aug 2020 20:10:37 -0700 Subject: [PATCH] added .Validate() error method for multiple types --- Dockerfile.base | 1 - handlers/groups_test.go | 2 +- handlers/notifications.go | 5 +++++ notifiers/notifiers_test.go | 36 +++++++++++++++++++++++++++++++++ types/groups/database.go | 15 ++++++++++++++ types/incidents/database.go | 31 ++++++++++++++++++++++++++++ types/messages/hooks.go | 20 +++++++++++------- types/notifications/database.go | 13 ++++++++++++ types/services/database.go | 21 +++++++++++++++++++ types/users/hooks.go | 21 ++++++++++++++++++- 10 files changed, 155 insertions(+), 10 deletions(-) diff --git a/Dockerfile.base b/Dockerfile.base index dca5e506..44c8ccb5 100644 --- a/Dockerfile.base +++ b/Dockerfile.base @@ -38,7 +38,6 @@ RUN go get github.com/stretchr/testify/assert && \ COPY . . COPY --from=frontend /statping/dist/ ./source/dist/ RUN make clean generate embed -RUN if [ "$GOARCH" = "arm" ] ; then export GOARM=6; fi RUN go build -a -ldflags "-s -w -extldflags -static -X main.VERSION=${VERSION} -X main.COMMIT=${COMMIT}" -o statping --tags "netgo linux" ./cmd RUN chmod a+x statping && mv statping /go/bin/statping # /go/bin/statping - statping binary diff --git a/handlers/groups_test.go b/handlers/groups_test.go index e52d7032..4e18fac9 100644 --- a/handlers/groups_test.go +++ b/handlers/groups_test.go @@ -141,7 +141,7 @@ func TestGroupAPIRoutes(t *testing.T) { }, { Name: "Statping View Unknown Group", - URL: "/api/groups/8383883838", + URL: "/api/groups/38383", Method: "GET", BeforeTest: SetTestENV, ExpectedStatus: 404, diff --git a/handlers/notifications.go b/handlers/notifications.go index 6af69258..5f2c3dd6 100644 --- a/handlers/notifications.go +++ b/handlers/notifications.go @@ -52,6 +52,11 @@ func apiNotifierUpdateHandler(w http.ResponseWriter, r *http.Request) { } notif := services.ReturnNotifier(notifer.Method) + if err := notif.Valid(notifer.Values()); err != nil { + sendErrorJson(err, w, r) + return + } + if _, err := notif.OnSave(); err != nil { sendErrorJson(err, w, r) return diff --git a/notifiers/notifiers_test.go b/notifiers/notifiers_test.go index 0c93175c..0e25a991 100644 --- a/notifiers/notifiers_test.go +++ b/notifiers/notifiers_test.go @@ -17,3 +17,39 @@ func TestReplaceTemplate(t *testing.T) { replaced = ReplaceTemplate(temp, replacer{Service: services.Example(false), Failure: failures.Example()}) assert.Equal(t, `{"id":6283,"name":"Statping Example","failure":"Response did not response a 200 status code"}`, replaced) } + +func TestPushover_Select(t *testing.T) { + tests := []struct { + Value string + Expected string + }{ + { + "lowest", + "-2", + }, + { + "low", + "-1", + }, + { + "normal", + "0", + }, + { + "high", + "1", + }, + { + "emergency", + "2", + }, + { + "", + "0", + }, + } + + for _, v := range tests { + assert.Equal(t, v.Expected, priority(v.Value)) + } +} diff --git a/types/groups/database.go b/types/groups/database.go index 2577620e..6523e2b9 100644 --- a/types/groups/database.go +++ b/types/groups/database.go @@ -17,6 +17,13 @@ func SetDB(database database.Database) { db = database.Model(&Group{}) } +func (g *Group) Validate() error { + if g.Name == "" { + return errors.New("group name is empty") + } + return nil +} + func (g *Group) AfterFind() { metrics.Query("group", "find") } @@ -29,6 +36,14 @@ func (g *Group) AfterDelete() { metrics.Query("group", "delete") } +func (g *Group) BeforeUpdate() error { + return g.Validate() +} + +func (g *Group) BeforeCreate() error { + return g.Validate() +} + func (g *Group) AfterCreate() { metrics.Query("group", "create") } diff --git a/types/incidents/database.go b/types/incidents/database.go index 9620e7bc..315ba06b 100644 --- a/types/incidents/database.go +++ b/types/incidents/database.go @@ -2,6 +2,7 @@ package incidents import ( "github.com/statping/statping/database" + "github.com/statping/statping/types/errors" "github.com/statping/statping/types/metrics" "github.com/statping/statping/utils" ) @@ -17,6 +18,21 @@ func SetDB(database database.Database) { dbUpdate = database.Model(&IncidentUpdate{}) } +func (i *Incident) Validate() error { + if i.Title == "" { + return errors.New("missing title") + } + return nil +} + +func (i *Incident) BeforeUpdate() error { + return i.Validate() +} + +func (i *Incident) BeforeCreate() error { + return i.Validate() +} + func (i *Incident) AfterFind() { db.Model(i).Related(&i.Updates).Order("DESC") metrics.Query("incident", "find") @@ -34,6 +50,21 @@ func (i *Incident) AfterDelete() { metrics.Query("incident", "delete") } +func (i *IncidentUpdate) Validate() error { + if i.Message == "" { + return errors.New("missing incident update title") + } + return nil +} + +func (i *IncidentUpdate) BeforeUpdate() error { + return i.Validate() +} + +func (i *IncidentUpdate) BeforeCreate() error { + return i.Validate() +} + func (i *IncidentUpdate) AfterFind() { metrics.Query("incident_update", "find") } diff --git a/types/messages/hooks.go b/types/messages/hooks.go index bf68e0f2..cfc303fe 100644 --- a/types/messages/hooks.go +++ b/types/messages/hooks.go @@ -1,17 +1,23 @@ package messages import ( + "github.com/statping/statping/types/errors" "github.com/statping/statping/types/metrics" - "github.com/statping/statping/utils" ) -// BeforeCreate for Message will set CreatedAt to UTC -func (m *Message) BeforeCreate() (err error) { - if m.CreatedAt.IsZero() { - m.CreatedAt = utils.Now() - m.UpdatedAt = utils.Now() +func (m *Message) Validate() error { + if m.Title == "" { + return errors.New("missing message title") } - return + return nil +} + +func (m *Message) BeforeUpdate() error { + return m.Validate() +} + +func (m *Message) BeforeCreate() error { + return m.Validate() } func (m *Message) AfterFind() { diff --git a/types/notifications/database.go b/types/notifications/database.go index efd32579..c034b076 100644 --- a/types/notifications/database.go +++ b/types/notifications/database.go @@ -12,6 +12,19 @@ func SetDB(database database.Database) { db = database.Model(&Notification{}) } +func (n *Notification) Values() Values { + return Values{ + Host: n.Host.String, + Port: n.Port.Int64, + Username: n.Username.String, + Password: n.Password.String, + Var1: n.Var1.String, + Var2: n.Var2.String, + ApiKey: n.ApiKey.String, + ApiSecret: n.ApiSecret.String, + } +} + func Find(method string) (*Notification, error) { var n Notification q := db.Where("method = ?", method).Find(&n) diff --git a/types/services/database.go b/types/services/database.go index 672ffdcb..80416734 100644 --- a/types/services/database.go +++ b/types/services/database.go @@ -15,6 +15,27 @@ var ( allServices map[int64]*Service ) +func (s *Service) Validate() error { + if s.Name == "" { + return errors.New("missing service name") + } else if s.Domain == "" { + return errors.New("missing domain name") + } else if s.Type == "" { + return errors.New("missing service type") + } else if s.Interval == 0 { + return errors.New("missing check interval") + } + return nil +} + +func (s *Service) BeforeCreate() error { + return s.Validate() +} + +func (s *Service) BeforeUpdate() error { + return s.Validate() +} + func (s *Service) AfterFind() { db.Model(s).Related(&s.Incidents).Related(&s.Messages).Related(&s.Checkins).Related(&s.Incidents) metrics.Query("service", "find") diff --git a/types/users/hooks.go b/types/users/hooks.go index c66ca1af..f267c301 100644 --- a/types/users/hooks.go +++ b/types/users/hooks.go @@ -1,9 +1,28 @@ package users -import "github.com/statping/statping/utils" +import ( + "github.com/statping/statping/types/errors" + "github.com/statping/statping/utils" +) + +func (u *User) Validate() error { + if u.Username == "" { + return errors.New("username is empty") + } else if u.Password == "" { + return errors.New("password is empty") + } + return nil +} func (u *User) BeforeCreate() error { + if err := u.Validate(); err != nil { + return err + } u.Password = utils.HashPassword(u.Password) u.ApiKey = utils.NewSHA256Hash() return nil } + +func (u *User) BeforeUpdate() error { + return u.Validate() +}