Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
02278f90
Commit
02278f90
authored
Feb 20, 2018
by
Marcus Efraimsson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
dashboards: make fake dashboard guardian available to other packages
parent
892bdecb
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
76 additions
and
128 deletions
+76
-128
pkg/services/dashboards/dashboard_service_test.go
+1
-50
pkg/services/dashboards/folder_service_test.go
+4
-4
pkg/services/guardian/guardian.go
+49
-0
pkg/services/sqlstore/dashboard_service_integration_test.go
+22
-74
No files found.
pkg/services/dashboards/dashboard_service_test.go
View file @
02278f90
...
@@ -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
{
canSav
e
:
true
})
guardian
.
MockDashboardGuardian
(
&
guardian
.
FakeDashboardGuardian
{
CanSaveValu
e
:
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
}
pkg/services/dashboards/folder_service_test.go
View file @
02278f90
...
@@ -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
(
&
f
akeDashboardGuardian
{})
guardian
.
MockDashboardGuardian
(
&
guardian
.
F
akeDashboardGuardian
{})
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
{
canSav
e
:
true
})
guardian
.
MockDashboardGuardian
(
&
guardian
.
FakeDashboardGuardian
{
CanSaveValu
e
:
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 erro
r"
,
func
()
{
Convey
(
"When get folder by uid should
return folde
r"
,
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
)
...
...
pkg/services/guardian/guardian.go
View file @
02278f90
...
@@ -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
}
}
pkg/services/sqlstore/dashboard_service_integration_test.go
View file @
02278f90
...
@@ -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
.
d
ashId
,
ShouldEqual
,
0
)
So
(
sc
.
dashboardGuardianMock
.
D
ashId
,
ShouldEqual
,
0
)
So
(
sc
.
dashboardGuardianMock
.
o
rgId
,
ShouldEqual
,
cmd
.
OrgId
)
So
(
sc
.
dashboardGuardianMock
.
O
rgId
,
ShouldEqual
,
cmd
.
OrgId
)
So
(
sc
.
dashboardGuardianMock
.
u
ser
.
UserId
,
ShouldEqual
,
cmd
.
UserId
)
So
(
sc
.
dashboardGuardianMock
.
U
ser
.
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
.
d
ashId
,
ShouldEqual
,
otherSavedFolder
.
Id
)
So
(
sc
.
dashboardGuardianMock
.
D
ashId
,
ShouldEqual
,
otherSavedFolder
.
Id
)
So
(
sc
.
dashboardGuardianMock
.
o
rgId
,
ShouldEqual
,
cmd
.
OrgId
)
So
(
sc
.
dashboardGuardianMock
.
O
rgId
,
ShouldEqual
,
cmd
.
OrgId
)
So
(
sc
.
dashboardGuardianMock
.
u
ser
.
UserId
,
ShouldEqual
,
cmd
.
UserId
)
So
(
sc
.
dashboardGuardianMock
.
U
ser
.
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
.
d
ashId
,
ShouldEqual
,
savedDashInGeneralFolder
.
Id
)
So
(
sc
.
dashboardGuardianMock
.
D
ashId
,
ShouldEqual
,
savedDashInGeneralFolder
.
Id
)
So
(
sc
.
dashboardGuardianMock
.
o
rgId
,
ShouldEqual
,
cmd
.
OrgId
)
So
(
sc
.
dashboardGuardianMock
.
O
rgId
,
ShouldEqual
,
cmd
.
OrgId
)
So
(
sc
.
dashboardGuardianMock
.
u
ser
.
UserId
,
ShouldEqual
,
cmd
.
UserId
)
So
(
sc
.
dashboardGuardianMock
.
U
ser
.
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
.
d
ashId
,
ShouldEqual
,
savedDashInFolder
.
Id
)
So
(
sc
.
dashboardGuardianMock
.
D
ashId
,
ShouldEqual
,
savedDashInFolder
.
Id
)
So
(
sc
.
dashboardGuardianMock
.
o
rgId
,
ShouldEqual
,
cmd
.
OrgId
)
So
(
sc
.
dashboardGuardianMock
.
O
rgId
,
ShouldEqual
,
cmd
.
OrgId
)
So
(
sc
.
dashboardGuardianMock
.
u
ser
.
UserId
,
ShouldEqual
,
cmd
.
UserId
)
So
(
sc
.
dashboardGuardianMock
.
U
ser
.
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
m
ockDashboardGuardian
(
mock
)
guardian
.
M
ockDashboardGuardian
(
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
m
ockDashboardGuardian
(
mock
)
guardian
.
M
ockDashboardGuardian
(
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
{
canSav
e
:
canSave
,
CanSaveValu
e
:
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
m
ockDashboardGuardian
(
mock
)
guardian
.
M
ockDashboardGuardian
(
mock
)
sc
:=
&
scenarioContext
{
sc
:=
&
scenarioContext
{
dashboardGuardianMock
:
mock
,
dashboardGuardianMock
:
mock
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment