| 
									
										
										
										
											2021-05-11 13:10:19 +08:00
										 |  |  | package libraryelements | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2021-11-05 22:06:14 +08:00
										 |  |  | 	"encoding/json" | 
					
						
							| 
									
										
										
										
											2021-05-11 13:10:19 +08:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/components/simplejson" | 
					
						
							| 
									
										
										
										
											2022-08-10 17:56:48 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/org" | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/web" | 
					
						
							| 
									
										
										
										
											2021-05-11 13:10:19 +08:00
										 |  |  | 	"github.com/stretchr/testify/require" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestDeleteLibraryElement(t *testing.T) { | 
					
						
							|  |  |  | 	scenarioWithPanel(t, "When an admin tries to delete a library panel that does not exist, it should fail", | 
					
						
							|  |  |  | 		func(t *testing.T, sc scenarioContext) { | 
					
						
							|  |  |  | 			resp := sc.service.deleteHandler(sc.reqContext) | 
					
						
							|  |  |  | 			require.Equal(t, 404, resp.Status()) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 22:06:14 +08:00
										 |  |  | 	scenarioWithPanel(t, "When an admin tries to delete a library panel that exists, it should succeed and return correct ID", | 
					
						
							| 
									
										
										
										
											2021-05-11 13:10:19 +08:00
										 |  |  | 		func(t *testing.T, sc scenarioContext) { | 
					
						
							| 
									
										
										
										
											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-05-11 13:10:19 +08:00
										 |  |  | 			resp := sc.service.deleteHandler(sc.reqContext) | 
					
						
							|  |  |  | 			require.Equal(t, 200, resp.Status()) | 
					
						
							| 
									
										
										
										
											2021-11-05 22:06:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			var result DeleteLibraryElementResponse | 
					
						
							|  |  |  | 			err := json.Unmarshal(resp.Body(), &result) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			require.NoError(t, err) | 
					
						
							|  |  |  | 			require.Equal(t, sc.initialResult.Result.ID, result.ID) | 
					
						
							| 
									
										
										
										
											2021-05-11 13:10:19 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	scenarioWithPanel(t, "When an admin tries to delete a library panel in another org, it should fail", | 
					
						
							|  |  |  | 		func(t *testing.T, sc scenarioContext) { | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 			sc.ctx.Req = web.SetURLParams(sc.ctx.Req, map[string]string{":uid": sc.initialResult.Result.UID}) | 
					
						
							| 
									
										
										
										
											2022-08-11 19:28:55 +08:00
										 |  |  | 			sc.reqContext.SignedInUser.OrgID = 2 | 
					
						
							| 
									
										
										
										
											2022-08-10 17:56:48 +08:00
										 |  |  | 			sc.reqContext.SignedInUser.OrgRole = org.RoleAdmin | 
					
						
							| 
									
										
										
										
											2021-05-11 13:10:19 +08:00
										 |  |  | 			resp := sc.service.deleteHandler(sc.reqContext) | 
					
						
							|  |  |  | 			require.Equal(t, 404, resp.Status()) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	scenarioWithPanel(t, "When an admin tries to delete a library panel that is connected, it should fail", | 
					
						
							|  |  |  | 		func(t *testing.T, sc scenarioContext) { | 
					
						
							|  |  |  | 			dashJSON := map[string]interface{}{ | 
					
						
							|  |  |  | 				"panels": []interface{}{ | 
					
						
							|  |  |  | 					map[string]interface{}{ | 
					
						
							|  |  |  | 						"id": int64(1), | 
					
						
							|  |  |  | 						"gridPos": map[string]interface{}{ | 
					
						
							|  |  |  | 							"h": 6, | 
					
						
							|  |  |  | 							"w": 6, | 
					
						
							|  |  |  | 							"x": 0, | 
					
						
							|  |  |  | 							"y": 0, | 
					
						
							|  |  |  | 						}, | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					map[string]interface{}{ | 
					
						
							|  |  |  | 						"id": int64(2), | 
					
						
							|  |  |  | 						"gridPos": map[string]interface{}{ | 
					
						
							|  |  |  | 							"h": 6, | 
					
						
							|  |  |  | 							"w": 6, | 
					
						
							|  |  |  | 							"x": 6, | 
					
						
							|  |  |  | 							"y": 0, | 
					
						
							|  |  |  | 						}, | 
					
						
							|  |  |  | 						"libraryPanel": map[string]interface{}{ | 
					
						
							|  |  |  | 							"uid":  sc.initialResult.Result.UID, | 
					
						
							|  |  |  | 							"name": sc.initialResult.Result.Name, | 
					
						
							|  |  |  | 						}, | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			dash := models.Dashboard{ | 
					
						
							|  |  |  | 				Title: "Testing deleteHandler ", | 
					
						
							|  |  |  | 				Data:  simplejson.NewFromAny(dashJSON), | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			dashInDB := createDashboard(t, sc.sqlStore, sc.user, &dash, sc.folder.Id) | 
					
						
							| 
									
										
										
										
											2021-09-27 15:04:36 +08:00
										 |  |  | 			err := sc.service.ConnectElementsToDashboard(sc.reqContext.Req.Context(), sc.reqContext.SignedInUser, []string{sc.initialResult.Result.UID}, dashInDB.Id) | 
					
						
							| 
									
										
										
										
											2021-05-11 13:10:19 +08:00
										 |  |  | 			require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-05-11 13:10:19 +08:00
										 |  |  | 			resp := sc.service.deleteHandler(sc.reqContext) | 
					
						
							|  |  |  | 			require.Equal(t, 403, resp.Status()) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | } |