2021-05-11 00:10:19 -05:00
package libraryelements
2021-03-01 08:33:17 -06:00
import (
"encoding/json"
2021-05-04 06:59:40 -05:00
"strconv"
2021-03-01 08:33:17 -06:00
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/models"
2021-05-11 00:10:19 -05:00
"github.com/grafana/grafana/pkg/services/search"
2021-03-01 08:33:17 -06:00
)
2021-05-11 00:10:19 -05:00
func TestGetAllLibraryElements ( t * testing . T ) {
2021-03-01 08:33:17 -06:00
testScenario ( t , "When an admin tries to get all library panels and none exists, it should return none" ,
func ( t * testing . T , sc scenarioContext ) {
resp := sc . service . getAllHandler ( sc . reqContext )
require . Equal ( t , 200 , resp . Status ( ) )
2021-05-11 00:10:19 -05:00
var result libraryElementsSearch
2021-03-01 08:33:17 -06:00
err := json . Unmarshal ( resp . Body ( ) , & result )
require . NoError ( t , err )
2021-05-11 00:10:19 -05:00
var expected = libraryElementsSearch {
Result : libraryElementsSearchResult {
TotalCount : 0 ,
Elements : [ ] libraryElement { } ,
Page : 1 ,
PerPage : 100 ,
} ,
}
if diff := cmp . Diff ( expected , result , getCompareOptions ( ) ... ) ; diff != "" {
t . Fatalf ( "Result mismatch (-want +got):\n%s" , diff )
}
} )
scenarioWithPanel ( t , "When an admin tries to get all panel elements and both panels and variables exist, it should only return panels" ,
func ( t * testing . T , sc scenarioContext ) {
command := getCreateVariableCommand ( sc . folder . Id , "query0" )
resp := sc . service . createHandler ( sc . reqContext , command )
require . Equal ( t , 200 , resp . Status ( ) )
err := sc . reqContext . Req . ParseForm ( )
require . NoError ( t , err )
2021-05-23 23:11:01 -05:00
sc . reqContext . Req . Form . Add ( "kind" , strconv . FormatInt ( int64 ( models . PanelElement ) , 10 ) )
2021-05-11 00:10:19 -05:00
resp = sc . service . getAllHandler ( sc . reqContext )
require . Equal ( t , 200 , resp . Status ( ) )
var result libraryElementsSearch
err = json . Unmarshal ( resp . Body ( ) , & result )
require . NoError ( t , err )
var expected = libraryElementsSearch {
Result : libraryElementsSearchResult {
TotalCount : 1 ,
Page : 1 ,
PerPage : 100 ,
Elements : [ ] libraryElement {
{
ID : 1 ,
OrgID : 1 ,
FolderID : 1 ,
UID : result . Result . Elements [ 0 ] . UID ,
Name : "Text - Library Panel" ,
2021-05-23 23:11:01 -05:00
Kind : int64 ( models . PanelElement ) ,
2021-05-11 00:10:19 -05:00
Type : "text" ,
Description : "A description" ,
Model : map [ string ] interface { } {
"datasource" : "${DS_GDEV-TESTDATA}" ,
"description" : "A description" ,
"id" : float64 ( 1 ) ,
"title" : "Text - Library Panel" ,
"type" : "text" ,
} ,
Version : 1 ,
Meta : LibraryElementDTOMeta {
2021-05-12 01:48:17 -05:00
FolderName : "ScenarioFolder" ,
FolderUID : sc . folder . Uid ,
ConnectedDashboards : 0 ,
Created : result . Result . Elements [ 0 ] . Meta . Created ,
Updated : result . Result . Elements [ 0 ] . Meta . Updated ,
2021-05-11 00:10:19 -05:00
CreatedBy : LibraryElementDTOMetaUser {
ID : 1 ,
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
} ,
UpdatedBy : LibraryElementDTOMetaUser {
ID : 1 ,
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
} ,
} ,
} ,
} ,
2021-03-18 05:19:41 -05:00
} ,
}
if diff := cmp . Diff ( expected , result , getCompareOptions ( ) ... ) ; diff != "" {
t . Fatalf ( "Result mismatch (-want +got):\n%s" , diff )
}
2021-03-01 08:33:17 -06:00
} )
2021-05-11 00:10:19 -05:00
scenarioWithPanel ( t , "When an admin tries to get all variable elements and both panels and variables exist, it should only return panels" ,
2021-03-01 08:33:17 -06:00
func ( t * testing . T , sc scenarioContext ) {
2021-05-11 00:10:19 -05:00
command := getCreateVariableCommand ( sc . folder . Id , "query0" )
2021-03-01 08:33:17 -06:00
resp := sc . service . createHandler ( sc . reqContext , command )
require . Equal ( t , 200 , resp . Status ( ) )
2021-05-11 00:10:19 -05:00
err := sc . reqContext . Req . ParseForm ( )
require . NoError ( t , err )
2021-05-23 23:11:01 -05:00
sc . reqContext . Req . Form . Add ( "kind" , strconv . FormatInt ( int64 ( models . VariableElement ) , 10 ) )
2021-05-11 00:10:19 -05:00
2021-03-01 08:33:17 -06:00
resp = sc . service . getAllHandler ( sc . reqContext )
require . Equal ( t , 200 , resp . Status ( ) )
2021-05-11 00:10:19 -05:00
var result libraryElementsSearch
err = json . Unmarshal ( resp . Body ( ) , & result )
require . NoError ( t , err )
var expected = libraryElementsSearch {
Result : libraryElementsSearchResult {
TotalCount : 1 ,
Page : 1 ,
PerPage : 100 ,
Elements : [ ] libraryElement {
{
ID : 2 ,
OrgID : 1 ,
FolderID : 1 ,
UID : result . Result . Elements [ 0 ] . UID ,
Name : "query0" ,
2021-05-23 23:11:01 -05:00
Kind : int64 ( models . VariableElement ) ,
2021-05-11 00:10:19 -05:00
Type : "query" ,
Description : "A description" ,
Model : map [ string ] interface { } {
"datasource" : "${DS_GDEV-TESTDATA}" ,
"name" : "query0" ,
"type" : "query" ,
"description" : "A description" ,
} ,
Version : 1 ,
Meta : LibraryElementDTOMeta {
2021-05-12 01:48:17 -05:00
FolderName : "ScenarioFolder" ,
FolderUID : sc . folder . Uid ,
ConnectedDashboards : 0 ,
Created : result . Result . Elements [ 0 ] . Meta . Created ,
Updated : result . Result . Elements [ 0 ] . Meta . Updated ,
2021-05-11 00:10:19 -05:00
CreatedBy : LibraryElementDTOMetaUser {
ID : 1 ,
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
} ,
UpdatedBy : LibraryElementDTOMetaUser {
ID : 1 ,
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
} ,
} ,
} ,
} ,
} ,
}
if diff := cmp . Diff ( expected , result , getCompareOptions ( ) ... ) ; diff != "" {
t . Fatalf ( "Result mismatch (-want +got):\n%s" , diff )
}
} )
scenarioWithPanel ( t , "When an admin tries to get all library panels and two exist, it should succeed" ,
func ( t * testing . T , sc scenarioContext ) {
command := getCreatePanelCommand ( sc . folder . Id , "Text - Library Panel2" )
resp := sc . service . createHandler ( sc . reqContext , command )
require . Equal ( t , 200 , resp . Status ( ) )
resp = sc . service . getAllHandler ( sc . reqContext )
require . Equal ( t , 200 , resp . Status ( ) )
var result libraryElementsSearch
2021-03-01 08:33:17 -06:00
err := json . Unmarshal ( resp . Body ( ) , & result )
require . NoError ( t , err )
2021-05-11 00:10:19 -05:00
var expected = libraryElementsSearch {
Result : libraryElementsSearchResult {
2021-03-18 05:19:41 -05:00
TotalCount : 2 ,
Page : 1 ,
PerPage : 100 ,
2021-05-11 00:10:19 -05:00
Elements : [ ] libraryElement {
2021-03-18 05:19:41 -05:00
{
2021-03-24 07:43:51 -05:00
ID : 1 ,
OrgID : 1 ,
FolderID : 1 ,
2021-05-11 00:10:19 -05:00
UID : result . Result . Elements [ 0 ] . UID ,
2021-03-24 07:43:51 -05:00
Name : "Text - Library Panel" ,
2021-05-23 23:11:01 -05:00
Kind : int64 ( models . PanelElement ) ,
2021-03-24 07:43:51 -05:00
Type : "text" ,
Description : "A description" ,
2021-03-18 05:19:41 -05:00
Model : map [ string ] interface { } {
2021-03-24 07:43:51 -05:00
"datasource" : "${DS_GDEV-TESTDATA}" ,
"description" : "A description" ,
"id" : float64 ( 1 ) ,
"title" : "Text - Library Panel" ,
"type" : "text" ,
2021-03-18 05:19:41 -05:00
} ,
Version : 1 ,
2021-05-11 00:10:19 -05:00
Meta : LibraryElementDTOMeta {
2021-05-12 01:48:17 -05:00
FolderName : "ScenarioFolder" ,
FolderUID : sc . folder . Uid ,
ConnectedDashboards : 0 ,
Created : result . Result . Elements [ 0 ] . Meta . Created ,
Updated : result . Result . Elements [ 0 ] . Meta . Updated ,
2021-05-11 00:10:19 -05:00
CreatedBy : LibraryElementDTOMetaUser {
2021-03-18 05:19:41 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-03-18 05:19:41 -05:00
} ,
2021-05-11 00:10:19 -05:00
UpdatedBy : LibraryElementDTOMetaUser {
2021-03-18 05:19:41 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-03-18 05:19:41 -05:00
} ,
} ,
2021-03-01 08:33:17 -06:00
} ,
2021-03-18 05:19:41 -05:00
{
2021-03-24 07:43:51 -05:00
ID : 2 ,
OrgID : 1 ,
FolderID : 1 ,
2021-05-11 00:10:19 -05:00
UID : result . Result . Elements [ 1 ] . UID ,
2021-03-24 07:43:51 -05:00
Name : "Text - Library Panel2" ,
2021-05-23 23:11:01 -05:00
Kind : int64 ( models . PanelElement ) ,
2021-03-24 07:43:51 -05:00
Type : "text" ,
Description : "A description" ,
2021-03-18 05:19:41 -05:00
Model : map [ string ] interface { } {
2021-03-24 07:43:51 -05:00
"datasource" : "${DS_GDEV-TESTDATA}" ,
"description" : "A description" ,
"id" : float64 ( 1 ) ,
"title" : "Text - Library Panel2" ,
"type" : "text" ,
2021-03-01 08:33:17 -06:00
} ,
2021-03-18 05:19:41 -05:00
Version : 1 ,
2021-05-11 00:10:19 -05:00
Meta : LibraryElementDTOMeta {
2021-05-12 01:48:17 -05:00
FolderName : "ScenarioFolder" ,
FolderUID : sc . folder . Uid ,
ConnectedDashboards : 0 ,
Created : result . Result . Elements [ 1 ] . Meta . Created ,
Updated : result . Result . Elements [ 1 ] . Meta . Updated ,
2021-05-11 00:10:19 -05:00
CreatedBy : LibraryElementDTOMetaUser {
2021-03-18 05:19:41 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-03-18 05:19:41 -05:00
} ,
2021-05-11 00:10:19 -05:00
UpdatedBy : LibraryElementDTOMetaUser {
2021-03-18 05:19:41 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-03-18 05:19:41 -05:00
} ,
2021-03-01 08:33:17 -06:00
} ,
} ,
} ,
2021-03-18 05:19:41 -05:00
} ,
}
if diff := cmp . Diff ( expected , result , getCompareOptions ( ) ... ) ; diff != "" {
t . Fatalf ( "Result mismatch (-want +got):\n%s" , diff )
}
} )
2021-05-11 00:10:19 -05:00
scenarioWithPanel ( t , "When an admin tries to get all library panels and two exist and sort desc is set, it should succeed and the result should be correct" ,
2021-04-28 02:18:13 -05:00
func ( t * testing . T , sc scenarioContext ) {
2021-05-11 00:10:19 -05:00
command := getCreatePanelCommand ( sc . folder . Id , "Text - Library Panel2" )
2021-04-28 02:18:13 -05:00
resp := sc . service . createHandler ( sc . reqContext , command )
require . Equal ( t , 200 , resp . Status ( ) )
err := sc . reqContext . Req . ParseForm ( )
require . NoError ( t , err )
sc . reqContext . Req . Form . Add ( "sortDirection" , search . SortAlphaDesc . Name )
resp = sc . service . getAllHandler ( sc . reqContext )
require . Equal ( t , 200 , resp . Status ( ) )
2021-05-11 00:10:19 -05:00
var result libraryElementsSearch
2021-04-28 02:18:13 -05:00
err = json . Unmarshal ( resp . Body ( ) , & result )
require . NoError ( t , err )
2021-05-11 00:10:19 -05:00
var expected = libraryElementsSearch {
Result : libraryElementsSearchResult {
2021-04-28 02:18:13 -05:00
TotalCount : 2 ,
Page : 1 ,
PerPage : 100 ,
2021-05-11 00:10:19 -05:00
Elements : [ ] libraryElement {
2021-04-28 02:18:13 -05:00
{
ID : 2 ,
OrgID : 1 ,
FolderID : 1 ,
2021-05-11 00:10:19 -05:00
UID : result . Result . Elements [ 0 ] . UID ,
2021-04-28 02:18:13 -05:00
Name : "Text - Library Panel2" ,
2021-05-23 23:11:01 -05:00
Kind : int64 ( models . PanelElement ) ,
2021-04-28 02:18:13 -05:00
Type : "text" ,
Description : "A description" ,
Model : map [ string ] interface { } {
"datasource" : "${DS_GDEV-TESTDATA}" ,
"description" : "A description" ,
"id" : float64 ( 1 ) ,
"title" : "Text - Library Panel2" ,
"type" : "text" ,
} ,
Version : 1 ,
2021-05-11 00:10:19 -05:00
Meta : LibraryElementDTOMeta {
2021-05-12 01:48:17 -05:00
FolderName : "ScenarioFolder" ,
FolderUID : sc . folder . Uid ,
ConnectedDashboards : 0 ,
Created : result . Result . Elements [ 0 ] . Meta . Created ,
Updated : result . Result . Elements [ 0 ] . Meta . Updated ,
2021-05-11 00:10:19 -05:00
CreatedBy : LibraryElementDTOMetaUser {
2021-04-28 02:18:13 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-04-28 02:18:13 -05:00
} ,
2021-05-11 00:10:19 -05:00
UpdatedBy : LibraryElementDTOMetaUser {
2021-04-28 02:18:13 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-04-28 02:18:13 -05:00
} ,
} ,
} ,
{
ID : 1 ,
OrgID : 1 ,
FolderID : 1 ,
2021-05-11 00:10:19 -05:00
UID : result . Result . Elements [ 1 ] . UID ,
2021-04-28 02:18:13 -05:00
Name : "Text - Library Panel" ,
2021-05-23 23:11:01 -05:00
Kind : int64 ( models . PanelElement ) ,
2021-04-28 02:18:13 -05:00
Type : "text" ,
Description : "A description" ,
Model : map [ string ] interface { } {
"datasource" : "${DS_GDEV-TESTDATA}" ,
"description" : "A description" ,
"id" : float64 ( 1 ) ,
"title" : "Text - Library Panel" ,
"type" : "text" ,
} ,
Version : 1 ,
2021-05-11 00:10:19 -05:00
Meta : LibraryElementDTOMeta {
2021-05-12 01:48:17 -05:00
FolderName : "ScenarioFolder" ,
FolderUID : sc . folder . Uid ,
ConnectedDashboards : 0 ,
Created : result . Result . Elements [ 1 ] . Meta . Created ,
Updated : result . Result . Elements [ 1 ] . Meta . Updated ,
2021-05-11 00:10:19 -05:00
CreatedBy : LibraryElementDTOMetaUser {
2021-04-28 02:18:13 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-04-28 02:18:13 -05:00
} ,
2021-05-11 00:10:19 -05:00
UpdatedBy : LibraryElementDTOMetaUser {
2021-04-28 02:18:13 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-04-28 02:18:13 -05:00
} ,
} ,
} ,
} ,
} ,
}
if diff := cmp . Diff ( expected , result , getCompareOptions ( ) ... ) ; diff != "" {
t . Fatalf ( "Result mismatch (-want +got):\n%s" , diff )
}
} )
2021-05-11 00:10:19 -05:00
scenarioWithPanel ( t , "When an admin tries to get all library panels and two exist and typeFilter is set to existing types, it should succeed and the result should be correct" ,
2021-04-28 02:18:13 -05:00
func ( t * testing . T , sc scenarioContext ) {
2021-05-23 23:11:01 -05:00
command := getCreateCommandWithModel ( sc . folder . Id , "Gauge - Library Panel" , models . PanelElement , [ ] byte ( `
2021-04-28 02:18:13 -05:00
{
"datasource" : "${DS_GDEV-TESTDATA}" ,
"id" : 1 ,
"title" : "Gauge - Library Panel" ,
"type" : "gauge" ,
"description" : "Gauge description"
}
` ) )
resp := sc . service . createHandler ( sc . reqContext , command )
require . Equal ( t , 200 , resp . Status ( ) )
2021-05-23 23:11:01 -05:00
command = getCreateCommandWithModel ( sc . folder . Id , "BarGauge - Library Panel" , models . PanelElement , [ ] byte ( `
2021-04-28 02:18:13 -05:00
{
"datasource" : "${DS_GDEV-TESTDATA}" ,
"id" : 1 ,
"title" : "BarGauge - Library Panel" ,
"type" : "bargauge" ,
"description" : "BarGauge description"
}
` ) )
resp = sc . service . createHandler ( sc . reqContext , command )
require . Equal ( t , 200 , resp . Status ( ) )
err := sc . reqContext . Req . ParseForm ( )
require . NoError ( t , err )
2021-05-11 00:10:19 -05:00
sc . reqContext . Req . Form . Add ( "typeFilter" , "bargauge,gauge" )
2021-04-28 02:18:13 -05:00
resp = sc . service . getAllHandler ( sc . reqContext )
require . Equal ( t , 200 , resp . Status ( ) )
2021-05-11 00:10:19 -05:00
var result libraryElementsSearch
2021-04-28 02:18:13 -05:00
err = json . Unmarshal ( resp . Body ( ) , & result )
require . NoError ( t , err )
2021-05-11 00:10:19 -05:00
var expected = libraryElementsSearch {
Result : libraryElementsSearchResult {
2021-04-28 02:18:13 -05:00
TotalCount : 2 ,
Page : 1 ,
PerPage : 100 ,
2021-05-11 00:10:19 -05:00
Elements : [ ] libraryElement {
2021-04-28 02:18:13 -05:00
{
ID : 3 ,
OrgID : 1 ,
FolderID : 1 ,
2021-05-11 00:10:19 -05:00
UID : result . Result . Elements [ 0 ] . UID ,
2021-04-28 02:18:13 -05:00
Name : "BarGauge - Library Panel" ,
2021-05-23 23:11:01 -05:00
Kind : int64 ( models . PanelElement ) ,
2021-04-28 02:18:13 -05:00
Type : "bargauge" ,
Description : "BarGauge description" ,
Model : map [ string ] interface { } {
"datasource" : "${DS_GDEV-TESTDATA}" ,
"description" : "BarGauge description" ,
"id" : float64 ( 1 ) ,
"title" : "BarGauge - Library Panel" ,
"type" : "bargauge" ,
} ,
Version : 1 ,
2021-05-11 00:10:19 -05:00
Meta : LibraryElementDTOMeta {
2021-05-12 01:48:17 -05:00
FolderName : "ScenarioFolder" ,
FolderUID : sc . folder . Uid ,
ConnectedDashboards : 0 ,
Created : result . Result . Elements [ 0 ] . Meta . Created ,
Updated : result . Result . Elements [ 0 ] . Meta . Updated ,
2021-05-11 00:10:19 -05:00
CreatedBy : LibraryElementDTOMetaUser {
2021-04-28 02:18:13 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-04-28 02:18:13 -05:00
} ,
2021-05-11 00:10:19 -05:00
UpdatedBy : LibraryElementDTOMetaUser {
2021-04-28 02:18:13 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-04-28 02:18:13 -05:00
} ,
} ,
} ,
{
ID : 2 ,
OrgID : 1 ,
FolderID : 1 ,
2021-05-11 00:10:19 -05:00
UID : result . Result . Elements [ 1 ] . UID ,
2021-04-28 02:18:13 -05:00
Name : "Gauge - Library Panel" ,
2021-05-23 23:11:01 -05:00
Kind : int64 ( models . PanelElement ) ,
2021-04-28 02:18:13 -05:00
Type : "gauge" ,
Description : "Gauge description" ,
Model : map [ string ] interface { } {
"datasource" : "${DS_GDEV-TESTDATA}" ,
"id" : float64 ( 1 ) ,
"title" : "Gauge - Library Panel" ,
"type" : "gauge" ,
"description" : "Gauge description" ,
} ,
Version : 1 ,
2021-05-11 00:10:19 -05:00
Meta : LibraryElementDTOMeta {
2021-05-12 01:48:17 -05:00
FolderName : "ScenarioFolder" ,
FolderUID : sc . folder . Uid ,
ConnectedDashboards : 0 ,
Created : result . Result . Elements [ 1 ] . Meta . Created ,
Updated : result . Result . Elements [ 1 ] . Meta . Updated ,
2021-05-11 00:10:19 -05:00
CreatedBy : LibraryElementDTOMetaUser {
2021-04-28 02:18:13 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-04-28 02:18:13 -05:00
} ,
2021-05-11 00:10:19 -05:00
UpdatedBy : LibraryElementDTOMetaUser {
2021-04-28 02:18:13 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-04-28 02:18:13 -05:00
} ,
} ,
} ,
} ,
} ,
}
if diff := cmp . Diff ( expected , result , getCompareOptions ( ) ... ) ; diff != "" {
t . Fatalf ( "Result mismatch (-want +got):\n%s" , diff )
}
} )
2021-05-11 00:10:19 -05:00
scenarioWithPanel ( t , "When an admin tries to get all library panels and two exist and typeFilter is set to a nonexistent type, it should succeed and the result should be correct" ,
2021-04-28 02:18:13 -05:00
func ( t * testing . T , sc scenarioContext ) {
2021-05-23 23:11:01 -05:00
command := getCreateCommandWithModel ( sc . folder . Id , "Gauge - Library Panel" , models . PanelElement , [ ] byte ( `
2021-04-28 02:18:13 -05:00
{
"datasource" : "${DS_GDEV-TESTDATA}" ,
"id" : 1 ,
"title" : "Gauge - Library Panel" ,
"type" : "gauge" ,
"description" : "Gauge description"
}
` ) )
resp := sc . service . createHandler ( sc . reqContext , command )
require . Equal ( t , 200 , resp . Status ( ) )
err := sc . reqContext . Req . ParseForm ( )
require . NoError ( t , err )
2021-05-11 00:10:19 -05:00
sc . reqContext . Req . Form . Add ( "typeFilter" , "unknown1,unknown2" )
2021-04-28 02:18:13 -05:00
resp = sc . service . getAllHandler ( sc . reqContext )
require . Equal ( t , 200 , resp . Status ( ) )
2021-05-11 00:10:19 -05:00
var result libraryElementsSearch
2021-04-28 02:18:13 -05:00
err = json . Unmarshal ( resp . Body ( ) , & result )
require . NoError ( t , err )
2021-05-11 00:10:19 -05:00
var expected = libraryElementsSearch {
Result : libraryElementsSearchResult {
TotalCount : 0 ,
Page : 1 ,
PerPage : 100 ,
Elements : [ ] libraryElement { } ,
2021-04-28 02:18:13 -05:00
} ,
}
if diff := cmp . Diff ( expected , result , getCompareOptions ( ) ... ) ; diff != "" {
t . Fatalf ( "Result mismatch (-want +got):\n%s" , diff )
}
} )
2021-05-11 00:10:19 -05:00
scenarioWithPanel ( t , "When an admin tries to get all library panels and two exist and folderFilter is set to existing folders, it should succeed and the result should be correct" ,
2021-05-04 06:59:40 -05:00
func ( t * testing . T , sc scenarioContext ) {
newFolder := createFolderWithACL ( t , sc . sqlStore , "NewFolder" , sc . user , [ ] folderACLItem { } )
2021-05-11 00:10:19 -05:00
command := getCreatePanelCommand ( newFolder . Id , "Text - Library Panel2" )
2021-05-04 06:59:40 -05:00
resp := sc . service . createHandler ( sc . reqContext , command )
require . Equal ( t , 200 , resp . Status ( ) )
folderFilter := strconv . FormatInt ( newFolder . Id , 10 )
err := sc . reqContext . Req . ParseForm ( )
require . NoError ( t , err )
sc . reqContext . Req . Form . Add ( "folderFilter" , folderFilter )
resp = sc . service . getAllHandler ( sc . reqContext )
require . Equal ( t , 200 , resp . Status ( ) )
2021-05-11 00:10:19 -05:00
var result libraryElementsSearch
2021-05-04 06:59:40 -05:00
err = json . Unmarshal ( resp . Body ( ) , & result )
require . NoError ( t , err )
2021-05-11 00:10:19 -05:00
var expected = libraryElementsSearch {
Result : libraryElementsSearchResult {
2021-05-04 06:59:40 -05:00
TotalCount : 1 ,
Page : 1 ,
PerPage : 100 ,
2021-05-11 00:10:19 -05:00
Elements : [ ] libraryElement {
2021-05-04 06:59:40 -05:00
{
ID : 2 ,
OrgID : 1 ,
FolderID : newFolder . Id ,
2021-05-11 00:10:19 -05:00
UID : result . Result . Elements [ 0 ] . UID ,
2021-05-04 06:59:40 -05:00
Name : "Text - Library Panel2" ,
2021-05-23 23:11:01 -05:00
Kind : int64 ( models . PanelElement ) ,
2021-05-04 06:59:40 -05:00
Type : "text" ,
Description : "A description" ,
Model : map [ string ] interface { } {
"datasource" : "${DS_GDEV-TESTDATA}" ,
"description" : "A description" ,
"id" : float64 ( 1 ) ,
"title" : "Text - Library Panel2" ,
"type" : "text" ,
} ,
Version : 1 ,
2021-05-11 00:10:19 -05:00
Meta : LibraryElementDTOMeta {
2021-05-12 01:48:17 -05:00
FolderName : "NewFolder" ,
FolderUID : newFolder . Uid ,
ConnectedDashboards : 0 ,
Created : result . Result . Elements [ 0 ] . Meta . Created ,
Updated : result . Result . Elements [ 0 ] . Meta . Updated ,
2021-05-11 00:10:19 -05:00
CreatedBy : LibraryElementDTOMetaUser {
2021-05-04 06:59:40 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-05-04 06:59:40 -05:00
} ,
2021-05-11 00:10:19 -05:00
UpdatedBy : LibraryElementDTOMetaUser {
2021-05-04 06:59:40 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-05-04 06:59:40 -05:00
} ,
} ,
} ,
} ,
} ,
}
if diff := cmp . Diff ( expected , result , getCompareOptions ( ) ... ) ; diff != "" {
t . Fatalf ( "Result mismatch (-want +got):\n%s" , diff )
}
} )
2021-05-11 00:10:19 -05:00
scenarioWithPanel ( t , "When an admin tries to get all library panels and two exist and folderFilter is set to a nonexistent folders, it should succeed and the result should be correct" ,
2021-05-04 06:59:40 -05:00
func ( t * testing . T , sc scenarioContext ) {
newFolder := createFolderWithACL ( t , sc . sqlStore , "NewFolder" , sc . user , [ ] folderACLItem { } )
2021-05-11 00:10:19 -05:00
command := getCreatePanelCommand ( newFolder . Id , "Text - Library Panel2" )
2021-05-04 06:59:40 -05:00
resp := sc . service . createHandler ( sc . reqContext , command )
require . Equal ( t , 200 , resp . Status ( ) )
folderFilter := "2020,2021"
err := sc . reqContext . Req . ParseForm ( )
require . NoError ( t , err )
sc . reqContext . Req . Form . Add ( "folderFilter" , folderFilter )
resp = sc . service . getAllHandler ( sc . reqContext )
require . Equal ( t , 200 , resp . Status ( ) )
2021-05-11 00:10:19 -05:00
var result libraryElementsSearch
2021-05-04 06:59:40 -05:00
err = json . Unmarshal ( resp . Body ( ) , & result )
require . NoError ( t , err )
2021-05-11 00:10:19 -05:00
var expected = libraryElementsSearch {
Result : libraryElementsSearchResult {
TotalCount : 0 ,
Page : 1 ,
PerPage : 100 ,
Elements : [ ] libraryElement { } ,
2021-05-04 06:59:40 -05:00
} ,
}
if diff := cmp . Diff ( expected , result , getCompareOptions ( ) ... ) ; diff != "" {
t . Fatalf ( "Result mismatch (-want +got):\n%s" , diff )
}
} )
2021-05-11 00:10:19 -05:00
scenarioWithPanel ( t , "When an admin tries to get all library panels and two exist and folderFilter is set to General folder, it should succeed and the result should be correct" ,
2021-05-04 06:59:40 -05:00
func ( t * testing . T , sc scenarioContext ) {
2021-05-11 00:10:19 -05:00
command := getCreatePanelCommand ( sc . folder . Id , "Text - Library Panel2" )
2021-05-04 06:59:40 -05:00
resp := sc . service . createHandler ( sc . reqContext , command )
require . Equal ( t , 200 , resp . Status ( ) )
folderFilter := "0"
err := sc . reqContext . Req . ParseForm ( )
require . NoError ( t , err )
sc . reqContext . Req . Form . Add ( "folderFilter" , folderFilter )
resp = sc . service . getAllHandler ( sc . reqContext )
require . Equal ( t , 200 , resp . Status ( ) )
2021-05-11 00:10:19 -05:00
var result libraryElementsSearch
2021-05-04 06:59:40 -05:00
err = json . Unmarshal ( resp . Body ( ) , & result )
require . NoError ( t , err )
2021-05-11 00:10:19 -05:00
var expected = libraryElementsSearch {
Result : libraryElementsSearchResult {
2021-05-04 06:59:40 -05:00
TotalCount : 0 ,
Page : 1 ,
PerPage : 100 ,
2021-05-11 00:10:19 -05:00
Elements : [ ] libraryElement {
2021-05-04 06:59:40 -05:00
{
ID : 1 ,
OrgID : 1 ,
FolderID : 1 ,
2021-05-11 00:10:19 -05:00
UID : result . Result . Elements [ 0 ] . UID ,
2021-05-04 06:59:40 -05:00
Name : "Text - Library Panel" ,
2021-05-23 23:11:01 -05:00
Kind : int64 ( models . PanelElement ) ,
2021-05-04 06:59:40 -05:00
Type : "text" ,
Description : "A description" ,
Model : map [ string ] interface { } {
"datasource" : "${DS_GDEV-TESTDATA}" ,
"description" : "A description" ,
"id" : float64 ( 1 ) ,
"title" : "Text - Library Panel" ,
"type" : "text" ,
} ,
Version : 1 ,
2021-05-11 00:10:19 -05:00
Meta : LibraryElementDTOMeta {
2021-05-12 01:48:17 -05:00
FolderName : "ScenarioFolder" ,
FolderUID : sc . folder . Uid ,
ConnectedDashboards : 0 ,
Created : result . Result . Elements [ 0 ] . Meta . Created ,
Updated : result . Result . Elements [ 0 ] . Meta . Updated ,
2021-05-11 00:10:19 -05:00
CreatedBy : LibraryElementDTOMetaUser {
2021-05-04 06:59:40 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-05-04 06:59:40 -05:00
} ,
2021-05-11 00:10:19 -05:00
UpdatedBy : LibraryElementDTOMetaUser {
2021-05-04 06:59:40 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-05-04 06:59:40 -05:00
} ,
} ,
} ,
{
ID : 2 ,
OrgID : 1 ,
FolderID : 1 ,
2021-05-11 00:10:19 -05:00
UID : result . Result . Elements [ 1 ] . UID ,
2021-05-04 06:59:40 -05:00
Name : "Text - Library Panel2" ,
2021-05-23 23:11:01 -05:00
Kind : int64 ( models . PanelElement ) ,
2021-05-04 06:59:40 -05:00
Type : "text" ,
Description : "A description" ,
Model : map [ string ] interface { } {
"datasource" : "${DS_GDEV-TESTDATA}" ,
"description" : "A description" ,
"id" : float64 ( 1 ) ,
"title" : "Text - Library Panel2" ,
"type" : "text" ,
} ,
Version : 1 ,
2021-05-11 00:10:19 -05:00
Meta : LibraryElementDTOMeta {
2021-05-12 01:48:17 -05:00
FolderName : "ScenarioFolder" ,
FolderUID : sc . folder . Uid ,
ConnectedDashboards : 0 ,
Created : result . Result . Elements [ 1 ] . Meta . Created ,
Updated : result . Result . Elements [ 1 ] . Meta . Updated ,
2021-05-11 00:10:19 -05:00
CreatedBy : LibraryElementDTOMetaUser {
2021-05-04 06:59:40 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-05-04 06:59:40 -05:00
} ,
2021-05-11 00:10:19 -05:00
UpdatedBy : LibraryElementDTOMetaUser {
2021-05-04 06:59:40 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-05-04 06:59:40 -05:00
} ,
} ,
} ,
} ,
} ,
}
if diff := cmp . Diff ( expected , result , getCompareOptions ( ) ... ) ; diff != "" {
t . Fatalf ( "Result mismatch (-want +got):\n%s" , diff )
}
} )
2021-05-11 00:10:19 -05:00
scenarioWithPanel ( t , "When an admin tries to get all library panels and two exist and excludeUID is set, it should succeed and the result should be correct" ,
2021-03-18 05:19:41 -05:00
func ( t * testing . T , sc scenarioContext ) {
2021-05-11 00:10:19 -05:00
command := getCreatePanelCommand ( sc . folder . Id , "Text - Library Panel2" )
2021-03-18 05:19:41 -05:00
resp := sc . service . createHandler ( sc . reqContext , command )
require . Equal ( t , 200 , resp . Status ( ) )
err := sc . reqContext . Req . ParseForm ( )
require . NoError ( t , err )
sc . reqContext . Req . Form . Add ( "excludeUid" , sc . initialResult . Result . UID )
resp = sc . service . getAllHandler ( sc . reqContext )
require . Equal ( t , 200 , resp . Status ( ) )
2021-05-11 00:10:19 -05:00
var result libraryElementsSearch
2021-03-18 05:19:41 -05:00
err = json . Unmarshal ( resp . Body ( ) , & result )
require . NoError ( t , err )
2021-05-11 00:10:19 -05:00
var expected = libraryElementsSearch {
Result : libraryElementsSearchResult {
2021-03-18 05:19:41 -05:00
TotalCount : 1 ,
Page : 1 ,
PerPage : 100 ,
2021-05-11 00:10:19 -05:00
Elements : [ ] libraryElement {
2021-03-18 05:19:41 -05:00
{
2021-03-24 07:43:51 -05:00
ID : 2 ,
OrgID : 1 ,
FolderID : 1 ,
2021-05-11 00:10:19 -05:00
UID : result . Result . Elements [ 0 ] . UID ,
2021-03-24 07:43:51 -05:00
Name : "Text - Library Panel2" ,
2021-05-23 23:11:01 -05:00
Kind : int64 ( models . PanelElement ) ,
2021-03-24 07:43:51 -05:00
Type : "text" ,
Description : "A description" ,
2021-03-18 05:19:41 -05:00
Model : map [ string ] interface { } {
2021-03-24 07:43:51 -05:00
"datasource" : "${DS_GDEV-TESTDATA}" ,
"description" : "A description" ,
"id" : float64 ( 1 ) ,
"title" : "Text - Library Panel2" ,
"type" : "text" ,
2021-03-18 05:19:41 -05:00
} ,
Version : 1 ,
2021-05-11 00:10:19 -05:00
Meta : LibraryElementDTOMeta {
2021-05-12 01:48:17 -05:00
FolderName : "ScenarioFolder" ,
FolderUID : sc . folder . Uid ,
ConnectedDashboards : 0 ,
Created : result . Result . Elements [ 0 ] . Meta . Created ,
Updated : result . Result . Elements [ 0 ] . Meta . Updated ,
2021-05-11 00:10:19 -05:00
CreatedBy : LibraryElementDTOMetaUser {
2021-03-18 05:19:41 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-03-18 05:19:41 -05:00
} ,
2021-05-11 00:10:19 -05:00
UpdatedBy : LibraryElementDTOMetaUser {
2021-03-18 05:19:41 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-03-18 05:19:41 -05:00
} ,
} ,
2021-03-01 08:33:17 -06:00
} ,
2021-03-18 05:19:41 -05:00
} ,
} ,
}
if diff := cmp . Diff ( expected , result , getCompareOptions ( ) ... ) ; diff != "" {
t . Fatalf ( "Result mismatch (-want +got):\n%s" , diff )
}
} )
2021-05-11 00:10:19 -05:00
scenarioWithPanel ( t , "When an admin tries to get all library panels and two exist and perPage is 1, it should succeed and the result should be correct" ,
2021-03-18 05:19:41 -05:00
func ( t * testing . T , sc scenarioContext ) {
2021-05-11 00:10:19 -05:00
command := getCreatePanelCommand ( sc . folder . Id , "Text - Library Panel2" )
2021-03-18 05:19:41 -05:00
resp := sc . service . createHandler ( sc . reqContext , command )
require . Equal ( t , 200 , resp . Status ( ) )
err := sc . reqContext . Req . ParseForm ( )
require . NoError ( t , err )
sc . reqContext . Req . Form . Add ( "perPage" , "1" )
resp = sc . service . getAllHandler ( sc . reqContext )
require . Equal ( t , 200 , resp . Status ( ) )
2021-05-11 00:10:19 -05:00
var result libraryElementsSearch
2021-03-18 05:19:41 -05:00
err = json . Unmarshal ( resp . Body ( ) , & result )
require . NoError ( t , err )
2021-05-11 00:10:19 -05:00
var expected = libraryElementsSearch {
Result : libraryElementsSearchResult {
2021-03-18 05:19:41 -05:00
TotalCount : 2 ,
Page : 1 ,
PerPage : 1 ,
2021-05-11 00:10:19 -05:00
Elements : [ ] libraryElement {
2021-03-18 05:19:41 -05:00
{
2021-03-24 07:43:51 -05:00
ID : 1 ,
OrgID : 1 ,
FolderID : 1 ,
2021-05-11 00:10:19 -05:00
UID : result . Result . Elements [ 0 ] . UID ,
2021-03-24 07:43:51 -05:00
Name : "Text - Library Panel" ,
2021-05-23 23:11:01 -05:00
Kind : int64 ( models . PanelElement ) ,
2021-03-24 07:43:51 -05:00
Type : "text" ,
Description : "A description" ,
2021-03-18 05:19:41 -05:00
Model : map [ string ] interface { } {
2021-03-24 07:43:51 -05:00
"datasource" : "${DS_GDEV-TESTDATA}" ,
"description" : "A description" ,
"id" : float64 ( 1 ) ,
"title" : "Text - Library Panel" ,
"type" : "text" ,
2021-03-01 08:33:17 -06:00
} ,
2021-03-18 05:19:41 -05:00
Version : 1 ,
2021-05-11 00:10:19 -05:00
Meta : LibraryElementDTOMeta {
2021-05-12 01:48:17 -05:00
FolderName : "ScenarioFolder" ,
FolderUID : sc . folder . Uid ,
ConnectedDashboards : 0 ,
Created : result . Result . Elements [ 0 ] . Meta . Created ,
Updated : result . Result . Elements [ 0 ] . Meta . Updated ,
2021-05-11 00:10:19 -05:00
CreatedBy : LibraryElementDTOMetaUser {
2021-03-18 05:19:41 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-03-18 05:19:41 -05:00
} ,
2021-05-11 00:10:19 -05:00
UpdatedBy : LibraryElementDTOMetaUser {
2021-03-18 05:19:41 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-03-18 05:19:41 -05:00
} ,
2021-03-01 08:33:17 -06:00
} ,
} ,
} ,
} ,
}
if diff := cmp . Diff ( expected , result , getCompareOptions ( ) ... ) ; diff != "" {
t . Fatalf ( "Result mismatch (-want +got):\n%s" , diff )
}
} )
2021-05-11 00:10:19 -05:00
scenarioWithPanel ( t , "When an admin tries to get all library panels and two exist and perPage is 1 and page is 2, it should succeed and the result should be correct" ,
2021-03-18 05:19:41 -05:00
func ( t * testing . T , sc scenarioContext ) {
2021-05-11 00:10:19 -05:00
command := getCreatePanelCommand ( sc . folder . Id , "Text - Library Panel2" )
2021-03-18 05:19:41 -05:00
resp := sc . service . createHandler ( sc . reqContext , command )
require . Equal ( t , 200 , resp . Status ( ) )
err := sc . reqContext . Req . ParseForm ( )
require . NoError ( t , err )
sc . reqContext . Req . Form . Add ( "perPage" , "1" )
sc . reqContext . Req . Form . Add ( "page" , "2" )
resp = sc . service . getAllHandler ( sc . reqContext )
require . Equal ( t , 200 , resp . Status ( ) )
2021-05-11 00:10:19 -05:00
var result libraryElementsSearch
2021-03-18 05:19:41 -05:00
err = json . Unmarshal ( resp . Body ( ) , & result )
require . NoError ( t , err )
2021-05-11 00:10:19 -05:00
var expected = libraryElementsSearch {
Result : libraryElementsSearchResult {
2021-03-18 05:19:41 -05:00
TotalCount : 2 ,
Page : 2 ,
PerPage : 1 ,
2021-05-11 00:10:19 -05:00
Elements : [ ] libraryElement {
2021-03-18 05:19:41 -05:00
{
2021-03-24 07:43:51 -05:00
ID : 2 ,
OrgID : 1 ,
FolderID : 1 ,
2021-05-11 00:10:19 -05:00
UID : result . Result . Elements [ 0 ] . UID ,
2021-03-24 07:43:51 -05:00
Name : "Text - Library Panel2" ,
2021-05-23 23:11:01 -05:00
Kind : int64 ( models . PanelElement ) ,
2021-03-24 07:43:51 -05:00
Type : "text" ,
Description : "A description" ,
2021-03-18 05:19:41 -05:00
Model : map [ string ] interface { } {
2021-03-24 07:43:51 -05:00
"datasource" : "${DS_GDEV-TESTDATA}" ,
"description" : "A description" ,
"id" : float64 ( 1 ) ,
"title" : "Text - Library Panel2" ,
"type" : "text" ,
2021-03-18 05:19:41 -05:00
} ,
Version : 1 ,
2021-05-11 00:10:19 -05:00
Meta : LibraryElementDTOMeta {
2021-05-12 01:48:17 -05:00
FolderName : "ScenarioFolder" ,
FolderUID : sc . folder . Uid ,
ConnectedDashboards : 0 ,
Created : result . Result . Elements [ 0 ] . Meta . Created ,
Updated : result . Result . Elements [ 0 ] . Meta . Updated ,
2021-05-11 00:10:19 -05:00
CreatedBy : LibraryElementDTOMetaUser {
2021-03-18 05:19:41 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-03-18 05:19:41 -05:00
} ,
2021-05-11 00:10:19 -05:00
UpdatedBy : LibraryElementDTOMetaUser {
2021-03-18 05:19:41 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-03-18 05:19:41 -05:00
} ,
} ,
} ,
} ,
} ,
}
if diff := cmp . Diff ( expected , result , getCompareOptions ( ) ... ) ; diff != "" {
t . Fatalf ( "Result mismatch (-want +got):\n%s" , diff )
}
} )
2021-05-11 00:10:19 -05:00
scenarioWithPanel ( t , "When an admin tries to get all library panels and two exist and searchString exists in the description, it should succeed and the result should be correct" ,
2021-04-28 02:18:13 -05:00
func ( t * testing . T , sc scenarioContext ) {
2021-05-23 23:11:01 -05:00
command := getCreateCommandWithModel ( sc . folder . Id , "Text - Library Panel2" , models . PanelElement , [ ] byte ( `
2021-04-28 02:18:13 -05:00
{
"datasource" : "${DS_GDEV-TESTDATA}" ,
"id" : 1 ,
"title" : "Text - Library Panel" ,
"type" : "text" ,
"description" : "Some other d e s c r i p t i o n"
}
` ) )
resp := sc . service . createHandler ( sc . reqContext , command )
require . Equal ( t , 200 , resp . Status ( ) )
err := sc . reqContext . Req . ParseForm ( )
require . NoError ( t , err )
sc . reqContext . Req . Form . Add ( "perPage" , "1" )
sc . reqContext . Req . Form . Add ( "page" , "1" )
sc . reqContext . Req . Form . Add ( "searchString" , "description" )
resp = sc . service . getAllHandler ( sc . reqContext )
require . Equal ( t , 200 , resp . Status ( ) )
2021-05-11 00:10:19 -05:00
var result libraryElementsSearch
2021-04-28 02:18:13 -05:00
err = json . Unmarshal ( resp . Body ( ) , & result )
require . NoError ( t , err )
2021-05-11 00:10:19 -05:00
var expected = libraryElementsSearch {
Result : libraryElementsSearchResult {
2021-04-28 02:18:13 -05:00
TotalCount : 1 ,
Page : 1 ,
PerPage : 1 ,
2021-05-11 00:10:19 -05:00
Elements : [ ] libraryElement {
2021-04-28 02:18:13 -05:00
{
ID : 1 ,
OrgID : 1 ,
FolderID : 1 ,
2021-05-11 00:10:19 -05:00
UID : result . Result . Elements [ 0 ] . UID ,
2021-04-28 02:18:13 -05:00
Name : "Text - Library Panel" ,
2021-05-23 23:11:01 -05:00
Kind : int64 ( models . PanelElement ) ,
2021-04-28 02:18:13 -05:00
Type : "text" ,
Description : "A description" ,
Model : map [ string ] interface { } {
"datasource" : "${DS_GDEV-TESTDATA}" ,
"description" : "A description" ,
"id" : float64 ( 1 ) ,
"title" : "Text - Library Panel" ,
"type" : "text" ,
} ,
Version : 1 ,
2021-05-11 00:10:19 -05:00
Meta : LibraryElementDTOMeta {
2021-05-12 01:48:17 -05:00
FolderName : "ScenarioFolder" ,
FolderUID : sc . folder . Uid ,
ConnectedDashboards : 0 ,
Created : result . Result . Elements [ 0 ] . Meta . Created ,
Updated : result . Result . Elements [ 0 ] . Meta . Updated ,
2021-05-11 00:10:19 -05:00
CreatedBy : LibraryElementDTOMetaUser {
2021-04-28 02:18:13 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-04-28 02:18:13 -05:00
} ,
2021-05-11 00:10:19 -05:00
UpdatedBy : LibraryElementDTOMetaUser {
2021-04-28 02:18:13 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-04-28 02:18:13 -05:00
} ,
} ,
} ,
} ,
} ,
}
if diff := cmp . Diff ( expected , result , getCompareOptions ( ) ... ) ; diff != "" {
t . Fatalf ( "Result mismatch (-want +got):\n%s" , diff )
}
} )
2021-05-11 00:10:19 -05:00
scenarioWithPanel ( t , "When an admin tries to get all library panels and two exist and searchString exists in both name and description, it should succeed and the result should be correct" ,
2021-04-28 02:18:13 -05:00
func ( t * testing . T , sc scenarioContext ) {
2021-05-23 23:11:01 -05:00
command := getCreateCommandWithModel ( sc . folder . Id , "Some Other" , models . PanelElement , [ ] byte ( `
2021-04-28 02:18:13 -05:00
{
"datasource" : "${DS_GDEV-TESTDATA}" ,
"id" : 1 ,
"title" : "Text - Library Panel" ,
"type" : "text" ,
"description" : "A Library Panel"
}
` ) )
resp := sc . service . createHandler ( sc . reqContext , command )
require . Equal ( t , 200 , resp . Status ( ) )
err := sc . reqContext . Req . ParseForm ( )
require . NoError ( t , err )
sc . reqContext . Req . Form . Add ( "searchString" , "Library Panel" )
resp = sc . service . getAllHandler ( sc . reqContext )
require . Equal ( t , 200 , resp . Status ( ) )
2021-05-11 00:10:19 -05:00
var result libraryElementsSearch
2021-04-28 02:18:13 -05:00
err = json . Unmarshal ( resp . Body ( ) , & result )
require . NoError ( t , err )
2021-05-11 00:10:19 -05:00
var expected = libraryElementsSearch {
Result : libraryElementsSearchResult {
2021-04-28 02:18:13 -05:00
TotalCount : 2 ,
Page : 1 ,
PerPage : 100 ,
2021-05-11 00:10:19 -05:00
Elements : [ ] libraryElement {
2021-04-28 02:18:13 -05:00
{
ID : 2 ,
OrgID : 1 ,
FolderID : 1 ,
2021-05-11 00:10:19 -05:00
UID : result . Result . Elements [ 0 ] . UID ,
2021-04-28 02:18:13 -05:00
Name : "Some Other" ,
2021-05-23 23:11:01 -05:00
Kind : int64 ( models . PanelElement ) ,
2021-04-28 02:18:13 -05:00
Type : "text" ,
Description : "A Library Panel" ,
Model : map [ string ] interface { } {
"datasource" : "${DS_GDEV-TESTDATA}" ,
"description" : "A Library Panel" ,
"id" : float64 ( 1 ) ,
"title" : "Some Other" ,
"type" : "text" ,
} ,
Version : 1 ,
2021-05-11 00:10:19 -05:00
Meta : LibraryElementDTOMeta {
2021-05-12 01:48:17 -05:00
FolderName : "ScenarioFolder" ,
FolderUID : sc . folder . Uid ,
ConnectedDashboards : 0 ,
Created : result . Result . Elements [ 0 ] . Meta . Created ,
Updated : result . Result . Elements [ 0 ] . Meta . Updated ,
2021-05-11 00:10:19 -05:00
CreatedBy : LibraryElementDTOMetaUser {
2021-04-28 02:18:13 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-04-28 02:18:13 -05:00
} ,
2021-05-11 00:10:19 -05:00
UpdatedBy : LibraryElementDTOMetaUser {
2021-04-28 02:18:13 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-04-28 02:18:13 -05:00
} ,
} ,
} ,
{
ID : 1 ,
OrgID : 1 ,
FolderID : 1 ,
2021-05-11 00:10:19 -05:00
UID : result . Result . Elements [ 1 ] . UID ,
2021-04-28 02:18:13 -05:00
Name : "Text - Library Panel" ,
2021-05-23 23:11:01 -05:00
Kind : int64 ( models . PanelElement ) ,
2021-04-28 02:18:13 -05:00
Type : "text" ,
Description : "A description" ,
Model : map [ string ] interface { } {
"datasource" : "${DS_GDEV-TESTDATA}" ,
"description" : "A description" ,
"id" : float64 ( 1 ) ,
"title" : "Text - Library Panel" ,
"type" : "text" ,
} ,
Version : 1 ,
2021-05-11 00:10:19 -05:00
Meta : LibraryElementDTOMeta {
2021-05-12 01:48:17 -05:00
FolderName : "ScenarioFolder" ,
FolderUID : sc . folder . Uid ,
ConnectedDashboards : 0 ,
Created : result . Result . Elements [ 1 ] . Meta . Created ,
Updated : result . Result . Elements [ 1 ] . Meta . Updated ,
2021-05-11 00:10:19 -05:00
CreatedBy : LibraryElementDTOMetaUser {
2021-04-28 02:18:13 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-04-28 02:18:13 -05:00
} ,
2021-05-11 00:10:19 -05:00
UpdatedBy : LibraryElementDTOMetaUser {
2021-04-28 02:18:13 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-04-28 02:18:13 -05:00
} ,
} ,
} ,
} ,
} ,
}
if diff := cmp . Diff ( expected , result , getCompareOptions ( ) ... ) ; diff != "" {
t . Fatalf ( "Result mismatch (-want +got):\n%s" , diff )
}
} )
2021-05-11 00:10:19 -05:00
scenarioWithPanel ( t , "When an admin tries to get all library panels and two exist and perPage is 1 and page is 1 and searchString is panel2, it should succeed and the result should be correct" ,
2021-03-18 05:19:41 -05:00
func ( t * testing . T , sc scenarioContext ) {
2021-05-11 00:10:19 -05:00
command := getCreatePanelCommand ( sc . folder . Id , "Text - Library Panel2" )
2021-03-18 05:19:41 -05:00
resp := sc . service . createHandler ( sc . reqContext , command )
require . Equal ( t , 200 , resp . Status ( ) )
err := sc . reqContext . Req . ParseForm ( )
require . NoError ( t , err )
sc . reqContext . Req . Form . Add ( "perPage" , "1" )
sc . reqContext . Req . Form . Add ( "page" , "1" )
2021-04-28 02:18:13 -05:00
sc . reqContext . Req . Form . Add ( "searchString" , "panel2" )
2021-03-18 05:19:41 -05:00
resp = sc . service . getAllHandler ( sc . reqContext )
require . Equal ( t , 200 , resp . Status ( ) )
2021-05-11 00:10:19 -05:00
var result libraryElementsSearch
2021-03-18 05:19:41 -05:00
err = json . Unmarshal ( resp . Body ( ) , & result )
require . NoError ( t , err )
2021-05-11 00:10:19 -05:00
var expected = libraryElementsSearch {
Result : libraryElementsSearchResult {
2021-03-18 05:19:41 -05:00
TotalCount : 1 ,
Page : 1 ,
PerPage : 1 ,
2021-05-11 00:10:19 -05:00
Elements : [ ] libraryElement {
2021-03-18 05:19:41 -05:00
{
2021-03-24 07:43:51 -05:00
ID : 2 ,
OrgID : 1 ,
FolderID : 1 ,
2021-05-11 00:10:19 -05:00
UID : result . Result . Elements [ 0 ] . UID ,
2021-03-24 07:43:51 -05:00
Name : "Text - Library Panel2" ,
2021-05-23 23:11:01 -05:00
Kind : int64 ( models . PanelElement ) ,
2021-03-24 07:43:51 -05:00
Type : "text" ,
Description : "A description" ,
2021-03-18 05:19:41 -05:00
Model : map [ string ] interface { } {
2021-03-24 07:43:51 -05:00
"datasource" : "${DS_GDEV-TESTDATA}" ,
"description" : "A description" ,
"id" : float64 ( 1 ) ,
"title" : "Text - Library Panel2" ,
"type" : "text" ,
2021-03-18 05:19:41 -05:00
} ,
Version : 1 ,
2021-05-11 00:10:19 -05:00
Meta : LibraryElementDTOMeta {
2021-05-12 01:48:17 -05:00
FolderName : "ScenarioFolder" ,
FolderUID : sc . folder . Uid ,
ConnectedDashboards : 0 ,
Created : result . Result . Elements [ 0 ] . Meta . Created ,
Updated : result . Result . Elements [ 0 ] . Meta . Updated ,
2021-05-11 00:10:19 -05:00
CreatedBy : LibraryElementDTOMetaUser {
2021-03-18 05:19:41 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-03-18 05:19:41 -05:00
} ,
2021-05-11 00:10:19 -05:00
UpdatedBy : LibraryElementDTOMetaUser {
2021-03-18 05:19:41 -05:00
ID : 1 ,
2021-05-11 00:10:19 -05:00
Name : userInDbName ,
AvatarURL : userInDbAvatar ,
2021-03-18 05:19:41 -05:00
} ,
} ,
} ,
} ,
} ,
}
if diff := cmp . Diff ( expected , result , getCompareOptions ( ) ... ) ; diff != "" {
t . Fatalf ( "Result mismatch (-want +got):\n%s" , diff )
}
} )
2021-05-11 00:10:19 -05:00
scenarioWithPanel ( t , "When an admin tries to get all library panels and two exist and perPage is 1 and page is 3 and searchString is panel, it should succeed and the result should be correct" ,
2021-03-18 05:19:41 -05:00
func ( t * testing . T , sc scenarioContext ) {
2021-05-11 00:10:19 -05:00
command := getCreatePanelCommand ( sc . folder . Id , "Text - Library Panel2" )
2021-03-18 05:19:41 -05:00
resp := sc . service . createHandler ( sc . reqContext , command )
require . Equal ( t , 200 , resp . Status ( ) )
err := sc . reqContext . Req . ParseForm ( )
require . NoError ( t , err )
sc . reqContext . Req . Form . Add ( "perPage" , "1" )
sc . reqContext . Req . Form . Add ( "page" , "3" )
2021-04-28 02:18:13 -05:00
sc . reqContext . Req . Form . Add ( "searchString" , "panel" )
2021-03-18 05:19:41 -05:00
resp = sc . service . getAllHandler ( sc . reqContext )
require . Equal ( t , 200 , resp . Status ( ) )
2021-05-11 00:10:19 -05:00
var result libraryElementsSearch
2021-03-18 05:19:41 -05:00
err = json . Unmarshal ( resp . Body ( ) , & result )
require . NoError ( t , err )
2021-05-11 00:10:19 -05:00
var expected = libraryElementsSearch {
Result : libraryElementsSearchResult {
TotalCount : 2 ,
Page : 3 ,
PerPage : 1 ,
Elements : [ ] libraryElement { } ,
2021-03-18 05:19:41 -05:00
} ,
}
if diff := cmp . Diff ( expected , result , getCompareOptions ( ) ... ) ; diff != "" {
t . Fatalf ( "Result mismatch (-want +got):\n%s" , diff )
}
} )
2021-05-11 00:10:19 -05:00
scenarioWithPanel ( t , "When an admin tries to get all library panels and two exist and perPage is 1 and page is 3 and searchString does not exist, it should succeed and the result should be correct" ,
2021-03-18 05:19:41 -05:00
func ( t * testing . T , sc scenarioContext ) {
2021-05-11 00:10:19 -05:00
command := getCreatePanelCommand ( sc . folder . Id , "Text - Library Panel2" )
2021-03-18 05:19:41 -05:00
resp := sc . service . createHandler ( sc . reqContext , command )
require . Equal ( t , 200 , resp . Status ( ) )
err := sc . reqContext . Req . ParseForm ( )
require . NoError ( t , err )
sc . reqContext . Req . Form . Add ( "perPage" , "1" )
sc . reqContext . Req . Form . Add ( "page" , "3" )
2021-04-28 02:18:13 -05:00
sc . reqContext . Req . Form . Add ( "searchString" , "monkey" )
2021-03-18 05:19:41 -05:00
resp = sc . service . getAllHandler ( sc . reqContext )
require . Equal ( t , 200 , resp . Status ( ) )
2021-05-11 00:10:19 -05:00
var result libraryElementsSearch
2021-03-18 05:19:41 -05:00
err = json . Unmarshal ( resp . Body ( ) , & result )
require . NoError ( t , err )
2021-05-11 00:10:19 -05:00
var expected = libraryElementsSearch {
Result : libraryElementsSearchResult {
TotalCount : 0 ,
Page : 3 ,
PerPage : 1 ,
Elements : [ ] libraryElement { } ,
2021-03-18 05:19:41 -05:00
} ,
}
if diff := cmp . Diff ( expected , result , getCompareOptions ( ) ... ) ; diff != "" {
t . Fatalf ( "Result mismatch (-want +got):\n%s" , diff )
}
} )
2021-05-11 00:10:19 -05:00
scenarioWithPanel ( t , "When an admin tries to get all library panels in a different org, none should be returned" ,
2021-03-01 08:33:17 -06:00
func ( t * testing . T , sc scenarioContext ) {
resp := sc . service . getAllHandler ( sc . reqContext )
require . Equal ( t , 200 , resp . Status ( ) )
2021-05-11 00:10:19 -05:00
var result libraryElementsSearch
2021-03-01 08:33:17 -06:00
err := json . Unmarshal ( resp . Body ( ) , & result )
require . NoError ( t , err )
2021-05-11 00:10:19 -05:00
require . Equal ( t , 1 , len ( result . Result . Elements ) )
require . Equal ( t , int64 ( 1 ) , result . Result . Elements [ 0 ] . FolderID )
require . Equal ( t , "Text - Library Panel" , result . Result . Elements [ 0 ] . Name )
2021-03-01 08:33:17 -06:00
sc . reqContext . SignedInUser . OrgId = 2
sc . reqContext . SignedInUser . OrgRole = models . ROLE_ADMIN
resp = sc . service . getAllHandler ( sc . reqContext )
require . Equal ( t , 200 , resp . Status ( ) )
2021-05-11 00:10:19 -05:00
result = libraryElementsSearch { }
2021-03-01 08:33:17 -06:00
err = json . Unmarshal ( resp . Body ( ) , & result )
require . NoError ( t , err )
2021-05-11 00:10:19 -05:00
var expected = libraryElementsSearch {
Result : libraryElementsSearchResult {
TotalCount : 0 ,
Elements : [ ] libraryElement { } ,
Page : 1 ,
PerPage : 100 ,
2021-03-18 05:19:41 -05:00
} ,
}
if diff := cmp . Diff ( expected , result , getCompareOptions ( ) ... ) ; diff != "" {
t . Fatalf ( "Result mismatch (-want +got):\n%s" , diff )
}
2021-03-01 08:33:17 -06:00
} )
}