Commit 02278f90 by Marcus Efraimsson

dashboards: make fake dashboard guardian available to other packages

parent 892bdecb
...@@ -17,7 +17,7 @@ func TestDashboardService(t *testing.T) { ...@@ -17,7 +17,7 @@ func TestDashboardService(t *testing.T) {
service := dashboardServiceImpl{} service := dashboardServiceImpl{}
origNewDashboardGuardian := guardian.New origNewDashboardGuardian := guardian.New
mockDashboardGuardian(&fakeDashboardGuardian{canSave: true}) guardian.MockDashboardGuardian(&guardian.FakeDashboardGuardian{CanSaveValue: true})
Convey("Save dashboard validation", func() { Convey("Save dashboard validation", func() {
dto := &SaveDashboardDTO{} dto := &SaveDashboardDTO{}
...@@ -93,52 +93,3 @@ func TestDashboardService(t *testing.T) { ...@@ -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) { ...@@ -20,7 +20,7 @@ func TestFolderService(t *testing.T) {
Convey("Given user has no permissions", func() { Convey("Given user has no permissions", func() {
origNewGuardian := guardian.New origNewGuardian := guardian.New
mockDashboardGuardian(&fakeDashboardGuardian{}) guardian.MockDashboardGuardian(&guardian.FakeDashboardGuardian{})
bus.AddHandler("test", func(query *models.GetDashboardQuery) error { bus.AddHandler("test", func(query *models.GetDashboardQuery) error {
query.Result = models.NewDashboardFolder("Folder") query.Result = models.NewDashboardFolder("Folder")
...@@ -77,7 +77,7 @@ func TestFolderService(t *testing.T) { ...@@ -77,7 +77,7 @@ func TestFolderService(t *testing.T) {
Convey("Given user has permission to save", func() { Convey("Given user has permission to save", func() {
origNewGuardian := guardian.New origNewGuardian := guardian.New
mockDashboardGuardian(&fakeDashboardGuardian{canSave: true}) guardian.MockDashboardGuardian(&guardian.FakeDashboardGuardian{CanSaveValue: true})
dash := models.NewDashboardFolder("Folder") dash := models.NewDashboardFolder("Folder")
dash.Id = 1 dash.Id = 1
...@@ -135,7 +135,7 @@ func TestFolderService(t *testing.T) { ...@@ -135,7 +135,7 @@ func TestFolderService(t *testing.T) {
Convey("Given user has permission to view", func() { Convey("Given user has permission to view", func() {
origNewGuardian := guardian.New origNewGuardian := guardian.New
mockDashboardGuardian(&fakeDashboardGuardian{canView: true}) guardian.MockDashboardGuardian(&guardian.FakeDashboardGuardian{CanViewValue: true})
dashFolder := models.NewDashboardFolder("Folder") dashFolder := models.NewDashboardFolder("Folder")
dashFolder.Id = 1 dashFolder.Id = 1
...@@ -153,7 +153,7 @@ func TestFolderService(t *testing.T) { ...@@ -153,7 +153,7 @@ func TestFolderService(t *testing.T) {
So(f.Title, ShouldEqual, dashFolder.Title) 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") f, _ := service.GetFolderByUid("uid")
So(f.Id, ShouldEqual, dashFolder.Id) So(f.Id, ShouldEqual, dashFolder.Id)
So(f.Uid, ShouldEqual, dashFolder.Uid) So(f.Uid, ShouldEqual, dashFolder.Uid)
......
...@@ -158,3 +158,52 @@ func (g *dashboardGuardianImpl) getTeams() ([]*m.Team, error) { ...@@ -158,3 +158,52 @@ func (g *dashboardGuardianImpl) getTeams() ([]*m.Team, error) {
g.groups = query.Result g.groups = query.Result
return query.Result, err 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) { ...@@ -142,9 +142,9 @@ func TestIntegratedDashboardService(t *testing.T) {
So(err, ShouldNotBeNil) So(err, ShouldNotBeNil)
So(err, ShouldEqual, models.ErrDashboardUpdateAccessDenied) So(err, ShouldEqual, models.ErrDashboardUpdateAccessDenied)
So(sc.dashboardGuardianMock.dashId, ShouldEqual, 0) So(sc.dashboardGuardianMock.DashId, ShouldEqual, 0)
So(sc.dashboardGuardianMock.orgId, ShouldEqual, cmd.OrgId) So(sc.dashboardGuardianMock.OrgId, ShouldEqual, cmd.OrgId)
So(sc.dashboardGuardianMock.user.UserId, ShouldEqual, cmd.UserId) So(sc.dashboardGuardianMock.User.UserId, ShouldEqual, cmd.UserId)
}) })
}) })
...@@ -165,9 +165,9 @@ func TestIntegratedDashboardService(t *testing.T) { ...@@ -165,9 +165,9 @@ func TestIntegratedDashboardService(t *testing.T) {
So(err, ShouldNotBeNil) So(err, ShouldNotBeNil)
So(err, ShouldEqual, models.ErrDashboardUpdateAccessDenied) So(err, ShouldEqual, models.ErrDashboardUpdateAccessDenied)
So(sc.dashboardGuardianMock.dashId, ShouldEqual, otherSavedFolder.Id) So(sc.dashboardGuardianMock.DashId, ShouldEqual, otherSavedFolder.Id)
So(sc.dashboardGuardianMock.orgId, ShouldEqual, cmd.OrgId) So(sc.dashboardGuardianMock.OrgId, ShouldEqual, cmd.OrgId)
So(sc.dashboardGuardianMock.user.UserId, ShouldEqual, cmd.UserId) So(sc.dashboardGuardianMock.User.UserId, ShouldEqual, cmd.UserId)
}) })
}) })
...@@ -189,9 +189,9 @@ func TestIntegratedDashboardService(t *testing.T) { ...@@ -189,9 +189,9 @@ func TestIntegratedDashboardService(t *testing.T) {
So(err, ShouldNotBeNil) So(err, ShouldNotBeNil)
So(err, ShouldEqual, models.ErrDashboardUpdateAccessDenied) So(err, ShouldEqual, models.ErrDashboardUpdateAccessDenied)
So(sc.dashboardGuardianMock.dashId, ShouldEqual, savedDashInGeneralFolder.Id) So(sc.dashboardGuardianMock.DashId, ShouldEqual, savedDashInGeneralFolder.Id)
So(sc.dashboardGuardianMock.orgId, ShouldEqual, cmd.OrgId) So(sc.dashboardGuardianMock.OrgId, ShouldEqual, cmd.OrgId)
So(sc.dashboardGuardianMock.user.UserId, ShouldEqual, cmd.UserId) So(sc.dashboardGuardianMock.User.UserId, ShouldEqual, cmd.UserId)
}) })
}) })
...@@ -213,9 +213,9 @@ func TestIntegratedDashboardService(t *testing.T) { ...@@ -213,9 +213,9 @@ func TestIntegratedDashboardService(t *testing.T) {
So(err, ShouldNotBeNil) So(err, ShouldNotBeNil)
So(err, ShouldEqual, models.ErrDashboardUpdateAccessDenied) So(err, ShouldEqual, models.ErrDashboardUpdateAccessDenied)
So(sc.dashboardGuardianMock.dashId, ShouldEqual, savedDashInFolder.Id) So(sc.dashboardGuardianMock.DashId, ShouldEqual, savedDashInFolder.Id)
So(sc.dashboardGuardianMock.orgId, ShouldEqual, cmd.OrgId) So(sc.dashboardGuardianMock.OrgId, ShouldEqual, cmd.OrgId)
So(sc.dashboardGuardianMock.user.UserId, ShouldEqual, cmd.UserId) So(sc.dashboardGuardianMock.User.UserId, ShouldEqual, cmd.UserId)
}) })
}) })
}) })
...@@ -785,68 +785,16 @@ func TestIntegratedDashboardService(t *testing.T) { ...@@ -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 { type scenarioContext struct {
dashboardGuardianMock *mockDashboardGuarder dashboardGuardianMock *guardian.FakeDashboardGuardian
} }
type scenarioFunc func(c *scenarioContext) 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() { Convey(desc, func() {
origNewDashboardGuardian := guardian.New origNewDashboardGuardian := guardian.New
mockDashboardGuardian(mock) guardian.MockDashboardGuardian(mock)
sc := &scenarioContext{ sc := &scenarioContext{
dashboardGuardianMock: mock, dashboardGuardianMock: mock,
...@@ -861,15 +809,15 @@ func dashboardGuardianScenario(desc string, mock *mockDashboardGuarder, fn scena ...@@ -861,15 +809,15 @@ func dashboardGuardianScenario(desc string, mock *mockDashboardGuarder, fn scena
} }
type dashboardPermissionScenarioContext struct { type dashboardPermissionScenarioContext struct {
dashboardGuardianMock *mockDashboardGuarder dashboardGuardianMock *guardian.FakeDashboardGuardian
} }
type dashboardPermissionScenarioFunc func(sc *dashboardPermissionScenarioContext) 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() { Convey(desc, func() {
origNewDashboardGuardian := guardian.New origNewDashboardGuardian := guardian.New
mockDashboardGuardian(mock) guardian.MockDashboardGuardian(mock)
sc := &dashboardPermissionScenarioContext{ sc := &dashboardPermissionScenarioContext{
dashboardGuardianMock: mock, dashboardGuardianMock: mock,
...@@ -884,8 +832,8 @@ func dashboardPermissionScenario(desc string, mock *mockDashboardGuarder, fn das ...@@ -884,8 +832,8 @@ func dashboardPermissionScenario(desc string, mock *mockDashboardGuarder, fn das
} }
func permissionScenario(desc string, canSave bool, fn dashboardPermissionScenarioFunc) { func permissionScenario(desc string, canSave bool, fn dashboardPermissionScenarioFunc) {
mock := &mockDashboardGuarder{ mock := &guardian.FakeDashboardGuardian{
canSave: canSave, CanSaveValue: canSave,
} }
dashboardPermissionScenario(desc, mock, fn) dashboardPermissionScenario(desc, mock, fn)
} }
...@@ -902,10 +850,10 @@ func callSaveWithError(cmd models.SaveDashboardCommand) error { ...@@ -902,10 +850,10 @@ func callSaveWithError(cmd models.SaveDashboardCommand) error {
return err return err
} }
func dashboardServiceScenario(desc string, mock *mockDashboardGuarder, fn scenarioFunc) { func dashboardServiceScenario(desc string, mock *guardian.FakeDashboardGuardian, fn scenarioFunc) {
Convey(desc, func() { Convey(desc, func() {
origNewDashboardGuardian := guardian.New origNewDashboardGuardian := guardian.New
mockDashboardGuardian(mock) guardian.MockDashboardGuardian(mock)
sc := &scenarioContext{ sc := &scenarioContext{
dashboardGuardianMock: mock, dashboardGuardianMock: mock,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment