mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
ensure dashboard title is unique in folder
This commit is contained in:
@@ -14,14 +14,15 @@ import (
|
|||||||
|
|
||||||
// Typed errors
|
// Typed errors
|
||||||
var (
|
var (
|
||||||
ErrDashboardNotFound = errors.New("Dashboard not found")
|
ErrDashboardNotFound = errors.New("Dashboard not found")
|
||||||
ErrDashboardSnapshotNotFound = errors.New("Dashboard snapshot not found")
|
ErrDashboardSnapshotNotFound = errors.New("Dashboard snapshot not found")
|
||||||
ErrDashboardWithSameUIDExists = errors.New("A dashboard with the same uid already exists")
|
ErrDashboardWithSameUIDExists = errors.New("A dashboard with the same uid already exists")
|
||||||
ErrDashboardVersionMismatch = errors.New("The dashboard has been changed by someone else")
|
ErrDashboardWithSameNameInFolderExists = errors.New("A dashboard with the same name in the folder already exists")
|
||||||
ErrDashboardTitleEmpty = errors.New("Dashboard title cannot be empty")
|
ErrDashboardVersionMismatch = errors.New("The dashboard has been changed by someone else")
|
||||||
ErrDashboardFolderCannotHaveParent = errors.New("A Dashboard Folder cannot be added to another folder")
|
ErrDashboardTitleEmpty = errors.New("Dashboard title cannot be empty")
|
||||||
ErrDashboardContainsInvalidAlertData = errors.New("Invalid alert data. Cannot save dashboard")
|
ErrDashboardFolderCannotHaveParent = errors.New("A Dashboard Folder cannot be added to another folder")
|
||||||
ErrDashboardFailedToUpdateAlertData = errors.New("Failed to save alert data")
|
ErrDashboardContainsInvalidAlertData = errors.New("Invalid alert data. Cannot save dashboard")
|
||||||
|
ErrDashboardFailedToUpdateAlertData = errors.New("Failed to save alert data")
|
||||||
)
|
)
|
||||||
|
|
||||||
type UpdatePluginDashboardError struct {
|
type UpdatePluginDashboardError struct {
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ func init() {
|
|||||||
bus.AddHandler("sql", GetDashboardsByPluginId)
|
bus.AddHandler("sql", GetDashboardsByPluginId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func SaveDashboard(cmd *m.SaveDashboardCommand) error {
|
func SaveDashboard(cmd *m.SaveDashboardCommand) error {
|
||||||
return inTransaction(func(sess *DBSession) error {
|
return inTransaction(func(sess *DBSession) error {
|
||||||
dash := cmd.GetDashboardModel()
|
dash := cmd.GetDashboardModel()
|
||||||
@@ -69,6 +71,11 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = guaranteeDashboardNameIsUniqueInFolder(sess, dash)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
err = setHasAcl(sess, dash)
|
err = setHasAcl(sess, dash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -140,6 +147,23 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func guaranteeDashboardNameIsUniqueInFolder(sess *DBSession, dash *m.Dashboard) error {
|
||||||
|
var sameNameInFolder m.Dashboard
|
||||||
|
sameNameInFolderExist, err := sess.Where("org_id=? AND title=? AND folder_id = ? AND uid <> ?",
|
||||||
|
dash.OrgId, dash.Title, dash.FolderId, dash.Uid).
|
||||||
|
Get(&sameNameInFolder)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if sameNameInFolderExist {
|
||||||
|
return m.ErrDashboardWithSameNameInFolderExists
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func setHasAcl(sess *DBSession, dash *m.Dashboard) error {
|
func setHasAcl(sess *DBSession, dash *m.Dashboard) error {
|
||||||
// check if parent has acl
|
// check if parent has acl
|
||||||
if dash.FolderId > 0 {
|
if dash.FolderId > 0 {
|
||||||
|
|||||||
219
pkg/services/sqlstore/dashboard_folder_test.go
Normal file
219
pkg/services/sqlstore/dashboard_folder_test.go
Normal file
@@ -0,0 +1,219 @@
|
|||||||
|
package sqlstore
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/go-xorm/xorm"
|
||||||
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
|
|
||||||
|
m "github.com/grafana/grafana/pkg/models"
|
||||||
|
"github.com/grafana/grafana/pkg/services/search"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDashboardFolderDataAccess(t *testing.T) {
|
||||||
|
var x *xorm.Engine
|
||||||
|
|
||||||
|
Convey("Testing DB", t, func() {
|
||||||
|
x = InitTestDB(t)
|
||||||
|
|
||||||
|
Convey("Given one dashboard folder with two dashboards and one dashboard in the root folder", func() {
|
||||||
|
folder := insertTestDashboard("1 test dash folder", 1, 0, true, "prod", "webapp")
|
||||||
|
dashInRoot := insertTestDashboard("test dash 67", 1, 0, false, "prod", "webapp")
|
||||||
|
childDash := insertTestDashboard("test dash 23", 1, folder.Id, false, "prod", "webapp")
|
||||||
|
insertTestDashboard("test dash 45", 1, folder.Id, false, "prod")
|
||||||
|
|
||||||
|
currentUser := createUser("viewer", "Viewer", false)
|
||||||
|
|
||||||
|
Convey("and no acls are set", func() {
|
||||||
|
Convey("should return all dashboards", func() {
|
||||||
|
query := &search.FindPersistedDashboardsQuery{SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1}, OrgId: 1, DashboardIds: []int64{folder.Id, dashInRoot.Id}}
|
||||||
|
err := SearchDashboards(query)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(len(query.Result), ShouldEqual, 2)
|
||||||
|
So(query.Result[0].Id, ShouldEqual, folder.Id)
|
||||||
|
So(query.Result[1].Id, ShouldEqual, dashInRoot.Id)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("and acl is set for dashboard folder", func() {
|
||||||
|
var otherUser int64 = 999
|
||||||
|
updateTestDashboardWithAcl(folder.Id, otherUser, m.PERMISSION_EDIT)
|
||||||
|
|
||||||
|
Convey("should not return folder", func() {
|
||||||
|
query := &search.FindPersistedDashboardsQuery{SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1}, OrgId: 1, DashboardIds: []int64{folder.Id, dashInRoot.Id}}
|
||||||
|
err := SearchDashboards(query)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(len(query.Result), ShouldEqual, 1)
|
||||||
|
So(query.Result[0].Id, ShouldEqual, dashInRoot.Id)
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("when the user is given permission", func() {
|
||||||
|
updateTestDashboardWithAcl(folder.Id, currentUser.Id, m.PERMISSION_EDIT)
|
||||||
|
|
||||||
|
Convey("should be able to access folder", func() {
|
||||||
|
query := &search.FindPersistedDashboardsQuery{SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1}, OrgId: 1, DashboardIds: []int64{folder.Id, dashInRoot.Id}}
|
||||||
|
err := SearchDashboards(query)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(len(query.Result), ShouldEqual, 2)
|
||||||
|
So(query.Result[0].Id, ShouldEqual, folder.Id)
|
||||||
|
So(query.Result[1].Id, ShouldEqual, dashInRoot.Id)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("when the user is an admin", func() {
|
||||||
|
Convey("should be able to access folder", func() {
|
||||||
|
query := &search.FindPersistedDashboardsQuery{
|
||||||
|
SignedInUser: &m.SignedInUser{
|
||||||
|
UserId: currentUser.Id,
|
||||||
|
OrgId: 1,
|
||||||
|
OrgRole: m.ROLE_ADMIN,
|
||||||
|
},
|
||||||
|
OrgId: 1,
|
||||||
|
DashboardIds: []int64{folder.Id, dashInRoot.Id},
|
||||||
|
}
|
||||||
|
err := SearchDashboards(query)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(len(query.Result), ShouldEqual, 2)
|
||||||
|
So(query.Result[0].Id, ShouldEqual, folder.Id)
|
||||||
|
So(query.Result[1].Id, ShouldEqual, dashInRoot.Id)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("and acl is set for dashboard child and folder has all permissions removed", func() {
|
||||||
|
var otherUser int64 = 999
|
||||||
|
aclId := updateTestDashboardWithAcl(folder.Id, otherUser, m.PERMISSION_EDIT)
|
||||||
|
removeAcl(aclId)
|
||||||
|
updateTestDashboardWithAcl(childDash.Id, otherUser, m.PERMISSION_EDIT)
|
||||||
|
|
||||||
|
Convey("should not return folder or child", func() {
|
||||||
|
query := &search.FindPersistedDashboardsQuery{SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1}, OrgId: 1, DashboardIds: []int64{folder.Id, childDash.Id, dashInRoot.Id}}
|
||||||
|
err := SearchDashboards(query)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(len(query.Result), ShouldEqual, 1)
|
||||||
|
So(query.Result[0].Id, ShouldEqual, dashInRoot.Id)
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("when the user is given permission to child", func() {
|
||||||
|
updateTestDashboardWithAcl(childDash.Id, currentUser.Id, m.PERMISSION_EDIT)
|
||||||
|
|
||||||
|
Convey("should be able to search for child dashboard but not folder", func() {
|
||||||
|
query := &search.FindPersistedDashboardsQuery{SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1}, OrgId: 1, DashboardIds: []int64{folder.Id, childDash.Id, dashInRoot.Id}}
|
||||||
|
err := SearchDashboards(query)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(len(query.Result), ShouldEqual, 2)
|
||||||
|
So(query.Result[0].Id, ShouldEqual, childDash.Id)
|
||||||
|
So(query.Result[1].Id, ShouldEqual, dashInRoot.Id)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("when the user is an admin", func() {
|
||||||
|
Convey("should be able to search for child dash and folder", func() {
|
||||||
|
query := &search.FindPersistedDashboardsQuery{
|
||||||
|
SignedInUser: &m.SignedInUser{
|
||||||
|
UserId: currentUser.Id,
|
||||||
|
OrgId: 1,
|
||||||
|
OrgRole: m.ROLE_ADMIN,
|
||||||
|
},
|
||||||
|
OrgId: 1,
|
||||||
|
DashboardIds: []int64{folder.Id, dashInRoot.Id, childDash.Id},
|
||||||
|
}
|
||||||
|
err := SearchDashboards(query)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(len(query.Result), ShouldEqual, 3)
|
||||||
|
So(query.Result[0].Id, ShouldEqual, folder.Id)
|
||||||
|
So(query.Result[1].Id, ShouldEqual, childDash.Id)
|
||||||
|
So(query.Result[2].Id, ShouldEqual, dashInRoot.Id)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("Given two dashboard folders with one dashboard each and one dashboard in the root folder", func() {
|
||||||
|
folder1 := insertTestDashboard("1 test dash folder", 1, 0, true, "prod")
|
||||||
|
folder2 := insertTestDashboard("2 test dash folder", 1, 0, true, "prod")
|
||||||
|
dashInRoot := insertTestDashboard("test dash 67", 1, 0, false, "prod")
|
||||||
|
childDash1 := insertTestDashboard("child dash 1", 1, folder1.Id, false, "prod")
|
||||||
|
childDash2 := insertTestDashboard("child dash 2", 1, folder2.Id, false, "prod")
|
||||||
|
|
||||||
|
currentUser := createUser("viewer", "Viewer", false)
|
||||||
|
var rootFolderId int64 = 0
|
||||||
|
|
||||||
|
Convey("and one folder is expanded, the other collapsed", func() {
|
||||||
|
Convey("should return dashboards in root and expanded folder", func() {
|
||||||
|
query := &search.FindPersistedDashboardsQuery{FolderIds: []int64{rootFolderId, folder1.Id}, SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1}, OrgId: 1}
|
||||||
|
err := SearchDashboards(query)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(len(query.Result), ShouldEqual, 4)
|
||||||
|
So(query.Result[0].Id, ShouldEqual, folder1.Id)
|
||||||
|
So(query.Result[1].Id, ShouldEqual, folder2.Id)
|
||||||
|
So(query.Result[2].Id, ShouldEqual, childDash1.Id)
|
||||||
|
So(query.Result[3].Id, ShouldEqual, dashInRoot.Id)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("and acl is set for one dashboard folder", func() {
|
||||||
|
var otherUser int64 = 999
|
||||||
|
updateTestDashboardWithAcl(folder1.Id, otherUser, m.PERMISSION_EDIT)
|
||||||
|
|
||||||
|
Convey("and a dashboard is moved from folder without acl to the folder with an acl", func() {
|
||||||
|
movedDash := moveDashboard(1, childDash2.Data, folder1.Id)
|
||||||
|
So(movedDash.HasAcl, ShouldBeTrue)
|
||||||
|
|
||||||
|
Convey("should not return folder with acl or its children", func() {
|
||||||
|
query := &search.FindPersistedDashboardsQuery{
|
||||||
|
SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1},
|
||||||
|
OrgId: 1,
|
||||||
|
DashboardIds: []int64{folder1.Id, childDash1.Id, childDash2.Id, dashInRoot.Id},
|
||||||
|
}
|
||||||
|
err := SearchDashboards(query)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(len(query.Result), ShouldEqual, 1)
|
||||||
|
So(query.Result[0].Id, ShouldEqual, dashInRoot.Id)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("and a dashboard is moved from folder with acl to the folder without an acl", func() {
|
||||||
|
movedDash := moveDashboard(1, childDash1.Data, folder2.Id)
|
||||||
|
So(movedDash.HasAcl, ShouldBeFalse)
|
||||||
|
|
||||||
|
Convey("should return folder without acl and its children", func() {
|
||||||
|
query := &search.FindPersistedDashboardsQuery{
|
||||||
|
SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1},
|
||||||
|
OrgId: 1,
|
||||||
|
DashboardIds: []int64{folder2.Id, childDash1.Id, childDash2.Id, dashInRoot.Id},
|
||||||
|
}
|
||||||
|
err := SearchDashboards(query)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(len(query.Result), ShouldEqual, 4)
|
||||||
|
So(query.Result[0].Id, ShouldEqual, folder2.Id)
|
||||||
|
So(query.Result[1].Id, ShouldEqual, childDash1.Id)
|
||||||
|
So(query.Result[2].Id, ShouldEqual, childDash2.Id)
|
||||||
|
So(query.Result[3].Id, ShouldEqual, dashInRoot.Id)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("and a dashboard with an acl is moved to the folder without an acl", func() {
|
||||||
|
updateTestDashboardWithAcl(childDash1.Id, otherUser, m.PERMISSION_EDIT)
|
||||||
|
movedDash := moveDashboard(1, childDash1.Data, folder2.Id)
|
||||||
|
So(movedDash.HasAcl, ShouldBeTrue)
|
||||||
|
|
||||||
|
Convey("should return folder without acl but not the dashboard with acl", func() {
|
||||||
|
query := &search.FindPersistedDashboardsQuery{
|
||||||
|
SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1},
|
||||||
|
OrgId: 1,
|
||||||
|
DashboardIds: []int64{folder2.Id, childDash1.Id, childDash2.Id, dashInRoot.Id},
|
||||||
|
}
|
||||||
|
err := SearchDashboards(query)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(len(query.Result), ShouldEqual, 3)
|
||||||
|
So(query.Result[0].Id, ShouldEqual, folder2.Id)
|
||||||
|
So(query.Result[1].Id, ShouldEqual, childDash2.Id)
|
||||||
|
So(query.Result[2].Id, ShouldEqual, dashInRoot.Id)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -196,18 +196,64 @@ func TestDashboardDataAccess(t *testing.T) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("Should not be able to save dashboard with same name", func() {
|
Convey("Should be able to save dashboards with same name in different folders", func() {
|
||||||
cmd := m.SaveDashboardCommand{
|
firstSaveCmd := m.SaveDashboardCommand{
|
||||||
OrgId: 1,
|
OrgId: 1,
|
||||||
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
||||||
"id": nil,
|
"id": nil,
|
||||||
"title": "test dash 23",
|
"title": "test dash folder and title",
|
||||||
"tags": []interface{}{},
|
"tags": []interface{}{},
|
||||||
|
"uid": "randomHash",
|
||||||
}),
|
}),
|
||||||
|
FolderId: 3,
|
||||||
}
|
}
|
||||||
|
|
||||||
err := SaveDashboard(&cmd)
|
err := SaveDashboard(&firstSaveCmd)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
|
secondSaveCmd := m.SaveDashboardCommand{
|
||||||
|
OrgId: 1,
|
||||||
|
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
||||||
|
"id": nil,
|
||||||
|
"title": "test dash folder and title",
|
||||||
|
"tags": []interface{}{},
|
||||||
|
"uid": "moreRandomHash",
|
||||||
|
}),
|
||||||
|
FolderId: 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = SaveDashboard(&secondSaveCmd)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("Should not be able to save dashboard with same name in the same folder", func() {
|
||||||
|
firstSaveCmd := m.SaveDashboardCommand{
|
||||||
|
OrgId: 1,
|
||||||
|
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
||||||
|
"id": nil,
|
||||||
|
"title": "test dash folder and title",
|
||||||
|
"tags": []interface{}{},
|
||||||
|
"uid": "randomHash",
|
||||||
|
}),
|
||||||
|
FolderId: 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := SaveDashboard(&firstSaveCmd)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
|
secondSaveCmd := m.SaveDashboardCommand{
|
||||||
|
OrgId: 1,
|
||||||
|
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
||||||
|
"id": nil,
|
||||||
|
"title": "test dash folder and title",
|
||||||
|
"tags": []interface{}{},
|
||||||
|
"uid": "moreRandomHash",
|
||||||
|
}),
|
||||||
|
FolderId: 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = SaveDashboard(&secondSaveCmd)
|
||||||
|
So(err, ShouldEqual, m.ErrDashboardWithSameNameInFolderExists)
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("Should not be able to save dashboard with same uid", func() {
|
Convey("Should not be able to save dashboard with same uid", func() {
|
||||||
@@ -226,6 +272,40 @@ func TestDashboardDataAccess(t *testing.T) {
|
|||||||
So(err, ShouldNotBeNil)
|
So(err, ShouldNotBeNil)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Convey("Should be able to update dashboard with the same title and folder id", func() {
|
||||||
|
cmd := m.SaveDashboardCommand{
|
||||||
|
OrgId: 1,
|
||||||
|
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
||||||
|
//"id": 1,
|
||||||
|
"uid": "randomHash",
|
||||||
|
"title": "folderId",
|
||||||
|
"style": "light",
|
||||||
|
"tags": []interface{}{},
|
||||||
|
}),
|
||||||
|
FolderId: 2,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := SaveDashboard(&cmd)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(cmd.Result.FolderId, ShouldEqual, 2)
|
||||||
|
|
||||||
|
cmd = m.SaveDashboardCommand{
|
||||||
|
OrgId: 1,
|
||||||
|
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
||||||
|
"id": cmd.Result.Id,
|
||||||
|
"uid": "randomHash",
|
||||||
|
"title": "folderId",
|
||||||
|
"style": "dark",
|
||||||
|
"version": cmd.Result.Version,
|
||||||
|
"tags": []interface{}{},
|
||||||
|
}),
|
||||||
|
FolderId: 2,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = SaveDashboard(&cmd)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
})
|
||||||
|
|
||||||
Convey("Should be able to update dashboard and remove folderId", func() {
|
Convey("Should be able to update dashboard and remove folderId", func() {
|
||||||
cmd := m.SaveDashboardCommand{
|
cmd := m.SaveDashboardCommand{
|
||||||
OrgId: 1,
|
OrgId: 1,
|
||||||
@@ -315,205 +395,6 @@ func TestDashboardDataAccess(t *testing.T) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("Given one dashboard folder with two dashboards and one dashboard in the root folder", func() {
|
|
||||||
folder := insertTestDashboard("1 test dash folder", 1, 0, true, "prod", "webapp")
|
|
||||||
dashInRoot := insertTestDashboard("test dash 67", 1, 0, false, "prod", "webapp")
|
|
||||||
childDash := insertTestDashboard("test dash 23", 1, folder.Id, false, "prod", "webapp")
|
|
||||||
insertTestDashboard("test dash 45", 1, folder.Id, false, "prod")
|
|
||||||
|
|
||||||
currentUser := createUser("viewer", "Viewer", false)
|
|
||||||
|
|
||||||
Convey("and no acls are set", func() {
|
|
||||||
Convey("should return all dashboards", func() {
|
|
||||||
query := &search.FindPersistedDashboardsQuery{SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1}, OrgId: 1, DashboardIds: []int64{folder.Id, dashInRoot.Id}}
|
|
||||||
err := SearchDashboards(query)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
So(len(query.Result), ShouldEqual, 2)
|
|
||||||
So(query.Result[0].Id, ShouldEqual, folder.Id)
|
|
||||||
So(query.Result[1].Id, ShouldEqual, dashInRoot.Id)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("and acl is set for dashboard folder", func() {
|
|
||||||
var otherUser int64 = 999
|
|
||||||
updateTestDashboardWithAcl(folder.Id, otherUser, m.PERMISSION_EDIT)
|
|
||||||
|
|
||||||
Convey("should not return folder", func() {
|
|
||||||
query := &search.FindPersistedDashboardsQuery{SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1}, OrgId: 1, DashboardIds: []int64{folder.Id, dashInRoot.Id}}
|
|
||||||
err := SearchDashboards(query)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
So(len(query.Result), ShouldEqual, 1)
|
|
||||||
So(query.Result[0].Id, ShouldEqual, dashInRoot.Id)
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("when the user is given permission", func() {
|
|
||||||
updateTestDashboardWithAcl(folder.Id, currentUser.Id, m.PERMISSION_EDIT)
|
|
||||||
|
|
||||||
Convey("should be able to access folder", func() {
|
|
||||||
query := &search.FindPersistedDashboardsQuery{SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1}, OrgId: 1, DashboardIds: []int64{folder.Id, dashInRoot.Id}}
|
|
||||||
err := SearchDashboards(query)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
So(len(query.Result), ShouldEqual, 2)
|
|
||||||
So(query.Result[0].Id, ShouldEqual, folder.Id)
|
|
||||||
So(query.Result[1].Id, ShouldEqual, dashInRoot.Id)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("when the user is an admin", func() {
|
|
||||||
Convey("should be able to access folder", func() {
|
|
||||||
query := &search.FindPersistedDashboardsQuery{
|
|
||||||
SignedInUser: &m.SignedInUser{
|
|
||||||
UserId: currentUser.Id,
|
|
||||||
OrgId: 1,
|
|
||||||
OrgRole: m.ROLE_ADMIN,
|
|
||||||
},
|
|
||||||
OrgId: 1,
|
|
||||||
DashboardIds: []int64{folder.Id, dashInRoot.Id},
|
|
||||||
}
|
|
||||||
err := SearchDashboards(query)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
So(len(query.Result), ShouldEqual, 2)
|
|
||||||
So(query.Result[0].Id, ShouldEqual, folder.Id)
|
|
||||||
So(query.Result[1].Id, ShouldEqual, dashInRoot.Id)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("and acl is set for dashboard child and folder has all permissions removed", func() {
|
|
||||||
var otherUser int64 = 999
|
|
||||||
aclId := updateTestDashboardWithAcl(folder.Id, otherUser, m.PERMISSION_EDIT)
|
|
||||||
removeAcl(aclId)
|
|
||||||
updateTestDashboardWithAcl(childDash.Id, otherUser, m.PERMISSION_EDIT)
|
|
||||||
|
|
||||||
Convey("should not return folder or child", func() {
|
|
||||||
query := &search.FindPersistedDashboardsQuery{SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1}, OrgId: 1, DashboardIds: []int64{folder.Id, childDash.Id, dashInRoot.Id}}
|
|
||||||
err := SearchDashboards(query)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
So(len(query.Result), ShouldEqual, 1)
|
|
||||||
So(query.Result[0].Id, ShouldEqual, dashInRoot.Id)
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("when the user is given permission to child", func() {
|
|
||||||
updateTestDashboardWithAcl(childDash.Id, currentUser.Id, m.PERMISSION_EDIT)
|
|
||||||
|
|
||||||
Convey("should be able to search for child dashboard but not folder", func() {
|
|
||||||
query := &search.FindPersistedDashboardsQuery{SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1}, OrgId: 1, DashboardIds: []int64{folder.Id, childDash.Id, dashInRoot.Id}}
|
|
||||||
err := SearchDashboards(query)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
So(len(query.Result), ShouldEqual, 2)
|
|
||||||
So(query.Result[0].Id, ShouldEqual, childDash.Id)
|
|
||||||
So(query.Result[1].Id, ShouldEqual, dashInRoot.Id)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("when the user is an admin", func() {
|
|
||||||
Convey("should be able to search for child dash and folder", func() {
|
|
||||||
query := &search.FindPersistedDashboardsQuery{
|
|
||||||
SignedInUser: &m.SignedInUser{
|
|
||||||
UserId: currentUser.Id,
|
|
||||||
OrgId: 1,
|
|
||||||
OrgRole: m.ROLE_ADMIN,
|
|
||||||
},
|
|
||||||
OrgId: 1,
|
|
||||||
DashboardIds: []int64{folder.Id, dashInRoot.Id, childDash.Id},
|
|
||||||
}
|
|
||||||
err := SearchDashboards(query)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
So(len(query.Result), ShouldEqual, 3)
|
|
||||||
So(query.Result[0].Id, ShouldEqual, folder.Id)
|
|
||||||
So(query.Result[1].Id, ShouldEqual, childDash.Id)
|
|
||||||
So(query.Result[2].Id, ShouldEqual, dashInRoot.Id)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Given two dashboard folders with one dashboard each and one dashboard in the root folder", func() {
|
|
||||||
folder1 := insertTestDashboard("1 test dash folder", 1, 0, true, "prod")
|
|
||||||
folder2 := insertTestDashboard("2 test dash folder", 1, 0, true, "prod")
|
|
||||||
dashInRoot := insertTestDashboard("test dash 67", 1, 0, false, "prod")
|
|
||||||
childDash1 := insertTestDashboard("child dash 1", 1, folder1.Id, false, "prod")
|
|
||||||
childDash2 := insertTestDashboard("child dash 2", 1, folder2.Id, false, "prod")
|
|
||||||
|
|
||||||
currentUser := createUser("viewer", "Viewer", false)
|
|
||||||
var rootFolderId int64 = 0
|
|
||||||
|
|
||||||
Convey("and one folder is expanded, the other collapsed", func() {
|
|
||||||
Convey("should return dashboards in root and expanded folder", func() {
|
|
||||||
query := &search.FindPersistedDashboardsQuery{FolderIds: []int64{rootFolderId, folder1.Id}, SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1}, OrgId: 1}
|
|
||||||
err := SearchDashboards(query)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
So(len(query.Result), ShouldEqual, 4)
|
|
||||||
So(query.Result[0].Id, ShouldEqual, folder1.Id)
|
|
||||||
So(query.Result[1].Id, ShouldEqual, folder2.Id)
|
|
||||||
So(query.Result[2].Id, ShouldEqual, childDash1.Id)
|
|
||||||
So(query.Result[3].Id, ShouldEqual, dashInRoot.Id)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("and acl is set for one dashboard folder", func() {
|
|
||||||
var otherUser int64 = 999
|
|
||||||
updateTestDashboardWithAcl(folder1.Id, otherUser, m.PERMISSION_EDIT)
|
|
||||||
|
|
||||||
Convey("and a dashboard is moved from folder without acl to the folder with an acl", func() {
|
|
||||||
movedDash := moveDashboard(1, childDash2.Data, folder1.Id)
|
|
||||||
So(movedDash.HasAcl, ShouldBeTrue)
|
|
||||||
|
|
||||||
Convey("should not return folder with acl or its children", func() {
|
|
||||||
query := &search.FindPersistedDashboardsQuery{
|
|
||||||
SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1},
|
|
||||||
OrgId: 1,
|
|
||||||
DashboardIds: []int64{folder1.Id, childDash1.Id, childDash2.Id, dashInRoot.Id},
|
|
||||||
}
|
|
||||||
err := SearchDashboards(query)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
So(len(query.Result), ShouldEqual, 1)
|
|
||||||
So(query.Result[0].Id, ShouldEqual, dashInRoot.Id)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("and a dashboard is moved from folder with acl to the folder without an acl", func() {
|
|
||||||
movedDash := moveDashboard(1, childDash1.Data, folder2.Id)
|
|
||||||
So(movedDash.HasAcl, ShouldBeFalse)
|
|
||||||
|
|
||||||
Convey("should return folder without acl and its children", func() {
|
|
||||||
query := &search.FindPersistedDashboardsQuery{
|
|
||||||
SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1},
|
|
||||||
OrgId: 1,
|
|
||||||
DashboardIds: []int64{folder2.Id, childDash1.Id, childDash2.Id, dashInRoot.Id},
|
|
||||||
}
|
|
||||||
err := SearchDashboards(query)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
So(len(query.Result), ShouldEqual, 4)
|
|
||||||
So(query.Result[0].Id, ShouldEqual, folder2.Id)
|
|
||||||
So(query.Result[1].Id, ShouldEqual, childDash1.Id)
|
|
||||||
So(query.Result[2].Id, ShouldEqual, childDash2.Id)
|
|
||||||
So(query.Result[3].Id, ShouldEqual, dashInRoot.Id)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("and a dashboard with an acl is moved to the folder without an acl", func() {
|
|
||||||
updateTestDashboardWithAcl(childDash1.Id, otherUser, m.PERMISSION_EDIT)
|
|
||||||
movedDash := moveDashboard(1, childDash1.Data, folder2.Id)
|
|
||||||
So(movedDash.HasAcl, ShouldBeTrue)
|
|
||||||
|
|
||||||
Convey("should return folder without acl but not the dashboard with acl", func() {
|
|
||||||
query := &search.FindPersistedDashboardsQuery{
|
|
||||||
SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1},
|
|
||||||
OrgId: 1,
|
|
||||||
DashboardIds: []int64{folder2.Id, childDash1.Id, childDash2.Id, dashInRoot.Id},
|
|
||||||
}
|
|
||||||
err := SearchDashboards(query)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
So(len(query.Result), ShouldEqual, 3)
|
|
||||||
So(query.Result[0].Id, ShouldEqual, folder2.Id)
|
|
||||||
So(query.Result[1].Id, ShouldEqual, childDash2.Id)
|
|
||||||
So(query.Result[2].Id, ShouldEqual, dashInRoot.Id)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Given a plugin with imported dashboards", func() {
|
Convey("Given a plugin with imported dashboards", func() {
|
||||||
pluginId := "test-app"
|
pluginId := "test-app"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user