2022-02-16 07:15:44 -06:00
package service
2021-03-17 10:06:10 -05:00
import (
2021-11-03 08:10:39 -05:00
"context"
2021-03-17 10:06:10 -05:00
"testing"
2022-05-17 13:52:22 -05:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2021-03-17 10:06:10 -05:00
"github.com/grafana/grafana/pkg/components/simplejson"
2022-10-19 08:02:15 -05:00
"github.com/grafana/grafana/pkg/infra/db"
2022-02-16 07:15:44 -06:00
"github.com/grafana/grafana/pkg/models"
2022-03-10 05:58:18 -06:00
accesscontrolmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock"
"github.com/grafana/grafana/pkg/services/alerting"
2022-06-30 08:31:54 -05:00
"github.com/grafana/grafana/pkg/services/dashboards"
2022-02-16 07:15:44 -06:00
"github.com/grafana/grafana/pkg/services/dashboards/database"
2022-03-10 05:58:18 -06:00
"github.com/grafana/grafana/pkg/services/featuremgmt"
2021-03-17 10:06:10 -05:00
"github.com/grafana/grafana/pkg/services/guardian"
2022-08-10 04:56:48 -05:00
"github.com/grafana/grafana/pkg/services/org"
2022-11-14 13:08:10 -06:00
"github.com/grafana/grafana/pkg/services/quota/quotatest"
2022-09-21 07:04:01 -05:00
"github.com/grafana/grafana/pkg/services/tag/tagimpl"
2022-09-20 11:58:04 -05:00
"github.com/grafana/grafana/pkg/services/team/teamtest"
2022-08-10 04:56:48 -05:00
"github.com/grafana/grafana/pkg/services/user"
2022-03-10 05:58:18 -06:00
"github.com/grafana/grafana/pkg/setting"
2021-03-17 10:06:10 -05:00
)
const testOrgID int64 = 1
2022-05-24 04:04:03 -05:00
func TestIntegrationIntegratedDashboardService ( t * testing . T ) {
2022-06-10 10:46:21 -05:00
if testing . Short ( ) {
t . Skip ( "skipping integration test" )
}
2021-03-17 10:06:10 -05:00
t . Run ( "Given saved folders and dashboards in organization A" , func ( t * testing . T ) {
// Basic validation tests
permissionScenario ( t , "When saving a dashboard with non-existing id" , true ,
func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : testOrgID ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"id" : float64 ( 123412321 ) ,
"title" : "Expect error" ,
} ) ,
}
2022-11-14 13:08:10 -06:00
err := callSaveWithError ( t , cmd , sc . sqlStore )
2022-06-30 08:31:54 -05:00
assert . Equal ( t , dashboards . ErrDashboardNotFound , err )
2021-03-17 10:06:10 -05:00
} )
// Given other organization
t . Run ( "Given organization B" , func ( t * testing . T ) {
const otherOrgId int64 = 2
permissionScenario ( t , "When creating a dashboard with same id as dashboard in organization A" ,
true , func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : otherOrgId ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
2023-01-16 09:33:55 -06:00
"id" : sc . savedDashInFolder . ID ,
2021-03-17 10:06:10 -05:00
"title" : "Expect error" ,
} ) ,
Overwrite : false ,
}
2022-11-14 13:08:10 -06:00
err := callSaveWithError ( t , cmd , sc . sqlStore )
2022-06-30 08:31:54 -05:00
assert . Equal ( t , dashboards . ErrDashboardNotFound , err )
2021-03-17 10:06:10 -05:00
} )
permissionScenario ( t , "When creating a dashboard with same uid as dashboard in organization A, it should create a new dashboard in org B" ,
true , func ( t * testing . T , sc * permissionScenarioContext ) {
const otherOrgId int64 = 2
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : otherOrgId ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
2023-01-16 09:33:55 -06:00
"uid" : sc . savedDashInFolder . UID ,
2021-03-17 10:06:10 -05:00
"title" : "Dash with existing uid in other org" ,
} ) ,
Overwrite : false ,
}
res := callSaveWithResult ( t , cmd , sc . sqlStore )
require . NotNil ( t , res )
2023-01-16 09:33:55 -06:00
_ , err := sc . dashboardStore . GetDashboard ( context . Background ( ) , & dashboards . GetDashboardQuery {
OrgID : otherOrgId ,
UID : sc . savedDashInFolder . UID ,
2022-02-04 10:21:06 -06:00
} )
2021-03-17 10:06:10 -05:00
require . NoError ( t , err )
} )
} )
t . Run ( "Given user has no permission to save" , func ( t * testing . T ) {
const canSave = false
permissionScenario ( t , "When creating a new dashboard in the General folder" , canSave ,
func ( t * testing . T , sc * permissionScenarioContext ) {
2022-10-19 08:02:15 -05:00
sqlStore := db . InitTestDB ( t )
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : testOrgID ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : "Dash" ,
} ) ,
2023-01-16 09:33:55 -06:00
UserID : 10000 ,
2021-03-17 10:06:10 -05:00
Overwrite : true ,
}
2022-11-14 13:08:10 -06:00
err := callSaveWithError ( t , cmd , sqlStore )
2022-06-30 08:31:54 -05:00
assert . Equal ( t , dashboards . ErrDashboardUpdateAccessDenied , err )
2021-03-17 10:06:10 -05:00
2022-12-15 08:34:17 -06:00
assert . Equal ( t , "" , sc . dashboardGuardianMock . DashUID )
2023-01-16 09:33:55 -06:00
assert . Equal ( t , cmd . OrgID , sc . dashboardGuardianMock . OrgId )
assert . Equal ( t , cmd . UserID , sc . dashboardGuardianMock . User . UserID )
2021-03-17 10:06:10 -05:00
} )
permissionScenario ( t , "When creating a new dashboard in other folder, it should create dashboard guardian for other folder with correct arguments and rsult in access denied error" ,
canSave , func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : testOrgID ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : "Dash" ,
} ) ,
2023-01-16 09:33:55 -06:00
FolderID : sc . otherSavedFolder . ID ,
UserID : 10000 ,
2021-03-17 10:06:10 -05:00
Overwrite : true ,
}
2022-11-14 13:08:10 -06:00
err := callSaveWithError ( t , cmd , sc . sqlStore )
2022-06-30 08:31:54 -05:00
require . Equal ( t , dashboards . ErrDashboardUpdateAccessDenied , err )
2021-03-17 10:06:10 -05:00
2023-01-16 09:33:55 -06:00
assert . Equal ( t , sc . otherSavedFolder . ID , sc . dashboardGuardianMock . DashID )
assert . Equal ( t , cmd . OrgID , sc . dashboardGuardianMock . OrgId )
assert . Equal ( t , cmd . UserID , sc . dashboardGuardianMock . User . UserID )
2021-03-17 10:06:10 -05:00
} )
2022-05-09 05:01:03 -05:00
permissionScenario ( t , "When creating a new dashboard by existing title in folder, it should create dashboard guardian for dashboard with correct arguments and result in access denied error" ,
2021-03-17 10:06:10 -05:00
canSave , func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : testOrgID ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : sc . savedDashInFolder . Title ,
} ) ,
2023-01-16 09:33:55 -06:00
FolderID : sc . savedFolder . ID ,
UserID : 10000 ,
2021-03-17 10:06:10 -05:00
Overwrite : true ,
}
2022-11-14 13:08:10 -06:00
err := callSaveWithError ( t , cmd , sc . sqlStore )
2022-06-30 08:31:54 -05:00
require . Equal ( t , dashboards . ErrDashboardUpdateAccessDenied , err )
2021-03-17 10:06:10 -05:00
2023-01-16 09:33:55 -06:00
assert . Equal ( t , sc . savedDashInFolder . UID , sc . dashboardGuardianMock . DashUID )
assert . Equal ( t , cmd . OrgID , sc . dashboardGuardianMock . OrgId )
assert . Equal ( t , cmd . UserID , sc . dashboardGuardianMock . User . UserID )
2021-03-17 10:06:10 -05:00
} )
2022-05-09 05:01:03 -05:00
permissionScenario ( t , "When creating a new dashboard by existing UID in folder, it should create dashboard guardian for dashboard with correct arguments and result in access denied error" ,
2021-03-17 10:06:10 -05:00
canSave , func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : testOrgID ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
2023-01-16 09:33:55 -06:00
"uid" : sc . savedDashInFolder . UID ,
2021-03-17 10:06:10 -05:00
"title" : "New dash" ,
} ) ,
2023-01-16 09:33:55 -06:00
FolderID : sc . savedFolder . ID ,
UserID : 10000 ,
2021-03-17 10:06:10 -05:00
Overwrite : true ,
}
2022-11-14 13:08:10 -06:00
err := callSaveWithError ( t , cmd , sc . sqlStore )
2022-06-30 08:31:54 -05:00
require . Equal ( t , dashboards . ErrDashboardUpdateAccessDenied , err )
2021-03-17 10:06:10 -05:00
2023-01-16 09:33:55 -06:00
assert . Equal ( t , sc . savedDashInFolder . UID , sc . dashboardGuardianMock . DashUID )
assert . Equal ( t , cmd . OrgID , sc . dashboardGuardianMock . OrgId )
assert . Equal ( t , cmd . UserID , sc . dashboardGuardianMock . User . UserID )
2021-03-17 10:06:10 -05:00
} )
permissionScenario ( t , "When updating a dashboard by existing id in the General folder, it should create dashboard guardian for dashboard with correct arguments and result in access denied error" ,
canSave , func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : testOrgID ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
2023-01-16 09:33:55 -06:00
"id" : sc . savedDashInGeneralFolder . ID ,
2021-03-17 10:06:10 -05:00
"title" : "Dash" ,
} ) ,
2023-01-16 09:33:55 -06:00
FolderID : sc . savedDashInGeneralFolder . FolderID ,
UserID : 10000 ,
2021-03-17 10:06:10 -05:00
Overwrite : true ,
}
2022-11-14 13:08:10 -06:00
err := callSaveWithError ( t , cmd , sc . sqlStore )
2022-06-30 08:31:54 -05:00
assert . Equal ( t , dashboards . ErrDashboardUpdateAccessDenied , err )
2021-03-17 10:06:10 -05:00
2023-01-16 09:33:55 -06:00
assert . Equal ( t , sc . savedDashInGeneralFolder . UID , sc . dashboardGuardianMock . DashUID )
assert . Equal ( t , cmd . OrgID , sc . dashboardGuardianMock . OrgId )
assert . Equal ( t , cmd . UserID , sc . dashboardGuardianMock . User . UserID )
2021-03-17 10:06:10 -05:00
} )
permissionScenario ( t , "When updating a dashboard by existing id in other folder, it should create dashboard guardian for dashboard with correct arguments and result in access denied error" ,
canSave , func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : testOrgID ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
2023-01-16 09:33:55 -06:00
"id" : sc . savedDashInFolder . ID ,
2021-03-17 10:06:10 -05:00
"title" : "Dash" ,
} ) ,
2023-01-16 09:33:55 -06:00
FolderID : sc . savedDashInFolder . FolderID ,
UserID : 10000 ,
2021-03-17 10:06:10 -05:00
Overwrite : true ,
}
2022-11-14 13:08:10 -06:00
err := callSaveWithError ( t , cmd , sc . sqlStore )
2022-06-30 08:31:54 -05:00
require . Equal ( t , dashboards . ErrDashboardUpdateAccessDenied , err )
2021-03-17 10:06:10 -05:00
2023-01-16 09:33:55 -06:00
assert . Equal ( t , sc . savedDashInFolder . UID , sc . dashboardGuardianMock . DashUID )
assert . Equal ( t , cmd . OrgID , sc . dashboardGuardianMock . OrgId )
assert . Equal ( t , cmd . UserID , sc . dashboardGuardianMock . User . UserID )
2021-03-17 10:06:10 -05:00
} )
2022-05-09 05:01:03 -05:00
permissionScenario ( t , "When moving a dashboard by existing ID to other folder from General folder, it should create dashboard guardian for dashboard with correct arguments and result in access denied error" ,
2021-03-17 10:06:10 -05:00
canSave , func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : testOrgID ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
2023-01-16 09:33:55 -06:00
"id" : sc . savedDashInGeneralFolder . ID ,
2021-03-17 10:06:10 -05:00
"title" : "Dash" ,
} ) ,
2023-01-16 09:33:55 -06:00
FolderID : sc . otherSavedFolder . ID ,
UserID : 10000 ,
2021-03-17 10:06:10 -05:00
Overwrite : true ,
}
2022-11-14 13:08:10 -06:00
err := callSaveWithError ( t , cmd , sc . sqlStore )
2022-06-30 08:31:54 -05:00
require . Equal ( t , dashboards . ErrDashboardUpdateAccessDenied , err )
2021-03-17 10:06:10 -05:00
2023-01-16 09:33:55 -06:00
assert . Equal ( t , sc . savedDashInGeneralFolder . UID , sc . dashboardGuardianMock . DashUID )
assert . Equal ( t , cmd . OrgID , sc . dashboardGuardianMock . OrgId )
assert . Equal ( t , cmd . UserID , sc . dashboardGuardianMock . User . UserID )
2021-03-17 10:06:10 -05:00
} )
2022-05-09 05:01:03 -05:00
permissionScenario ( t , "When moving a dashboard by existing id to the General folder from other folder, it should create dashboard guardian for dashboard with correct arguments and result in access denied error" ,
2021-03-17 10:06:10 -05:00
canSave , func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : testOrgID ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
2023-01-16 09:33:55 -06:00
"id" : sc . savedDashInFolder . ID ,
2021-03-17 10:06:10 -05:00
"title" : "Dash" ,
} ) ,
2023-01-16 09:33:55 -06:00
FolderID : 0 ,
UserID : 10000 ,
2021-03-17 10:06:10 -05:00
Overwrite : true ,
}
2022-11-14 13:08:10 -06:00
err := callSaveWithError ( t , cmd , sc . sqlStore )
2022-06-30 08:31:54 -05:00
assert . Equal ( t , dashboards . ErrDashboardUpdateAccessDenied , err )
2021-03-17 10:06:10 -05:00
2023-01-16 09:33:55 -06:00
assert . Equal ( t , sc . savedDashInFolder . UID , sc . dashboardGuardianMock . DashUID )
assert . Equal ( t , cmd . OrgID , sc . dashboardGuardianMock . OrgId )
assert . Equal ( t , cmd . UserID , sc . dashboardGuardianMock . User . UserID )
2021-03-17 10:06:10 -05:00
} )
2022-05-09 05:01:03 -05:00
permissionScenario ( t , "When moving a dashboard by existing uid to other folder from General folder, it should create dashboard guardian for dashboard with correct arguments and result in access denied error" ,
2021-03-17 10:06:10 -05:00
canSave , func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : testOrgID ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
2023-01-16 09:33:55 -06:00
"uid" : sc . savedDashInGeneralFolder . UID ,
2021-03-17 10:06:10 -05:00
"title" : "Dash" ,
} ) ,
2023-01-16 09:33:55 -06:00
FolderID : sc . otherSavedFolder . ID ,
UserID : 10000 ,
2021-03-17 10:06:10 -05:00
Overwrite : true ,
}
2022-11-14 13:08:10 -06:00
err := callSaveWithError ( t , cmd , sc . sqlStore )
2022-06-30 08:31:54 -05:00
require . Equal ( t , dashboards . ErrDashboardUpdateAccessDenied , err )
2021-03-17 10:06:10 -05:00
2023-01-16 09:33:55 -06:00
assert . Equal ( t , sc . savedDashInGeneralFolder . UID , sc . dashboardGuardianMock . DashUID )
assert . Equal ( t , cmd . OrgID , sc . dashboardGuardianMock . OrgId )
assert . Equal ( t , cmd . UserID , sc . dashboardGuardianMock . User . UserID )
2021-03-17 10:06:10 -05:00
} )
2022-05-09 05:01:03 -05:00
permissionScenario ( t , "When moving a dashboard by existing UID to the General folder from other folder, it should create dashboard guardian for dashboard with correct arguments and result in access denied error" ,
2021-03-17 10:06:10 -05:00
canSave , func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : testOrgID ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
2023-01-16 09:33:55 -06:00
"uid" : sc . savedDashInFolder . UID ,
2021-03-17 10:06:10 -05:00
"title" : "Dash" ,
} ) ,
2023-01-16 09:33:55 -06:00
FolderID : 0 ,
UserID : 10000 ,
2021-03-17 10:06:10 -05:00
Overwrite : true ,
}
2022-11-14 13:08:10 -06:00
err := callSaveWithError ( t , cmd , sc . sqlStore )
2022-06-30 08:31:54 -05:00
require . Equal ( t , dashboards . ErrDashboardUpdateAccessDenied , err )
2021-03-17 10:06:10 -05:00
2023-01-16 09:33:55 -06:00
assert . Equal ( t , sc . savedDashInFolder . UID , sc . dashboardGuardianMock . DashUID )
assert . Equal ( t , cmd . OrgID , sc . dashboardGuardianMock . OrgId )
assert . Equal ( t , cmd . UserID , sc . dashboardGuardianMock . User . UserID )
2021-03-17 10:06:10 -05:00
} )
} )
t . Run ( "Given user has permission to save" , func ( t * testing . T ) {
const canSave = true
t . Run ( "and overwrite flag is set to false" , func ( t * testing . T ) {
const shouldOverwrite = false
permissionScenario ( t , "When creating a dashboard in General folder with same name as dashboard in other folder" ,
canSave , func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : testOrgID ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"id" : nil ,
"title" : sc . savedDashInFolder . Title ,
} ) ,
2023-01-16 09:33:55 -06:00
FolderID : 0 ,
2021-03-17 10:06:10 -05:00
Overwrite : shouldOverwrite ,
}
res := callSaveWithResult ( t , cmd , sc . sqlStore )
require . NotNil ( t , res )
2023-01-16 09:33:55 -06:00
_ , err := sc . dashboardStore . GetDashboard ( context . Background ( ) , & dashboards . GetDashboardQuery {
ID : res . ID ,
OrgID : cmd . OrgID ,
2022-02-04 10:21:06 -06:00
} )
2021-03-17 10:06:10 -05:00
require . NoError ( t , err )
} )
permissionScenario ( t , "When creating a dashboard in other folder with same name as dashboard in General folder" ,
canSave , func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : testOrgID ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"id" : nil ,
"title" : sc . savedDashInGeneralFolder . Title ,
} ) ,
2023-01-16 09:33:55 -06:00
FolderID : sc . savedFolder . ID ,
2021-03-17 10:06:10 -05:00
Overwrite : shouldOverwrite ,
}
res := callSaveWithResult ( t , cmd , sc . sqlStore )
require . NotNil ( t , res )
2023-01-16 09:33:55 -06:00
assert . NotEqual ( t , sc . savedDashInGeneralFolder . ID , res . ID )
2021-03-17 10:06:10 -05:00
2023-01-16 09:33:55 -06:00
_ , err := sc . dashboardStore . GetDashboard ( context . Background ( ) , & dashboards . GetDashboardQuery {
ID : res . ID ,
OrgID : cmd . OrgID ,
2022-02-04 10:21:06 -06:00
} )
2021-03-17 10:06:10 -05:00
require . NoError ( t , err )
} )
permissionScenario ( t , "When creating a folder with same name as dashboard in other folder" ,
canSave , func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : testOrgID ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"id" : nil ,
"title" : sc . savedDashInFolder . Title ,
} ) ,
IsFolder : true ,
Overwrite : shouldOverwrite ,
}
res := callSaveWithResult ( t , cmd , sc . sqlStore )
require . NotNil ( t , res )
2023-01-16 09:33:55 -06:00
assert . NotEqual ( t , sc . savedDashInGeneralFolder . ID , res . ID )
2021-03-17 10:06:10 -05:00
assert . True ( t , res . IsFolder )
2023-01-16 09:33:55 -06:00
_ , err := sc . dashboardStore . GetDashboard ( context . Background ( ) , & dashboards . GetDashboardQuery {
ID : res . ID ,
OrgID : cmd . OrgID ,
2022-02-04 10:21:06 -06:00
} )
2021-03-17 10:06:10 -05:00
require . NoError ( t , err )
} )
permissionScenario ( t , "When saving a dashboard without id and uid and unique title in folder" ,
canSave , func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : testOrgID ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : "Dash without id and uid" ,
} ) ,
Overwrite : shouldOverwrite ,
}
res := callSaveWithResult ( t , cmd , sc . sqlStore )
require . NotNil ( t , res )
2023-01-16 09:33:55 -06:00
assert . Greater ( t , res . ID , int64 ( 0 ) )
assert . NotEmpty ( t , res . UID )
_ , err := sc . dashboardStore . GetDashboard ( context . Background ( ) , & dashboards . GetDashboardQuery {
ID : res . ID ,
OrgID : cmd . OrgID ,
2022-02-04 10:21:06 -06:00
} )
2021-03-17 10:06:10 -05:00
require . NoError ( t , err )
} )
permissionScenario ( t , "When saving a dashboard when dashboard id is zero " , canSave ,
func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : testOrgID ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"id" : 0 ,
"title" : "Dash with zero id" ,
} ) ,
Overwrite : shouldOverwrite ,
}
res := callSaveWithResult ( t , cmd , sc . sqlStore )
require . NotNil ( t , res )
2023-01-16 09:33:55 -06:00
_ , err := sc . dashboardStore . GetDashboard ( context . Background ( ) , & dashboards . GetDashboardQuery {
ID : res . ID ,
OrgID : cmd . OrgID ,
2022-02-04 10:21:06 -06:00
} )
2021-03-17 10:06:10 -05:00
require . NoError ( t , err )
} )
permissionScenario ( t , "When saving a dashboard in non-existing folder" , canSave ,
func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : testOrgID ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : "Expect error" ,
} ) ,
2023-01-16 09:33:55 -06:00
FolderID : 123412321 ,
2021-03-17 10:06:10 -05:00
Overwrite : shouldOverwrite ,
}
2022-11-14 13:08:10 -06:00
err := callSaveWithError ( t , cmd , sc . sqlStore )
2022-06-30 08:31:54 -05:00
assert . Equal ( t , dashboards . ErrDashboardFolderNotFound , err )
2021-03-17 10:06:10 -05:00
} )
permissionScenario ( t , "When updating an existing dashboard by id without current version" , canSave ,
func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
2023-01-16 09:33:55 -06:00
"id" : sc . savedDashInGeneralFolder . ID ,
2021-03-17 10:06:10 -05:00
"title" : "test dash 23" ,
} ) ,
2023-01-16 09:33:55 -06:00
FolderID : sc . savedFolder . ID ,
2021-03-17 10:06:10 -05:00
Overwrite : shouldOverwrite ,
}
2022-11-14 13:08:10 -06:00
err := callSaveWithError ( t , cmd , sc . sqlStore )
2022-06-30 08:31:54 -05:00
assert . Equal ( t , dashboards . ErrDashboardVersionMismatch , err )
2021-03-17 10:06:10 -05:00
} )
permissionScenario ( t , "When updating an existing dashboard by id with current version" , canSave ,
func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
2023-01-16 09:33:55 -06:00
"id" : sc . savedDashInGeneralFolder . ID ,
2021-03-17 10:06:10 -05:00
"title" : "Updated title" ,
"version" : sc . savedDashInGeneralFolder . Version ,
} ) ,
2023-01-16 09:33:55 -06:00
FolderID : sc . savedFolder . ID ,
2021-03-17 10:06:10 -05:00
Overwrite : shouldOverwrite ,
}
res := callSaveWithResult ( t , cmd , sc . sqlStore )
require . NotNil ( t , res )
2023-01-16 09:33:55 -06:00
_ , err := sc . dashboardStore . GetDashboard ( context . Background ( ) , & dashboards . GetDashboardQuery {
ID : sc . savedDashInGeneralFolder . ID ,
OrgID : cmd . OrgID ,
2022-02-04 10:21:06 -06:00
} )
2021-03-17 10:06:10 -05:00
require . NoError ( t , err )
} )
permissionScenario ( t , "When updating an existing dashboard by uid without current version" , canSave ,
func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
2023-01-16 09:33:55 -06:00
"uid" : sc . savedDashInFolder . UID ,
2021-03-17 10:06:10 -05:00
"title" : "test dash 23" ,
} ) ,
2023-01-16 09:33:55 -06:00
FolderID : 0 ,
2021-03-17 10:06:10 -05:00
Overwrite : shouldOverwrite ,
}
2022-11-14 13:08:10 -06:00
err := callSaveWithError ( t , cmd , sc . sqlStore )
2022-06-30 08:31:54 -05:00
assert . Equal ( t , dashboards . ErrDashboardVersionMismatch , err )
2021-03-17 10:06:10 -05:00
} )
permissionScenario ( t , "When updating an existing dashboard by uid with current version" , canSave ,
func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
2023-01-16 09:33:55 -06:00
"uid" : sc . savedDashInFolder . UID ,
2021-03-17 10:06:10 -05:00
"title" : "Updated title" ,
"version" : sc . savedDashInFolder . Version ,
} ) ,
2023-01-16 09:33:55 -06:00
FolderID : 0 ,
2021-03-17 10:06:10 -05:00
Overwrite : shouldOverwrite ,
}
res := callSaveWithResult ( t , cmd , sc . sqlStore )
require . NotNil ( t , res )
2023-01-16 09:33:55 -06:00
_ , err := sc . dashboardStore . GetDashboard ( context . Background ( ) , & dashboards . GetDashboardQuery {
ID : sc . savedDashInFolder . ID ,
OrgID : cmd . OrgID ,
2022-02-04 10:21:06 -06:00
} )
2021-03-17 10:06:10 -05:00
require . NoError ( t , err )
} )
permissionScenario ( t , "When creating a dashboard with same name as dashboard in other folder" ,
canSave , func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : testOrgID ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"id" : nil ,
"title" : sc . savedDashInFolder . Title ,
} ) ,
2023-01-16 09:33:55 -06:00
FolderID : sc . savedDashInFolder . FolderID ,
2021-03-17 10:06:10 -05:00
Overwrite : shouldOverwrite ,
}
2022-11-14 13:08:10 -06:00
err := callSaveWithError ( t , cmd , sc . sqlStore )
2022-06-30 08:31:54 -05:00
assert . Equal ( t , dashboards . ErrDashboardWithSameNameInFolderExists , err )
2021-03-17 10:06:10 -05:00
} )
permissionScenario ( t , "When creating a dashboard with same name as dashboard in General folder" ,
canSave , func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : testOrgID ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"id" : nil ,
"title" : sc . savedDashInGeneralFolder . Title ,
} ) ,
2023-01-16 09:33:55 -06:00
FolderID : sc . savedDashInGeneralFolder . FolderID ,
2021-03-17 10:06:10 -05:00
Overwrite : shouldOverwrite ,
}
2022-11-14 13:08:10 -06:00
err := callSaveWithError ( t , cmd , sc . sqlStore )
2022-06-30 08:31:54 -05:00
assert . Equal ( t , dashboards . ErrDashboardWithSameNameInFolderExists , err )
2021-03-17 10:06:10 -05:00
} )
permissionScenario ( t , "When creating a folder with same name as existing folder" , canSave ,
func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : testOrgID ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"id" : nil ,
"title" : sc . savedFolder . Title ,
} ) ,
IsFolder : true ,
Overwrite : shouldOverwrite ,
}
2022-11-14 13:08:10 -06:00
err := callSaveWithError ( t , cmd , sc . sqlStore )
2022-06-30 08:31:54 -05:00
assert . Equal ( t , dashboards . ErrDashboardWithSameNameInFolderExists , err )
2021-03-17 10:06:10 -05:00
} )
} )
t . Run ( "and overwrite flag is set to true" , func ( t * testing . T ) {
const shouldOverwrite = true
permissionScenario ( t , "When updating an existing dashboard by id without current version" , canSave ,
func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
2023-01-16 09:33:55 -06:00
"id" : sc . savedDashInGeneralFolder . ID ,
2021-03-17 10:06:10 -05:00
"title" : "Updated title" ,
} ) ,
2023-01-16 09:33:55 -06:00
FolderID : sc . savedFolder . ID ,
2021-03-17 10:06:10 -05:00
Overwrite : shouldOverwrite ,
}
res := callSaveWithResult ( t , cmd , sc . sqlStore )
require . NotNil ( t , res )
2023-01-16 09:33:55 -06:00
_ , err := sc . dashboardStore . GetDashboard ( context . Background ( ) , & dashboards . GetDashboardQuery {
ID : sc . savedDashInGeneralFolder . ID ,
OrgID : cmd . OrgID ,
2022-02-04 10:21:06 -06:00
} )
2021-03-17 10:06:10 -05:00
require . NoError ( t , err )
} )
permissionScenario ( t , "When updating an existing dashboard by uid without current version" , canSave ,
func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
2023-01-16 09:33:55 -06:00
"uid" : sc . savedDashInFolder . UID ,
2021-03-17 10:06:10 -05:00
"title" : "Updated title" ,
} ) ,
2023-01-16 09:33:55 -06:00
FolderID : 0 ,
2021-03-17 10:06:10 -05:00
Overwrite : shouldOverwrite ,
}
res := callSaveWithResult ( t , cmd , sc . sqlStore )
require . NotNil ( t , res )
2023-01-16 09:33:55 -06:00
_ , err := sc . dashboardStore . GetDashboard ( context . Background ( ) , & dashboards . GetDashboardQuery {
ID : sc . savedDashInFolder . ID ,
OrgID : cmd . OrgID ,
2022-02-04 10:21:06 -06:00
} )
2021-03-17 10:06:10 -05:00
require . NoError ( t , err )
} )
permissionScenario ( t , "When updating uid for existing dashboard using id" , canSave ,
func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
2023-01-16 09:33:55 -06:00
"id" : sc . savedDashInFolder . ID ,
2021-03-17 10:06:10 -05:00
"uid" : "new-uid" ,
"title" : sc . savedDashInFolder . Title ,
} ) ,
Overwrite : shouldOverwrite ,
}
res := callSaveWithResult ( t , cmd , sc . sqlStore )
require . NotNil ( t , res )
2023-01-16 09:33:55 -06:00
assert . Equal ( t , sc . savedDashInFolder . ID , res . ID )
assert . Equal ( t , "new-uid" , res . UID )
2021-03-17 10:06:10 -05:00
2023-01-16 09:33:55 -06:00
_ , err := sc . dashboardStore . GetDashboard ( context . Background ( ) , & dashboards . GetDashboardQuery {
ID : sc . savedDashInFolder . ID ,
OrgID : cmd . OrgID ,
2022-02-04 10:21:06 -06:00
} )
2021-03-17 10:06:10 -05:00
require . NoError ( t , err )
} )
permissionScenario ( t , "When updating uid to an existing uid for existing dashboard using id" , canSave ,
func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
2023-01-16 09:33:55 -06:00
"id" : sc . savedDashInFolder . ID ,
"uid" : sc . savedDashInGeneralFolder . UID ,
2021-03-17 10:06:10 -05:00
"title" : sc . savedDashInFolder . Title ,
} ) ,
Overwrite : shouldOverwrite ,
}
2022-11-14 13:08:10 -06:00
err := callSaveWithError ( t , cmd , sc . sqlStore )
2022-06-30 08:31:54 -05:00
assert . Equal ( t , dashboards . ErrDashboardWithSameUIDExists , err )
2021-03-17 10:06:10 -05:00
} )
permissionScenario ( t , "When creating a dashboard with same name as dashboard in other folder" , canSave ,
func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : testOrgID ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"id" : nil ,
"title" : sc . savedDashInFolder . Title ,
} ) ,
2023-01-16 09:33:55 -06:00
FolderID : sc . savedDashInFolder . FolderID ,
2021-03-17 10:06:10 -05:00
Overwrite : shouldOverwrite ,
}
res := callSaveWithResult ( t , cmd , sc . sqlStore )
require . NotNil ( t , res )
2023-01-16 09:33:55 -06:00
assert . Equal ( t , sc . savedDashInFolder . ID , res . ID )
assert . Equal ( t , sc . savedDashInFolder . UID , res . UID )
2021-03-17 10:06:10 -05:00
2023-01-16 09:33:55 -06:00
_ , err := sc . dashboardStore . GetDashboard ( context . Background ( ) , & dashboards . GetDashboardQuery {
ID : res . ID ,
OrgID : cmd . OrgID ,
2022-02-04 10:21:06 -06:00
} )
2021-03-17 10:06:10 -05:00
require . NoError ( t , err )
} )
permissionScenario ( t , "When creating a dashboard with same name as dashboard in General folder" , canSave ,
func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : testOrgID ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"id" : nil ,
"title" : sc . savedDashInGeneralFolder . Title ,
} ) ,
2023-01-16 09:33:55 -06:00
FolderID : sc . savedDashInGeneralFolder . FolderID ,
2021-03-17 10:06:10 -05:00
Overwrite : shouldOverwrite ,
}
res := callSaveWithResult ( t , cmd , sc . sqlStore )
require . NotNil ( t , res )
2023-01-16 09:33:55 -06:00
assert . Equal ( t , sc . savedDashInGeneralFolder . ID , res . ID )
assert . Equal ( t , sc . savedDashInGeneralFolder . UID , res . UID )
2021-03-17 10:06:10 -05:00
2023-01-16 09:33:55 -06:00
_ , err := sc . dashboardStore . GetDashboard ( context . Background ( ) , & dashboards . GetDashboardQuery {
ID : res . ID ,
OrgID : cmd . OrgID ,
2022-02-04 10:21:06 -06:00
} )
2021-03-17 10:06:10 -05:00
require . NoError ( t , err )
} )
permissionScenario ( t , "When updating existing folder to a dashboard using id" , canSave ,
func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
2023-01-16 09:33:55 -06:00
"id" : sc . savedFolder . ID ,
2021-03-17 10:06:10 -05:00
"title" : "new title" ,
} ) ,
IsFolder : false ,
Overwrite : shouldOverwrite ,
}
2022-11-14 13:08:10 -06:00
err := callSaveWithError ( t , cmd , sc . sqlStore )
2022-06-30 08:31:54 -05:00
assert . Equal ( t , dashboards . ErrDashboardTypeMismatch , err )
2021-03-17 10:06:10 -05:00
} )
permissionScenario ( t , "When updating existing dashboard to a folder using id" , canSave ,
func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
2023-01-16 09:33:55 -06:00
"id" : sc . savedDashInFolder . ID ,
2021-03-17 10:06:10 -05:00
"title" : "new folder title" ,
} ) ,
IsFolder : true ,
Overwrite : shouldOverwrite ,
}
2022-11-14 13:08:10 -06:00
err := callSaveWithError ( t , cmd , sc . sqlStore )
2022-06-30 08:31:54 -05:00
assert . Equal ( t , dashboards . ErrDashboardTypeMismatch , err )
2021-03-17 10:06:10 -05:00
} )
permissionScenario ( t , "When updating existing folder to a dashboard using uid" , canSave ,
func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
2023-01-16 09:33:55 -06:00
"uid" : sc . savedFolder . UID ,
2021-03-17 10:06:10 -05:00
"title" : "new title" ,
} ) ,
IsFolder : false ,
Overwrite : shouldOverwrite ,
}
2022-11-14 13:08:10 -06:00
err := callSaveWithError ( t , cmd , sc . sqlStore )
2022-06-30 08:31:54 -05:00
assert . Equal ( t , dashboards . ErrDashboardTypeMismatch , err )
2021-03-17 10:06:10 -05:00
} )
permissionScenario ( t , "When updating existing dashboard to a folder using uid" , canSave ,
func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
2023-01-16 09:33:55 -06:00
"uid" : sc . savedDashInFolder . UID ,
2021-03-17 10:06:10 -05:00
"title" : "new folder title" ,
} ) ,
IsFolder : true ,
Overwrite : shouldOverwrite ,
}
2022-11-14 13:08:10 -06:00
err := callSaveWithError ( t , cmd , sc . sqlStore )
2022-06-30 08:31:54 -05:00
assert . Equal ( t , dashboards . ErrDashboardTypeMismatch , err )
2021-03-17 10:06:10 -05:00
} )
permissionScenario ( t , "When updating existing folder to a dashboard using title" , canSave ,
func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : sc . savedFolder . Title ,
} ) ,
IsFolder : false ,
Overwrite : shouldOverwrite ,
}
2022-11-14 13:08:10 -06:00
err := callSaveWithError ( t , cmd , sc . sqlStore )
2022-06-30 08:31:54 -05:00
assert . Equal ( t , dashboards . ErrDashboardWithSameNameAsFolder , err )
2021-03-17 10:06:10 -05:00
} )
permissionScenario ( t , "When updating existing dashboard to a folder using title" , canSave ,
func ( t * testing . T , sc * permissionScenarioContext ) {
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
2021-03-17 10:06:10 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : sc . savedDashInGeneralFolder . Title ,
} ) ,
IsFolder : true ,
Overwrite : shouldOverwrite ,
}
2022-11-14 13:08:10 -06:00
err := callSaveWithError ( t , cmd , sc . sqlStore )
2022-06-30 08:31:54 -05:00
assert . Equal ( t , dashboards . ErrDashboardFolderWithSameNameAsDashboard , err )
2021-03-17 10:06:10 -05:00
} )
} )
} )
} )
}
type permissionScenarioContext struct {
dashboardGuardianMock * guardian . FakeDashboardGuardian
2022-10-14 14:33:06 -05:00
sqlStore db . DB
2022-06-30 08:31:54 -05:00
dashboardStore dashboards . Store
2023-01-16 09:33:55 -06:00
savedFolder * dashboards . Dashboard
savedDashInFolder * dashboards . Dashboard
otherSavedFolder * dashboards . Dashboard
savedDashInGeneralFolder * dashboards . Dashboard
2021-03-17 10:06:10 -05:00
}
type permissionScenarioFunc func ( t * testing . T , sc * permissionScenarioContext )
func permissionScenario ( t * testing . T , desc string , canSave bool , fn permissionScenarioFunc ) {
t . Helper ( )
mock := & guardian . FakeDashboardGuardian {
CanSaveValue : canSave ,
}
t . Run ( desc , func ( t * testing . T ) {
2022-08-11 08:37:31 -05:00
cfg := setting . NewCfg ( )
cfg . RBACEnabled = false
2022-10-14 14:33:06 -05:00
cfg . IsFeatureToggleEnabled = featuremgmt . WithFeatures ( ) . IsEnabled
2022-10-19 08:02:15 -05:00
sqlStore := db . InitTestDB ( t )
2022-11-14 13:08:10 -06:00
quotaService := quotatest . New ( false , nil )
dashboardStore , err := database . ProvideDashboardStore ( sqlStore , cfg , featuremgmt . WithFeatures ( ) , tagimpl . ProvideService ( sqlStore , cfg ) , quotaService )
require . NoError ( t , err )
2022-06-01 13:16:26 -05:00
service := ProvideDashboardService (
2023-01-19 10:38:07 -06:00
cfg , dashboardStore , dashboardStore , & dummyDashAlertExtractor { } ,
2022-06-01 13:16:26 -05:00
featuremgmt . WithFeatures ( ) ,
accesscontrolmock . NewMockedPermissionsService ( ) ,
accesscontrolmock . NewMockedPermissionsService ( ) ,
2022-06-07 04:02:20 -05:00
accesscontrolmock . New ( ) ,
2022-06-01 13:16:26 -05:00
)
2022-09-20 11:58:04 -05:00
guardian . InitLegacyGuardian ( sqlStore , service , & teamtest . FakeService { } )
2021-03-17 10:06:10 -05:00
savedFolder := saveTestFolder ( t , "Saved folder" , testOrgID , sqlStore )
2023-01-16 09:33:55 -06:00
savedDashInFolder := saveTestDashboard ( t , "Saved dash in folder" , testOrgID , savedFolder . ID , sqlStore )
saveTestDashboard ( t , "Other saved dash in folder" , testOrgID , savedFolder . ID , sqlStore )
2021-03-17 10:06:10 -05:00
savedDashInGeneralFolder := saveTestDashboard ( t , "Saved dashboard in general folder" , testOrgID , 0 , sqlStore )
otherSavedFolder := saveTestFolder ( t , "Other saved folder" , testOrgID , sqlStore )
require . Equal ( t , "Saved folder" , savedFolder . Title )
require . Equal ( t , "saved-folder" , savedFolder . Slug )
2023-01-16 09:33:55 -06:00
require . NotEqual ( t , int64 ( 0 ) , savedFolder . ID )
2021-03-17 10:06:10 -05:00
require . True ( t , savedFolder . IsFolder )
2023-01-16 09:33:55 -06:00
require . Equal ( t , int64 ( 0 ) , savedFolder . FolderID )
require . NotEmpty ( t , savedFolder . UID )
2021-03-17 10:06:10 -05:00
require . Equal ( t , "Saved dash in folder" , savedDashInFolder . Title )
require . Equal ( t , "saved-dash-in-folder" , savedDashInFolder . Slug )
2023-01-16 09:33:55 -06:00
require . NotEqual ( t , int64 ( 0 ) , savedDashInFolder . ID )
2021-03-17 10:06:10 -05:00
require . False ( t , savedDashInFolder . IsFolder )
2023-01-16 09:33:55 -06:00
require . Equal ( t , savedFolder . ID , savedDashInFolder . FolderID )
require . NotEmpty ( t , savedDashInFolder . UID )
2021-03-17 10:06:10 -05:00
origNewDashboardGuardian := guardian . New
t . Cleanup ( func ( ) {
guardian . New = origNewDashboardGuardian
} )
guardian . MockDashboardGuardian ( mock )
sc := & permissionScenarioContext {
dashboardGuardianMock : mock ,
sqlStore : sqlStore ,
savedDashInFolder : savedDashInFolder ,
otherSavedFolder : otherSavedFolder ,
savedDashInGeneralFolder : savedDashInGeneralFolder ,
savedFolder : savedFolder ,
2022-05-17 13:52:22 -05:00
dashboardStore : dashboardStore ,
2021-03-17 10:06:10 -05:00
}
fn ( t , sc )
} )
}
2023-01-16 09:33:55 -06:00
func callSaveWithResult ( t * testing . T , cmd dashboards . SaveDashboardCommand , sqlStore db . DB ) * dashboards . Dashboard {
2021-03-17 10:06:10 -05:00
t . Helper ( )
dto := toSaveDashboardDto ( cmd )
2022-05-05 10:31:14 -05:00
cfg := setting . NewCfg ( )
2022-08-11 08:37:31 -05:00
cfg . RBACEnabled = false
2022-05-05 10:31:14 -05:00
cfg . IsFeatureToggleEnabled = featuremgmt . WithFeatures ( ) . IsEnabled
2022-11-14 13:08:10 -06:00
quotaService := quotatest . New ( false , nil )
dashboardStore , err := database . ProvideDashboardStore ( sqlStore , cfg , featuremgmt . WithFeatures ( ) , tagimpl . ProvideService ( sqlStore , cfg ) , quotaService )
require . NoError ( t , err )
2022-03-10 05:58:18 -06:00
service := ProvideDashboardService (
2023-01-19 10:38:07 -06:00
cfg , dashboardStore , dashboardStore , & dummyDashAlertExtractor { } ,
2022-05-10 08:48:47 -05:00
featuremgmt . WithFeatures ( ) ,
accesscontrolmock . NewMockedPermissionsService ( ) ,
accesscontrolmock . NewMockedPermissionsService ( ) ,
2022-06-07 04:02:20 -05:00
accesscontrolmock . New ( ) ,
2022-03-10 05:58:18 -06:00
)
res , err := service . SaveDashboard ( context . Background ( ) , & dto , false )
2021-03-17 10:06:10 -05:00
require . NoError ( t , err )
return res
}
2023-01-16 09:33:55 -06:00
func callSaveWithError ( t * testing . T , cmd dashboards . SaveDashboardCommand , sqlStore db . DB ) error {
2021-03-17 10:06:10 -05:00
dto := toSaveDashboardDto ( cmd )
2022-05-05 10:31:14 -05:00
cfg := setting . NewCfg ( )
2022-08-11 08:37:31 -05:00
cfg . RBACEnabled = false
2022-05-05 10:31:14 -05:00
cfg . IsFeatureToggleEnabled = featuremgmt . WithFeatures ( ) . IsEnabled
2022-11-14 13:08:10 -06:00
quotaService := quotatest . New ( false , nil )
dashboardStore , err := database . ProvideDashboardStore ( sqlStore , cfg , featuremgmt . WithFeatures ( ) , tagimpl . ProvideService ( sqlStore , cfg ) , quotaService )
require . NoError ( t , err )
2022-03-10 05:58:18 -06:00
service := ProvideDashboardService (
2023-01-19 10:38:07 -06:00
cfg , dashboardStore , dashboardStore , & dummyDashAlertExtractor { } ,
2022-05-10 08:48:47 -05:00
featuremgmt . WithFeatures ( ) ,
accesscontrolmock . NewMockedPermissionsService ( ) ,
accesscontrolmock . NewMockedPermissionsService ( ) ,
2022-06-07 04:02:20 -05:00
accesscontrolmock . New ( ) ,
2022-03-10 05:58:18 -06:00
)
2022-11-14 13:08:10 -06:00
_ , err = service . SaveDashboard ( context . Background ( ) , & dto , false )
2021-03-17 10:06:10 -05:00
return err
}
2023-01-16 09:33:55 -06:00
func saveTestDashboard ( t * testing . T , title string , orgID , folderID int64 , sqlStore db . DB ) * dashboards . Dashboard {
2021-03-17 10:06:10 -05:00
t . Helper ( )
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : orgID ,
FolderID : folderID ,
2021-03-17 10:06:10 -05:00
IsFolder : false ,
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"id" : nil ,
"title" : title ,
} ) ,
}
2022-06-30 08:31:54 -05:00
dto := dashboards . SaveDashboardDTO {
2023-01-16 09:33:55 -06:00
OrgID : orgID ,
2021-03-17 10:06:10 -05:00
Dashboard : cmd . GetDashboardModel ( ) ,
2022-08-10 04:56:48 -05:00
User : & user . SignedInUser {
2022-08-11 06:28:55 -05:00
UserID : 1 ,
2022-08-10 04:56:48 -05:00
OrgRole : org . RoleAdmin ,
2021-03-17 10:06:10 -05:00
} ,
}
2022-05-05 10:31:14 -05:00
cfg := setting . NewCfg ( )
2022-08-11 08:37:31 -05:00
cfg . RBACEnabled = false
2022-05-05 10:31:14 -05:00
cfg . IsFeatureToggleEnabled = featuremgmt . WithFeatures ( ) . IsEnabled
2022-11-14 13:08:10 -06:00
quotaService := quotatest . New ( false , nil )
dashboardStore , err := database . ProvideDashboardStore ( sqlStore , cfg , featuremgmt . WithFeatures ( ) , tagimpl . ProvideService ( sqlStore , cfg ) , quotaService )
require . NoError ( t , err )
2022-03-10 05:58:18 -06:00
service := ProvideDashboardService (
2023-01-19 10:38:07 -06:00
cfg , dashboardStore , dashboardStore , & dummyDashAlertExtractor { } ,
2022-05-10 08:48:47 -05:00
featuremgmt . WithFeatures ( ) ,
2022-06-07 04:02:20 -05:00
accesscontrolmock . NewMockedPermissionsService ( ) ,
accesscontrolmock . NewMockedPermissionsService ( ) ,
accesscontrolmock . New ( ) ,
2022-03-10 05:58:18 -06:00
)
res , err := service . SaveDashboard ( context . Background ( ) , & dto , false )
2021-03-17 10:06:10 -05:00
require . NoError ( t , err )
return res
}
2023-01-16 09:33:55 -06:00
func saveTestFolder ( t * testing . T , title string , orgID int64 , sqlStore db . DB ) * dashboards . Dashboard {
2021-03-17 10:06:10 -05:00
t . Helper ( )
2023-01-16 09:33:55 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : orgID ,
FolderID : 0 ,
2021-03-17 10:06:10 -05:00
IsFolder : true ,
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"id" : nil ,
"title" : title ,
} ) ,
}
2022-06-30 08:31:54 -05:00
dto := dashboards . SaveDashboardDTO {
2023-01-16 09:33:55 -06:00
OrgID : orgID ,
2021-03-17 10:06:10 -05:00
Dashboard : cmd . GetDashboardModel ( ) ,
2022-08-10 04:56:48 -05:00
User : & user . SignedInUser {
2022-08-11 06:28:55 -05:00
UserID : 1 ,
2022-08-10 04:56:48 -05:00
OrgRole : org . RoleAdmin ,
2021-03-17 10:06:10 -05:00
} ,
}
2022-05-05 10:31:14 -05:00
cfg := setting . NewCfg ( )
2022-08-11 08:37:31 -05:00
cfg . RBACEnabled = false
2022-05-05 10:31:14 -05:00
cfg . IsFeatureToggleEnabled = featuremgmt . WithFeatures ( ) . IsEnabled
2022-11-14 13:08:10 -06:00
quotaService := quotatest . New ( false , nil )
dashboardStore , err := database . ProvideDashboardStore ( sqlStore , cfg , featuremgmt . WithFeatures ( ) , tagimpl . ProvideService ( sqlStore , cfg ) , quotaService )
require . NoError ( t , err )
2022-03-10 05:58:18 -06:00
service := ProvideDashboardService (
2023-01-19 10:38:07 -06:00
cfg , dashboardStore , dashboardStore , & dummyDashAlertExtractor { } ,
2022-05-10 08:48:47 -05:00
featuremgmt . WithFeatures ( ) ,
2022-06-07 04:02:20 -05:00
accesscontrolmock . NewMockedPermissionsService ( ) ,
accesscontrolmock . NewMockedPermissionsService ( ) ,
accesscontrolmock . New ( ) ,
2022-03-10 05:58:18 -06:00
)
res , err := service . SaveDashboard ( context . Background ( ) , & dto , false )
2021-03-17 10:06:10 -05:00
require . NoError ( t , err )
return res
}
2023-01-16 09:33:55 -06:00
func toSaveDashboardDto ( cmd dashboards . SaveDashboardCommand ) dashboards . SaveDashboardDTO {
2021-03-17 10:06:10 -05:00
dash := ( & cmd ) . GetDashboardModel ( )
2022-06-30 08:31:54 -05:00
return dashboards . SaveDashboardDTO {
2021-03-17 10:06:10 -05:00
Dashboard : dash ,
Message : cmd . Message ,
2023-01-16 09:33:55 -06:00
OrgID : cmd . OrgID ,
User : & user . SignedInUser { UserID : cmd . UserID } ,
2021-03-17 10:06:10 -05:00
Overwrite : cmd . Overwrite ,
}
}
2022-02-28 02:54:56 -06:00
type dummyDashAlertExtractor struct {
}
func ( d * dummyDashAlertExtractor ) GetAlerts ( ctx context . Context , dashAlertInfo alerting . DashAlertInfo ) ( [ ] * models . Alert , error ) {
return nil , nil
}
func ( d * dummyDashAlertExtractor ) ValidateAlerts ( ctx context . Context , dashAlertInfo alerting . DashAlertInfo ) error {
return nil
}