2018-02-20 11:11:50 -06:00
package api
import (
"testing"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/bus"
2018-02-26 13:14:21 -06:00
"github.com/grafana/grafana/pkg/components/simplejson"
2018-02-20 11:11:50 -06:00
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/guardian"
. "github.com/smartystreets/goconvey/convey"
)
2018-02-21 04:45:19 -06:00
func TestFolderPermissionApiEndpoint ( t * testing . T ) {
2018-02-20 11:11:50 -06:00
Convey ( "Folder permissions test" , t , func ( ) {
2018-02-26 13:14:21 -06:00
Convey ( "Given folder not exists" , func ( ) {
mock := & fakeFolderService {
2018-03-22 16:13:46 -05:00
GetFolderByUIDError : m . ErrFolderNotFound ,
2018-02-26 13:14:21 -06:00
}
origNewFolderService := dashboards . NewFolderService
mockFolderService ( mock )
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/folders/uid/permissions" , "/api/folders/:uid/permissions" , m . ROLE_EDITOR , func ( sc * scenarioContext ) {
callGetFolderPermissions ( sc )
So ( sc . resp . Code , ShouldEqual , 404 )
} )
cmd := dtos . UpdateDashboardAclCommand {
Items : [ ] dtos . DashboardAclUpdateItem {
{ UserId : 1000 , Permission : m . PERMISSION_ADMIN } ,
} ,
}
updateFolderPermissionScenario ( "When calling POST on" , "/api/folders/uid/permissions" , "/api/folders/:uid/permissions" , cmd , func ( sc * scenarioContext ) {
callUpdateFolderPermissions ( sc )
So ( sc . resp . Code , ShouldEqual , 404 )
} )
Reset ( func ( ) {
dashboards . NewFolderService = origNewFolderService
} )
} )
2018-02-20 11:11:50 -06:00
Convey ( "Given user has no admin permissions" , func ( ) {
origNewGuardian := guardian . New
guardian . MockDashboardGuardian ( & guardian . FakeDashboardGuardian { CanAdminValue : false } )
mock := & fakeFolderService {
2018-03-22 16:13:46 -05:00
GetFolderByUIDResult : & m . Folder {
2018-02-20 11:11:50 -06:00
Id : 1 ,
Uid : "uid" ,
Title : "Folder" ,
} ,
}
origNewFolderService := dashboards . NewFolderService
mockFolderService ( mock )
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/folders/uid/permissions" , "/api/folders/:uid/permissions" , m . ROLE_EDITOR , func ( sc * scenarioContext ) {
callGetFolderPermissions ( sc )
So ( sc . resp . Code , ShouldEqual , 403 )
} )
cmd := dtos . UpdateDashboardAclCommand {
Items : [ ] dtos . DashboardAclUpdateItem {
{ UserId : 1000 , Permission : m . PERMISSION_ADMIN } ,
} ,
}
updateFolderPermissionScenario ( "When calling POST on" , "/api/folders/uid/permissions" , "/api/folders/:uid/permissions" , cmd , func ( sc * scenarioContext ) {
callUpdateFolderPermissions ( sc )
So ( sc . resp . Code , ShouldEqual , 403 )
} )
Reset ( func ( ) {
guardian . New = origNewGuardian
dashboards . NewFolderService = origNewFolderService
} )
} )
Convey ( "Given user has admin permissions and permissions to update" , func ( ) {
origNewGuardian := guardian . New
2018-02-26 13:14:21 -06:00
guardian . MockDashboardGuardian ( & guardian . FakeDashboardGuardian {
CanAdminValue : true ,
CheckPermissionBeforeUpdateValue : true ,
GetAclValue : [ ] * m . DashboardAclInfoDTO {
{ OrgId : 1 , DashboardId : 1 , UserId : 2 , Permission : m . PERMISSION_VIEW } ,
{ OrgId : 1 , DashboardId : 1 , UserId : 3 , Permission : m . PERMISSION_EDIT } ,
{ OrgId : 1 , DashboardId : 1 , UserId : 4 , Permission : m . PERMISSION_ADMIN } ,
{ OrgId : 1 , DashboardId : 1 , TeamId : 1 , Permission : m . PERMISSION_VIEW } ,
{ OrgId : 1 , DashboardId : 1 , TeamId : 2 , Permission : m . PERMISSION_ADMIN } ,
} ,
} )
2018-02-20 11:11:50 -06:00
mock := & fakeFolderService {
2018-03-22 16:13:46 -05:00
GetFolderByUIDResult : & m . Folder {
2018-02-20 11:11:50 -06:00
Id : 1 ,
Uid : "uid" ,
Title : "Folder" ,
} ,
}
origNewFolderService := dashboards . NewFolderService
mockFolderService ( mock )
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/folders/uid/permissions" , "/api/folders/:uid/permissions" , m . ROLE_ADMIN , func ( sc * scenarioContext ) {
callGetFolderPermissions ( sc )
So ( sc . resp . Code , ShouldEqual , 200 )
2018-02-26 13:14:21 -06:00
respJSON , err := simplejson . NewJson ( sc . resp . Body . Bytes ( ) )
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 , m . PERMISSION_VIEW )
2018-02-20 11:11:50 -06:00
} )
cmd := dtos . UpdateDashboardAclCommand {
Items : [ ] dtos . DashboardAclUpdateItem {
{ UserId : 1000 , Permission : m . PERMISSION_ADMIN } ,
} ,
}
updateFolderPermissionScenario ( "When calling POST on" , "/api/folders/uid/permissions" , "/api/folders/:uid/permissions" , cmd , func ( sc * scenarioContext ) {
callUpdateFolderPermissions ( sc )
So ( sc . resp . Code , ShouldEqual , 200 )
} )
Reset ( func ( ) {
guardian . New = origNewGuardian
dashboards . NewFolderService = origNewFolderService
} )
} )
2018-02-26 13:14:21 -06:00
Convey ( "When trying to update permissions with duplicate permissions" , func ( ) {
origNewGuardian := guardian . New
guardian . MockDashboardGuardian ( & guardian . FakeDashboardGuardian {
CanAdminValue : true ,
CheckPermissionBeforeUpdateValue : false ,
2018-02-27 09:03:11 -06:00
CheckPermissionBeforeUpdateError : guardian . ErrGuardianPermissionExists ,
2018-02-26 13:14:21 -06:00
} )
mock := & fakeFolderService {
2018-03-22 16:13:46 -05:00
GetFolderByUIDResult : & m . Folder {
2018-02-26 13:14:21 -06:00
Id : 1 ,
Uid : "uid" ,
Title : "Folder" ,
} ,
}
origNewFolderService := dashboards . NewFolderService
mockFolderService ( mock )
cmd := dtos . UpdateDashboardAclCommand {
Items : [ ] dtos . DashboardAclUpdateItem {
{ UserId : 1000 , Permission : m . PERMISSION_ADMIN } ,
} ,
}
updateFolderPermissionScenario ( "When calling POST on" , "/api/folders/uid/permissions" , "/api/folders/:uid/permissions" , cmd , func ( sc * scenarioContext ) {
callUpdateFolderPermissions ( sc )
So ( sc . resp . Code , ShouldEqual , 400 )
} )
Reset ( func ( ) {
guardian . New = origNewGuardian
dashboards . NewFolderService = origNewFolderService
} )
} )
Convey ( "When trying to override inherited permissions with lower presedence" , func ( ) {
origNewGuardian := guardian . New
guardian . MockDashboardGuardian ( & guardian . FakeDashboardGuardian {
CanAdminValue : true ,
CheckPermissionBeforeUpdateValue : false ,
2018-02-27 09:03:11 -06:00
CheckPermissionBeforeUpdateError : guardian . ErrGuardianOverride } ,
2018-02-26 13:14:21 -06:00
)
mock := & fakeFolderService {
2018-03-22 16:13:46 -05:00
GetFolderByUIDResult : & m . Folder {
2018-02-26 13:14:21 -06:00
Id : 1 ,
Uid : "uid" ,
Title : "Folder" ,
} ,
}
origNewFolderService := dashboards . NewFolderService
mockFolderService ( mock )
cmd := dtos . UpdateDashboardAclCommand {
Items : [ ] dtos . DashboardAclUpdateItem {
{ UserId : 1000 , Permission : m . PERMISSION_ADMIN } ,
} ,
}
updateFolderPermissionScenario ( "When calling POST on" , "/api/folders/uid/permissions" , "/api/folders/:uid/permissions" , cmd , func ( sc * scenarioContext ) {
callUpdateFolderPermissions ( sc )
So ( sc . resp . Code , ShouldEqual , 400 )
} )
Reset ( func ( ) {
guardian . New = origNewGuardian
dashboards . NewFolderService = origNewFolderService
} )
} )
2018-02-20 11:11:50 -06:00
} )
}
func callGetFolderPermissions ( sc * scenarioContext ) {
sc . handlerFunc = GetFolderPermissionList
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
}
func callUpdateFolderPermissions ( sc * scenarioContext ) {
bus . AddHandler ( "test" , func ( cmd * m . UpdateDashboardAclCommand ) error {
return nil
} )
sc . fakeReqWithParams ( "POST" , sc . url , map [ string ] string { } ) . exec ( )
}
func updateFolderPermissionScenario ( desc string , url string , routePattern string , cmd dtos . UpdateDashboardAclCommand , fn scenarioFunc ) {
Convey ( desc + " " + url , func ( ) {
defer bus . ClearBusHandlers ( )
sc := setupScenarioContext ( url )
2018-07-02 10:13:59 -05:00
sc . defaultHandler = Wrap ( func ( c * m . ReqContext ) Response {
2018-02-20 11:11:50 -06:00
sc . context = c
sc . context . OrgId = TestOrgID
sc . context . UserId = TestUserID
return UpdateFolderPermissions ( c , cmd )
} )
sc . m . Post ( routePattern , sc . defaultHandler )
fn ( sc )
} )
}