mirror of https://github.com/grafana/grafana
Merge pull request #10921 from bergquist/invalid_uid
dashboard: whitelist allowed chars for uidpull/10927/head
commit
aa902ef826
@ -0,0 +1,43 @@ |
||||
package dashboards |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/grafana/grafana/pkg/bus" |
||||
"github.com/grafana/grafana/pkg/models" |
||||
"github.com/grafana/grafana/pkg/services/alerting" |
||||
"github.com/grafana/grafana/pkg/util" |
||||
) |
||||
|
||||
func TestDashboardsService(t *testing.T) { |
||||
|
||||
bus.ClearBusHandlers() |
||||
|
||||
bus.AddHandler("test", func(cmd *alerting.ValidateDashboardAlertsCommand) error { |
||||
return nil |
||||
}) |
||||
|
||||
testCases := []struct { |
||||
Uid string |
||||
Error error |
||||
}{ |
||||
{Uid: "", Error: nil}, |
||||
{Uid: "asdf90_-", Error: nil}, |
||||
{Uid: "asdf/90", Error: util.ErrDashboardInvalidUid}, |
||||
{Uid: "asdfghjklqwertyuiopzxcvbnmasdfghjklqwertyuiopzxcvbnmasdfghjklqwertyuiopzxcvbnm", Error: util.ErrDashboardUidToLong}, |
||||
} |
||||
|
||||
repo := &DashboardRepository{} |
||||
|
||||
for _, tc := range testCases { |
||||
dto := &SaveDashboardDTO{ |
||||
Dashboard: &models.Dashboard{Title: "title", Uid: tc.Uid}, |
||||
} |
||||
|
||||
_, err := repo.buildSaveDashboardCommand(dto) |
||||
|
||||
if err != tc.Error { |
||||
t.Fatalf("expected %s to return %v", tc.Uid, tc.Error) |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,12 @@ |
||||
package util |
||||
|
||||
import "testing" |
||||
|
||||
func TestAllowedCharMatchesUidPattern(t *testing.T) { |
||||
for _, c := range allowedChars { |
||||
err := VerifyUid(string(c)) |
||||
if err != nil { |
||||
t.Fatalf("charset for creating new shortids contains chars not present in uid pattern") |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue