mirror of
https://github.com/grafana/grafana.git
synced 2024-11-26 02:40:26 -06:00
dashboards: make fake dashboard guardian available to other packages
This commit is contained in:
parent
892bdecb19
commit
02278f90a7
@ -17,7 +17,7 @@ func TestDashboardService(t *testing.T) {
|
||||
service := dashboardServiceImpl{}
|
||||
|
||||
origNewDashboardGuardian := guardian.New
|
||||
mockDashboardGuardian(&fakeDashboardGuardian{canSave: true})
|
||||
guardian.MockDashboardGuardian(&guardian.FakeDashboardGuardian{CanSaveValue: true})
|
||||
|
||||
Convey("Save dashboard validation", func() {
|
||||
dto := &SaveDashboardDTO{}
|
||||
@ -93,52 +93,3 @@ func TestDashboardService(t *testing.T) {
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func mockDashboardGuardian(mock *fakeDashboardGuardian) {
|
||||
guardian.New = func(dashId int64, orgId int64, user *models.SignedInUser) guardian.DashboardGuardian {
|
||||
mock.orgId = orgId
|
||||
mock.dashId = dashId
|
||||
mock.user = user
|
||||
return mock
|
||||
}
|
||||
}
|
||||
|
||||
type fakeDashboardGuardian struct {
|
||||
dashId int64
|
||||
orgId int64
|
||||
user *models.SignedInUser
|
||||
canSave bool
|
||||
canEdit bool
|
||||
canView bool
|
||||
canAdmin bool
|
||||
hasPermission bool
|
||||
checkPermissionBeforeUpdate bool
|
||||
}
|
||||
|
||||
func (g *fakeDashboardGuardian) CanSave() (bool, error) {
|
||||
return g.canSave, nil
|
||||
}
|
||||
|
||||
func (g *fakeDashboardGuardian) CanEdit() (bool, error) {
|
||||
return g.canEdit, nil
|
||||
}
|
||||
|
||||
func (g *fakeDashboardGuardian) CanView() (bool, error) {
|
||||
return g.canView, nil
|
||||
}
|
||||
|
||||
func (g *fakeDashboardGuardian) CanAdmin() (bool, error) {
|
||||
return g.canAdmin, nil
|
||||
}
|
||||
|
||||
func (g *fakeDashboardGuardian) HasPermission(permission models.PermissionType) (bool, error) {
|
||||
return g.hasPermission, nil
|
||||
}
|
||||
|
||||
func (g *fakeDashboardGuardian) CheckPermissionBeforeUpdate(permission models.PermissionType, updatePermissions []*models.DashboardAcl) (bool, error) {
|
||||
return g.checkPermissionBeforeUpdate, nil
|
||||
}
|
||||
|
||||
func (g *fakeDashboardGuardian) GetAcl() ([]*models.DashboardAclInfoDTO, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ func TestFolderService(t *testing.T) {
|
||||
|
||||
Convey("Given user has no permissions", func() {
|
||||
origNewGuardian := guardian.New
|
||||
mockDashboardGuardian(&fakeDashboardGuardian{})
|
||||
guardian.MockDashboardGuardian(&guardian.FakeDashboardGuardian{})
|
||||
|
||||
bus.AddHandler("test", func(query *models.GetDashboardQuery) error {
|
||||
query.Result = models.NewDashboardFolder("Folder")
|
||||
@ -77,7 +77,7 @@ func TestFolderService(t *testing.T) {
|
||||
|
||||
Convey("Given user has permission to save", func() {
|
||||
origNewGuardian := guardian.New
|
||||
mockDashboardGuardian(&fakeDashboardGuardian{canSave: true})
|
||||
guardian.MockDashboardGuardian(&guardian.FakeDashboardGuardian{CanSaveValue: true})
|
||||
|
||||
dash := models.NewDashboardFolder("Folder")
|
||||
dash.Id = 1
|
||||
@ -135,7 +135,7 @@ func TestFolderService(t *testing.T) {
|
||||
|
||||
Convey("Given user has permission to view", func() {
|
||||
origNewGuardian := guardian.New
|
||||
mockDashboardGuardian(&fakeDashboardGuardian{canView: true})
|
||||
guardian.MockDashboardGuardian(&guardian.FakeDashboardGuardian{CanViewValue: true})
|
||||
|
||||
dashFolder := models.NewDashboardFolder("Folder")
|
||||
dashFolder.Id = 1
|
||||
@ -153,7 +153,7 @@ func TestFolderService(t *testing.T) {
|
||||
So(f.Title, ShouldEqual, dashFolder.Title)
|
||||
})
|
||||
|
||||
Convey("When get folder by uid should not return access denied error", func() {
|
||||
Convey("When get folder by uid should return folder", func() {
|
||||
f, _ := service.GetFolderByUid("uid")
|
||||
So(f.Id, ShouldEqual, dashFolder.Id)
|
||||
So(f.Uid, ShouldEqual, dashFolder.Uid)
|
||||
|
@ -158,3 +158,52 @@ func (g *dashboardGuardianImpl) getTeams() ([]*m.Team, error) {
|
||||
g.groups = query.Result
|
||||
return query.Result, err
|
||||
}
|
||||
|
||||
type FakeDashboardGuardian struct {
|
||||
DashId int64
|
||||
OrgId int64
|
||||
User *m.SignedInUser
|
||||
CanSaveValue bool
|
||||
CanEditValue bool
|
||||
CanViewValue bool
|
||||
CanAdminValue bool
|
||||
HasPermissionValue bool
|
||||
CheckPermissionBeforeUpdateValue bool
|
||||
}
|
||||
|
||||
func (g *FakeDashboardGuardian) CanSave() (bool, error) {
|
||||
return g.CanSaveValue, nil
|
||||
}
|
||||
|
||||
func (g *FakeDashboardGuardian) CanEdit() (bool, error) {
|
||||
return g.CanEditValue, nil
|
||||
}
|
||||
|
||||
func (g *FakeDashboardGuardian) CanView() (bool, error) {
|
||||
return g.CanViewValue, nil
|
||||
}
|
||||
|
||||
func (g *FakeDashboardGuardian) CanAdmin() (bool, error) {
|
||||
return g.CanAdminValue, nil
|
||||
}
|
||||
|
||||
func (g *FakeDashboardGuardian) HasPermission(permission m.PermissionType) (bool, error) {
|
||||
return g.HasPermissionValue, nil
|
||||
}
|
||||
|
||||
func (g *FakeDashboardGuardian) CheckPermissionBeforeUpdate(permission m.PermissionType, updatePermissions []*m.DashboardAcl) (bool, error) {
|
||||
return g.CheckPermissionBeforeUpdateValue, nil
|
||||
}
|
||||
|
||||
func (g *FakeDashboardGuardian) GetAcl() ([]*m.DashboardAclInfoDTO, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func MockDashboardGuardian(mock *FakeDashboardGuardian) {
|
||||
New = func(dashId int64, orgId int64, user *m.SignedInUser) DashboardGuardian {
|
||||
mock.OrgId = orgId
|
||||
mock.DashId = dashId
|
||||
mock.User = user
|
||||
return mock
|
||||
}
|
||||
}
|
||||
|
@ -142,9 +142,9 @@ func TestIntegratedDashboardService(t *testing.T) {
|
||||
So(err, ShouldNotBeNil)
|
||||
So(err, ShouldEqual, models.ErrDashboardUpdateAccessDenied)
|
||||
|
||||
So(sc.dashboardGuardianMock.dashId, ShouldEqual, 0)
|
||||
So(sc.dashboardGuardianMock.orgId, ShouldEqual, cmd.OrgId)
|
||||
So(sc.dashboardGuardianMock.user.UserId, ShouldEqual, cmd.UserId)
|
||||
So(sc.dashboardGuardianMock.DashId, ShouldEqual, 0)
|
||||
So(sc.dashboardGuardianMock.OrgId, ShouldEqual, cmd.OrgId)
|
||||
So(sc.dashboardGuardianMock.User.UserId, ShouldEqual, cmd.UserId)
|
||||
})
|
||||
})
|
||||
|
||||
@ -165,9 +165,9 @@ func TestIntegratedDashboardService(t *testing.T) {
|
||||
So(err, ShouldNotBeNil)
|
||||
So(err, ShouldEqual, models.ErrDashboardUpdateAccessDenied)
|
||||
|
||||
So(sc.dashboardGuardianMock.dashId, ShouldEqual, otherSavedFolder.Id)
|
||||
So(sc.dashboardGuardianMock.orgId, ShouldEqual, cmd.OrgId)
|
||||
So(sc.dashboardGuardianMock.user.UserId, ShouldEqual, cmd.UserId)
|
||||
So(sc.dashboardGuardianMock.DashId, ShouldEqual, otherSavedFolder.Id)
|
||||
So(sc.dashboardGuardianMock.OrgId, ShouldEqual, cmd.OrgId)
|
||||
So(sc.dashboardGuardianMock.User.UserId, ShouldEqual, cmd.UserId)
|
||||
})
|
||||
})
|
||||
|
||||
@ -189,9 +189,9 @@ func TestIntegratedDashboardService(t *testing.T) {
|
||||
So(err, ShouldNotBeNil)
|
||||
So(err, ShouldEqual, models.ErrDashboardUpdateAccessDenied)
|
||||
|
||||
So(sc.dashboardGuardianMock.dashId, ShouldEqual, savedDashInGeneralFolder.Id)
|
||||
So(sc.dashboardGuardianMock.orgId, ShouldEqual, cmd.OrgId)
|
||||
So(sc.dashboardGuardianMock.user.UserId, ShouldEqual, cmd.UserId)
|
||||
So(sc.dashboardGuardianMock.DashId, ShouldEqual, savedDashInGeneralFolder.Id)
|
||||
So(sc.dashboardGuardianMock.OrgId, ShouldEqual, cmd.OrgId)
|
||||
So(sc.dashboardGuardianMock.User.UserId, ShouldEqual, cmd.UserId)
|
||||
})
|
||||
})
|
||||
|
||||
@ -213,9 +213,9 @@ func TestIntegratedDashboardService(t *testing.T) {
|
||||
So(err, ShouldNotBeNil)
|
||||
So(err, ShouldEqual, models.ErrDashboardUpdateAccessDenied)
|
||||
|
||||
So(sc.dashboardGuardianMock.dashId, ShouldEqual, savedDashInFolder.Id)
|
||||
So(sc.dashboardGuardianMock.orgId, ShouldEqual, cmd.OrgId)
|
||||
So(sc.dashboardGuardianMock.user.UserId, ShouldEqual, cmd.UserId)
|
||||
So(sc.dashboardGuardianMock.DashId, ShouldEqual, savedDashInFolder.Id)
|
||||
So(sc.dashboardGuardianMock.OrgId, ShouldEqual, cmd.OrgId)
|
||||
So(sc.dashboardGuardianMock.User.UserId, ShouldEqual, cmd.UserId)
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -785,68 +785,16 @@ func TestIntegratedDashboardService(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func mockDashboardGuardian(mock *mockDashboardGuarder) {
|
||||
guardian.New = func(dashId int64, orgId int64, user *models.SignedInUser) guardian.DashboardGuardian {
|
||||
mock.orgId = orgId
|
||||
mock.dashId = dashId
|
||||
mock.user = user
|
||||
return mock
|
||||
}
|
||||
}
|
||||
|
||||
type mockDashboardGuarder struct {
|
||||
dashId int64
|
||||
orgId int64
|
||||
user *models.SignedInUser
|
||||
canSave bool
|
||||
canSaveCallCounter int
|
||||
canEdit bool
|
||||
canView bool
|
||||
canAdmin bool
|
||||
hasPermission bool
|
||||
checkPermissionBeforeRemove bool
|
||||
checkPermissionBeforeUpdate bool
|
||||
}
|
||||
|
||||
func (g *mockDashboardGuarder) CanSave() (bool, error) {
|
||||
g.canSaveCallCounter++
|
||||
return g.canSave, nil
|
||||
}
|
||||
|
||||
func (g *mockDashboardGuarder) CanEdit() (bool, error) {
|
||||
return g.canEdit, nil
|
||||
}
|
||||
|
||||
func (g *mockDashboardGuarder) CanView() (bool, error) {
|
||||
return g.canView, nil
|
||||
}
|
||||
|
||||
func (g *mockDashboardGuarder) CanAdmin() (bool, error) {
|
||||
return g.canAdmin, nil
|
||||
}
|
||||
|
||||
func (g *mockDashboardGuarder) HasPermission(permission models.PermissionType) (bool, error) {
|
||||
return g.hasPermission, nil
|
||||
}
|
||||
|
||||
func (g *mockDashboardGuarder) CheckPermissionBeforeUpdate(permission models.PermissionType, updatePermissions []*models.DashboardAcl) (bool, error) {
|
||||
return g.checkPermissionBeforeUpdate, nil
|
||||
}
|
||||
|
||||
func (g *mockDashboardGuarder) GetAcl() ([]*models.DashboardAclInfoDTO, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
type scenarioContext struct {
|
||||
dashboardGuardianMock *mockDashboardGuarder
|
||||
dashboardGuardianMock *guardian.FakeDashboardGuardian
|
||||
}
|
||||
|
||||
type scenarioFunc func(c *scenarioContext)
|
||||
|
||||
func dashboardGuardianScenario(desc string, mock *mockDashboardGuarder, fn scenarioFunc) {
|
||||
func dashboardGuardianScenario(desc string, mock *guardian.FakeDashboardGuardian, fn scenarioFunc) {
|
||||
Convey(desc, func() {
|
||||
origNewDashboardGuardian := guardian.New
|
||||
mockDashboardGuardian(mock)
|
||||
guardian.MockDashboardGuardian(mock)
|
||||
|
||||
sc := &scenarioContext{
|
||||
dashboardGuardianMock: mock,
|
||||
@ -861,15 +809,15 @@ func dashboardGuardianScenario(desc string, mock *mockDashboardGuarder, fn scena
|
||||
}
|
||||
|
||||
type dashboardPermissionScenarioContext struct {
|
||||
dashboardGuardianMock *mockDashboardGuarder
|
||||
dashboardGuardianMock *guardian.FakeDashboardGuardian
|
||||
}
|
||||
|
||||
type dashboardPermissionScenarioFunc func(sc *dashboardPermissionScenarioContext)
|
||||
|
||||
func dashboardPermissionScenario(desc string, mock *mockDashboardGuarder, fn dashboardPermissionScenarioFunc) {
|
||||
func dashboardPermissionScenario(desc string, mock *guardian.FakeDashboardGuardian, fn dashboardPermissionScenarioFunc) {
|
||||
Convey(desc, func() {
|
||||
origNewDashboardGuardian := guardian.New
|
||||
mockDashboardGuardian(mock)
|
||||
guardian.MockDashboardGuardian(mock)
|
||||
|
||||
sc := &dashboardPermissionScenarioContext{
|
||||
dashboardGuardianMock: mock,
|
||||
@ -884,8 +832,8 @@ func dashboardPermissionScenario(desc string, mock *mockDashboardGuarder, fn das
|
||||
}
|
||||
|
||||
func permissionScenario(desc string, canSave bool, fn dashboardPermissionScenarioFunc) {
|
||||
mock := &mockDashboardGuarder{
|
||||
canSave: canSave,
|
||||
mock := &guardian.FakeDashboardGuardian{
|
||||
CanSaveValue: canSave,
|
||||
}
|
||||
dashboardPermissionScenario(desc, mock, fn)
|
||||
}
|
||||
@ -902,10 +850,10 @@ func callSaveWithError(cmd models.SaveDashboardCommand) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func dashboardServiceScenario(desc string, mock *mockDashboardGuarder, fn scenarioFunc) {
|
||||
func dashboardServiceScenario(desc string, mock *guardian.FakeDashboardGuardian, fn scenarioFunc) {
|
||||
Convey(desc, func() {
|
||||
origNewDashboardGuardian := guardian.New
|
||||
mockDashboardGuardian(mock)
|
||||
guardian.MockDashboardGuardian(mock)
|
||||
|
||||
sc := &scenarioContext{
|
||||
dashboardGuardianMock: mock,
|
||||
|
Loading…
Reference in New Issue
Block a user