@ -23,7 +23,7 @@ import (
"github.com/grafana/grafana/pkg/services/alerting"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/dashboards/database"
service "github.com/grafana/grafana/pkg/services/dashboards/manager "
service "github.com/grafana/grafana/pkg/services/dashboards/service "
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/guardian"
"github.com/grafana/grafana/pkg/services/libraryelements"
@ -101,7 +101,7 @@ func newTestLive(t *testing.T, store *sqlstore.SQLStore) *live.GrafanaLive {
nil ,
& usagestats . UsageStatsMock { T : t } ,
nil ,
features , accesscontrolmock . New ( ) )
features , accesscontrolmock . New ( ) , & dashboards . FakeDashboardService { } )
require . NoError ( t , err )
return gLive
}
@ -118,18 +118,22 @@ func TestDashboardAPIEndpoint(t *testing.T) {
fakeDash . Id = 1
fakeDash . FolderId = 1
fakeDash . HasAcl = false
dashboardService := & dashboards . FakeDashboardService { }
dashboardService . GetDashboardFn = func ( ctx context . Context , cmd * models . GetDashboardQuery ) error {
cmd . Result = fakeDash
return nil
}
mockSQLStore := mockstore . NewSQLStoreMock ( )
mockSQLStore . ExpectedDashboard = fakeDash
hs := & HTTPServer {
Cfg : setting . NewCfg ( ) ,
pluginStore : & fakePluginStore { } ,
SQLStore : mockSQLStore ,
AccessControl : accesscontrolmock . New ( ) ,
Features : featuremgmt . WithFeatures ( ) ,
Cfg : setting . NewCfg ( ) ,
pluginStore : & fakePluginStore { } ,
SQLStore : mockSQLStore ,
AccessControl : accesscontrolmock . New ( ) ,
Features : featuremgmt . WithFeatures ( ) ,
dashboardService : dashboardService ,
}
hs . SQLStore = mockSQLStore
setUp := func ( ) {
viewerRole := models . ROLE_VIEWER
@ -153,7 +157,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
setUp ( )
sc . sqlStore = mockSQLStore
dash := getDashboardShouldReturn200 ( t , sc )
dash := getDashboardShouldReturn200WithConfig ( t , sc , nil , nil , dashboardService )
assert . False ( t , dash . Meta . CanEdit )
assert . False ( t , dash . Meta . CanSave )
@ -185,7 +189,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
setUp ( )
sc . sqlStore = mockSQLStore
dash := getDashboardShouldReturn200 ( t , sc )
dash := getDashboardShouldReturn200WithConfig ( t , sc , nil , nil , dashboardService )
assert . True ( t , dash . Meta . CanEdit )
assert . True ( t , dash . Meta . CanSave )
@ -216,14 +220,16 @@ func TestDashboardAPIEndpoint(t *testing.T) {
fakeDash . Id = 1
fakeDash . FolderId = 1
fakeDash . HasAcl = true
dashboardService := & dashboards . FakeDashboardService { }
dashboardService . GetDashboardFn = func ( ctx context . Context , cmd * models . GetDashboardQuery ) error {
cmd . Result = fakeDash
return nil
}
mockSQLStore := mockstore . NewSQLStoreMock ( )
mockSQLStore . ExpectedDashboard = fakeDash
cfg := setting . NewCfg ( )
features := featuremgmt . WithFeatures ( )
sql := sqlstore . InitTestDB ( t )
dashboardStore := database . ProvideDashboardStore ( sql )
hs := & HTTPServer {
Cfg : cfg ,
Live : newTestLive ( t , sql ) ,
@ -231,12 +237,8 @@ func TestDashboardAPIEndpoint(t *testing.T) {
LibraryElementService : & mockLibraryElementService { } ,
SQLStore : mockSQLStore ,
AccessControl : accesscontrolmock . New ( ) ,
dashboardService : service . ProvideDashboardService (
cfg , dashboardStore , nil , features ,
accesscontrolmock . NewMockedPermissionsService ( ) , accesscontrolmock . NewMockedPermissionsService ( ) ,
) ,
dashboardService : dashboardService ,
}
hs . SQLStore = mockSQLStore
setUp := func ( ) {
origCanEdit := setting . ViewersCanEdit
@ -281,7 +283,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
setUp ( )
sc . sqlStore = mockSQLStore
hs . callDeleteDashboardByUID ( t , sc , nil )
hs . callDeleteDashboardByUID ( t , sc , & dashboards . FakeDashboardService { } )
assert . Equal ( t , 403 , sc . resp . Code )
} , mockSQLStore )
@ -319,7 +321,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" ,
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
setUp ( )
hs . callDeleteDashboardByUID ( t , sc , nil )
hs . callDeleteDashboardByUID ( t , sc , & dashboards . FakeDashboardService { } )
assert . Equal ( t , 403 , sc . resp . Code )
} , mockSQLStore )
@ -357,7 +359,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
setUpInner ( )
sc . sqlStore = mockSQLStore
dash := getDashboardShouldReturn200 ( t , sc )
dash := getDashboardShouldReturn200WithConfig ( t , sc , nil , nil , dashboardService )
assert . True ( t , dash . Meta . CanEdit )
assert . True ( t , dash . Meta . CanSave )
@ -420,7 +422,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
require . True ( t , setting . ViewersCanEdit )
sc . sqlStore = mockSQLStore
dash := getDashboardShouldReturn200 ( t , sc )
dash := getDashboardShouldReturn200WithConfig ( t , sc , nil , nil , dashboardService )
assert . True ( t , dash . Meta . CanEdit )
assert . False ( t , dash . Meta . CanSave )
@ -430,7 +432,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
setUpInner ( )
hs . callDeleteDashboardByUID ( t , sc , nil )
hs . callDeleteDashboardByUID ( t , sc , dashboardService )
assert . Equal ( t , 403 , sc . resp . Code )
} , mockSQLStore )
} )
@ -450,7 +452,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
setUpInner ( )
sc . sqlStore = mockSQLStore
dash := getDashboardShouldReturn200 ( t , sc )
dash := getDashboardShouldReturn200WithConfig ( t , sc , nil , nil , dashboardService )
assert . True ( t , dash . Meta . CanEdit )
assert . True ( t , dash . Meta . CanSave )
@ -495,7 +497,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
setUpInner ( )
sc . sqlStore = mockSQLStore
dash := getDashboardShouldReturn200 ( t , sc )
dash := getDashboardShouldReturn200WithConfig ( t , sc , nil , nil , dashboardService )
assert . False ( t , dash . Meta . CanEdit )
assert . False ( t , dash . Meta . CanSave )
@ -503,7 +505,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
setUpInner ( )
hs . callDeleteDashboardByUID ( t , sc , nil )
hs . callDeleteDashboardByUID ( t , sc , dashboardService )
assert . Equal ( t , 403 , sc . resp . Code )
} , mockSQLStore )
@ -780,13 +782,19 @@ func TestDashboardAPIEndpoint(t *testing.T) {
fakeDash . FolderId = folderID
fakeDash . HasAcl = false
saved := & models . Dashboard {
Id : 2 ,
Uid : "uid" ,
Title : "Dash" ,
Slug : "dash" ,
Version : 1 ,
}
mock := & dashboards . FakeDashboardService {
SaveDashboardResult : & models . Dashboard {
Id : 2 ,
Uid : "uid" ,
Title : "Dash" ,
Slug : "dash" ,
Version : 1 ,
SaveDashboardResult : saved ,
GetDashboardFn : func ( ctx context . Context , cmd * models . GetDashboardQuery ) error {
cmd . Result = fakeDash
return nil
} ,
}
@ -794,7 +802,6 @@ func TestDashboardAPIEndpoint(t *testing.T) {
Version : 1 ,
}
mockSQLStore := mockstore . NewSQLStoreMock ( )
mockSQLStore . ExpectedDashboard = fakeDash
mockSQLStore . ExpectedDashboardVersions = [ ] * models . DashboardVersion {
{
DashboardId : 2 ,
@ -818,13 +825,19 @@ func TestDashboardAPIEndpoint(t *testing.T) {
fakeDash . Id = 2
fakeDash . HasAcl = false
saved := & models . Dashboard {
Id : 2 ,
Uid : "uid" ,
Title : "Dash" ,
Slug : "dash" ,
Version : 1 ,
}
mock := & dashboards . FakeDashboardService {
SaveDashboardResult : & models . Dashboard {
Id : 2 ,
Uid : "uid" ,
Title : "Dash" ,
Slug : "dash" ,
Version : 1 ,
SaveDashboardResult : saved ,
GetDashboardFn : func ( ctx context . Context , cmd * models . GetDashboardQuery ) error {
cmd . Result = fakeDash
return nil
} ,
}
@ -832,7 +845,6 @@ func TestDashboardAPIEndpoint(t *testing.T) {
Version : 1 ,
}
mockSQLStore := mockstore . NewSQLStoreMock ( )
mockSQLStore . ExpectedDashboard = fakeDash
mockSQLStore . ExpectedDashboardVersions = [ ] * models . DashboardVersion {
{
DashboardId : 2 ,
@ -861,7 +873,12 @@ func TestDashboardAPIEndpoint(t *testing.T) {
dataValue , err := simplejson . NewJson ( [ ] byte ( ` { "id": 1, "editable": true, "style": "dark"} ` ) )
require . NoError ( t , err )
mockSQLStore . ExpectedDashboard = & models . Dashboard { Id : 1 , Data : dataValue }
dashboardService := & dashboards . FakeDashboardService {
GetDashboardFn : func ( ctx context . Context , cmd * models . GetDashboardQuery ) error {
cmd . Result = & models . Dashboard { Id : 1 , Data : dataValue }
return nil
} ,
}
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/dash" , "/api/dashboards/uid/:uid" , models . ROLE_EDITOR , func ( sc * scenarioContext ) {
setUp ( )
@ -875,7 +892,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
dashboardStore . On ( "GetProvisionedDataByDashboardID" , mock . Anything ) . Return ( & models . DashboardProvisioning { ExternalId : "/dashboard1.json" } , nil ) . Once ( )
dash := getDashboardShouldReturn200WithConfig ( t , sc , fakeProvisioningService , dashboardStore )
dash := getDashboardShouldReturn200WithConfig ( t , sc , fakeProvisioningService , dashboardStore , dashboardService )
assert . Equal ( t , "../../../dashboard1.json" , dash . Meta . ProvisionedExternalId , mockSQLStore )
} , mockSQLStore )
@ -899,6 +916,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
dashboardProvisioningService : mockDashboardProvisioningService { } ,
SQLStore : mockSQLStore ,
AccessControl : accesscontrolmock . New ( ) ,
dashboardService : dashboardService ,
}
hs . callGetDashboard ( sc )
@ -913,7 +931,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
} )
}
func getDashboardShouldReturn200WithConfig ( t * testing . T , sc * scenarioContext , provisioningService provisioning . ProvisioningService , dashboardStore dashboards . Store ) dtos . DashboardFullWithMeta {
func getDashboardShouldReturn200WithConfig ( t * testing . T , sc * scenarioContext , provisioningService provisioning . ProvisioningService , dashboardStore dashboards . Store , dashboardService dashboards . DashboardService ) dtos . DashboardFullWithMeta {
t . Helper ( )
if provisioningService == nil {
@ -925,6 +943,10 @@ func getDashboardShouldReturn200WithConfig(t *testing.T, sc *scenarioContext, pr
dashboardStore = database . ProvideDashboardStore ( sql )
}
if dashboardService == nil {
dashboardService = & dashboards . FakeDashboardService { }
}
libraryPanelsService := mockLibraryPanelService { }
libraryElementsService := mockLibraryElementService { }
cfg := setting . NewCfg ( )
@ -941,6 +963,7 @@ func getDashboardShouldReturn200WithConfig(t *testing.T, sc *scenarioContext, pr
cfg , dashboardStore , nil , features ,
accesscontrolmock . NewMockedPermissionsService ( ) , accesscontrolmock . NewMockedPermissionsService ( ) ,
) ,
dashboardService : dashboardService ,
}
hs . callGetDashboard ( sc )
@ -954,10 +977,6 @@ func getDashboardShouldReturn200WithConfig(t *testing.T, sc *scenarioContext, pr
return dash
}
func getDashboardShouldReturn200 ( t * testing . T , sc * scenarioContext ) dtos . DashboardFullWithMeta {
return getDashboardShouldReturn200WithConfig ( t , sc , nil , nil )
}
func ( hs * HTTPServer ) callGetDashboard ( sc * scenarioContext ) {
sc . handlerFunc = hs . GetDashboard
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )