@ -9,12 +9,13 @@ import (
"os"
"testing"
"github.com/grafana/grafana/pkg/services/publicdashboards"
"github.com/grafana/grafana/pkg/services/publicdashboards/api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/services/publicdashboards"
"github.com/grafana/grafana/pkg/services/publicdashboards/api"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/api/routing"
@ -165,7 +166,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
{ Role : & editorRole , Permission : dashboards . PERMISSION_EDIT } ,
}
dashboardService . On ( "GetDashboardACLInfoList" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardACLInfoListQuery" ) ) . Return ( qResult , nil )
guardian . InitLegacyGuardian ( mockSQLStore , dashboardService , teamService )
guardian . InitLegacyGuardian ( hs . Cfg , mockSQLStore , dashboardService , teamService )
}
// This tests two scenarios:
@ -274,12 +275,8 @@ func TestDashboardAPIEndpoint(t *testing.T) {
}
setUp := func ( ) {
origCanEdit := setting . ViewersCanEdit
t . Cleanup ( func ( ) {
setting . ViewersCanEdit = origCanEdit
} )
setting . ViewersCanEdit = false
guardian . InitLegacyGuardian ( mockSQLStore , dashboardService , teamService )
cfg . ViewersCanEdit = false
guardian . InitLegacyGuardian ( cfg , mockSQLStore , dashboardService , teamService )
}
// This tests six scenarios:
@ -370,18 +367,14 @@ func TestDashboardAPIEndpoint(t *testing.T) {
role := org . RoleViewer
setUpInner := func ( ) {
origCanEdit := setting . ViewersCanEdit
t . Cleanup ( func ( ) {
setting . ViewersCanEdit = origCanEdit
} )
setting . ViewersCanEdit = false
cfg . ViewersCanEdit = false
dashboardService := dashboards . NewFakeDashboardService ( t )
qResult := [ ] * dashboards . DashboardACLInfoDTO {
{ OrgID : 1 , DashboardID : 2 , UserID : 1 , Permission : dashboards . PERMISSION_EDIT } ,
}
dashboardService . On ( "GetDashboardACLInfoList" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardACLInfoListQuery" ) ) . Return ( qResult , nil )
guardian . InitLegacyGuardian ( mockSQLStore , dashboardService , teamService )
guardian . InitLegacyGuardian ( cfg , mockSQLStore , dashboardService , teamService )
}
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" ,
@ -414,24 +407,20 @@ func TestDashboardAPIEndpoint(t *testing.T) {
role := org . RoleViewer
setUpInner := func ( ) {
origCanEdit := setting . ViewersCanEdit
t . Cleanup ( func ( ) {
setting . ViewersCanEdit = origCanEdit
} )
setting . ViewersCanEdit = true
cfg . ViewersCanEdit = true
dashboardService := dashboards . NewFakeDashboardService ( t )
qResult := [ ] * dashboards . DashboardACLInfoDTO {
{ OrgID : 1 , DashboardID : 2 , UserID : 1 , Permission : dashboards . PERMISSION_VIEW } ,
}
dashboardService . On ( "GetDashboardACLInfoList" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardACLInfoListQuery" ) ) . Return ( qResult , nil )
guardian . InitLegacyGuardian ( mockSQLStore , dashboardService , teamService )
guardian . InitLegacyGuardian ( cfg , mockSQLStore , dashboardService , teamService )
}
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
setUpInner ( )
require . True ( t , settin g. ViewersCanEdit )
require . True ( t , cf g. ViewersCanEdit )
sc . sqlStore = mockSQLStore
dash := getDashboardShouldReturn200WithConfig ( t , sc , nil , nil , dashboardService , nil )
@ -452,18 +441,14 @@ func TestDashboardAPIEndpoint(t *testing.T) {
role := org . RoleViewer
setUpInner := func ( ) {
origCanEdit := setting . ViewersCanEdit
t . Cleanup ( func ( ) {
setting . ViewersCanEdit = origCanEdit
} )
setting . ViewersCanEdit = true
cfg . ViewersCanEdit = true
dashboardService := dashboards . NewFakeDashboardService ( t )
qResult := [ ] * dashboards . DashboardACLInfoDTO {
{ OrgID : 1 , DashboardID : 2 , UserID : 1 , Permission : dashboards . PERMISSION_ADMIN } ,
}
dashboardService . On ( "GetDashboardACLInfoList" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardACLInfoListQuery" ) ) . Return ( qResult , nil )
guardian . InitLegacyGuardian ( mockSQLStore , dashboardService , teamService )
guardian . InitLegacyGuardian ( cfg , mockSQLStore , dashboardService , teamService )
}
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
@ -509,12 +494,13 @@ func TestDashboardAPIEndpoint(t *testing.T) {
role := org . RoleEditor
setUpInner := func ( ) {
cfg := setting . NewCfg ( )
dashboardService := dashboards . NewFakeDashboardService ( t )
qResult := [ ] * dashboards . DashboardACLInfoDTO {
{ OrgID : 1 , DashboardID : 2 , UserID : 1 , Permission : dashboards . PERMISSION_VIEW } ,
}
dashboardService . On ( "GetDashboardACLInfoList" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardACLInfoListQuery" ) ) . Return ( qResult , nil )
guardian . InitLegacyGuardian ( mockSQLStore , dashboardService , teamService )
guardian . InitLegacyGuardian ( cfg , mockSQLStore , dashboardService , teamService )
}
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
@ -782,7 +768,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
dashSvc . On ( "GetDashboardACLInfoList" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardACLInfoListQuery" ) ) . Return ( nil , nil )
qResult := & dashboards . Dashboard { }
dashSvc . On ( "GetDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardQuery" ) ) . Return ( qResult , nil )
guardian . InitLegacyGuardian ( sqlmock , dashSvc , teamSvc )
guardian . InitLegacyGuardian ( setting . NewCfg ( ) , s qlmock , dashSvc , teamSvc )
}
cmd := dtos . CalculateDiffOptions {
@ -901,7 +887,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
dashboardService . On ( "GetDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardQuery" ) ) . Return ( qResult , nil )
qResult2 := [ ] * dashboards . DashboardACLInfoDTO { { OrgID : testOrgID , DashboardID : 1 , UserID : testUserID , Permission : dashboards . PERMISSION_EDIT } }
dashboardService . On ( "GetDashboardACLInfoList" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardACLInfoListQuery" ) ) . Return ( qResult2 , nil )
guardian . InitLegacyGuardian ( mockSQLStore , dashboardService , teamService )
guardian . InitLegacyGuardian ( setting . NewCfg ( ) , mockSQLStore , dashboardService , teamService )
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/dash" , "/api/dashboards/uid/:uid" , org . RoleEditor , func ( sc * scenarioContext ) {
fakeProvisioningService := provisioning . NewProvisioningServiceMock ( context . Background ( ) )