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-11-15 03:08:10 +08:00
"github.com/grafana/grafana/pkg/services/quota/quotatest"
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
2023-01-16 23:33:55 +08:00
var savedFolder , savedDash , savedDash2 * dashboards . 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-15 03:08:10 +08:00
quotaService := quotatest . New ( false , nil )
var err error
dashboardStore , err = ProvideDashboardStore ( sqlStore , cfg , testFeatureToggles , tagimpl . ProvideService ( sqlStore , cfg ) , quotaService )
require . NoError ( t , err )
2022-02-16 21:15:44 +08:00
savedFolder = insertTestDashboard ( t , dashboardStore , "1 test dash folder" , 1 , 0 , true , "prod" , "webapp" )
2023-01-16 23:33:55 +08:00
savedDash = insertTestDashboard ( t , dashboardStore , "test dash 23" , 1 , savedFolder . ID , false , "prod" , "webapp" )
insertTestDashboard ( t , dashboardStore , "test dash 45" , 1 , savedFolder . ID , false , "prod" )
2022-02-16 21:15:44 +08:00
savedDash2 = insertTestDashboard ( t , dashboardStore , "test dash 67" , 1 , 0 , false , "prod" )
2023-01-16 23:33:55 +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" )
2023-01-16 23:33:55 +08:00
require . NotEqual ( t , savedDash . ID , 0 )
2021-10-18 20:06:47 +08:00
require . False ( t , savedDash . IsFolder )
2023-01-16 23:33:55 +08:00
require . Positive ( t , savedDash . FolderID )
require . Positive ( t , len ( savedDash . UID ) )
2021-10-18 20:06:47 +08:00
require . Equal ( t , savedFolder . Title , "1 test dash folder" )
require . Equal ( t , savedFolder . Slug , "1-test-dash-folder" )
2023-01-16 23:33:55 +08:00
require . NotEqual ( t , savedFolder . ID , 0 )
2021-10-18 20:06:47 +08:00
require . True ( t , savedFolder . IsFolder )
2023-01-16 23:33:55 +08:00
require . EqualValues ( t , savedFolder . FolderID , 0 )
require . Positive ( t , len ( savedFolder . UID ) )
2021-10-18 20:06:47 +08:00
} )
t . Run ( "Should be able to get dashboard by id" , func ( t * testing . T ) {
setup ( )
2023-01-16 23:33:55 +08:00
query := dashboards . GetDashboardQuery {
ID : savedDash . ID ,
OrgID : 1 ,
2021-10-18 20:06:47 +08:00
}
2023-01-25 17:36:26 +08:00
queryResult , err := dashboardStore . GetDashboard ( context . Background ( ) , & query )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
2023-01-25 17:36:26 +08:00
require . Equal ( t , queryResult . Title , "test dash 23" )
require . Equal ( t , queryResult . Slug , "test-dash-23" )
require . Equal ( t , queryResult . ID , savedDash . ID )
require . Equal ( t , queryResult . UID , savedDash . UID )
require . False ( t , queryResult . IsFolder )
2021-10-18 20:06:47 +08:00
} )
t . Run ( "Should be able to get dashboard by slug" , func ( t * testing . T ) {
setup ( )
2023-01-16 23:33:55 +08:00
query := dashboards . GetDashboardQuery {
2021-10-18 20:06:47 +08:00
Slug : "test-dash-23" ,
2023-01-16 23:33:55 +08:00
OrgID : 1 ,
2021-10-18 20:06:47 +08:00
}
2023-01-25 17:36:26 +08:00
queryResult , err := dashboardStore . GetDashboard ( context . Background ( ) , & query )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
2023-01-25 17:36:26 +08:00
require . Equal ( t , queryResult . Title , "test dash 23" )
require . Equal ( t , queryResult . Slug , "test-dash-23" )
require . Equal ( t , queryResult . ID , savedDash . ID )
require . Equal ( t , queryResult . UID , savedDash . UID )
require . False ( t , queryResult . IsFolder )
2021-10-18 20:06:47 +08:00
} )
t . Run ( "Should be able to get dashboard by uid" , func ( t * testing . T ) {
setup ( )
2023-01-16 23:33:55 +08:00
query := dashboards . GetDashboardQuery {
UID : savedDash . UID ,
OrgID : 1 ,
2021-10-18 20:06:47 +08:00
}
2023-01-25 17:36:26 +08:00
queryResult , err := dashboardStore . GetDashboard ( context . Background ( ) , & query )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
2023-01-25 17:36:26 +08:00
require . Equal ( t , queryResult . Title , "test dash 23" )
require . Equal ( t , queryResult . Slug , "test-dash-23" )
require . Equal ( t , queryResult . ID , savedDash . ID )
require . Equal ( t , queryResult . UID , savedDash . UID )
require . False ( t , queryResult . IsFolder )
2021-10-18 20:06:47 +08:00
} )
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 ( )
2023-01-16 23:33:55 +08:00
query := dashboards . GetDashboardRefByIDQuery { ID : savedDash . ID }
2023-01-25 17:36:26 +08:00
queryResult , err := dashboardStore . GetDashboardUIDByID ( context . Background ( ) , & query )
2022-05-19 22:13:02 +08:00
require . NoError ( t , err )
2023-01-25 17:36:26 +08:00
require . Equal ( t , queryResult . UID , savedDash . UID )
2022-05-19 22:13:02 +08:00
} )
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 ( )
2023-01-16 23:33:55 +08:00
query := dashboards . GetDashboardQuery {
OrgID : 1 ,
2021-10-18 20:06:47 +08:00
}
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 ( )
2023-01-16 23:33:55 +08:00
query := dashboards . GetDashboardsQuery { DashboardIDs : [ ] int64 { savedDash . ID , savedDash2 . ID } }
2023-01-25 17:36:26 +08:00
queryResult , err := dashboardStore . GetDashboards ( context . Background ( ) , & query )
2022-05-19 22:59:12 +08:00
require . NoError ( t , err )
2023-01-25 17:36:26 +08:00
assert . Equal ( t , len ( queryResult ) , 2 )
2022-05-19 22:59:12 +08:00
2023-01-16 23:33:55 +08:00
query = dashboards . GetDashboardsQuery { DashboardUIDs : [ ] string { savedDash . UID , savedDash2 . UID } }
2023-01-25 17:36:26 +08:00
queryResult , err = dashboardStore . GetDashboards ( context . Background ( ) , & query )
2022-05-19 22:59:12 +08:00
require . NoError ( t , err )
2023-01-25 17:36:26 +08:00
assert . Equal ( t , len ( queryResult ) , 2 )
2022-05-19 22:59:12 +08:00
} )
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
2023-01-18 20:52:41 +08:00
err := dashboardStore . DeleteDashboard ( context . Background ( ) , & dashboards . DeleteDashboardCommand {
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 ( )
2023-01-16 23:33:55 +08:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
2021-10-18 20:06:47 +08:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : "folderId" ,
"tags" : [ ] interface { } { } ,
} ) ,
2023-01-16 23:33:55 +08:00
UserID : 100 ,
2021-10-18 20:06:47 +08:00
}
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 ( )
2023-01-16 23:33:55 +08:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
2021-10-18 20:06:47 +08:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
2023-01-16 23:33:55 +08:00
"id" : savedDash . ID ,
2021-10-18 20:06:47 +08:00
"title" : "folderId" ,
"tags" : [ ] interface { } { } ,
} ) ,
Overwrite : true ,
2023-01-16 23:33:55 +08:00
FolderID : 2 ,
UserID : 100 ,
2021-10-18 20:06:47 +08:00
}
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 )
2023-01-16 23:33:55 +08:00
require . EqualValues ( t , dash . FolderID , 2 )
2021-10-18 20:06:47 +08:00
2023-01-16 23:33:55 +08:00
cmd = dashboards . SaveDashboardCommand {
OrgID : 1 ,
2021-10-18 20:06:47 +08:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
2023-01-16 23:33:55 +08:00
"id" : savedDash . ID ,
2021-10-18 20:06:47 +08:00
"title" : "folderId" ,
"tags" : [ ] interface { } { } ,
} ) ,
2023-01-16 23:33:55 +08:00
FolderID : 0 ,
2021-10-18 20:06:47 +08:00
Overwrite : true ,
2023-01-16 23:33:55 +08:00
UserID : 100 ,
2021-10-18 20:06:47 +08:00
}
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 )
2023-01-16 23:33:55 +08:00
query := dashboards . GetDashboardQuery {
ID : savedDash . ID ,
OrgID : 1 ,
2021-10-18 20:06:47 +08:00
}
2023-01-25 17:36:26 +08:00
queryResult , err := dashboardStore . GetDashboard ( context . Background ( ) , & query )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
2023-01-25 17:36:26 +08:00
require . Equal ( t , queryResult . FolderID , int64 ( 0 ) )
require . Equal ( t , queryResult . CreatedBy , savedDash . CreatedBy )
require . WithinDuration ( t , queryResult . Created , savedDash . Created , 3 * time . Second )
require . Equal ( t , queryResult . UpdatedBy , int64 ( 100 ) )
require . False ( t , queryResult . Updated . IsZero ( ) )
2021-10-18 20:06:47 +08:00
} )
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
2023-01-18 20:52:41 +08:00
deleteCmd := & dashboards . 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 ( )
2023-01-18 20:52:41 +08:00
deleteCmd := & dashboards . 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 ,
2023-01-16 23:33:55 +08:00
DashboardUid : savedDash . UID ,
OrgId : savedDash . OrgID ,
2022-10-20 03:47:08 +08:00
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 )
2023-01-18 20:52:41 +08:00
deleteCmd := & dashboards . DeleteDashboardCommand { ID : savedDash . ID , OrgID : savedDash . OrgID }
2022-10-20 03:47:08 +08:00
err = dashboardStore . DeleteDashboard ( context . Background ( ) , deleteCmd )
require . NoError ( t , err )
2023-01-16 23:33:55 +08:00
query := dashboards . GetDashboardQuery { UID : savedDash . UID , OrgID : savedDash . OrgID }
2022-10-20 03:47:08 +08:00
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 ,
2023-01-16 23:33:55 +08:00
DashboardUid : savedDash . UID ,
OrgId : savedDash . OrgID ,
2022-10-20 03:47:08 +08:00
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 )
2023-01-18 20:52:41 +08:00
deleteCmd := & dashboards . DeleteDashboardCommand { ID : savedFolder . ID , ForceDeleteFolderRules : true }
2022-10-20 03:47:08 +08:00
err = dashboardStore . DeleteDashboard ( context . Background ( ) , deleteCmd )
require . NoError ( t , err )
2023-01-16 23:33:55 +08:00
query := dashboards . GetDashboardsQuery {
DashboardIDs : [ ] int64 { savedFolder . ID , savedDash . ID } ,
2022-10-20 03:47:08 +08:00
}
2023-01-25 17:36:26 +08:00
queryResult , err := dashboardStore . GetDashboards ( context . Background ( ) , & query )
2022-10-20 03:47:08 +08:00
require . NoError ( t , err )
2023-01-25 17:36:26 +08:00
require . Equal ( t , len ( queryResult ) , 0 )
2022-10-20 03:47:08 +08:00
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 ( )
2023-01-18 20:52:41 +08:00
deleteCmd := & dashboards . 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 ,
2023-01-16 23:33:55 +08:00
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
2023-01-16 23:33:55 +08:00
exists , err := sess . Table ( "alert_rule" ) . Where ( "namespace_uid = (SELECT uid FROM dashboard WHERE id = ?)" , savedFolder . ID ) . Cols ( "id" ) . Get ( & existingRuleID )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
require . False ( t , exists )
var existingRuleVersionID int64
2023-01-16 23:33:55 +08:00
exists , err = sess . Table ( "alert_rule_version" ) . Where ( "rule_namespace_uid = (SELECT uid FROM dashboard WHERE id = ?)" , savedFolder . ID ) . Cols ( "id" ) . Get ( & existingRuleVersionID )
2021-10-18 20:06:47 +08:00
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 ) {
2023-01-16 23:33:55 +08:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
2021-10-18 20:06:47 +08:00
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 ) {
2023-01-16 23:33:55 +08:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
2021-10-18 20:06:47 +08:00
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 ( )
2023-01-18 20:52:41 +08:00
query := dashboards . GetDashboardTagsQuery { OrgID : 1 }
2021-10-18 20:06:47 +08:00
2023-01-25 17:36:26 +08:00
queryResult , err := dashboardStore . GetDashboardTags ( context . Background ( ) , & query )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
2023-01-25 17:36:26 +08:00
require . Equal ( t , len ( queryResult ) , 2 )
2021-10-18 20:06:47 +08:00
} )
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 )
2023-01-16 23:33:55 +08:00
require . Equal ( t , hit . URL , fmt . Sprintf ( "/dashboards/f/%s/%s" , savedFolder . UID , savedFolder . Slug ) )
2021-10-18 20:06:47 +08:00
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 ,
2023-01-16 23:33:55 +08:00
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 ]
2023-01-16 23:33:55 +08:00
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 )
2021-10-18 20:06:47 +08:00
require . Equal ( t , hit . FolderTitle , savedFolder . Title )
2023-01-16 23:33:55 +08:00
require . Equal ( t , hit . FolderURL , fmt . Sprintf ( "/dashboards/f/%s/%s" , savedFolder . UID , savedFolder . Slug ) )
2021-10-18 20:06:47 +08:00
} )
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 {
2023-01-16 23:33:55 +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 {
2023-01-16 23:33:55 +08:00
DashboardID : starredDash . ID ,
2022-05-19 20:32:10 +08:00
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 {
2023-01-16 23:33:55 +08:00
DashboardID : savedDash . ID ,
2022-05-19 20:32:10 +08:00
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 ( ) ,
2023-01-16 23:33:55 +08:00
& dashboards . CountDashboardsInFolderRequest { FolderID : savedFolder . ID , OrgID : 1 } )
2022-11-02 21:15:50 +08:00
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-15 03:08:10 +08:00
quotaService := quotatest . New ( false , nil )
dashboardStore , err := ProvideDashboardStore ( sqlStore , & setting . Cfg { } , testFeatureToggles , tagimpl . ProvideService ( sqlStore , cfg ) , quotaService )
require . NoError ( t , err )
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 )
2023-01-16 23:33:55 +08:00
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
2023-01-18 20:52:41 +08:00
query := dashboards . GetDashboardsByPluginIDQuery {
PluginID : pluginId ,
OrgID : 1 ,
2021-10-18 20:06:47 +08:00
}
2023-01-25 17:36:26 +08:00
queryResult , err := dashboardStore . GetDashboardsByPluginID ( context . Background ( ) , & query )
2021-10-18 20:06:47 +08:00
require . NoError ( t , err )
2023-01-25 17:36:26 +08:00
require . Equal ( t , len ( queryResult ) , 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-15 03:08:10 +08:00
quotaService := quotatest . New ( false , nil )
dashboardStore , err := ProvideDashboardStore ( sqlStore , & setting . Cfg { } , testFeatureToggles , tagimpl . ProvideService ( sqlStore , cfg ) , quotaService )
require . NoError ( t , err )
2022-03-19 05:38:32 +08:00
dashB := insertTestDashboard ( t , dashboardStore , "Beta" , 1 , 0 , false )
dashA := insertTestDashboard ( t , dashboardStore , "Alfa" , 1 , 0 , false )
2023-01-16 23:33:55 +08:00
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 )
2023-01-16 23:33:55 +08:00
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 )
2023-01-16 23:33:55 +08:00
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-15 03:08:10 +08:00
quotaService := quotatest . New ( false , nil )
dashboardStore , err := ProvideDashboardStore ( sqlStore , cfg , testFeatureToggles , tagimpl . ProvideService ( sqlStore , cfg ) , quotaService )
require . NoError ( t , err )
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 )
2023-01-16 23:33:55 +08:00
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-12-13 18:03:36 +08:00
func insertTestRule ( t * testing . T , sqlStore db . DB , foderOrgID int64 , folderUID string ) {
2022-10-19 21:02:15 +08:00
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
func insertTestDashboard ( t * testing . T , dashboardStore * DashboardStore , title string , orgId int64 ,
2023-01-16 23:33:55 +08:00
folderId int64 , isFolder bool , tags ... interface { } ) * dashboards . Dashboard {
2022-02-16 21:15:44 +08:00
t . Helper ( )
2023-01-16 23:33:55 +08:00
cmd := dashboards . SaveDashboardCommand {
OrgID : orgId ,
FolderID : folderId ,
2022-02-16 21:15:44 +08:00
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 )
2023-01-16 23:33:55 +08:00
dash . Data . Set ( "id" , dash . ID )
dash . Data . Set ( "uid" , dash . UID )
2022-02-16 21:15:44 +08:00
return dash
}
func insertTestDashboardForPlugin ( t * testing . T , dashboardStore * DashboardStore , title string , orgId int64 ,
2023-01-16 23:33:55 +08:00
folderId int64 , isFolder bool , pluginId string ) * dashboards . Dashboard {
2021-03-17 23:06:10 +08:00
t . Helper ( )
2023-01-16 23:33:55 +08:00
cmd := dashboards . SaveDashboardCommand {
OrgID : orgId ,
FolderID : folderId ,
2017-11-28 00:08:39 +08:00
IsFolder : isFolder ,
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"id" : nil ,
"title" : title ,
} ) ,
2023-01-16 23:33:55 +08:00
PluginID : pluginId ,
2017-11-28 00:08:39 +08:00
}
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 ,
2023-01-20 21:58:47 +08:00
items ... dashboards . DashboardACL ) error {
2020-11-12 04:33:32 +08:00
t . Helper ( )
2022-02-16 21:15:44 +08:00
2023-01-20 21:58:47 +08:00
var itemPtrs [ ] * dashboards . 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 ,
2023-01-18 20:52:41 +08:00
URL : dashboards . GetDashboardFolderURL ( item . IsFolder , item . UID , item . Slug ) ,
2022-05-24 21:24:55 +08:00
Type : hitType ,
FolderID : item . FolderID ,
FolderUID : item . FolderUID ,
FolderTitle : item . FolderTitle ,
Tags : [ ] string { } ,
}
if item . FolderID > 0 {
2023-01-18 20:52:41 +08:00
hit . FolderURL = dashboards . GetFolderURL ( item . FolderUID , item . FolderSlug )
2022-05-24 21:24:55 +08:00
}
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 )
}
}
}