2021-05-11 13:10:19 +08:00
package libraryelements
2021-03-01 22:33:17 +08:00
import (
"testing"
2023-01-30 12:14:12 +08:00
"github.com/grafana/grafana/pkg/kinds/librarypanel"
2023-02-02 00:32:05 +08:00
"github.com/grafana/grafana/pkg/services/libraryelements/model"
2021-09-10 17:22:13 +08:00
"github.com/grafana/grafana/pkg/util"
2021-03-01 22:33:17 +08:00
"github.com/google/go-cmp/cmp"
2021-10-11 20:30:59 +08:00
"github.com/grafana/grafana/pkg/web"
"github.com/stretchr/testify/require"
2021-03-01 22:33:17 +08:00
)
2021-05-11 13:10:19 +08:00
func TestPatchLibraryElement ( t * testing . T ) {
scenarioWithPanel ( t , "When an admin tries to patch a library panel that does not exist, it should fail" ,
2021-03-01 22:33:17 +08:00
func ( t * testing . T , sc scenarioContext ) {
2023-02-02 00:32:05 +08:00
cmd := model . PatchLibraryElementCommand { Kind : int64 ( model . PanelElement ) , Version : 1 }
2021-10-11 20:30:59 +08:00
sc . ctx . Req = web . SetURLParams ( sc . ctx . Req , map [ string ] string { ":uid" : "unknown" } )
2021-11-29 17:18:01 +08:00
sc . reqContext . Req . Body = mockRequestBody ( cmd )
resp := sc . service . patchHandler ( sc . reqContext )
2021-03-01 22:33:17 +08:00
require . Equal ( t , 404 , resp . Status ( ) )
} )
2021-05-11 13:10:19 +08:00
scenarioWithPanel ( t , "When an admin tries to patch a library panel that exists, it should succeed" ,
2021-03-01 22:33:17 +08:00
func ( t * testing . T , sc scenarioContext ) {
2023-04-20 17:24:41 +08:00
newFolder := createFolder ( t , sc , "NewFolder" )
2023-02-02 00:32:05 +08:00
cmd := model . PatchLibraryElementCommand {
2024-03-02 00:16:43 +08:00
FolderID : newFolder . ID , // nolint:staticcheck
Name : "Panel - New name" ,
2021-03-01 22:33:17 +08:00
Model : [ ] byte ( `
{
"datasource" : "${DS_GDEV-TESTDATA}" ,
2021-03-24 20:43:51 +08:00
"description" : "An updated description" ,
2021-03-01 22:33:17 +08:00
"id" : 1 ,
"title" : "Model - New name" ,
2021-03-24 20:43:51 +08:00
"type" : "graph"
2021-03-01 22:33:17 +08:00
}
` ) ,
2023-02-02 00:32:05 +08:00
Kind : int64 ( model . PanelElement ) ,
2021-03-02 20:33:26 +08:00
Version : 1 ,
2021-03-01 22:33:17 +08:00
}
2021-10-11 20:30:59 +08:00
sc . ctx . Req = web . SetURLParams ( sc . ctx . Req , map [ string ] string { ":uid" : sc . initialResult . Result . UID } )
2021-11-29 17:18:01 +08:00
sc . reqContext . Req . Body = mockRequestBody ( cmd )
resp := sc . service . patchHandler ( sc . reqContext )
2021-03-01 22:33:17 +08:00
require . Equal ( t , 200 , resp . Status ( ) )
var result = validateAndUnMarshalResponse ( t , resp )
2021-05-11 13:10:19 +08:00
var expected = libraryElementResult {
Result : libraryElement {
2021-03-24 20:43:51 +08:00
ID : 1 ,
OrgID : 1 ,
2023-11-07 00:31:15 +08:00
FolderID : newFolder . ID , // nolint:staticcheck
2021-03-24 20:43:51 +08:00
UID : sc . initialResult . Result . UID ,
Name : "Panel - New name" ,
2023-02-02 00:32:05 +08:00
Kind : int64 ( model . PanelElement ) ,
2021-03-24 20:43:51 +08:00
Type : "graph" ,
Description : "An updated description" ,
2023-08-30 23:46:47 +08:00
Model : map [ string ] any {
2021-03-24 20:43:51 +08:00
"datasource" : "${DS_GDEV-TESTDATA}" ,
"description" : "An updated description" ,
"id" : float64 ( 1 ) ,
2021-09-01 19:27:43 +08:00
"title" : "Model - New name" ,
2021-03-24 20:43:51 +08:00
"type" : "graph" ,
2021-03-01 22:33:17 +08:00
} ,
2021-03-02 20:33:26 +08:00
Version : 2 ,
2023-02-02 00:32:05 +08:00
Meta : model . LibraryElementDTOMeta {
2022-05-05 16:04:54 +08:00
FolderName : "NewFolder" ,
FolderUID : "NewFolder" ,
2021-05-12 14:48:17 +08:00
ConnectedDashboards : 0 ,
Created : sc . initialResult . Result . Meta . Created ,
Updated : result . Result . Meta . Updated ,
2023-01-30 12:14:12 +08:00
CreatedBy : librarypanel . LibraryElementDTOMetaUser {
Id : 1 ,
2021-05-11 13:10:19 +08:00
Name : userInDbName ,
2023-01-30 12:14:12 +08:00
AvatarUrl : userInDbAvatar ,
2021-03-01 22:33:17 +08:00
} ,
2023-01-30 12:14:12 +08:00
UpdatedBy : librarypanel . LibraryElementDTOMetaUser {
Id : 1 ,
2021-03-01 22:33:17 +08:00
Name : "signed_in_user" ,
2023-01-30 12:14:12 +08:00
AvatarUrl : "/avatar/37524e1eb8b3e32850b57db0a19af93b" ,
2021-03-01 22:33:17 +08:00
} ,
} ,
} ,
}
if diff := cmp . Diff ( expected , result , getCompareOptions ( ) ... ) ; diff != "" {
t . Fatalf ( "Result mismatch (-want +got):\n%s" , diff )
}
} )
2021-05-11 13:10:19 +08:00
scenarioWithPanel ( t , "When an admin tries to patch a library panel with folder only, it should change folder successfully and return correct result" ,
2021-03-01 22:33:17 +08:00
func ( t * testing . T , sc scenarioContext ) {
2023-04-20 17:24:41 +08:00
newFolder := createFolder ( t , sc , "NewFolder" )
2023-02-02 00:32:05 +08:00
cmd := model . PatchLibraryElementCommand {
2024-03-02 00:16:43 +08:00
FolderID : newFolder . ID , // nolint:staticcheck
Kind : int64 ( model . PanelElement ) ,
Version : 1 ,
2021-03-01 22:33:17 +08:00
}
2021-10-11 20:30:59 +08:00
sc . ctx . Req = web . SetURLParams ( sc . ctx . Req , map [ string ] string { ":uid" : sc . initialResult . Result . UID } )
2021-11-29 17:18:01 +08:00
sc . reqContext . Req . Body = mockRequestBody ( cmd )
resp := sc . service . patchHandler ( sc . reqContext )
2021-03-01 22:33:17 +08:00
require . Equal ( t , 200 , resp . Status ( ) )
var result = validateAndUnMarshalResponse ( t , resp )
2023-11-07 00:31:15 +08:00
// nolint:staticcheck
2022-11-10 17:41:03 +08:00
sc . initialResult . Result . FolderID = newFolder . ID
2021-05-11 13:10:19 +08:00
sc . initialResult . Result . Meta . CreatedBy . Name = userInDbName
2023-01-30 12:14:12 +08:00
sc . initialResult . Result . Meta . CreatedBy . AvatarUrl = userInDbAvatar
2021-10-22 15:19:25 +08:00
sc . initialResult . Result . Meta . Updated = result . Result . Meta . Updated
2021-03-02 20:33:26 +08:00
sc . initialResult . Result . Version = 2
2022-05-05 16:04:54 +08:00
sc . initialResult . Result . Meta . FolderName = "NewFolder"
sc . initialResult . Result . Meta . FolderUID = "NewFolder"
2021-03-01 22:33:17 +08:00
if diff := cmp . Diff ( sc . initialResult . Result , result . Result , getCompareOptions ( ) ... ) ; diff != "" {
t . Fatalf ( "Result mismatch (-want +got):\n%s" , diff )
}
} )
2021-09-01 19:27:43 +08:00
scenarioWithPanel ( t , "When an admin tries to patch a library panel with name only, it should change name successfully and return correct result" ,
2021-03-01 22:33:17 +08:00
func ( t * testing . T , sc scenarioContext ) {
2023-02-02 00:32:05 +08:00
cmd := model . PatchLibraryElementCommand {
2023-11-07 00:31:01 +08:00
FolderID : - 1 , // nolint:staticcheck
2021-03-01 22:33:17 +08:00
Name : "New Name" ,
2023-02-02 00:32:05 +08:00
Kind : int64 ( model . PanelElement ) ,
2021-03-02 20:33:26 +08:00
Version : 1 ,
2021-03-01 22:33:17 +08:00
}
2021-10-11 20:30:59 +08:00
sc . ctx . Req = web . SetURLParams ( sc . ctx . Req , map [ string ] string { ":uid" : sc . initialResult . Result . UID } )
2021-11-29 17:18:01 +08:00
sc . ctx . Req . Body = mockRequestBody ( cmd )
resp := sc . service . patchHandler ( sc . reqContext )
2021-03-01 22:33:17 +08:00
var result = validateAndUnMarshalResponse ( t , resp )
sc . initialResult . Result . Name = "New Name"
2021-05-11 13:10:19 +08:00
sc . initialResult . Result . Meta . CreatedBy . Name = userInDbName
2023-01-30 12:14:12 +08:00
sc . initialResult . Result . Meta . CreatedBy . AvatarUrl = userInDbAvatar
2021-10-22 15:19:25 +08:00
sc . initialResult . Result . Meta . Updated = result . Result . Meta . Updated
2021-09-01 19:27:43 +08:00
sc . initialResult . Result . Model [ "title" ] = "Text - Library Panel"
2021-03-02 20:33:26 +08:00
sc . initialResult . Result . Version = 2
2021-03-01 22:33:17 +08:00
if diff := cmp . Diff ( sc . initialResult . Result , result . Result , getCompareOptions ( ) ... ) ; diff != "" {
t . Fatalf ( "Result mismatch (-want +got):\n%s" , diff )
}
} )
2021-09-10 17:22:13 +08:00
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 ) {
2023-02-02 00:32:05 +08:00
cmd := model . PatchLibraryElementCommand {
2023-11-07 00:31:01 +08:00
FolderID : - 1 , // nolint:staticcheck
2021-09-10 17:22:13 +08:00
UID : util . GenerateShortUID ( ) ,
2023-02-02 00:32:05 +08:00
Kind : int64 ( model . PanelElement ) ,
2021-09-10 17:22:13 +08:00
Version : 1 ,
}
2021-10-11 20:30:59 +08:00
sc . ctx . Req = web . SetURLParams ( sc . ctx . Req , map [ string ] string { ":uid" : sc . initialResult . Result . UID } )
2021-11-29 17:18:01 +08:00
sc . ctx . Req . Body = mockRequestBody ( cmd )
resp := sc . service . patchHandler ( sc . reqContext )
2021-09-10 17:22:13 +08:00
var result = validateAndUnMarshalResponse ( t , resp )
sc . initialResult . Result . UID = cmd . UID
sc . initialResult . Result . Meta . CreatedBy . Name = userInDbName
2023-01-30 12:14:12 +08:00
sc . initialResult . Result . Meta . CreatedBy . AvatarUrl = userInDbAvatar
2021-10-22 15:19:25 +08:00
sc . initialResult . Result . Meta . Updated = result . Result . Meta . Updated
2021-09-10 17:22:13 +08:00
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 ) {
2023-02-02 00:32:05 +08:00
cmd := model . PatchLibraryElementCommand {
2023-11-07 00:31:01 +08:00
FolderID : - 1 , // nolint:staticcheck
2021-09-10 17:22:13 +08:00
UID : "Testing an invalid UID" ,
2023-02-02 00:32:05 +08:00
Kind : int64 ( model . PanelElement ) ,
2021-09-10 17:22:13 +08:00
Version : 1 ,
}
2021-10-11 20:30:59 +08:00
sc . ctx . Req = web . SetURLParams ( sc . ctx . Req , map [ string ] string { ":uid" : sc . initialResult . Result . UID } )
2021-11-29 17:18:01 +08:00
sc . ctx . Req . Body = mockRequestBody ( cmd )
resp := sc . service . patchHandler ( sc . reqContext )
2021-09-10 17:22:13 +08:00
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 ) {
2023-02-02 00:32:05 +08:00
cmd := model . PatchLibraryElementCommand {
2024-03-02 00:16:43 +08:00
FolderID : - 1 , // nolint:staticcheck
UID : "j6T00KRZzj6T00KRZzj6T00KRZzj6T00KRZzj6T00K" ,
Kind : int64 ( model . PanelElement ) ,
Version : 1 ,
2021-09-10 17:22:13 +08:00
}
2021-10-11 20:30:59 +08:00
sc . ctx . Req = web . SetURLParams ( sc . ctx . Req , map [ string ] string { ":uid" : sc . initialResult . Result . UID } )
2021-11-29 17:18:01 +08:00
sc . ctx . Req . Body = mockRequestBody ( cmd )
resp := sc . service . patchHandler ( sc . reqContext )
2021-09-10 17:22:13 +08:00
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 ) {
2023-11-21 04:44:51 +08:00
// nolint:staticcheck
2024-03-02 00:16:43 +08:00
command := getCreatePanelCommand ( sc . folder . ID , "Existing UID" )
2021-09-10 17:22:13 +08:00
command . UID = util . GenerateShortUID ( )
2021-11-29 17:18:01 +08:00
sc . reqContext . Req . Body = mockRequestBody ( command )
resp := sc . service . createHandler ( sc . reqContext )
2021-09-10 17:22:13 +08:00
require . Equal ( t , 200 , resp . Status ( ) )
2023-02-02 00:32:05 +08:00
cmd := model . PatchLibraryElementCommand {
2024-03-02 00:16:43 +08:00
FolderID : - 1 , // nolint:staticcheck
UID : command . UID ,
Kind : int64 ( model . PanelElement ) ,
Version : 1 ,
2021-09-10 17:22:13 +08:00
}
2021-10-11 20:30:59 +08:00
sc . ctx . Req = web . SetURLParams ( sc . ctx . Req , map [ string ] string { ":uid" : sc . initialResult . Result . UID } )
2021-11-29 17:18:01 +08:00
sc . ctx . Req . Body = mockRequestBody ( cmd )
resp = sc . service . patchHandler ( sc . reqContext )
2021-09-10 17:22:13 +08:00
require . Equal ( t , 400 , resp . Status ( ) )
} )
2021-09-01 19:27:43 +08:00
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" ,
2021-03-01 22:33:17 +08:00
func ( t * testing . T , sc scenarioContext ) {
2023-02-02 00:32:05 +08:00
cmd := model . PatchLibraryElementCommand {
2023-11-07 00:31:01 +08:00
FolderID : - 1 , // nolint:staticcheck
2021-03-24 20:43:51 +08:00
Model : [ ] byte ( ` { "title": "New Model Title", "name": "New Model Name", "type":"graph", "description": "New description" } ` ) ,
2023-02-02 00:32:05 +08:00
Kind : int64 ( model . PanelElement ) ,
2021-03-02 20:33:26 +08:00
Version : 1 ,
2021-03-01 22:33:17 +08:00
}
2021-10-11 20:30:59 +08:00
sc . ctx . Req = web . SetURLParams ( sc . ctx . Req , map [ string ] string { ":uid" : sc . initialResult . Result . UID } )
2021-11-29 17:18:01 +08:00
sc . ctx . Req . Body = mockRequestBody ( cmd )
resp := sc . service . patchHandler ( sc . reqContext )
2021-03-01 22:33:17 +08:00
var result = validateAndUnMarshalResponse ( t , resp )
2021-03-24 20:43:51 +08:00
sc . initialResult . Result . Type = "graph"
sc . initialResult . Result . Description = "New description"
2023-08-30 23:46:47 +08:00
sc . initialResult . Result . Model = map [ string ] any {
2021-09-01 19:27:43 +08:00
"title" : "New Model Title" ,
2021-03-24 20:43:51 +08:00
"name" : "New Model Name" ,
"type" : "graph" ,
"description" : "New description" ,
2021-03-01 22:33:17 +08:00
}
2021-05-11 13:10:19 +08:00
sc . initialResult . Result . Meta . CreatedBy . Name = userInDbName
2023-01-30 12:14:12 +08:00
sc . initialResult . Result . Meta . CreatedBy . AvatarUrl = userInDbAvatar
2021-10-22 15:19:25 +08:00
sc . initialResult . Result . Meta . Updated = result . Result . Meta . Updated
2021-03-24 20:43:51 +08:00
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 )
}
} )
2021-09-01 19:27:43 +08:00
scenarioWithPanel ( t , "When an admin tries to patch a library panel with model.description only, it should change model successfully, sync type and description fields and return correct result" ,
2021-03-24 20:43:51 +08:00
func ( t * testing . T , sc scenarioContext ) {
2023-02-02 00:32:05 +08:00
cmd := model . PatchLibraryElementCommand {
2023-11-07 00:31:01 +08:00
FolderID : - 1 , // nolint:staticcheck
2021-03-24 20:43:51 +08:00
Model : [ ] byte ( ` { "description": "New description" } ` ) ,
2023-02-02 00:32:05 +08:00
Kind : int64 ( model . PanelElement ) ,
2021-03-24 20:43:51 +08:00
Version : 1 ,
}
2021-10-11 20:30:59 +08:00
sc . ctx . Req = web . SetURLParams ( sc . ctx . Req , map [ string ] string { ":uid" : sc . initialResult . Result . UID } )
2021-11-29 17:18:01 +08:00
sc . ctx . Req . Body = mockRequestBody ( cmd )
resp := sc . service . patchHandler ( sc . reqContext )
2021-03-24 20:43:51 +08:00
var result = validateAndUnMarshalResponse ( t , resp )
sc . initialResult . Result . Type = "text"
sc . initialResult . Result . Description = "New description"
2023-08-30 23:46:47 +08:00
sc . initialResult . Result . Model = map [ string ] any {
2021-03-24 20:43:51 +08:00
"type" : "text" ,
"description" : "New description" ,
}
2021-05-11 13:10:19 +08:00
sc . initialResult . Result . Meta . CreatedBy . Name = userInDbName
2023-01-30 12:14:12 +08:00
sc . initialResult . Result . Meta . CreatedBy . AvatarUrl = userInDbAvatar
2021-10-22 15:19:25 +08:00
sc . initialResult . Result . Meta . Updated = result . Result . Meta . Updated
2021-03-24 20:43:51 +08:00
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 )
}
} )
2021-09-01 19:27:43 +08:00
scenarioWithPanel ( t , "When an admin tries to patch a library panel with model.type only, it should change model successfully, sync type and description fields and return correct result" ,
2021-03-24 20:43:51 +08:00
func ( t * testing . T , sc scenarioContext ) {
2023-02-02 00:32:05 +08:00
cmd := model . PatchLibraryElementCommand {
2023-11-07 00:31:01 +08:00
FolderID : - 1 , // nolint:staticcheck
2021-03-24 20:43:51 +08:00
Model : [ ] byte ( ` { "type": "graph" } ` ) ,
2023-02-02 00:32:05 +08:00
Kind : int64 ( model . PanelElement ) ,
2021-03-24 20:43:51 +08:00
Version : 1 ,
}
2021-10-11 20:30:59 +08:00
sc . ctx . Req = web . SetURLParams ( sc . ctx . Req , map [ string ] string { ":uid" : sc . initialResult . Result . UID } )
2021-11-29 17:18:01 +08:00
sc . ctx . Req . Body = mockRequestBody ( cmd )
resp := sc . service . patchHandler ( sc . reqContext )
2021-03-24 20:43:51 +08:00
var result = validateAndUnMarshalResponse ( t , resp )
sc . initialResult . Result . Type = "graph"
sc . initialResult . Result . Description = "A description"
2023-08-30 23:46:47 +08:00
sc . initialResult . Result . Model = map [ string ] any {
2021-03-24 20:43:51 +08:00
"type" : "graph" ,
"description" : "A description" ,
}
2021-05-11 13:10:19 +08:00
sc . initialResult . Result . Meta . CreatedBy . Name = userInDbName
2023-01-30 12:14:12 +08:00
sc . initialResult . Result . Meta . CreatedBy . AvatarUrl = userInDbAvatar
2021-10-22 15:19:25 +08:00
sc . initialResult . Result . Meta . Updated = result . Result . Meta . Updated
2021-03-02 20:33:26 +08:00
sc . initialResult . Result . Version = 2
2021-03-01 22:33:17 +08:00
if diff := cmp . Diff ( sc . initialResult . Result , result . Result , getCompareOptions ( ) ... ) ; diff != "" {
t . Fatalf ( "Result mismatch (-want +got):\n%s" , diff )
}
} )
2021-10-22 15:19:25 +08:00
scenarioWithPanel ( t , "When another admin tries to patch a library panel, it should change UpdatedBy successfully and return correct result" ,
func ( t * testing . T , sc scenarioContext ) {
2023-11-07 00:31:01 +08:00
// nolint:staticcheck
2023-02-02 00:32:05 +08:00
cmd := model . PatchLibraryElementCommand { FolderID : - 1 , Version : 1 , Kind : int64 ( model . PanelElement ) }
2022-08-11 19:28:55 +08:00
sc . reqContext . UserID = 2
2021-10-22 15:19:25 +08:00
sc . ctx . Req = web . SetURLParams ( sc . ctx . Req , map [ string ] string { ":uid" : sc . initialResult . Result . UID } )
2021-11-29 17:18:01 +08:00
sc . ctx . Req . Body = mockRequestBody ( cmd )
resp := sc . service . patchHandler ( sc . reqContext )
2021-10-22 15:19:25 +08:00
var result = validateAndUnMarshalResponse ( t , resp )
2023-01-30 12:14:12 +08:00
sc . initialResult . Result . Meta . UpdatedBy . Id = int64 ( 2 )
2021-10-22 15:19:25 +08:00
sc . initialResult . Result . Meta . CreatedBy . Name = userInDbName
2023-01-30 12:14:12 +08:00
sc . initialResult . Result . Meta . CreatedBy . AvatarUrl = userInDbAvatar
2021-10-22 15:19:25 +08:00
sc . initialResult . Result . Meta . Updated = result . Result . Meta . Updated
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 )
}
} )
2021-03-01 22:33:17 +08:00
2021-05-11 13:10:19 +08:00
scenarioWithPanel ( t , "When an admin tries to patch a library panel with a name that already exists, it should fail" ,
2021-03-01 22:33:17 +08:00
func ( t * testing . T , sc scenarioContext ) {
2023-11-21 04:44:51 +08:00
// nolint:staticcheck
2024-03-02 00:16:43 +08:00
command := getCreatePanelCommand ( sc . folder . ID , "Another Panel" )
2021-11-29 17:18:01 +08:00
sc . ctx . Req . Body = mockRequestBody ( command )
resp := sc . service . createHandler ( sc . reqContext )
2021-03-01 22:33:17 +08:00
var result = validateAndUnMarshalResponse ( t , resp )
2023-02-02 00:32:05 +08:00
cmd := model . PatchLibraryElementCommand {
2021-03-02 20:33:26 +08:00
Name : "Text - Library Panel" ,
Version : 1 ,
2023-02-02 00:32:05 +08:00
Kind : int64 ( model . PanelElement ) ,
2021-03-01 22:33:17 +08:00
}
2021-10-11 20:30:59 +08:00
sc . ctx . Req = web . SetURLParams ( sc . ctx . Req , map [ string ] string { ":uid" : result . Result . UID } )
2021-11-29 17:18:01 +08:00
sc . ctx . Req . Body = mockRequestBody ( cmd )
resp = sc . service . patchHandler ( sc . reqContext )
2021-03-01 22:33:17 +08:00
require . Equal ( t , 400 , resp . Status ( ) )
} )
2021-05-11 13:10:19 +08:00
scenarioWithPanel ( t , "When an admin tries to patch a library panel with a folder where a library panel with the same name already exists, it should fail" ,
2021-03-01 22:33:17 +08:00
func ( t * testing . T , sc scenarioContext ) {
2023-04-20 17:24:41 +08:00
newFolder := createFolder ( t , sc , "NewFolder" )
2023-11-21 04:44:51 +08:00
// nolint:staticcheck
2024-03-02 00:16:43 +08:00
command := getCreatePanelCommand ( newFolder . ID , "Text - Library Panel" )
2021-11-29 17:18:01 +08:00
sc . ctx . Req . Body = mockRequestBody ( command )
resp := sc . service . createHandler ( sc . reqContext )
2021-03-01 22:33:17 +08:00
var result = validateAndUnMarshalResponse ( t , resp )
2023-02-02 00:32:05 +08:00
cmd := model . PatchLibraryElementCommand {
2024-03-02 00:16:43 +08:00
FolderID : 1 , // nolint:staticcheck
Version : 1 ,
Kind : int64 ( model . PanelElement ) ,
2021-03-01 22:33:17 +08:00
}
2021-10-11 20:30:59 +08:00
sc . ctx . Req = web . SetURLParams ( sc . ctx . Req , map [ string ] string { ":uid" : result . Result . UID } )
2021-11-29 17:18:01 +08:00
sc . ctx . Req . Body = mockRequestBody ( cmd )
resp = sc . service . patchHandler ( sc . reqContext )
2021-03-01 22:33:17 +08:00
require . Equal ( t , 400 , resp . Status ( ) )
} )
2021-05-11 13:10:19 +08:00
scenarioWithPanel ( t , "When an admin tries to patch a library panel in another org, it should fail" ,
2021-03-01 22:33:17 +08:00
func ( t * testing . T , sc scenarioContext ) {
2023-02-02 00:32:05 +08:00
cmd := model . PatchLibraryElementCommand {
2024-03-02 00:16:43 +08:00
FolderID : sc . folder . ID , // nolint:staticcheck
Version : 1 ,
Kind : int64 ( model . PanelElement ) ,
2021-03-01 22:33:17 +08:00
}
2022-08-11 19:28:55 +08:00
sc . reqContext . OrgID = 2
2021-10-11 20:30:59 +08:00
sc . ctx . Req = web . SetURLParams ( sc . ctx . Req , map [ string ] string { ":uid" : sc . initialResult . Result . UID } )
2021-11-29 17:18:01 +08:00
sc . ctx . Req . Body = mockRequestBody ( cmd )
resp := sc . service . patchHandler ( sc . reqContext )
2024-03-02 00:16:43 +08:00
require . Equal ( t , 404 , resp . Status ( ) )
2021-03-01 22:33:17 +08:00
} )
2021-03-02 20:33:26 +08:00
2021-05-11 13:10:19 +08:00
scenarioWithPanel ( t , "When an admin tries to patch a library panel with an old version number, it should fail" ,
2021-03-02 20:33:26 +08:00
func ( t * testing . T , sc scenarioContext ) {
2023-02-02 00:32:05 +08:00
cmd := model . PatchLibraryElementCommand {
2024-03-02 00:16:43 +08:00
FolderID : sc . folder . ID , // nolint:staticcheck
Version : 1 ,
Kind : int64 ( model . PanelElement ) ,
2021-03-02 20:33:26 +08:00
}
2021-10-11 20:30:59 +08:00
sc . ctx . Req = web . SetURLParams ( sc . ctx . Req , map [ string ] string { ":uid" : sc . initialResult . Result . UID } )
2021-11-29 17:18:01 +08:00
sc . ctx . Req . Body = mockRequestBody ( cmd )
resp := sc . service . patchHandler ( sc . reqContext )
2021-03-02 20:33:26 +08:00
require . Equal ( t , 200 , resp . Status ( ) )
2021-11-29 17:18:01 +08:00
sc . ctx . Req . Body = mockRequestBody ( cmd )
resp = sc . service . patchHandler ( sc . reqContext )
2021-03-02 20:33:26 +08:00
require . Equal ( t , 412 , resp . Status ( ) )
} )
2021-05-11 13:10:19 +08:00
scenarioWithPanel ( t , "When an admin tries to patch a library panel with an other kind, it should succeed but panel should not change" ,
func ( t * testing . T , sc scenarioContext ) {
2023-02-02 00:32:05 +08:00
cmd := model . PatchLibraryElementCommand {
2024-03-02 00:16:43 +08:00
FolderID : sc . folder . ID , // nolint:staticcheck
Version : 1 ,
Kind : int64 ( model . VariableElement ) ,
2021-05-11 13:10:19 +08:00
}
2021-10-11 20:30:59 +08:00
sc . ctx . Req = web . SetURLParams ( sc . ctx . Req , map [ string ] string { ":uid" : sc . initialResult . Result . UID } )
2021-11-29 17:18:01 +08:00
sc . ctx . Req . Body = mockRequestBody ( cmd )
resp := sc . service . patchHandler ( sc . reqContext )
2021-05-11 13:10:19 +08:00
require . Equal ( t , 200 , resp . Status ( ) )
var result = validateAndUnMarshalResponse ( t , resp )
sc . initialResult . Result . Type = "text"
2023-02-02 00:32:05 +08:00
sc . initialResult . Result . Kind = int64 ( model . PanelElement )
2021-05-11 13:10:19 +08:00
sc . initialResult . Result . Description = "A description"
2023-08-30 23:46:47 +08:00
sc . initialResult . Result . Model = map [ string ] any {
2021-05-11 13:10:19 +08:00
"datasource" : "${DS_GDEV-TESTDATA}" ,
"id" : float64 ( 1 ) ,
"title" : "Text - Library Panel" ,
"type" : "text" ,
"description" : "A description" ,
}
sc . initialResult . Result . Meta . CreatedBy . Name = userInDbName
2023-01-30 12:14:12 +08:00
sc . initialResult . Result . Meta . CreatedBy . AvatarUrl = userInDbAvatar
2021-10-22 15:19:25 +08:00
sc . initialResult . Result . Meta . Updated = result . Result . Meta . Updated
2021-05-11 13:10:19 +08:00
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 )
}
} )
2021-03-01 22:33:17 +08:00
}