added .Validate() error method for multiple types

pull/778/head
hunterlong 4 years ago
parent 889661f175
commit ec6c9f6702

@ -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

@ -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,

@ -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

@ -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))
}
}

@ -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")
}

@ -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")
}

@ -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() {

@ -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)

@ -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")

@ -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()
}

Loading…
Cancel
Save