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