2022-03-16 22:07:04 +08:00
package permissions
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
2022-08-10 17:56:48 +08:00
"github.com/grafana/grafana/pkg/services/user"
2022-03-16 22:07:04 +08:00
"github.com/grafana/grafana/pkg/util"
)
func TestNewAccessControlDashboardPermissionFilter ( t * testing . T ) {
randomType := "random_" + util . GenerateShortUID ( )
testCases := [ ] struct {
permission models . PermissionType
queryType string
expectedDashboardActions [ ] string
expectedFolderActions [ ] string
} {
{
queryType : searchstore . TypeAlertFolder ,
permission : models . PERMISSION_ADMIN ,
expectedDashboardActions : nil ,
expectedFolderActions : [ ] string {
dashboards . ActionFoldersRead ,
accesscontrol . ActionAlertingRuleRead ,
2022-04-02 07:33:26 +08:00
accesscontrol . ActionAlertingRuleCreate ,
2022-03-16 22:07:04 +08:00
} ,
} ,
{
queryType : searchstore . TypeAlertFolder ,
permission : models . PERMISSION_EDIT ,
expectedDashboardActions : nil ,
expectedFolderActions : [ ] string {
dashboards . ActionFoldersRead ,
accesscontrol . ActionAlertingRuleRead ,
2022-04-02 07:33:26 +08:00
accesscontrol . ActionAlertingRuleCreate ,
2022-03-16 22:07:04 +08:00
} ,
} ,
{
queryType : searchstore . TypeAlertFolder ,
permission : models . PERMISSION_VIEW ,
expectedDashboardActions : nil ,
expectedFolderActions : [ ] string {
dashboards . ActionFoldersRead ,
accesscontrol . ActionAlertingRuleRead ,
} ,
} ,
{
queryType : randomType ,
permission : models . PERMISSION_ADMIN ,
expectedDashboardActions : [ ] string {
2022-05-04 22:12:09 +08:00
dashboards . ActionDashboardsRead ,
dashboards . ActionDashboardsWrite ,
2022-03-16 22:07:04 +08:00
} ,
expectedFolderActions : [ ] string {
dashboards . ActionFoldersRead ,
2022-05-04 22:12:09 +08:00
dashboards . ActionDashboardsCreate ,
2022-03-16 22:07:04 +08:00
} ,
} ,
{
queryType : randomType ,
permission : models . PERMISSION_EDIT ,
expectedDashboardActions : [ ] string {
2022-05-04 22:12:09 +08:00
dashboards . ActionDashboardsRead ,
dashboards . ActionDashboardsWrite ,
2022-03-16 22:07:04 +08:00
} ,
expectedFolderActions : [ ] string {
dashboards . ActionFoldersRead ,
2022-05-04 22:12:09 +08:00
dashboards . ActionDashboardsCreate ,
2022-03-16 22:07:04 +08:00
} ,
} ,
{
queryType : randomType ,
permission : models . PERMISSION_VIEW ,
expectedDashboardActions : [ ] string {
2022-05-04 22:12:09 +08:00
dashboards . ActionDashboardsRead ,
2022-03-16 22:07:04 +08:00
} ,
expectedFolderActions : [ ] string {
dashboards . ActionFoldersRead ,
} ,
} ,
}
for _ , testCase := range testCases {
t . Run ( fmt . Sprintf ( "query type %s, permissions %s" , testCase . queryType , testCase . permission ) , func ( t * testing . T ) {
2022-08-10 17:56:48 +08:00
filters := NewAccessControlDashboardPermissionFilter ( & user . SignedInUser { } , testCase . permission , testCase . queryType )
2022-03-16 22:07:04 +08:00
require . Equal ( t , testCase . expectedDashboardActions , filters . dashboardActions )
require . Equal ( t , testCase . expectedFolderActions , filters . folderActions )
} )
}
}
func TestAccessControlDashboardPermissionFilter_Where ( t * testing . T ) {
testCases := [ ] struct {
title string
dashboardActions [ ] string
folderActions [ ] string
expectedResult string
} {
{
title : "folder and dashboard actions are defined" ,
dashboardActions : [ ] string { "test" } ,
folderActions : [ ] string { "test" } ,
2022-03-30 21:14:26 +08:00
expectedResult : "((( 1 = 0 OR dashboard.folder_id IN(SELECT id FROM dashboard WHERE 1 = 0)) AND NOT dashboard.is_folder) OR ( 1 = 0 AND dashboard.is_folder))" ,
2022-03-16 22:07:04 +08:00
} ,
{
title : "folder actions are defined but not dashboard actions" ,
dashboardActions : nil ,
folderActions : [ ] string { "test" } ,
expectedResult : "(( 1 = 0 AND dashboard.is_folder))" ,
} ,
{
title : "dashboard actions are defined but not folder actions" ,
dashboardActions : [ ] string { "test" } ,
folderActions : nil ,
2022-03-30 21:14:26 +08:00
expectedResult : "((( 1 = 0 OR dashboard.folder_id IN(SELECT id FROM dashboard WHERE 1 = 0)) AND NOT dashboard.is_folder))" ,
2022-03-16 22:07:04 +08:00
} ,
{
title : "dashboard actions are defined but not folder actions" ,
dashboardActions : nil ,
folderActions : nil ,
expectedResult : "()" ,
} ,
}
for _ , testCase := range testCases {
t . Run ( testCase . title , func ( t * testing . T ) {
filter := AccessControlDashboardPermissionFilter {
2022-08-10 17:56:48 +08:00
User : & user . SignedInUser { Permissions : map [ int64 ] map [ string ] [ ] string { } } ,
2022-03-16 22:07:04 +08:00
dashboardActions : testCase . dashboardActions ,
folderActions : testCase . folderActions ,
}
query , args := filter . Where ( )
assert . Empty ( t , args )
assert . Equal ( t , testCase . expectedResult , query )
} )
}
}