@ -3,6 +3,8 @@ package libraryelements
import (
"testing"
"github.com/grafana/grafana/pkg/util"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
@ -122,6 +124,70 @@ func TestPatchLibraryElement(t *testing.T) {
}
} )
scenarioWithPanel ( t , "When an admin tries to patch a library panel with a nonexistent UID, it should change UID successfully and return correct result" ,
func ( t * testing . T , sc scenarioContext ) {
cmd := patchLibraryElementCommand {
FolderID : - 1 ,
UID : util . GenerateShortUID ( ) ,
Kind : int64 ( models . PanelElement ) ,
Version : 1 ,
}
sc . reqContext . ReplaceAllParams ( map [ string ] string { ":uid" : sc . initialResult . Result . UID } )
resp := sc . service . patchHandler ( sc . reqContext , cmd )
var result = validateAndUnMarshalResponse ( t , resp )
sc . initialResult . Result . UID = cmd . UID
sc . initialResult . Result . Meta . CreatedBy . Name = userInDbName
sc . initialResult . Result . Meta . CreatedBy . AvatarURL = userInDbAvatar
sc . initialResult . Result . Model [ "title" ] = "Text - Library Panel"
sc . initialResult . Result . Version = 2
if diff := cmp . Diff ( sc . initialResult . Result , result . Result , getCompareOptions ( ) ... ) ; diff != "" {
t . Fatalf ( "Result mismatch (-want +got):\n%s" , diff )
}
} )
scenarioWithPanel ( t , "When an admin tries to patch a library panel with an invalid UID, it should fail" ,
func ( t * testing . T , sc scenarioContext ) {
cmd := patchLibraryElementCommand {
FolderID : - 1 ,
UID : "Testing an invalid UID" ,
Kind : int64 ( models . PanelElement ) ,
Version : 1 ,
}
sc . reqContext . ReplaceAllParams ( map [ string ] string { ":uid" : sc . initialResult . Result . UID } )
resp := sc . service . patchHandler ( sc . reqContext , cmd )
require . Equal ( t , 400 , resp . Status ( ) )
} )
scenarioWithPanel ( t , "When an admin tries to patch a library panel with an UID that is too long, it should fail" ,
func ( t * testing . T , sc scenarioContext ) {
cmd := patchLibraryElementCommand {
FolderID : - 1 ,
UID : "j6T00KRZzj6T00KRZzj6T00KRZzj6T00KRZzj6T00K" ,
Kind : int64 ( models . PanelElement ) ,
Version : 1 ,
}
sc . reqContext . ReplaceAllParams ( map [ string ] string { ":uid" : sc . initialResult . Result . UID } )
resp := sc . service . patchHandler ( sc . reqContext , cmd )
require . Equal ( t , 400 , resp . Status ( ) )
} )
scenarioWithPanel ( t , "When an admin tries to patch a library panel with an existing UID, it should fail" ,
func ( t * testing . T , sc scenarioContext ) {
command := getCreatePanelCommand ( sc . folder . Id , "Existing UID" )
command . UID = util . GenerateShortUID ( )
resp := sc . service . createHandler ( sc . reqContext , command )
require . Equal ( t , 200 , resp . Status ( ) )
cmd := patchLibraryElementCommand {
FolderID : - 1 ,
UID : command . UID ,
Kind : int64 ( models . PanelElement ) ,
Version : 1 ,
}
sc . reqContext . ReplaceAllParams ( map [ string ] string { ":uid" : sc . initialResult . Result . UID } )
resp = sc . service . patchHandler ( sc . reqContext , cmd )
require . Equal ( t , 400 , resp . Status ( ) )
} )
scenarioWithPanel ( t , "When an admin tries to patch a library panel with model only, it should change model successfully, sync type and description fields and return correct result" ,
func ( t * testing . T , sc scenarioContext ) {
cmd := patchLibraryElementCommand {