| 
									
										
										
										
											2021-05-11 13:10:19 +08:00
										 |  |  | package libraryelements | 
					
						
							| 
									
										
										
										
											2021-03-01 22:33:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/google/go-cmp/cmp" | 
					
						
							|  |  |  | 	"github.com/stretchr/testify/require" | 
					
						
							| 
									
										
										
										
											2021-05-24 12:11:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2021-09-10 17:22:13 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/util" | 
					
						
							| 
									
										
										
										
											2021-03-01 22:33:17 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-11 13:10:19 +08:00
										 |  |  | func TestCreateLibraryElement(t *testing.T) { | 
					
						
							|  |  |  | 	scenarioWithPanel(t, "When an admin tries to create a library panel that already exists, it should fail", | 
					
						
							| 
									
										
										
										
											2021-03-01 22:33:17 +08:00
										 |  |  | 		func(t *testing.T, sc scenarioContext) { | 
					
						
							| 
									
										
										
										
											2021-05-11 13:10:19 +08:00
										 |  |  | 			command := getCreatePanelCommand(sc.folder.Id, "Text - Library Panel") | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 			sc.reqContext.Req.Body = mockRequestBody(command) | 
					
						
							|  |  |  | 			resp := sc.service.createHandler(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 create a library panel that does not exists, it should succeed", | 
					
						
							| 
									
										
										
										
											2021-03-01 22:33:17 +08:00
										 |  |  | 		func(t *testing.T, sc scenarioContext) { | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							|  |  |  | 					FolderID:    1, | 
					
						
							|  |  |  | 					UID:         sc.initialResult.Result.UID, | 
					
						
							|  |  |  | 					Name:        "Text - Library Panel", | 
					
						
							| 
									
										
										
										
											2021-05-24 12:11:01 +08:00
										 |  |  | 					Kind:        int64(models.PanelElement), | 
					
						
							| 
									
										
										
										
											2021-03-24 20:43:51 +08:00
										 |  |  | 					Type:        "text", | 
					
						
							|  |  |  | 					Description: "A description", | 
					
						
							| 
									
										
										
										
											2021-03-01 22:33:17 +08:00
										 |  |  | 					Model: map[string]interface{}{ | 
					
						
							| 
									
										
										
										
											2021-03-24 20:43:51 +08:00
										 |  |  | 						"datasource":  "${DS_GDEV-TESTDATA}", | 
					
						
							|  |  |  | 						"description": "A description", | 
					
						
							|  |  |  | 						"id":          float64(1), | 
					
						
							|  |  |  | 						"title":       "Text - Library Panel", | 
					
						
							|  |  |  | 						"type":        "text", | 
					
						
							| 
									
										
										
										
											2021-03-01 22:33:17 +08:00
										 |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2021-03-02 20:33:26 +08:00
										 |  |  | 					Version: 1, | 
					
						
							| 
									
										
										
										
											2021-05-11 13:10:19 +08:00
										 |  |  | 					Meta: LibraryElementDTOMeta{ | 
					
						
							| 
									
										
										
										
											2022-05-05 16:04:54 +08:00
										 |  |  | 						FolderName:          "ScenarioFolder", | 
					
						
							|  |  |  | 						FolderUID:           "ScenarioFolder", | 
					
						
							| 
									
										
										
										
											2021-05-12 14:48:17 +08:00
										 |  |  | 						ConnectedDashboards: 0, | 
					
						
							|  |  |  | 						Created:             sc.initialResult.Result.Meta.Created, | 
					
						
							|  |  |  | 						Updated:             sc.initialResult.Result.Meta.Updated, | 
					
						
							| 
									
										
										
										
											2021-05-11 13:10:19 +08:00
										 |  |  | 						CreatedBy: LibraryElementDTOMetaUser{ | 
					
						
							| 
									
										
										
										
											2021-03-01 22:33:17 +08:00
										 |  |  | 							ID:        1, | 
					
						
							|  |  |  | 							Name:      "signed_in_user", | 
					
						
							| 
									
										
										
										
											2021-05-11 13:10:19 +08:00
										 |  |  | 							AvatarURL: "/avatar/37524e1eb8b3e32850b57db0a19af93b", | 
					
						
							| 
									
										
										
										
											2021-03-01 22:33:17 +08:00
										 |  |  | 						}, | 
					
						
							| 
									
										
										
										
											2021-05-11 13:10:19 +08:00
										 |  |  | 						UpdatedBy: LibraryElementDTOMetaUser{ | 
					
						
							| 
									
										
										
										
											2021-03-01 22:33:17 +08:00
										 |  |  | 							ID:        1, | 
					
						
							|  |  |  | 							Name:      "signed_in_user", | 
					
						
							| 
									
										
										
										
											2021-05-11 13:10:19 +08:00
										 |  |  | 							AvatarURL: "/avatar/37524e1eb8b3e32850b57db0a19af93b", | 
					
						
							| 
									
										
										
										
											2021-03-01 22:33:17 +08:00
										 |  |  | 						}, | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if diff := cmp.Diff(expected, sc.initialResult, getCompareOptions()...); diff != "" { | 
					
						
							|  |  |  | 				t.Fatalf("Result mismatch (-want +got):\n%s", diff) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-10 17:22:13 +08:00
										 |  |  | 	testScenario(t, "When an admin tries to create a library panel that does not exists using an nonexistent UID, it should succeed", | 
					
						
							|  |  |  | 		func(t *testing.T, sc scenarioContext) { | 
					
						
							|  |  |  | 			command := getCreatePanelCommand(sc.folder.Id, "Nonexistent UID") | 
					
						
							|  |  |  | 			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
										 |  |  | 			var result = validateAndUnMarshalResponse(t, resp) | 
					
						
							|  |  |  | 			var expected = libraryElementResult{ | 
					
						
							|  |  |  | 				Result: libraryElement{ | 
					
						
							|  |  |  | 					ID:          1, | 
					
						
							|  |  |  | 					OrgID:       1, | 
					
						
							|  |  |  | 					FolderID:    1, | 
					
						
							|  |  |  | 					UID:         command.UID, | 
					
						
							|  |  |  | 					Name:        "Nonexistent UID", | 
					
						
							|  |  |  | 					Kind:        int64(models.PanelElement), | 
					
						
							|  |  |  | 					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{ | 
					
						
							| 
									
										
										
										
											2022-05-05 16:04:54 +08:00
										 |  |  | 						FolderName:          "ScenarioFolder", | 
					
						
							|  |  |  | 						FolderUID:           "ScenarioFolder", | 
					
						
							| 
									
										
										
										
											2021-09-10 17:22:13 +08:00
										 |  |  | 						ConnectedDashboards: 0, | 
					
						
							|  |  |  | 						Created:             result.Result.Meta.Created, | 
					
						
							|  |  |  | 						Updated:             result.Result.Meta.Updated, | 
					
						
							|  |  |  | 						CreatedBy: LibraryElementDTOMetaUser{ | 
					
						
							|  |  |  | 							ID:        1, | 
					
						
							|  |  |  | 							Name:      "signed_in_user", | 
					
						
							|  |  |  | 							AvatarURL: "/avatar/37524e1eb8b3e32850b57db0a19af93b", | 
					
						
							|  |  |  | 						}, | 
					
						
							|  |  |  | 						UpdatedBy: LibraryElementDTOMetaUser{ | 
					
						
							|  |  |  | 							ID:        1, | 
					
						
							|  |  |  | 							Name:      "signed_in_user", | 
					
						
							|  |  |  | 							AvatarURL: "/avatar/37524e1eb8b3e32850b57db0a19af93b", | 
					
						
							|  |  |  | 						}, | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if diff := cmp.Diff(expected, result, getCompareOptions()...); diff != "" { | 
					
						
							|  |  |  | 				t.Fatalf("Result mismatch (-want +got):\n%s", diff) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	scenarioWithPanel(t, "When an admin tries to create a library panel that does not exists using an existent UID, it should fail", | 
					
						
							|  |  |  | 		func(t *testing.T, sc scenarioContext) { | 
					
						
							|  |  |  | 			command := getCreatePanelCommand(sc.folder.Id, "Existing UID") | 
					
						
							|  |  |  | 			command.UID = sc.initialResult.Result.UID | 
					
						
							| 
									
										
										
										
											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, 400, resp.Status()) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	scenarioWithPanel(t, "When an admin tries to create a library panel that does not exists using an invalid UID, it should fail", | 
					
						
							|  |  |  | 		func(t *testing.T, sc scenarioContext) { | 
					
						
							|  |  |  | 			command := getCreatePanelCommand(sc.folder.Id, "Invalid UID") | 
					
						
							|  |  |  | 			command.UID = "Testing an invalid UID" | 
					
						
							| 
									
										
										
										
											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, 400, resp.Status()) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	scenarioWithPanel(t, "When an admin tries to create a library panel that does not exists using an UID that is too long, it should fail", | 
					
						
							|  |  |  | 		func(t *testing.T, sc scenarioContext) { | 
					
						
							|  |  |  | 			command := getCreatePanelCommand(sc.folder.Id, "Invalid UID") | 
					
						
							|  |  |  | 			command.UID = "j6T00KRZzj6T00KRZzj6T00KRZzj6T00KRZzj6T00K" | 
					
						
							| 
									
										
										
										
											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, 400, resp.Status()) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-01 19:27:43 +08:00
										 |  |  | 	testScenario(t, "When an admin tries to create a library panel where name and panel title differ, it should not update panel title", | 
					
						
							| 
									
										
										
										
											2021-03-01 22:33:17 +08:00
										 |  |  | 		func(t *testing.T, sc scenarioContext) { | 
					
						
							| 
									
										
										
										
											2021-05-11 13:10:19 +08:00
										 |  |  | 			command := getCreatePanelCommand(1, "Library Panel Name") | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 			sc.reqContext.Req.Body = mockRequestBody(command) | 
					
						
							|  |  |  | 			resp := sc.service.createHandler(sc.reqContext) | 
					
						
							| 
									
										
										
										
											2021-03-01 22:33:17 +08:00
										 |  |  | 			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, | 
					
						
							|  |  |  | 					FolderID:    1, | 
					
						
							|  |  |  | 					UID:         result.Result.UID, | 
					
						
							|  |  |  | 					Name:        "Library Panel Name", | 
					
						
							| 
									
										
										
										
											2021-05-24 12:11:01 +08:00
										 |  |  | 					Kind:        int64(models.PanelElement), | 
					
						
							| 
									
										
										
										
											2021-03-24 20:43:51 +08:00
										 |  |  | 					Type:        "text", | 
					
						
							|  |  |  | 					Description: "A description", | 
					
						
							| 
									
										
										
										
											2021-03-01 22:33:17 +08:00
										 |  |  | 					Model: map[string]interface{}{ | 
					
						
							| 
									
										
										
										
											2021-03-24 20:43:51 +08:00
										 |  |  | 						"datasource":  "${DS_GDEV-TESTDATA}", | 
					
						
							|  |  |  | 						"description": "A description", | 
					
						
							|  |  |  | 						"id":          float64(1), | 
					
						
							| 
									
										
										
										
											2021-09-01 19:27:43 +08:00
										 |  |  | 						"title":       "Text - Library Panel", | 
					
						
							| 
									
										
										
										
											2021-03-24 20:43:51 +08:00
										 |  |  | 						"type":        "text", | 
					
						
							| 
									
										
										
										
											2021-03-01 22:33:17 +08:00
										 |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2021-03-02 20:33:26 +08:00
										 |  |  | 					Version: 1, | 
					
						
							| 
									
										
										
										
											2021-05-11 13:10:19 +08:00
										 |  |  | 					Meta: LibraryElementDTOMeta{ | 
					
						
							| 
									
										
										
										
											2022-05-05 16:04:54 +08:00
										 |  |  | 						FolderName:          "ScenarioFolder", | 
					
						
							|  |  |  | 						FolderUID:           "ScenarioFolder", | 
					
						
							| 
									
										
										
										
											2021-05-12 14:48:17 +08:00
										 |  |  | 						ConnectedDashboards: 0, | 
					
						
							|  |  |  | 						Created:             result.Result.Meta.Created, | 
					
						
							|  |  |  | 						Updated:             result.Result.Meta.Updated, | 
					
						
							| 
									
										
										
										
											2021-05-11 13:10:19 +08:00
										 |  |  | 						CreatedBy: LibraryElementDTOMetaUser{ | 
					
						
							| 
									
										
										
										
											2021-03-01 22:33:17 +08:00
										 |  |  | 							ID:        1, | 
					
						
							|  |  |  | 							Name:      "signed_in_user", | 
					
						
							| 
									
										
										
										
											2021-05-11 13:10:19 +08:00
										 |  |  | 							AvatarURL: "/avatar/37524e1eb8b3e32850b57db0a19af93b", | 
					
						
							| 
									
										
										
										
											2021-03-01 22:33:17 +08:00
										 |  |  | 						}, | 
					
						
							| 
									
										
										
										
											2021-05-11 13:10:19 +08:00
										 |  |  | 						UpdatedBy: LibraryElementDTOMetaUser{ | 
					
						
							| 
									
										
										
										
											2021-03-01 22:33:17 +08:00
										 |  |  | 							ID:        1, | 
					
						
							|  |  |  | 							Name:      "signed_in_user", | 
					
						
							| 
									
										
										
										
											2021-05-11 13:10:19 +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) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | } |