PublicDashboards: use share type (#63059)
This commit is contained in:
@@ -13,6 +13,11 @@ func ValidatePublicDashboard(dto *SavePublicDashboardDTO, dashboard *dashboards.
|
||||
return ErrPublicDashboardHasTemplateVariables.Errorf("ValidateSavePublicDashboard: public dashboard has template variables")
|
||||
}
|
||||
|
||||
// if it is empty we override it in the service with public for retro compatibility
|
||||
if dto.PublicDashboard.Share != "" && !IsValidShareType(dto.PublicDashboard.Share) {
|
||||
return ErrInvalidShareType.Errorf("ValidateSavePublicDashboard: invalid share type")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -58,3 +63,12 @@ func IsValidAccessToken(token string) bool {
|
||||
func IsValidShortUID(uid string) bool {
|
||||
return uid != "" && util.IsValidShortUID(uid)
|
||||
}
|
||||
|
||||
func IsValidShareType(shareType ShareType) bool {
|
||||
for _, t := range ValidShareTypes {
|
||||
if t == shareType {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -37,11 +37,39 @@ func TestValidatePublicDashboard(t *testing.T) {
|
||||
}`)
|
||||
dashboardData, _ := simplejson.NewJson(templateVars)
|
||||
dashboard := dashboards.NewDashboardFromJson(dashboardData)
|
||||
dto := &SavePublicDashboardDTO{DashboardUid: "abc123", OrgId: 1, UserId: 1, PublicDashboard: nil}
|
||||
dto := &SavePublicDashboardDTO{DashboardUid: "abc123", OrgId: 1, UserId: 1, PublicDashboard: &PublicDashboard{}}
|
||||
|
||||
err := ValidatePublicDashboard(dto, dashboard)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("Returns no error when valid shareType value is received", func(t *testing.T) {
|
||||
templateVars := []byte(`{
|
||||
"templating": {
|
||||
"list": []
|
||||
}
|
||||
}`)
|
||||
dashboardData, _ := simplejson.NewJson(templateVars)
|
||||
dashboard := dashboards.NewDashboardFromJson(dashboardData)
|
||||
dto := &SavePublicDashboardDTO{DashboardUid: "abc123", OrgId: 1, UserId: 1, PublicDashboard: &PublicDashboard{Share: EmailShareType}}
|
||||
|
||||
err := ValidatePublicDashboard(dto, dashboard)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("Returns error when invalid shareType value", func(t *testing.T) {
|
||||
templateVars := []byte(`{
|
||||
"templating": {
|
||||
"list": []
|
||||
}
|
||||
}`)
|
||||
dashboardData, _ := simplejson.NewJson(templateVars)
|
||||
dashboard := dashboards.NewDashboardFromJson(dashboardData)
|
||||
dto := &SavePublicDashboardDTO{DashboardUid: "abc123", OrgId: 1, UserId: 1, PublicDashboard: &PublicDashboard{Share: "invalid"}}
|
||||
|
||||
err := ValidatePublicDashboard(dto, dashboard)
|
||||
require.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestValidateQueryPublicDashboardRequest(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user