PublicDashboards: Add validation on update (#70993)

This commit is contained in:
Ezequiel Victorero 2023-07-04 12:03:39 -03:00 committed by GitHub
parent 467c818c47
commit 82f788b7a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 29 deletions

View File

@ -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) 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) publicDashboard := newUpdatePublicDashboard(dto, existingPubdash)
// set values to update // set values to update

View File

@ -503,21 +503,22 @@ func assertFalseIfNull(t *testing.T, expectedValue bool, nullableValue *bool) {
} }
func TestUpdatePublicDashboard(t *testing.T) { 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) { 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 isEnabled, annotationsEnabled, timeSelectionEnabled := true, false, false
dto := &SavePublicDashboardDTO{ dto := &SavePublicDashboardDTO{
DashboardUid: dashboard.UID, DashboardUid: dashboard.UID,
@ -566,22 +567,8 @@ func TestUpdatePublicDashboard(t *testing.T) {
}) })
t.Run("Updating set empty time settings", func(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 isEnabled := true
dto := &SavePublicDashboardDTO{ dto := &SavePublicDashboardDTO{
DashboardUid: dashboard.UID, DashboardUid: dashboard.UID,
UserId: 7, UserId: 7,
@ -609,6 +596,34 @@ func TestUpdatePublicDashboard(t *testing.T) {
assert.Equal(t, &TimeSettings{}, updatedPubdash.TimeSettings) 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 trueBooleanField := true
timeSettings := &TimeSettings{From: "now-8", To: "now"} timeSettings := &TimeSettings{From: "now-8", To: "now"}
shareType := EmailShareType shareType := EmailShareType