diff --git a/pkg/services/publicdashboards/service/service.go b/pkg/services/publicdashboards/service/service.go index 55a4e98485b..77926942f88 100644 --- a/pkg/services/publicdashboards/service/service.go +++ b/pkg/services/publicdashboards/service/service.go @@ -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 diff --git a/pkg/services/publicdashboards/service/service_test.go b/pkg/services/publicdashboards/service/service_test.go index fabdb9de9ec..35b3734435f 100644 --- a/pkg/services/publicdashboards/service/service_test.go +++ b/pkg/services/publicdashboards/service/service_test.go @@ -503,21 +503,22 @@ func assertFalseIfNull(t *testing.T, expectedValue bool, nullableValue *bool) { } func TestUpdatePublicDashboard(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) + 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) { - 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, annotationsEnabled, timeSelectionEnabled := true, false, false dto := &SavePublicDashboardDTO{ DashboardUid: dashboard.UID, @@ -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, @@ -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