Skip to content

Commit

Permalink
PublicDashboards: Add validation on update (#70993)
Browse files Browse the repository at this point in the history
  • Loading branch information
evictorero authored and polibb committed Jul 14, 2023
1 parent 1e17ec4 commit 8cb7f37
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 29 deletions.
5 changes: 5 additions & 0 deletions pkg/services/publicdashboards/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ func (pd *PublicDashboardServiceImpl) Update(ctx context.Context, u *user.Signed
return nil, ErrPublicDashboardNotFound.Errorf("Update: public dashboard not found by uid: %s", dto.Uid)
}

// validate the public dashboard belongs to the dashboard
if existingPubdash.DashboardUid != dto.DashboardUid {
return nil, ErrInvalidUid.Errorf("Update: the public dashboard does not belong to the dashboard")
}

publicDashboard := newUpdatePublicDashboard(dto, existingPubdash)

// set values to update
Expand Down
73 changes: 44 additions & 29 deletions pkg/services/publicdashboards/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,21 +503,22 @@ func assertFalseIfNull(t *testing.T, expectedValue bool, nullableValue *bool) {
}

func TestUpdatePublicDashboard(t *testing.T) {
t.Run("Updating public dashboard", func(t *testing.T) {
sqlStore := db.InitTestDB(t)
quotaService := quotatest.New(false, nil)
dashboardStore, err := dashboardsDB.ProvideDashboardStore(sqlStore, sqlStore.Cfg, featuremgmt.WithFeatures(), tagimpl.ProvideService(sqlStore, sqlStore.Cfg), quotaService)
require.NoError(t, err)
publicdashboardStore := database.ProvideStore(sqlStore, sqlStore.Cfg, featuremgmt.WithFeatures())
serviceWrapper := ProvideServiceWrapper(publicdashboardStore)
dashboard := insertTestDashboard(t, dashboardStore, "testDashie", 1, 0, true, []map[string]interface{}{}, nil)

service := &PublicDashboardServiceImpl{
log: log.New("test.logger"),
store: publicdashboardStore,
serviceWrapper: serviceWrapper,
}
sqlStore := db.InitTestDB(t)
quotaService := quotatest.New(false, nil)
dashboardStore, err := dashboardsDB.ProvideDashboardStore(sqlStore, sqlStore.Cfg, featuremgmt.WithFeatures(), tagimpl.ProvideService(sqlStore, sqlStore.Cfg), quotaService)
require.NoError(t, err)
publicdashboardStore := database.ProvideStore(sqlStore, sqlStore.Cfg, featuremgmt.WithFeatures())
serviceWrapper := ProvideServiceWrapper(publicdashboardStore)
dashboard := insertTestDashboard(t, dashboardStore, "testDashie", 1, 0, true, []map[string]interface{}{}, nil)
dashboard2 := insertTestDashboard(t, dashboardStore, "testDashie2", 1, 0, true, []map[string]interface{}{}, nil)

service := &PublicDashboardServiceImpl{
log: log.New("test.logger"),
store: publicdashboardStore,
serviceWrapper: serviceWrapper,
}

t.Run("Updating public dashboard", func(t *testing.T) {
isEnabled, annotationsEnabled, timeSelectionEnabled := true, false, false
dto := &SavePublicDashboardDTO{
DashboardUid: dashboard.UID,
Expand Down Expand Up @@ -566,22 +567,8 @@ func TestUpdatePublicDashboard(t *testing.T) {
})

t.Run("Updating set empty time settings", func(t *testing.T) {
sqlStore := db.InitTestDB(t)
quotaService := quotatest.New(false, nil)
dashboardStore, err := dashboardsDB.ProvideDashboardStore(sqlStore, sqlStore.Cfg, featuremgmt.WithFeatures(), tagimpl.ProvideService(sqlStore, sqlStore.Cfg), quotaService)
require.NoError(t, err)
publicdashboardStore := database.ProvideStore(sqlStore, sqlStore.Cfg, featuremgmt.WithFeatures())
serviceWrapper := ProvideServiceWrapper(publicdashboardStore)

dashboard := insertTestDashboard(t, dashboardStore, "testDashie", 1, 0, true, []map[string]interface{}{}, nil)

service := &PublicDashboardServiceImpl{
log: log.New("test.logger"),
store: publicdashboardStore,
serviceWrapper: serviceWrapper,
}

isEnabled := true

dto := &SavePublicDashboardDTO{
DashboardUid: dashboard.UID,
UserId: 7,
Expand Down Expand Up @@ -609,6 +596,34 @@ func TestUpdatePublicDashboard(t *testing.T) {
assert.Equal(t, &TimeSettings{}, updatedPubdash.TimeSettings)
})

t.Run("Should fail when public dashboard uid does not match dashboard uid", func(t *testing.T) {
isEnabled := true

dto := &SavePublicDashboardDTO{
DashboardUid: dashboard.UID,
UserId: 7,
PublicDashboard: &PublicDashboardDTO{
IsEnabled: &isEnabled,
},
}

// insert initial pubdash
savedPubdash, err := service.Create(context.Background(), SignedInUser, dto)
require.NoError(t, err)

dto = &SavePublicDashboardDTO{
Uid: savedPubdash.Uid,
DashboardUid: dashboard2.UID,
OrgID: 9,
UserId: 8,
PublicDashboard: &PublicDashboardDTO{
IsEnabled: &isEnabled,
},
}
_, err = service.Update(context.Background(), SignedInUser, dto)
assert.Error(t, err)
})

trueBooleanField := true
timeSettings := &TimeSettings{From: "now-8", To: "now"}
shareType := EmailShareType
Expand Down

0 comments on commit 8cb7f37

Please sign in to comment.