2021-09-01 09:38:56 -05:00
//go:build integration
2020-10-16 02:46:14 -05:00
// +build integration
2022-02-16 07:15:44 -06:00
package database
2015-01-05 10:04:29 -06:00
import (
2018-06-15 14:23:57 -05:00
"context"
2021-07-22 04:27:13 -05:00
"encoding/json"
"errors"
2018-01-30 08:24:14 -06:00
"fmt"
2022-01-19 02:55:38 -06:00
"testing"
"time"
2022-03-18 16:38:32 -05:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2016-03-12 03:13:49 -06:00
"github.com/grafana/grafana/pkg/components/simplejson"
2020-02-29 06:35:15 -06:00
"github.com/grafana/grafana/pkg/models"
2022-02-16 07:15:44 -06:00
"github.com/grafana/grafana/pkg/services/sqlstore"
2020-05-06 04:42:52 -05:00
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
2017-06-16 19:33:53 -05:00
"github.com/grafana/grafana/pkg/setting"
2015-01-05 10:04:29 -06:00
)
func TestDashboardDataAccess ( t * testing . T ) {
2022-02-16 07:15:44 -06:00
var sqlStore * sqlstore . SQLStore
2021-10-18 07:06:47 -05:00
var savedFolder , savedDash , savedDash2 * models . Dashboard
2022-02-16 07:15:44 -06:00
var dashboardStore * DashboardStore
2021-10-18 07:06:47 -05:00
setup := func ( ) {
2022-02-16 07:15:44 -06:00
sqlStore = sqlstore . InitTestDB ( t )
dashboardStore = ProvideDashboardStore ( sqlStore )
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 07:06:47 -05:00
insertTestRule ( t , sqlStore , savedFolder . OrgId , savedFolder . Uid )
}
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-02-04 10:21:06 -06:00
err := sqlStore . GetDashboard ( context . Background ( ) , & query )
2021-10-18 07:06:47 -05: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-02-04 10:21:06 -06:00
err := sqlStore . GetDashboard ( context . Background ( ) , & query )
2021-10-18 07:06:47 -05: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-02-04 10:21:06 -06:00
err := sqlStore . GetDashboard ( context . Background ( ) , & query )
2021-10-18 07:06:47 -05: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 ( "Shouldn't be able to get a dashboard with just an OrgID" , func ( t * testing . T ) {
setup ( )
query := models . GetDashboardQuery {
OrgId : 1 ,
}
2022-02-04 10:21:06 -06:00
err := sqlStore . GetDashboard ( context . Background ( ) , & query )
2021-10-18 07:06:47 -05:00
require . Equal ( t , err , models . ErrDashboardIdentifierNotSet )
} )
t . Run ( "Should be able to delete dashboard" , func ( t * testing . T ) {
setup ( )
2022-02-16 07:15:44 -06:00
dash := insertTestDashboard ( t , dashboardStore , "delete me" , 1 , 0 , false , "delete this" )
2015-01-05 10:04:29 -06:00
2022-03-22 08:36:50 -05:00
err := dashboardStore . DeleteDashboard ( context . Background ( ) , & models . DeleteDashboardCommand {
2021-10-18 07:06:47 -05:00
Id : dash . Id ,
OrgId : 1 ,
2015-01-07 05:37:24 -06:00
} )
2021-10-18 07:06:47 -05:00
require . NoError ( t , err )
} )
2017-06-16 19:33:53 -05:00
2021-10-18 07:06:47 -05: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-02-16 07:15:44 -06:00
dashboard , err := dashboardStore . SaveDashboard ( cmd )
2021-10-18 07:06:47 -05: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 07:15:44 -06:00
2021-10-18 07:06:47 -05: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-02-16 07:15:44 -06:00
dash , err := dashboardStore . SaveDashboard ( cmd )
2021-10-18 07:06:47 -05: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-02-16 07:15:44 -06:00
_ , err = dashboardStore . SaveDashboard ( cmd )
2021-10-18 07:06:47 -05:00
require . NoError ( t , err )
query := models . GetDashboardQuery {
Id : savedDash . Id ,
OrgId : 1 ,
}
2022-02-04 10:21:06 -06:00
err = sqlStore . GetDashboard ( context . Background ( ) , & query )
2021-10-18 07:06:47 -05: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 07:15:44 -06:00
emptyFolder := insertTestDashboard ( t , dashboardStore , "2 test dash folder" , 1 , 0 , true , "prod" , "webapp" )
2021-10-18 07:06:47 -05:00
deleteCmd := & models . DeleteDashboardCommand { Id : emptyFolder . Id }
2022-03-22 08:36:50 -05:00
err := dashboardStore . DeleteDashboard ( context . Background ( ) , deleteCmd )
2021-10-18 07:06:47 -05: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 08:36:50 -05:00
err := dashboardStore . DeleteDashboard ( context . Background ( ) , deleteCmd )
2021-10-18 07:06:47 -05:00
require . True ( t , errors . Is ( err , models . ErrFolderContainsAlertRules ) )
} )
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 08:36:50 -05:00
err := dashboardStore . DeleteDashboard ( context . Background ( ) , deleteCmd )
2021-10-18 07:06:47 -05:00
require . NoError ( t , err )
2022-03-21 10:54:30 -05:00
query := models . FindPersistedDashboardsQuery {
2021-10-18 07:06:47 -05:00
OrgId : 1 ,
FolderIds : [ ] int64 { savedFolder . Id } ,
SignedInUser : & models . SignedInUser { } ,
}
2017-11-27 10:08:39 -06:00
2021-10-28 04:29:07 -05:00
err = sqlStore . SearchDashboards ( context . Background ( ) , & query )
2021-10-18 07:06:47 -05:00
require . NoError ( t , err )
require . Equal ( t , len ( query . Result ) , 0 )
2022-02-16 07:15:44 -06:00
sqlStore . WithDbSession ( context . Background ( ) , func ( sess * sqlstore . DBSession ) error {
2021-10-18 07:06:47 -05: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-27 10:08:39 -06:00
} )
2015-01-07 05:37:24 -06:00
} )
2021-10-18 07:06:47 -05: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-02-16 07:15:44 -06:00
_ , err := dashboardStore . SaveDashboard ( cmd )
2021-10-18 07:06:47 -05:00
require . Equal ( t , err , models . ErrDashboardNotFound )
} )
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-02-16 07:15:44 -06:00
_ , err := dashboardStore . SaveDashboard ( cmd )
2021-10-18 07:06:47 -05: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-02-07 05:43:43 -06:00
err := sqlStore . GetDashboardTags ( context . Background ( ) , & query )
2021-10-18 07:06:47 -05:00
require . NoError ( t , err )
require . Equal ( t , len ( query . Result ) , 2 )
} )
t . Run ( "Should be able to search for dashboard folder" , func ( t * testing . T ) {
setup ( )
2022-03-21 10:54:30 -05:00
query := models . FindPersistedDashboardsQuery {
2021-10-18 07:06:47 -05:00
Title : "1 test dash folder" ,
OrgId : 1 ,
SignedInUser : & models . SignedInUser { OrgId : 1 , OrgRole : models . ROLE_EDITOR } ,
}
2021-10-28 04:29:07 -05:00
err := sqlStore . SearchDashboards ( context . Background ( ) , & query )
2021-10-18 07:06:47 -05:00
require . NoError ( t , err )
require . Equal ( t , len ( query . Result ) , 1 )
hit := query . Result [ 0 ]
2022-03-21 10:54:30 -05:00
require . Equal ( t , hit . Type , models . DashHitFolder )
2021-10-18 07:06:47 -05:00
require . Equal ( t , hit . URL , fmt . Sprintf ( "/dashboards/f/%s/%s" , savedFolder . Uid , savedFolder . Slug ) )
require . Equal ( t , hit . FolderTitle , "" )
} )
t . Run ( "Should be able to limit search" , func ( t * testing . T ) {
setup ( )
2022-03-21 10:54:30 -05:00
query := models . FindPersistedDashboardsQuery {
2021-10-18 07:06:47 -05:00
OrgId : 1 ,
Limit : 1 ,
SignedInUser : & models . SignedInUser { OrgId : 1 , OrgRole : models . ROLE_EDITOR } ,
}
2021-10-28 04:29:07 -05:00
err := sqlStore . SearchDashboards ( context . Background ( ) , & query )
2021-10-18 07:06:47 -05:00
require . NoError ( t , err )
require . Equal ( t , len ( query . Result ) , 1 )
require . EqualValues ( t , query . Result [ 0 ] . Title , "1 test dash folder" )
} )
t . Run ( "Should be able to search beyond limit using paging" , func ( t * testing . T ) {
setup ( )
2022-03-21 10:54:30 -05:00
query := models . FindPersistedDashboardsQuery {
2021-10-18 07:06:47 -05:00
OrgId : 1 ,
Limit : 1 ,
Page : 2 ,
SignedInUser : & models . SignedInUser { OrgId : 1 , OrgRole : models . ROLE_EDITOR } ,
}
2021-10-28 04:29:07 -05:00
err := sqlStore . SearchDashboards ( context . Background ( ) , & query )
2021-10-18 07:06:47 -05: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 10:54:30 -05:00
query := models . FindPersistedDashboardsQuery {
2021-10-18 07:06:47 -05:00
OrgId : 1 ,
Type : "dash-db" ,
Tags : [ ] string { "prod" } ,
SignedInUser : & models . SignedInUser { OrgId : 1 , OrgRole : models . ROLE_EDITOR } ,
}
2021-10-28 04:29:07 -05:00
err := sqlStore . SearchDashboards ( context . Background ( ) , & query )
2021-10-18 07:06:47 -05:00
require . NoError ( t , err )
require . Equal ( t , len ( query . Result ) , 3 )
require . Equal ( t , query . Result [ 0 ] . Title , "test dash 23" )
} )
t . Run ( "Should be able to search for a dashboard folder's children" , func ( t * testing . T ) {
setup ( )
2022-03-21 10:54:30 -05:00
query := models . FindPersistedDashboardsQuery {
2021-10-18 07:06:47 -05:00
OrgId : 1 ,
FolderIds : [ ] int64 { savedFolder . Id } ,
SignedInUser : & models . SignedInUser { OrgId : 1 , OrgRole : models . ROLE_EDITOR } ,
}
2021-10-28 04:29:07 -05:00
err := sqlStore . SearchDashboards ( context . Background ( ) , & query )
2021-10-18 07:06:47 -05: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 ) )
} )
t . Run ( "Should be able to search for dashboard by dashboard ids" , func ( t * testing . T ) {
setup ( )
2022-03-21 10:54:30 -05:00
query := models . FindPersistedDashboardsQuery {
2021-10-18 07:06:47 -05:00
DashboardIds : [ ] int64 { savedDash . Id , savedDash2 . Id } ,
SignedInUser : & models . SignedInUser { OrgId : 1 , OrgRole : models . ROLE_EDITOR } ,
}
2021-10-28 04:29:07 -05:00
err := sqlStore . SearchDashboards ( context . Background ( ) , & query )
2021-10-18 07:06:47 -05: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 )
} )
t . Run ( "Should be able to search for starred dashboards" , func ( t * testing . T ) {
setup ( )
2022-02-16 07:15:44 -06:00
starredDash := insertTestDashboard ( t , dashboardStore , "starred dash" , 1 , 0 , false )
2021-10-18 07:06:47 -05:00
err := sqlStore . StarDashboard ( context . Background ( ) , & models . StarDashboardCommand {
DashboardId : starredDash . Id ,
UserId : 10 ,
} )
require . NoError ( t , err )
err = sqlStore . StarDashboard ( context . Background ( ) , & models . StarDashboardCommand {
DashboardId : savedDash . Id ,
UserId : 1 ,
} )
require . NoError ( t , err )
2022-03-21 10:54:30 -05:00
query := models . FindPersistedDashboardsQuery {
2021-10-18 07:06:47 -05:00
SignedInUser : & models . SignedInUser { UserId : 10 , OrgId : 1 , OrgRole : models . ROLE_EDITOR } ,
IsStarred : true ,
}
2021-10-28 04:29:07 -05:00
err = sqlStore . SearchDashboards ( context . Background ( ) , & query )
2021-10-18 07:06:47 -05:00
require . NoError ( t , err )
require . Equal ( t , len ( query . Result ) , 1 )
require . Equal ( t , query . Result [ 0 ] . Title , "starred dash" )
} )
}
func TestDashboardDataAccessGivenPluginWithImportedDashboards ( t * testing . T ) {
2022-02-16 07:15:44 -06:00
sqlStore := sqlstore . InitTestDB ( t )
dashboardStore := ProvideDashboardStore ( sqlStore )
2021-10-18 07:06:47 -05:00
pluginId := "test-app"
2022-02-16 07:15:44 -06: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 07:06:47 -05:00
query := models . GetDashboardsByPluginIdQuery {
PluginId : pluginId ,
OrgId : 1 ,
}
2022-03-10 11:38:04 -06:00
err := dashboardStore . GetDashboardsByPluginID ( context . Background ( ) , & query )
2021-10-18 07:06:47 -05:00
require . NoError ( t , err )
require . Equal ( t , len ( query . Result ) , 2 )
2015-01-05 10:04:29 -06:00
}
2017-06-16 19:33:53 -05:00
2020-05-06 04:42:52 -05:00
func TestDashboard_SortingOptions ( t * testing . T ) {
2022-02-16 07:15:44 -06:00
sqlStore := sqlstore . InitTestDB ( t )
dashboardStore := ProvideDashboardStore ( sqlStore )
2022-03-18 16:38:32 -05: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 10:54:30 -05:00
qNoSort := & models . FindPersistedDashboardsQuery {
2022-03-18 16:38:32 -05:00
SignedInUser : & models . SignedInUser { OrgId : 1 , UserId : 1 , OrgRole : models . ROLE_ADMIN } ,
}
dashboards , err := sqlStore . FindDashboards ( context . Background ( ) , qNoSort )
require . NoError ( t , err )
require . Len ( t , dashboards , 2 )
assert . Equal ( t , dashA . Id , dashboards [ 0 ] . ID )
assert . Equal ( t , dashB . Id , dashboards [ 1 ] . ID )
2022-03-21 10:54:30 -05:00
qSort := & models . FindPersistedDashboardsQuery {
2022-03-18 16:38:32 -05:00
SignedInUser : & models . SignedInUser { OrgId : 1 , UserId : 1 , OrgRole : models . ROLE_ADMIN } ,
2022-03-21 10:54:30 -05:00
Sort : models . SortOption {
Filter : [ ] models . SortOptionFilter {
2020-05-06 04:42:52 -05:00
searchstore . TitleSorter { Descending : true } ,
} ,
2022-03-18 16:38:32 -05:00
} ,
}
dashboards , err = sqlStore . FindDashboards ( context . Background ( ) , qSort )
require . NoError ( t , err )
require . Len ( t , dashboards , 2 )
assert . Equal ( t , dashB . Id , dashboards [ 0 ] . ID )
assert . Equal ( t , dashA . Id , dashboards [ 1 ] . ID )
}
func TestDashboard_Filter ( t * testing . T ) {
sqlStore := sqlstore . InitTestDB ( t )
dashboardStore := ProvideDashboardStore ( sqlStore )
insertTestDashboard ( t , dashboardStore , "Alfa" , 1 , 0 , false )
dashB := insertTestDashboard ( t , dashboardStore , "Beta" , 1 , 0 , false )
2022-03-21 10:54:30 -05:00
qNoFilter := & models . FindPersistedDashboardsQuery {
2022-03-18 16:38:32 -05:00
SignedInUser : & models . SignedInUser { OrgId : 1 , UserId : 1 , OrgRole : models . ROLE_ADMIN } ,
}
dashboards , err := sqlStore . FindDashboards ( context . Background ( ) , qNoFilter )
require . NoError ( t , err )
require . Len ( t , dashboards , 2 )
2022-03-21 10:54:30 -05:00
qFilter := & models . FindPersistedDashboardsQuery {
2022-03-18 16:38:32 -05:00
SignedInUser : & models . SignedInUser { OrgId : 1 , UserId : 1 , OrgRole : models . ROLE_ADMIN } ,
Filters : [ ] interface { } {
searchstore . TitleFilter {
Dialect : sqlStore . Dialect ,
Title : "Beta" ,
} ,
} ,
}
dashboards , err = sqlStore . FindDashboards ( context . Background ( ) , qFilter )
require . NoError ( t , err )
require . Len ( t , dashboards , 1 )
assert . Equal ( t , dashB . Id , dashboards [ 0 ] . ID )
2020-05-06 04:42:52 -05:00
}
2022-02-16 07:15:44 -06:00
func insertTestRule ( t * testing . T , sqlStore * sqlstore . SQLStore , foderOrgID int64 , folderUID string ) {
sqlStore . WithDbSession ( context . Background ( ) , func ( sess * sqlstore . DBSession ) error {
2021-07-22 04:27:13 -05: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-02-16 07:15:44 -06:00
func CreateUser ( t * testing . T , sqlStore * sqlstore . SQLStore , name string , role string , isAdmin bool ) models . User {
t . Helper ( )
setting . AutoAssignOrg = true
setting . AutoAssignOrgId = 1
setting . AutoAssignOrgRole = role
currentUserCmd := models . CreateUserCommand { Login : name , Email : name + "@test.com" , Name : "a " + name , IsAdmin : isAdmin }
currentUser , err := sqlStore . CreateUser ( context . Background ( ) , currentUserCmd )
require . NoError ( t , err )
q1 := models . GetUserOrgListQuery { UserId : currentUser . Id }
err = sqlStore . GetUserOrgList ( context . Background ( ) , & q1 )
require . NoError ( t , err )
require . Equal ( t , models . RoleType ( role ) , q1 . Result [ 0 ] . Role )
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 ,
} ) ,
}
dash , err := dashboardStore . SaveDashboard ( cmd )
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 10:06:10 -05:00
folderId int64 , isFolder bool , pluginId string ) * models . Dashboard {
t . Helper ( )
2020-02-29 06:35:15 -06:00
cmd := models . SaveDashboardCommand {
2017-11-27 10:08:39 -06:00
OrgId : orgId ,
FolderId : folderId ,
IsFolder : isFolder ,
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"id" : nil ,
"title" : title ,
} ) ,
PluginId : pluginId ,
}
2022-02-16 07:15:44 -06:00
dash , err := dashboardStore . SaveDashboard ( cmd )
2021-10-18 07:06:47 -05:00
require . NoError ( t , err )
2017-11-27 10:08:39 -06:00
2021-03-17 10:06:10 -05:00
return dash
2017-11-27 10:08:39 -06:00
}
2022-02-16 07:15:44 -06:00
func updateDashboardAcl ( t * testing . T , dashboardStore * DashboardStore , dashboardID int64 ,
items ... models . DashboardAcl ) error {
2020-11-11 14:33:32 -06:00
t . Helper ( )
2022-02-16 07:15:44 -06:00
var itemPtrs [ ] * models . DashboardAcl
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-23 16:22:09 -05:00
}