2022-02-16 21:15:44 +08:00
package database
2015-01-06 00:04:29 +08:00
import (
2018-06-16 03:23:57 +08:00
"context"
2021-07-22 17:27:13 +08:00
"encoding/json"
"errors"
2018-01-30 22:24:14 +08:00
"fmt"
2022-01-19 16:55:38 +08:00
"testing"
"time"
2022-03-19 05:38:32 +08:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2016-03-12 17:13:49 +08:00
"github.com/grafana/grafana/pkg/components/simplejson"
2022-10-19 21:02:15 +08:00
"github.com/grafana/grafana/pkg/infra/db"
2020-02-29 20:35:15 +08:00
"github.com/grafana/grafana/pkg/models"
2022-05-16 18:45:41 +08:00
"github.com/grafana/grafana/pkg/services/dashboards"
2022-08-10 17:56:48 +08:00
"github.com/grafana/grafana/pkg/services/org"
2022-10-20 03:47:08 +08:00
"github.com/grafana/grafana/pkg/services/publicdashboards/database"
publicDashboardModels "github.com/grafana/grafana/pkg/services/publicdashboards/models"
2022-02-16 21:15:44 +08:00
"github.com/grafana/grafana/pkg/services/sqlstore"
2020-05-06 17:42:52 +08:00
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
2022-05-19 20:32:10 +08:00
"github.com/grafana/grafana/pkg/services/star"
"github.com/grafana/grafana/pkg/services/star/starimpl"
2022-09-21 20:04:01 +08:00
"github.com/grafana/grafana/pkg/services/tag/tagimpl"
2022-06-28 20:32:25 +08:00
"github.com/grafana/grafana/pkg/services/user"
2022-10-15 03:33:06 +08:00
"github.com/grafana/grafana/pkg/setting"
2022-10-26 03:29:18 +08:00
"github.com/grafana/grafana/pkg/util"
2015-01-06 00:04:29 +08:00
)
2022-05-24 17:04:03 +08:00
func TestIntegrationDashboardDataAccess ( t * testing . T ) {
2022-06-10 23:46:21 +08:00
if testing . Short ( ) {
t . Skip ( "skipping integration test" )
}
2022-02-16 21:15:44 +08:00
var sqlStore * sqlstore . SQLStore
2022-10-19 21:02:15 +08:00
var cfg * setting . Cfg
2021-10-18 20:06:47 +08:00
var savedFolder , savedDash , savedDash2 * models . Dashboard
2022-02-16 21:15:44 +08:00
var dashboardStore * DashboardStore
2022-05-19 20:32:10 +08:00
var starService star . Service
2022-10-20 03:47:08 +08:00
var publicDashboardStore * database . PublicDashboardStoreImpl
2021-10-18 20:06:47 +08:00
setup := func ( ) {
2022-10-19 21:02:15 +08:00
sqlStore , cfg = db . InitTestDBwithCfg ( t )
starService = starimpl . ProvideService ( sqlStore , cfg )
2022-11-08 17:52:07 +08:00
dashboardStore = ProvideDashboardStore ( sqlStore , cfg , testFeatureToggles , tagimpl . ProvideService ( sqlStore , cfg ) )
2022-02-16 21:15:44 +08:00
savedFolder = insertTestDashboard ( t , dashboardStore , "1 test dash folder" , 1 , 0 , true , "prod" , "webapp" )
savedDash = insertTestDashboard ( t , dashboardStore , "test dash 23" , 1 , savedFolder . Id , false , "prod" , "webapp" )
insertTestDashboard ( t , dashboardStore , "test dash 45" , 1 , savedFolder . Id , false , "prod" )
savedDash2 = insertTestDashboard ( t , dashboardStore , "test dash 67" , 1 , 0 , false , "prod" )
2021-10-18 20:06:47 +08:00
insertTestRule ( t , sqlStore , savedFolder . OrgId , savedFolder . Uid )
2022-10-20 03:47:08 +08:00
publicDashboardStore = database . ProvideStore ( sqlStore )
2021-10-18 20:06:47 +08:00
}
t . Run ( "Should return dashboard model" , func ( t * testing . T ) {
setup ( )
require . Equal ( t , savedDash . Title , "test dash 23" )
require . Equal ( t , savedDash . Slug , "test-dash-23" )
require . NotEqual ( t , savedDash . Id , 0 )
require . False ( t , savedDash . IsFolder )
require . Positive ( t , savedDash . FolderId )
require . Positive ( t , len ( savedDash . Uid ) )
require . Equal ( t , savedFolder . Title , "1 test dash folder" )
require . Equal ( t , savedFolder . Slug , "1-test-dash-folder" )
require . NotEqual ( t , savedFolder . Id , 0 )
require . True ( t , savedFolder . IsFolder )
require . EqualValues ( t , savedFolder . FolderId , 0 )
require . Positive ( t , len ( savedFolder . Uid ) )
} )
t . Run ( "Should be able to get dashboard by id" , func ( t * testing . T ) {
setup ( )
query := models . GetDashboardQuery {
Id : savedDash . Id ,
OrgId : 1 ,
}
2022-06-07 17:02:20 +08:00
_ , err := dashboardStore . GetDashboard ( context . Background ( ) , & query )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
require . Equal ( t , query . Result . Title , "test dash 23" )
require . Equal ( t , query . Result . Slug , "test-dash-23" )
require . Equal ( t , query . Result . Id , savedDash . Id )
require . Equal ( t , query . Result . Uid , savedDash . Uid )
require . False ( t , query . Result . IsFolder )
} )
t . Run ( "Should be able to get dashboard by slug" , func ( t * testing . T ) {
setup ( )
query := models . GetDashboardQuery {
Slug : "test-dash-23" ,
OrgId : 1 ,
}
2022-06-07 17:02:20 +08:00
_ , err := dashboardStore . GetDashboard ( context . Background ( ) , & query )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
require . Equal ( t , query . Result . Title , "test dash 23" )
require . Equal ( t , query . Result . Slug , "test-dash-23" )
require . Equal ( t , query . Result . Id , savedDash . Id )
require . Equal ( t , query . Result . Uid , savedDash . Uid )
require . False ( t , query . Result . IsFolder )
} )
t . Run ( "Should be able to get dashboard by uid" , func ( t * testing . T ) {
setup ( )
query := models . GetDashboardQuery {
Uid : savedDash . Uid ,
OrgId : 1 ,
}
2022-06-07 17:02:20 +08:00
_ , err := dashboardStore . GetDashboard ( context . Background ( ) , & query )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
require . Equal ( t , query . Result . Title , "test dash 23" )
require . Equal ( t , query . Result . Slug , "test-dash-23" )
require . Equal ( t , query . Result . Id , savedDash . Id )
require . Equal ( t , query . Result . Uid , savedDash . Uid )
require . False ( t , query . Result . IsFolder )
} )
2022-05-19 22:13:02 +08:00
t . Run ( "Should be able to get a dashboard UID by ID" , func ( t * testing . T ) {
setup ( )
query := models . GetDashboardRefByIdQuery { Id : savedDash . Id }
err := dashboardStore . GetDashboardUIDById ( context . Background ( ) , & query )
require . NoError ( t , err )
require . Equal ( t , query . Result . Uid , savedDash . Uid )
} )
2021-10-18 20:06:47 +08:00
t . Run ( "Shouldn't be able to get a dashboard with just an OrgID" , func ( t * testing . T ) {
setup ( )
query := models . GetDashboardQuery {
OrgId : 1 ,
}
2022-06-07 17:02:20 +08:00
_ , err := dashboardStore . GetDashboard ( context . Background ( ) , & query )
2022-06-30 21:31:54 +08:00
require . Equal ( t , err , dashboards . ErrDashboardIdentifierNotSet )
2021-10-18 20:06:47 +08:00
} )
2022-05-19 22:59:12 +08:00
t . Run ( "Should be able to get dashboards by IDs & UIDs" , func ( t * testing . T ) {
setup ( )
query := models . GetDashboardsQuery { DashboardIds : [ ] int64 { savedDash . Id , savedDash2 . Id } }
err := dashboardStore . GetDashboards ( context . Background ( ) , & query )
require . NoError ( t , err )
assert . Equal ( t , len ( query . Result ) , 2 )
query = models . GetDashboardsQuery { DashboardUIds : [ ] string { savedDash . Uid , savedDash2 . Uid } }
err = dashboardStore . GetDashboards ( context . Background ( ) , & query )
require . NoError ( t , err )
assert . Equal ( t , len ( query . Result ) , 2 )
} )
2021-10-18 20:06:47 +08:00
t . Run ( "Should be able to delete dashboard" , func ( t * testing . T ) {
setup ( )
2022-02-16 21:15:44 +08:00
dash := insertTestDashboard ( t , dashboardStore , "delete me" , 1 , 0 , false , "delete this" )
2015-01-06 00:04:29 +08:00
2022-03-22 21:36:50 +08:00
err := dashboardStore . DeleteDashboard ( context . Background ( ) , & models . DeleteDashboardCommand {
2021-10-18 20:06:47 +08:00
Id : dash . Id ,
OrgId : 1 ,
2015-01-07 19:37:24 +08:00
} )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
} )
2017-06-17 08:33:53 +08:00
2021-10-18 20:06:47 +08:00
t . Run ( "Should be able to create dashboard" , func ( t * testing . T ) {
setup ( )
cmd := models . SaveDashboardCommand {
OrgId : 1 ,
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : "folderId" ,
"tags" : [ ] interface { } { } ,
} ) ,
UserId : 100 ,
}
2022-09-21 20:04:01 +08:00
dashboard , err := dashboardStore . SaveDashboard ( context . Background ( ) , cmd )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
require . EqualValues ( t , dashboard . CreatedBy , 100 )
require . False ( t , dashboard . Created . IsZero ( ) )
require . EqualValues ( t , dashboard . UpdatedBy , 100 )
require . False ( t , dashboard . Updated . IsZero ( ) )
} )
2022-02-16 21:15:44 +08:00
2021-10-18 20:06:47 +08:00
t . Run ( "Should be able to update dashboard by id and remove folderId" , func ( t * testing . T ) {
setup ( )
cmd := models . SaveDashboardCommand {
OrgId : 1 ,
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"id" : savedDash . Id ,
"title" : "folderId" ,
"tags" : [ ] interface { } { } ,
} ) ,
Overwrite : true ,
FolderId : 2 ,
UserId : 100 ,
}
2022-09-21 20:04:01 +08:00
dash , err := dashboardStore . SaveDashboard ( context . Background ( ) , cmd )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
require . EqualValues ( t , dash . FolderId , 2 )
cmd = models . SaveDashboardCommand {
OrgId : 1 ,
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"id" : savedDash . Id ,
"title" : "folderId" ,
"tags" : [ ] interface { } { } ,
} ) ,
FolderId : 0 ,
Overwrite : true ,
UserId : 100 ,
}
2022-09-21 20:04:01 +08:00
_ , err = dashboardStore . SaveDashboard ( context . Background ( ) , cmd )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
query := models . GetDashboardQuery {
Id : savedDash . Id ,
OrgId : 1 ,
}
2022-06-07 17:02:20 +08:00
_ , err = dashboardStore . GetDashboard ( context . Background ( ) , & query )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
require . Equal ( t , query . Result . FolderId , int64 ( 0 ) )
require . Equal ( t , query . Result . CreatedBy , savedDash . CreatedBy )
require . WithinDuration ( t , query . Result . Created , savedDash . Created , 3 * time . Second )
require . Equal ( t , query . Result . UpdatedBy , int64 ( 100 ) )
require . False ( t , query . Result . Updated . IsZero ( ) )
} )
t . Run ( "Should be able to delete empty folder" , func ( t * testing . T ) {
setup ( )
2022-02-16 21:15:44 +08:00
emptyFolder := insertTestDashboard ( t , dashboardStore , "2 test dash folder" , 1 , 0 , true , "prod" , "webapp" )
2021-10-18 20:06:47 +08:00
deleteCmd := & models . DeleteDashboardCommand { Id : emptyFolder . Id }
2022-03-22 21:36:50 +08:00
err := dashboardStore . DeleteDashboard ( context . Background ( ) , deleteCmd )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
} )
t . Run ( "Should be not able to delete a dashboard if force delete rules is disabled" , func ( t * testing . T ) {
setup ( )
deleteCmd := & models . DeleteDashboardCommand { Id : savedFolder . Id , ForceDeleteFolderRules : false }
2022-03-22 21:36:50 +08:00
err := dashboardStore . DeleteDashboard ( context . Background ( ) , deleteCmd )
2022-06-30 21:31:54 +08:00
require . True ( t , errors . Is ( err , dashboards . ErrFolderContainsAlertRules ) )
2021-10-18 20:06:47 +08:00
} )
2022-10-20 03:47:08 +08:00
t . Run ( "Should be able to delete dashboard and related public dashboard" , func ( t * testing . T ) {
setup ( )
2022-10-26 03:29:18 +08:00
uid := util . GenerateShortUID ( )
2022-10-28 09:08:11 +08:00
cmd := publicDashboardModels . SavePublicDashboardCommand {
2022-10-20 03:47:08 +08:00
PublicDashboard : publicDashboardModels . PublicDashboard {
Uid : uid ,
DashboardUid : savedDash . Uid ,
OrgId : savedDash . OrgId ,
IsEnabled : true ,
TimeSettings : & publicDashboardModels . TimeSettings { } ,
CreatedBy : 1 ,
CreatedAt : time . Now ( ) ,
AccessToken : "an-access-token" ,
} ,
}
2022-11-04 03:30:12 +08:00
_ , err := publicDashboardStore . Create ( context . Background ( ) , cmd )
2022-10-20 03:47:08 +08:00
require . NoError ( t , err )
2022-10-26 23:53:33 +08:00
pubdashConfig , _ := publicDashboardStore . FindByAccessToken ( context . Background ( ) , "an-access-token" )
2022-10-20 03:47:08 +08:00
require . NotNil ( t , pubdashConfig )
deleteCmd := & models . DeleteDashboardCommand { Id : savedDash . Id , OrgId : savedDash . OrgId }
err = dashboardStore . DeleteDashboard ( context . Background ( ) , deleteCmd )
require . NoError ( t , err )
query := models . GetDashboardQuery { Uid : savedDash . Uid , OrgId : savedDash . OrgId }
dash , getErr := dashboardStore . GetDashboard ( context . Background ( ) , & query )
require . Equal ( t , getErr , dashboards . ErrDashboardNotFound )
assert . Nil ( t , dash )
2022-10-26 23:53:33 +08:00
pubdashConfig , err = publicDashboardStore . FindByAccessToken ( context . Background ( ) , "an-access-token" )
require . Nil ( t , err )
2022-10-20 03:47:08 +08:00
require . Nil ( t , pubdashConfig )
} )
t . Run ( "Should be able to delete a dashboard folder, with its dashboard and related public dashboard" , func ( t * testing . T ) {
setup ( )
2022-10-26 03:29:18 +08:00
uid := util . GenerateShortUID ( )
2022-10-28 09:08:11 +08:00
cmd := publicDashboardModels . SavePublicDashboardCommand {
2022-10-20 03:47:08 +08:00
PublicDashboard : publicDashboardModels . PublicDashboard {
Uid : uid ,
DashboardUid : savedDash . Uid ,
OrgId : savedDash . OrgId ,
IsEnabled : true ,
TimeSettings : & publicDashboardModels . TimeSettings { } ,
CreatedBy : 1 ,
CreatedAt : time . Now ( ) ,
AccessToken : "an-access-token" ,
} ,
}
2022-11-04 03:30:12 +08:00
_ , err := publicDashboardStore . Create ( context . Background ( ) , cmd )
2022-10-20 03:47:08 +08:00
require . NoError ( t , err )
2022-10-26 23:53:33 +08:00
pubdashConfig , _ := publicDashboardStore . FindByAccessToken ( context . Background ( ) , "an-access-token" )
2022-10-20 03:47:08 +08:00
require . NotNil ( t , pubdashConfig )
deleteCmd := & models . DeleteDashboardCommand { Id : savedFolder . Id , ForceDeleteFolderRules : true }
err = dashboardStore . DeleteDashboard ( context . Background ( ) , deleteCmd )
require . NoError ( t , err )
query := models . GetDashboardsQuery {
DashboardIds : [ ] int64 { savedFolder . Id , savedDash . Id } ,
}
err = dashboardStore . GetDashboards ( context . Background ( ) , & query )
require . NoError ( t , err )
require . Equal ( t , len ( query . Result ) , 0 )
2022-10-26 23:53:33 +08:00
pubdashConfig , err = publicDashboardStore . FindByAccessToken ( context . Background ( ) , "an-access-token" )
require . Nil ( t , err )
2022-10-20 03:47:08 +08:00
require . Nil ( t , pubdashConfig )
} )
2021-10-18 20:06:47 +08:00
t . Run ( "Should be able to delete a dashboard folder and its children if force delete rules is enabled" , func ( t * testing . T ) {
setup ( )
deleteCmd := & models . DeleteDashboardCommand { Id : savedFolder . Id , ForceDeleteFolderRules : true }
2022-03-22 21:36:50 +08:00
err := dashboardStore . DeleteDashboard ( context . Background ( ) , deleteCmd )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
2022-03-21 23:54:30 +08:00
query := models . FindPersistedDashboardsQuery {
2021-10-18 20:06:47 +08:00
OrgId : 1 ,
FolderIds : [ ] int64 { savedFolder . Id } ,
2022-08-10 17:56:48 +08:00
SignedInUser : & user . SignedInUser { } ,
2021-10-18 20:06:47 +08:00
}
2017-11-28 00:08:39 +08:00
2022-05-24 21:24:55 +08:00
res , err := dashboardStore . FindDashboards ( context . Background ( ) , & query )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
2022-05-24 21:24:55 +08:00
require . Equal ( t , len ( res ) , 0 )
2021-10-18 20:06:47 +08:00
2022-10-19 21:02:15 +08:00
err = sqlStore . WithDbSession ( context . Background ( ) , func ( sess * db . Session ) error {
2021-10-18 20:06:47 +08:00
var existingRuleID int64
exists , err := sess . Table ( "alert_rule" ) . Where ( "namespace_uid = (SELECT uid FROM dashboard WHERE id = ?)" , savedFolder . Id ) . Cols ( "id" ) . Get ( & existingRuleID )
require . NoError ( t , err )
require . False ( t , exists )
var existingRuleVersionID int64
exists , err = sess . Table ( "alert_rule_version" ) . Where ( "rule_namespace_uid = (SELECT uid FROM dashboard WHERE id = ?)" , savedFolder . Id ) . Cols ( "id" ) . Get ( & existingRuleVersionID )
require . NoError ( t , err )
require . False ( t , exists )
return nil
2017-11-28 00:08:39 +08:00
} )
2022-06-02 02:55:22 +08:00
require . NoError ( t , err )
2015-01-07 19:37:24 +08:00
} )
2021-10-18 20:06:47 +08:00
t . Run ( "Should return error if no dashboard is found for update when dashboard id is greater than zero" , func ( t * testing . T ) {
cmd := models . SaveDashboardCommand {
OrgId : 1 ,
Overwrite : true ,
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"id" : float64 ( 123412321 ) ,
"title" : "Expect error" ,
"tags" : [ ] interface { } { } ,
} ) ,
}
2022-09-21 20:04:01 +08:00
_ , err := dashboardStore . SaveDashboard ( context . Background ( ) , cmd )
2022-06-30 21:31:54 +08:00
require . Equal ( t , err , dashboards . ErrDashboardNotFound )
2021-10-18 20:06:47 +08:00
} )
t . Run ( "Should not return error if no dashboard is found for update when dashboard id is zero" , func ( t * testing . T ) {
cmd := models . SaveDashboardCommand {
OrgId : 1 ,
Overwrite : true ,
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"id" : 0 ,
"title" : "New dash" ,
"tags" : [ ] interface { } { } ,
} ) ,
}
2022-09-21 20:04:01 +08:00
_ , err := dashboardStore . SaveDashboard ( context . Background ( ) , cmd )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
} )
t . Run ( "Should be able to get dashboard tags" , func ( t * testing . T ) {
setup ( )
query := models . GetDashboardTagsQuery { OrgId : 1 }
2022-06-02 22:00:47 +08:00
err := dashboardStore . GetDashboardTags ( context . Background ( ) , & query )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
require . Equal ( t , len ( query . Result ) , 2 )
} )
2022-05-24 21:24:55 +08:00
t . Run ( "Should be able to find dashboard folder" , func ( t * testing . T ) {
2021-10-18 20:06:47 +08:00
setup ( )
2022-03-21 23:54:30 +08:00
query := models . FindPersistedDashboardsQuery {
2022-05-16 18:45:41 +08:00
Title : "1 test dash folder" ,
OrgId : 1 ,
2022-08-10 17:56:48 +08:00
SignedInUser : & user . SignedInUser {
2022-08-11 19:28:55 +08:00
OrgID : 1 ,
2022-08-10 17:56:48 +08:00
OrgRole : org . RoleEditor ,
2022-05-16 18:45:41 +08:00
Permissions : map [ int64 ] map [ string ] [ ] string {
1 : { dashboards . ActionFoldersRead : [ ] string { dashboards . ScopeFoldersAll } } ,
} ,
} ,
2021-10-18 20:06:47 +08:00
}
2022-05-24 21:24:55 +08:00
err := testSearchDashboards ( dashboardStore , & query )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
require . Equal ( t , len ( query . Result ) , 1 )
hit := query . Result [ 0 ]
2022-03-21 23:54:30 +08:00
require . Equal ( t , hit . Type , models . DashHitFolder )
2021-10-18 20:06:47 +08:00
require . Equal ( t , hit . URL , fmt . Sprintf ( "/dashboards/f/%s/%s" , savedFolder . Uid , savedFolder . Slug ) )
require . Equal ( t , hit . FolderTitle , "" )
} )
2022-05-24 21:24:55 +08:00
t . Run ( "Should be able to limit find results" , func ( t * testing . T ) {
2021-10-18 20:06:47 +08:00
setup ( )
2022-03-21 23:54:30 +08:00
query := models . FindPersistedDashboardsQuery {
2022-05-16 18:45:41 +08:00
OrgId : 1 ,
Limit : 1 ,
2022-08-10 17:56:48 +08:00
SignedInUser : & user . SignedInUser {
2022-08-11 19:28:55 +08:00
OrgID : 1 ,
2022-08-10 17:56:48 +08:00
OrgRole : org . RoleEditor ,
2022-05-16 18:45:41 +08:00
Permissions : map [ int64 ] map [ string ] [ ] string {
1 : { dashboards . ActionFoldersRead : [ ] string { dashboards . ScopeFoldersAll } } ,
} ,
} ,
2021-10-18 20:06:47 +08:00
}
2022-05-24 21:24:55 +08:00
err := testSearchDashboards ( dashboardStore , & query )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
require . Equal ( t , len ( query . Result ) , 1 )
require . EqualValues ( t , query . Result [ 0 ] . Title , "1 test dash folder" )
} )
2022-05-24 21:24:55 +08:00
t . Run ( "Should be able to find results beyond limit using paging" , func ( t * testing . T ) {
2021-10-18 20:06:47 +08:00
setup ( )
2022-03-21 23:54:30 +08:00
query := models . FindPersistedDashboardsQuery {
2022-05-16 18:45:41 +08:00
OrgId : 1 ,
Limit : 1 ,
Page : 2 ,
2022-08-10 17:56:48 +08:00
SignedInUser : & user . SignedInUser {
2022-08-11 19:28:55 +08:00
OrgID : 1 ,
2022-08-10 17:56:48 +08:00
OrgRole : org . RoleEditor ,
2022-05-16 18:45:41 +08:00
Permissions : map [ int64 ] map [ string ] [ ] string {
1 : {
dashboards . ActionDashboardsRead : [ ] string { dashboards . ScopeDashboardsAll } ,
dashboards . ActionFoldersRead : [ ] string { dashboards . ScopeFoldersAll } ,
} ,
} ,
} ,
2021-10-18 20:06:47 +08:00
}
2022-05-24 21:24:55 +08:00
err := testSearchDashboards ( dashboardStore , & query )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
require . Equal ( t , len ( query . Result ) , 1 )
require . EqualValues ( t , query . Result [ 0 ] . Title , "test dash 23" )
} )
t . Run ( "Should be able to filter by tag and type" , func ( t * testing . T ) {
setup ( )
2022-03-21 23:54:30 +08:00
query := models . FindPersistedDashboardsQuery {
2022-05-16 18:45:41 +08:00
OrgId : 1 ,
Type : "dash-db" ,
Tags : [ ] string { "prod" } ,
2022-08-10 17:56:48 +08:00
SignedInUser : & user . SignedInUser {
2022-08-11 19:28:55 +08:00
OrgID : 1 ,
2022-08-10 17:56:48 +08:00
OrgRole : org . RoleEditor ,
2022-05-16 18:45:41 +08:00
Permissions : map [ int64 ] map [ string ] [ ] string {
1 : { dashboards . ActionDashboardsRead : [ ] string { dashboards . ScopeDashboardsAll } } ,
} ,
} ,
2021-10-18 20:06:47 +08:00
}
2022-05-24 21:24:55 +08:00
err := testSearchDashboards ( dashboardStore , & query )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
require . Equal ( t , len ( query . Result ) , 3 )
require . Equal ( t , query . Result [ 0 ] . Title , "test dash 23" )
} )
2022-05-24 21:24:55 +08:00
t . Run ( "Should be able to find a dashboard folder's children" , func ( t * testing . T ) {
2021-10-18 20:06:47 +08:00
setup ( )
2022-03-21 23:54:30 +08:00
query := models . FindPersistedDashboardsQuery {
2022-05-16 18:45:41 +08:00
OrgId : 1 ,
FolderIds : [ ] int64 { savedFolder . Id } ,
2022-08-10 17:56:48 +08:00
SignedInUser : & user . SignedInUser {
2022-08-11 19:28:55 +08:00
OrgID : 1 ,
2022-08-10 17:56:48 +08:00
OrgRole : org . RoleEditor ,
2022-05-16 18:45:41 +08:00
Permissions : map [ int64 ] map [ string ] [ ] string {
1 : { dashboards . ActionDashboardsRead : [ ] string { dashboards . ScopeDashboardsAll } } ,
} ,
} ,
2021-10-18 20:06:47 +08:00
}
2022-05-24 21:24:55 +08:00
err := testSearchDashboards ( dashboardStore , & query )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
require . Equal ( t , len ( query . Result ) , 2 )
hit := query . Result [ 0 ]
require . Equal ( t , hit . ID , savedDash . Id )
require . Equal ( t , hit . URL , fmt . Sprintf ( "/d/%s/%s" , savedDash . Uid , savedDash . Slug ) )
require . Equal ( t , hit . FolderID , savedFolder . Id )
require . Equal ( t , hit . FolderUID , savedFolder . Uid )
require . Equal ( t , hit . FolderTitle , savedFolder . Title )
require . Equal ( t , hit . FolderURL , fmt . Sprintf ( "/dashboards/f/%s/%s" , savedFolder . Uid , savedFolder . Slug ) )
} )
2022-05-24 21:24:55 +08:00
t . Run ( "Should be able to find dashboards by ids" , func ( t * testing . T ) {
2021-10-18 20:06:47 +08:00
setup ( )
2022-03-21 23:54:30 +08:00
query := models . FindPersistedDashboardsQuery {
2021-10-18 20:06:47 +08:00
DashboardIds : [ ] int64 { savedDash . Id , savedDash2 . Id } ,
2022-08-10 17:56:48 +08:00
SignedInUser : & user . SignedInUser {
2022-08-11 19:28:55 +08:00
OrgID : 1 ,
2022-08-10 17:56:48 +08:00
OrgRole : org . RoleEditor ,
2022-05-16 18:45:41 +08:00
Permissions : map [ int64 ] map [ string ] [ ] string {
1 : { dashboards . ActionDashboardsRead : [ ] string { dashboards . ScopeDashboardsAll } } ,
} ,
} ,
2021-10-18 20:06:47 +08:00
}
2022-05-24 21:24:55 +08:00
err := testSearchDashboards ( dashboardStore , & query )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
require . Equal ( t , len ( query . Result ) , 2 )
hit := query . Result [ 0 ]
require . Equal ( t , len ( hit . Tags ) , 2 )
hit2 := query . Result [ 1 ]
require . Equal ( t , len ( hit2 . Tags ) , 1 )
} )
2022-05-24 21:24:55 +08:00
t . Run ( "Should be able to find starred dashboards" , func ( t * testing . T ) {
2021-10-18 20:06:47 +08:00
setup ( )
2022-02-16 21:15:44 +08:00
starredDash := insertTestDashboard ( t , dashboardStore , "starred dash" , 1 , 0 , false )
2022-05-19 20:32:10 +08:00
err := starService . Add ( context . Background ( ) , & star . StarDashboardCommand {
DashboardID : starredDash . Id ,
UserID : 10 ,
2021-10-18 20:06:47 +08:00
} )
require . NoError ( t , err )
2022-05-19 20:32:10 +08:00
err = starService . Add ( context . Background ( ) , & star . StarDashboardCommand {
DashboardID : savedDash . Id ,
UserID : 1 ,
2021-10-18 20:06:47 +08:00
} )
require . NoError ( t , err )
2022-03-21 23:54:30 +08:00
query := models . FindPersistedDashboardsQuery {
2022-08-10 17:56:48 +08:00
SignedInUser : & user . SignedInUser {
2022-08-11 19:28:55 +08:00
UserID : 10 ,
OrgID : 1 ,
2022-08-10 17:56:48 +08:00
OrgRole : org . RoleEditor ,
2022-05-16 18:45:41 +08:00
Permissions : map [ int64 ] map [ string ] [ ] string {
1 : { dashboards . ActionDashboardsRead : [ ] string { dashboards . ScopeDashboardsAll } } ,
} ,
} ,
IsStarred : true ,
2021-10-18 20:06:47 +08:00
}
2022-05-24 21:24:55 +08:00
res , err := dashboardStore . FindDashboards ( context . Background ( ) , & query )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
2022-05-24 21:24:55 +08:00
require . Equal ( t , len ( res ) , 1 )
require . Equal ( t , res [ 0 ] . Title , "starred dash" )
2021-10-18 20:06:47 +08:00
} )
2022-11-02 21:15:50 +08:00
t . Run ( "Can count dashboards by parent folder" , func ( t * testing . T ) {
setup ( )
// setup() saves one dashboard in the general folder and two in the "savedFolder".
count , err := dashboardStore . CountDashboardsInFolder (
context . Background ( ) ,
& dashboards . CountDashboardsInFolderRequest { FolderID : 0 , OrgID : 1 } )
require . NoError ( t , err )
require . Equal ( t , int64 ( 1 ) , count )
count , err = dashboardStore . CountDashboardsInFolder (
context . Background ( ) ,
& dashboards . CountDashboardsInFolderRequest { FolderID : savedFolder . Id , OrgID : 1 } )
require . NoError ( t , err )
require . Equal ( t , int64 ( 2 ) , count )
} )
2021-10-18 20:06:47 +08:00
}
2022-05-24 17:04:03 +08:00
func TestIntegrationDashboardDataAccessGivenPluginWithImportedDashboards ( t * testing . T ) {
2022-06-10 23:46:21 +08:00
if testing . Short ( ) {
t . Skip ( "skipping integration test" )
}
2022-10-19 21:02:15 +08:00
sqlStore := db . InitTestDB ( t )
2022-10-15 03:33:06 +08:00
cfg := setting . NewCfg ( )
cfg . IsFeatureToggleEnabled = func ( key string ) bool { return false }
2022-11-08 17:52:07 +08:00
dashboardStore := ProvideDashboardStore ( sqlStore , & setting . Cfg { } , testFeatureToggles , tagimpl . ProvideService ( sqlStore , cfg ) )
2021-10-18 20:06:47 +08:00
pluginId := "test-app"
2022-02-16 21:15:44 +08:00
appFolder := insertTestDashboardForPlugin ( t , dashboardStore , "app-test" , 1 , 0 , true , pluginId )
insertTestDashboardForPlugin ( t , dashboardStore , "app-dash1" , 1 , appFolder . Id , false , pluginId )
insertTestDashboardForPlugin ( t , dashboardStore , "app-dash2" , 1 , appFolder . Id , false , pluginId )
2021-10-18 20:06:47 +08:00
query := models . GetDashboardsByPluginIdQuery {
PluginId : pluginId ,
OrgId : 1 ,
}
2022-11-08 17:52:07 +08:00
err := dashboardStore . GetDashboardsByPluginID ( context . Background ( ) , & query )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
require . Equal ( t , len ( query . Result ) , 2 )
2015-01-06 00:04:29 +08:00
}
2017-06-17 08:33:53 +08:00
2022-05-24 17:04:03 +08:00
func TestIntegrationDashboard_SortingOptions ( t * testing . T ) {
2022-06-10 23:46:21 +08:00
if testing . Short ( ) {
t . Skip ( "skipping integration test" )
}
2022-10-19 21:02:15 +08:00
sqlStore := db . InitTestDB ( t )
2022-10-15 03:33:06 +08:00
cfg := setting . NewCfg ( )
cfg . IsFeatureToggleEnabled = func ( key string ) bool { return false }
2022-11-08 17:52:07 +08:00
dashboardStore := ProvideDashboardStore ( sqlStore , & setting . Cfg { } , testFeatureToggles , tagimpl . ProvideService ( sqlStore , cfg ) )
2022-03-19 05:38:32 +08:00
dashB := insertTestDashboard ( t , dashboardStore , "Beta" , 1 , 0 , false )
dashA := insertTestDashboard ( t , dashboardStore , "Alfa" , 1 , 0 , false )
assert . NotZero ( t , dashA . Id )
assert . Less ( t , dashB . Id , dashA . Id )
2022-03-21 23:54:30 +08:00
qNoSort := & models . FindPersistedDashboardsQuery {
2022-08-10 17:56:48 +08:00
SignedInUser : & user . SignedInUser {
2022-08-11 19:28:55 +08:00
OrgID : 1 ,
UserID : 1 ,
2022-08-10 17:56:48 +08:00
OrgRole : org . RoleAdmin ,
2022-05-16 18:45:41 +08:00
Permissions : map [ int64 ] map [ string ] [ ] string {
1 : { dashboards . ActionDashboardsRead : [ ] string { dashboards . ScopeDashboardsAll } } ,
} ,
} ,
2022-03-19 05:38:32 +08:00
}
2022-05-24 21:24:55 +08:00
results , err := dashboardStore . FindDashboards ( context . Background ( ) , qNoSort )
2022-03-19 05:38:32 +08:00
require . NoError ( t , err )
2022-05-16 18:45:41 +08:00
require . Len ( t , results , 2 )
assert . Equal ( t , dashA . Id , results [ 0 ] . ID )
assert . Equal ( t , dashB . Id , results [ 1 ] . ID )
2022-03-19 05:38:32 +08:00
2022-03-21 23:54:30 +08:00
qSort := & models . FindPersistedDashboardsQuery {
2022-08-10 17:56:48 +08:00
SignedInUser : & user . SignedInUser {
2022-08-11 19:28:55 +08:00
OrgID : 1 ,
UserID : 1 ,
2022-08-10 17:56:48 +08:00
OrgRole : org . RoleAdmin ,
2022-05-16 18:45:41 +08:00
Permissions : map [ int64 ] map [ string ] [ ] string {
1 : { dashboards . ActionDashboardsRead : [ ] string { dashboards . ScopeDashboardsAll } } ,
} ,
} ,
2022-03-21 23:54:30 +08:00
Sort : models . SortOption {
Filter : [ ] models . SortOptionFilter {
2020-05-06 17:42:52 +08:00
searchstore . TitleSorter { Descending : true } ,
} ,
2022-03-19 05:38:32 +08:00
} ,
}
2022-05-24 21:24:55 +08:00
results , err = dashboardStore . FindDashboards ( context . Background ( ) , qSort )
2022-03-19 05:38:32 +08:00
require . NoError ( t , err )
2022-05-16 18:45:41 +08:00
require . Len ( t , results , 2 )
assert . Equal ( t , dashB . Id , results [ 0 ] . ID )
assert . Equal ( t , dashA . Id , results [ 1 ] . ID )
2022-03-19 05:38:32 +08:00
}
2022-05-24 17:04:03 +08:00
func TestIntegrationDashboard_Filter ( t * testing . T ) {
2022-06-10 23:46:21 +08:00
if testing . Short ( ) {
t . Skip ( "skipping integration test" )
}
2022-10-19 21:02:15 +08:00
sqlStore := db . InitTestDB ( t )
2022-10-15 03:33:06 +08:00
cfg := setting . NewCfg ( )
cfg . IsFeatureToggleEnabled = func ( key string ) bool { return false }
2022-11-08 17:52:07 +08:00
dashboardStore := ProvideDashboardStore ( sqlStore , cfg , testFeatureToggles , tagimpl . ProvideService ( sqlStore , cfg ) )
2022-03-19 05:38:32 +08:00
insertTestDashboard ( t , dashboardStore , "Alfa" , 1 , 0 , false )
dashB := insertTestDashboard ( t , dashboardStore , "Beta" , 1 , 0 , false )
2022-03-21 23:54:30 +08:00
qNoFilter := & models . FindPersistedDashboardsQuery {
2022-08-10 17:56:48 +08:00
SignedInUser : & user . SignedInUser {
2022-08-11 19:28:55 +08:00
OrgID : 1 ,
UserID : 1 ,
2022-08-10 17:56:48 +08:00
OrgRole : org . RoleAdmin ,
2022-05-16 18:45:41 +08:00
Permissions : map [ int64 ] map [ string ] [ ] string {
1 : { dashboards . ActionDashboardsRead : [ ] string { dashboards . ScopeDashboardsAll } } ,
} ,
} ,
2022-03-19 05:38:32 +08:00
}
2022-05-24 21:24:55 +08:00
results , err := dashboardStore . FindDashboards ( context . Background ( ) , qNoFilter )
2022-03-19 05:38:32 +08:00
require . NoError ( t , err )
2022-05-16 18:45:41 +08:00
require . Len ( t , results , 2 )
2022-03-19 05:38:32 +08:00
2022-03-21 23:54:30 +08:00
qFilter := & models . FindPersistedDashboardsQuery {
2022-08-10 17:56:48 +08:00
SignedInUser : & user . SignedInUser {
2022-08-11 19:28:55 +08:00
OrgID : 1 ,
UserID : 1 ,
2022-08-10 17:56:48 +08:00
OrgRole : org . RoleAdmin ,
2022-05-16 18:45:41 +08:00
Permissions : map [ int64 ] map [ string ] [ ] string {
1 : { dashboards . ActionDashboardsRead : [ ] string { dashboards . ScopeDashboardsAll } } ,
} ,
} ,
2022-03-19 05:38:32 +08:00
Filters : [ ] interface { } {
searchstore . TitleFilter {
Dialect : sqlStore . Dialect ,
Title : "Beta" ,
} ,
} ,
}
2022-05-24 21:24:55 +08:00
results , err = dashboardStore . FindDashboards ( context . Background ( ) , qFilter )
2022-03-19 05:38:32 +08:00
require . NoError ( t , err )
2022-05-16 18:45:41 +08:00
require . Len ( t , results , 1 )
assert . Equal ( t , dashB . Id , results [ 0 ] . ID )
2020-05-06 17:42:52 +08:00
}
2022-02-16 21:15:44 +08:00
2022-10-19 21:02:15 +08:00
func insertTestRule ( t * testing . T , sqlStore sqlstore . Store , foderOrgID int64 , folderUID string ) {
err := sqlStore . WithDbSession ( context . Background ( ) , func ( sess * db . Session ) error {
2021-07-22 17:27:13 +08:00
type alertQuery struct {
RefID string
DatasourceUID string
Model json . RawMessage
}
type alertRule struct {
ID int64 ` xorm:"pk autoincr 'id'" `
OrgID int64 ` xorm:"org_id" `
Title string
Updated time . Time
UID string ` xorm:"uid" `
NamespaceUID string ` xorm:"namespace_uid" `
RuleGroup string
Condition string
Data [ ] alertQuery
}
rule := alertRule {
OrgID : foderOrgID ,
NamespaceUID : folderUID ,
UID : "rule" ,
RuleGroup : "rulegroup" ,
Updated : time . Now ( ) ,
Condition : "A" ,
Data : [ ] alertQuery {
{
RefID : "A" ,
DatasourceUID : "-100" ,
Model : json . RawMessage ( ` {
"type" : "math" ,
"expression" : "2 + 3 > 1"
} ` ) ,
} ,
} ,
}
_ , err := sess . Insert ( & rule )
require . NoError ( t , err )
type alertRuleVersion struct {
ID int64 ` xorm:"pk autoincr 'id'" `
RuleOrgID int64 ` xorm:"rule_org_id" `
RuleUID string ` xorm:"rule_uid" `
RuleNamespaceUID string ` xorm:"rule_namespace_uid" `
RuleGroup string
ParentVersion int64
RestoredFrom int64
Version int64
Created time . Time
Title string
Condition string
Data [ ] alertQuery
IntervalSeconds int64
}
ruleVersion := alertRuleVersion {
RuleOrgID : rule . OrgID ,
RuleUID : rule . UID ,
RuleNamespaceUID : rule . NamespaceUID ,
RuleGroup : rule . RuleGroup ,
Created : rule . Updated ,
Condition : rule . Condition ,
Data : rule . Data ,
ParentVersion : 0 ,
RestoredFrom : 0 ,
Version : 1 ,
IntervalSeconds : 60 ,
}
_ , err = sess . Insert ( & ruleVersion )
require . NoError ( t , err )
return err
} )
2022-06-02 02:55:22 +08:00
require . NoError ( t , err )
2021-07-22 17:27:13 +08:00
}
2022-02-16 21:15:44 +08:00
2022-06-28 20:32:25 +08:00
func CreateUser ( t * testing . T , sqlStore * sqlstore . SQLStore , name string , role string , isAdmin bool ) user . User {
2022-02-16 21:15:44 +08:00
t . Helper ( )
2022-06-07 21:49:18 +08:00
sqlStore . Cfg . AutoAssignOrg = true
sqlStore . Cfg . AutoAssignOrgId = 1
sqlStore . Cfg . AutoAssignOrgRole = role
2022-06-28 20:32:25 +08:00
currentUserCmd := user . CreateUserCommand { Login : name , Email : name + "@test.com" , Name : "a " + name , IsAdmin : isAdmin }
2022-02-16 21:15:44 +08:00
currentUser , err := sqlStore . CreateUser ( context . Background ( ) , currentUserCmd )
2022-10-19 21:02:15 +08:00
2022-02-16 21:15:44 +08:00
require . NoError ( t , err )
2022-06-28 20:32:25 +08:00
q1 := models . GetUserOrgListQuery { UserId : currentUser . ID }
2022-02-16 21:15:44 +08:00
err = sqlStore . GetUserOrgList ( context . Background ( ) , & q1 )
require . NoError ( t , err )
2022-08-10 17:56:48 +08:00
require . Equal ( t , org . RoleType ( role ) , q1 . Result [ 0 ] . Role )
2022-02-16 21:15:44 +08:00
return * currentUser
}
func insertTestDashboard ( t * testing . T , dashboardStore * DashboardStore , title string , orgId int64 ,
folderId int64 , isFolder bool , tags ... interface { } ) * models . Dashboard {
t . Helper ( )
cmd := models . SaveDashboardCommand {
OrgId : orgId ,
FolderId : folderId ,
IsFolder : isFolder ,
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"id" : nil ,
"title" : title ,
"tags" : tags ,
} ) ,
}
2022-09-21 20:04:01 +08:00
dash , err := dashboardStore . SaveDashboard ( context . Background ( ) , cmd )
2022-02-16 21:15:44 +08:00
require . NoError ( t , err )
require . NotNil ( t , dash )
dash . Data . Set ( "id" , dash . Id )
dash . Data . Set ( "uid" , dash . Uid )
return dash
}
func insertTestDashboardForPlugin ( t * testing . T , dashboardStore * DashboardStore , title string , orgId int64 ,
2021-03-17 23:06:10 +08:00
folderId int64 , isFolder bool , pluginId string ) * models . Dashboard {
t . Helper ( )
2020-02-29 20:35:15 +08:00
cmd := models . SaveDashboardCommand {
2017-11-28 00:08:39 +08:00
OrgId : orgId ,
FolderId : folderId ,
IsFolder : isFolder ,
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"id" : nil ,
"title" : title ,
} ) ,
PluginId : pluginId ,
}
2022-09-21 20:04:01 +08:00
dash , err := dashboardStore . SaveDashboard ( context . Background ( ) , cmd )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
2017-11-28 00:08:39 +08:00
2021-03-17 23:06:10 +08:00
return dash
2017-11-28 00:08:39 +08:00
}
2022-02-16 21:15:44 +08:00
2022-07-18 21:14:58 +08:00
func updateDashboardACL ( t * testing . T , dashboardStore * DashboardStore , dashboardID int64 ,
items ... models . DashboardACL ) error {
2020-11-12 04:33:32 +08:00
t . Helper ( )
2022-02-16 21:15:44 +08:00
2022-07-18 21:14:58 +08:00
var itemPtrs [ ] * models . DashboardACL
2022-02-16 21:15:44 +08:00
for _ , it := range items {
item := it
item . Created = time . Now ( )
item . Updated = time . Now ( )
itemPtrs = append ( itemPtrs , & item )
}
return dashboardStore . UpdateDashboardACL ( context . Background ( ) , dashboardID , itemPtrs )
2017-06-24 05:22:09 +08:00
}
2022-05-24 21:24:55 +08:00
// testSearchDashboards is a (near) copy of the dashboard service
// SearchDashboards, which is a wrapper around FindDashboards.
func testSearchDashboards ( d * DashboardStore , query * models . FindPersistedDashboardsQuery ) error {
res , err := d . FindDashboards ( context . Background ( ) , query )
if err != nil {
return err
}
makeQueryResult ( query , res )
return nil
}
func makeQueryResult ( query * models . FindPersistedDashboardsQuery , res [ ] dashboards . DashboardSearchProjection ) {
query . Result = make ( [ ] * models . Hit , 0 )
hits := make ( map [ int64 ] * models . Hit )
for _ , item := range res {
hit , exists := hits [ item . ID ]
if ! exists {
hitType := models . DashHitDB
if item . IsFolder {
hitType = models . DashHitFolder
}
hit = & models . Hit {
ID : item . ID ,
UID : item . UID ,
Title : item . Title ,
URI : "db/" + item . Slug ,
URL : models . GetDashboardFolderUrl ( item . IsFolder , item . UID , item . Slug ) ,
Type : hitType ,
FolderID : item . FolderID ,
FolderUID : item . FolderUID ,
FolderTitle : item . FolderTitle ,
Tags : [ ] string { } ,
}
if item . FolderID > 0 {
hit . FolderURL = models . GetFolderUrl ( item . FolderUID , item . FolderSlug )
}
if query . Sort . MetaName != "" {
hit . SortMeta = item . SortMeta
hit . SortMetaName = query . Sort . MetaName
}
query . Result = append ( query . Result , hit )
hits [ item . ID ] = hit
}
if len ( item . Term ) > 0 {
hit . Tags = append ( hit . Tags , item . Term )
}
}
}