@ -5,40 +5,40 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
m "github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
func TestDashboardAclApiEndpoint ( t * testing . T ) {
Convey ( "Given a dashboard acl" , t , func ( ) {
mockResult := [ ] * models . DashboardAcl {
{ Id : 1 , OrgId : 1 , DashboardId : 1 , UserId : 2 , Permission : models . PERMISSION_VIEW } ,
{ Id : 2 , OrgId : 1 , DashboardId : 1 , UserId : 3 , Permission : models . PERMISSION_EDIT } ,
{ Id : 3 , OrgId : 1 , DashboardId : 1 , UserId : 4 , Permission : models . PERMISSION_ADMIN } ,
{ Id : 4 , OrgId : 1 , DashboardId : 1 , UserGroupId : 1 , Permission : models . PERMISSION_VIEW } ,
{ Id : 5 , OrgId : 1 , DashboardId : 1 , UserGroupId : 2 , Permission : models . PERMISSION_ADMIN } ,
mockResult := [ ] * m . DashboardAclInfoDTO {
{ Id : 1 , OrgId : 1 , DashboardId : 1 , UserId : 2 , Permission : m . PERMISSION_VIEW } ,
{ Id : 2 , OrgId : 1 , DashboardId : 1 , UserId : 3 , Permission : m . PERMISSION_EDIT } ,
{ Id : 3 , OrgId : 1 , DashboardId : 1 , UserId : 4 , Permission : m . PERMISSION_ADMIN } ,
{ Id : 4 , OrgId : 1 , DashboardId : 1 , UserGroupId : 1 , Permission : m . PERMISSION_VIEW } ,
{ Id : 5 , OrgId : 1 , DashboardId : 1 , UserGroupId : 2 , Permission : m . PERMISSION_ADMIN } ,
}
dtoRes := transformDashboardAclsToDTOs ( mockResult )
bus . AddHandler ( "test" , func ( query * models . GetDashboardAclInfoListQuery ) error {
bus . AddHandler ( "test" , func ( query * m . GetDashboardAclInfoListQuery ) error {
query . Result = dtoRes
return nil
} )
bus . AddHandler ( "test" , func ( query * models . GetDashboardAclInfoListQuery ) error {
bus . AddHandler ( "test" , func ( query * m . GetDashboardAclInfoListQuery ) error {
query . Result = mockResult
return nil
} )
userGroupResp := [ ] * models . UserGroup { }
bus . AddHandler ( "test" , func ( query * models . GetUserGroupsByUserQuery ) error {
userGroupResp := [ ] * m . UserGroup { }
bus . AddHandler ( "test" , func ( query * m . GetUserGroupsByUserQuery ) error {
query . Result = userGroupResp
return nil
} )
Convey ( "When user is org admin" , func ( ) {
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/id/1/acl" , "/api/dashboards/id/:dashboardsId/acl" , models . ROLE_ADMIN , func ( sc * scenarioContext ) {
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/id/1/acl" , "/api/dashboards/id/:dashboardsId/acl" , m . ROLE_ADMIN , func ( sc * scenarioContext ) {
Convey ( "Should be able to access ACL" , func ( ) {
sc . handlerFunc = GetDashboardAclList
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
@ -49,14 +49,14 @@ func TestDashboardAclApiEndpoint(t *testing.T) {
So ( err , ShouldBeNil )
So ( len ( respJSON . MustArray ( ) ) , ShouldEqual , 5 )
So ( respJSON . GetIndex ( 0 ) . Get ( "userId" ) . MustInt ( ) , ShouldEqual , 2 )
So ( respJSON . GetIndex ( 0 ) . Get ( "permission" ) . MustInt ( ) , ShouldEqual , models . PERMISSION_VIEW )
So ( respJSON . GetIndex ( 0 ) . Get ( "permission" ) . MustInt ( ) , ShouldEqual , m . PERMISSION_VIEW )
} )
} )
} )
Convey ( "When user is editor and has admin permission in the ACL" , func ( ) {
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/id/1/acl" , "/api/dashboards/id/:dashboardId/acl" , models . ROLE_EDITOR , func ( sc * scenarioContext ) {
mockResult = append ( mockResult , & models . DashboardAcl { Id : 1 , OrgId : 1 , DashboardId : 1 , UserId : 1 , Permission : models . PERMISSION_ADMIN } )
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/id/1/acl" , "/api/dashboards/id/:dashboardId/acl" , m . ROLE_EDITOR , func ( sc * scenarioContext ) {
mockResult = append ( mockResult , & m . DashboardAclInfoDTO { Id : 1 , OrgId : 1 , DashboardId : 1 , UserId : 1 , Permission : m . PERMISSION_ADMIN } )
Convey ( "Should be able to access ACL" , func ( ) {
sc . handlerFunc = GetDashboardAclList
@ -66,10 +66,10 @@ func TestDashboardAclApiEndpoint(t *testing.T) {
} )
} )
loggedInUserScenarioWithRole ( "When calling DELETE on" , "DELETE" , "/api/dashboards/id/1/acl/1" , "/api/dashboards/id/:dashboardId/acl/:aclId" , models . ROLE_EDITOR , func ( sc * scenarioContext ) {
mockResult = append ( mockResult , & models . DashboardAcl { Id : 1 , OrgId : 1 , DashboardId : 1 , UserId : 1 , Permission : models . PERMISSION_ADMIN } )
loggedInUserScenarioWithRole ( "When calling DELETE on" , "DELETE" , "/api/dashboards/id/1/acl/1" , "/api/dashboards/id/:dashboardId/acl/:aclId" , m . ROLE_EDITOR , func ( sc * scenarioContext ) {
mockResult = append ( mockResult , & m . DashboardAclInfoDTO { Id : 1 , OrgId : 1 , DashboardId : 1 , UserId : 1 , Permission : m . PERMISSION_ADMIN } )
bus . AddHandler ( "test3" , func ( cmd * models . RemoveDashboardAclCommand ) error {
bus . AddHandler ( "test3" , func ( cmd * m . RemoveDashboardAclCommand ) error {
return nil
} )
@ -82,10 +82,10 @@ func TestDashboardAclApiEndpoint(t *testing.T) {
} )
Convey ( "When user is a member of a user group in the ACL with admin permission" , func ( ) {
loggedInUserScenarioWithRole ( "When calling DELETE on" , "DELETE" , "/api/dashboards/id/1/acl/1" , "/api/dashboards/id/:dashboardsId/acl/:aclId" , models . ROLE_EDITOR , func ( sc * scenarioContext ) {
userGroupResp = append ( userGroupResp , & models . UserGroup { Id : 2 , OrgId : 1 , Name : "UG2" } )
loggedInUserScenarioWithRole ( "When calling DELETE on" , "DELETE" , "/api/dashboards/id/1/acl/1" , "/api/dashboards/id/:dashboardsId/acl/:aclId" , m . ROLE_EDITOR , func ( sc * scenarioContext ) {
userGroupResp = append ( userGroupResp , & m . UserGroup { Id : 2 , OrgId : 1 , Name : "UG2" } )
bus . AddHandler ( "test3" , func ( cmd * models . RemoveDashboardAclCommand ) error {
bus . AddHandler ( "test3" , func ( cmd * m . RemoveDashboardAclCommand ) error {
return nil
} )
@ -100,8 +100,8 @@ func TestDashboardAclApiEndpoint(t *testing.T) {
} )
Convey ( "When user is editor and has edit permission in the ACL" , func ( ) {
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/id/1/acl" , "/api/dashboards/id/:dashboardId/acl" , models . ROLE_EDITOR , func ( sc * scenarioContext ) {
mockResult = append ( mockResult , & models . DashboardAcl { Id : 1 , OrgId : 1 , DashboardId : 1 , UserId : 1 , Permission : models . PERMISSION_EDIT } )
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/id/1/acl" , "/api/dashboards/id/:dashboardId/acl" , m . ROLE_EDITOR , func ( sc * scenarioContext ) {
mockResult = append ( mockResult , & m . DashboardAclInfoDTO { Id : 1 , OrgId : 1 , DashboardId : 1 , UserId : 1 , Permission : m . PERMISSION_EDIT } )
Convey ( "Should not be able to access ACL" , func ( ) {
sc . handlerFunc = GetDashboardAclList
@ -111,10 +111,10 @@ func TestDashboardAclApiEndpoint(t *testing.T) {
} )
} )
loggedInUserScenarioWithRole ( "When calling DELETE on" , "DELETE" , "/api/dashboards/id/1/acl/1" , "/api/dashboards/id/:dashboardId/acl/:aclId" , models . ROLE_EDITOR , func ( sc * scenarioContext ) {
mockResult = append ( mockResult , & models . DashboardAcl { Id : 1 , OrgId : 1 , DashboardId : 1 , UserId : 1 , Permission : models . PERMISSION_EDIT } )
loggedInUserScenarioWithRole ( "When calling DELETE on" , "DELETE" , "/api/dashboards/id/1/acl/1" , "/api/dashboards/id/:dashboardId/acl/:aclId" , m . ROLE_EDITOR , func ( sc * scenarioContext ) {
mockResult = append ( mockResult , & m . DashboardAclInfoDTO { Id : 1 , OrgId : 1 , DashboardId : 1 , UserId : 1 , Permission : m . PERMISSION_EDIT } )
bus . AddHandler ( "test3" , func ( cmd * models . RemoveDashboardAclCommand ) error {
bus . AddHandler ( "test3" , func ( cmd * m . RemoveDashboardAclCommand ) error {
return nil
} )
@ -128,7 +128,7 @@ func TestDashboardAclApiEndpoint(t *testing.T) {
} )
Convey ( "When user is editor and not in the ACL" , func ( ) {
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/id/1/acl" , "/api/dashboards/id/:dashboardsId/acl" , models . ROLE_EDITOR , func ( sc * scenarioContext ) {
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/id/1/acl" , "/api/dashboards/id/:dashboardsId/acl" , m . ROLE_EDITOR , func ( sc * scenarioContext ) {
Convey ( "Should not be able to access ACL" , func ( ) {
sc . handlerFunc = GetDashboardAclList
@ -138,9 +138,9 @@ func TestDashboardAclApiEndpoint(t *testing.T) {
} )
} )
loggedInUserScenarioWithRole ( "When calling DELETE on" , "DELETE" , "/api/dashboards/id/1/acl/user/1" , "/api/dashboards/id/:dashboardsId/acl/user/:userId" , models . ROLE_EDITOR , func ( sc * scenarioContext ) {
mockResult = append ( mockResult , & models . DashboardAcl { Id : 1 , OrgId : 1 , DashboardId : 1 , UserId : 1 , Permission : models . PERMISSION_VIEW } )
bus . AddHandler ( "test3" , func ( cmd * models . RemoveDashboardAclCommand ) error {
loggedInUserScenarioWithRole ( "When calling DELETE on" , "DELETE" , "/api/dashboards/id/1/acl/user/1" , "/api/dashboards/id/:dashboardsId/acl/user/:userId" , m . ROLE_EDITOR , func ( sc * scenarioContext ) {
mockResult = append ( mockResult , & m . DashboardAclInfoDTO { Id : 1 , OrgId : 1 , DashboardId : 1 , UserId : 1 , Permission : m . PERMISSION_VIEW } )
bus . AddHandler ( "test3" , func ( cmd * m . RemoveDashboardAclCommand ) error {
return nil
} )
@ -155,11 +155,11 @@ func TestDashboardAclApiEndpoint(t *testing.T) {
} )
}
func transformDashboardAclsToDTOs ( acls [ ] * models . DashboardAcl ) [ ] * models . DashboardAclInfoDTO {
dtos := make ( [ ] * models . DashboardAclInfoDTO , 0 )
func transformDashboardAclsToDTOs ( acls [ ] * m . DashboardAclInfoDTO ) [ ] * m . DashboardAclInfoDTO {
dtos := make ( [ ] * m . DashboardAclInfoDTO , 0 )
for _ , acl := range acls {
dto := & models . DashboardAclInfoDTO {
dto := & m . DashboardAclInfoDTO {
Id : acl . Id ,
OrgId : acl . OrgId ,
DashboardId : acl . DashboardId ,