feat(http): validate dashboard cell updates (#1199)

* feat(http): validate dashboard cell updates

* run formatter
pull/10616/head
Jade McGough 2018-10-25 16:05:12 -07:00 committed by GitHub
parent bbe73b5cc1
commit 91dbd48258
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 77 additions and 2 deletions

View File

@ -288,6 +288,10 @@ func (c *Client) RemoveDashboardCell(ctx context.Context, dashboardID, cellID pl
// UpdateDashboardCell udpates a cell on a dashboard.
func (c *Client) UpdateDashboardCell(ctx context.Context, dashboardID, cellID platform.ID, upd platform.CellUpdate) (*platform.Cell, error) {
if err := upd.Valid(); err != nil {
return nil, err
}
var cell *platform.Cell
err := c.db.Update(func(tx *bolt.Tx) error {
d, err := c.findDashboardByID(ctx, tx, dashboardID)

View File

@ -173,3 +173,12 @@ func (u CellUpdate) Apply(c *Cell) error {
return nil
}
// Valid returns an error if the cell update is invalid.
func (u CellUpdate) Valid() error {
if u.H == nil && u.W == nil && u.Y == nil && u.X == nil && !u.ViewID.Valid() {
return fmt.Errorf("must update at least one attribute")
}
return nil
}

View File

@ -616,6 +616,10 @@ func decodePatchDashboardCellRequest(ctx context.Context, r *http.Request) (*pat
return nil, errors.MalformedDataf(err.Error())
}
if err := req.upd.Valid(); err != nil {
return nil, errors.InvalidDataf(err.Error())
}
return req, nil
}
@ -886,12 +890,15 @@ func (s *DashboardService) RemoveDashboardCell(ctx context.Context, dashboardID,
// UpdateDashboardCell replaces the dashboard cell with the provided ID.
func (s *DashboardService) UpdateDashboardCell(ctx context.Context, dashboardID, cellID platform.ID, upd platform.CellUpdate) (*platform.Cell, error) {
if err := upd.Valid(); err != nil {
return nil, err
}
u, err := newURL(s.Addr, dashboardCellIDPath(dashboardID, cellID))
if err != nil {
return nil, err
}
// fixme > in case upd does not containa a valid ViewID this errors out
b, err := json.Marshal(upd)
if err != nil {
return nil, err

View File

@ -182,6 +182,10 @@ func (s *Service) RemoveDashboardCell(ctx context.Context, dashboardID platform.
// UpdateDashboardCell will remove a cell from a dashboard.
func (s *Service) UpdateDashboardCell(ctx context.Context, dashboardID platform.ID, cellID platform.ID, upd platform.CellUpdate) (*platform.Cell, error) {
if err := upd.Valid(); err != nil {
return nil, err
}
d, err := s.FindDashboardByID(ctx, dashboardID)
if err != nil {
return nil, err

View File

@ -1038,7 +1038,7 @@ func UpdateDashboardCell(
wants wants
}{
{
name: "basic remove cell",
name: "basic update cell",
fields: DashboardFields{
NowFn: func() time.Time { return time.Date(2009, time.November, 10, 24, 0, 0, 0, time.UTC) },
IDGenerator: &mock.IDGenerator{
@ -1094,6 +1094,57 @@ func UpdateDashboardCell(
},
},
},
{
name: "invalid cell update",
fields: DashboardFields{
NowFn: func() time.Time { return time.Date(2009, time.November, 10, 24, 0, 0, 0, time.UTC) },
IDGenerator: &mock.IDGenerator{
IDFn: func() platform.ID {
return MustIDBase16(dashTwoID)
},
},
Dashboards: []*platform.Dashboard{
{
ID: MustIDBase16(dashOneID),
Name: "dashboard1",
Cells: []*platform.Cell{
{
ID: MustIDBase16(dashTwoID),
ViewID: MustIDBase16(dashTwoID),
},
{
ID: MustIDBase16(dashOneID),
ViewID: MustIDBase16(dashOneID),
},
},
},
},
},
args: args{
dashboardID: MustIDBase16(dashOneID),
cellID: MustIDBase16(dashTwoID),
cellUpdate: platform.CellUpdate{},
},
wants: wants{
dashboards: []*platform.Dashboard{
{
ID: MustIDBase16(dashOneID),
Name: "dashboard1",
Cells: []*platform.Cell{
{
ID: MustIDBase16(dashTwoID),
ViewID: MustIDBase16(dashTwoID),
},
{
ID: MustIDBase16(dashOneID),
ViewID: MustIDBase16(dashOneID),
},
},
},
},
err: fmt.Errorf("must update at least one attribute"),
},
},
}
for _, tt := range tests {