| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2022-08-03 22:31:23 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2018-12-11 05:40:26 +08:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"net/http/httptest" | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-02 02:16:26 +08:00
										 |  |  | 	"github.com/stretchr/testify/assert" | 
					
						
							|  |  |  | 	"github.com/stretchr/testify/mock" | 
					
						
							|  |  |  | 	"github.com/stretchr/testify/require" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/components/simplejson" | 
					
						
							| 
									
										
										
										
											2023-01-18 23:01:25 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/db/dbtest" | 
					
						
							| 
									
										
										
										
											2022-06-02 02:16:26 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/dashboards" | 
					
						
							| 
									
										
										
										
											2022-06-17 21:09:01 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/dashboardsnapshots" | 
					
						
							| 
									
										
										
										
											2022-03-21 17:49:49 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/guardian" | 
					
						
							| 
									
										
										
										
											2022-08-10 17:56:48 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/org" | 
					
						
							| 
									
										
										
										
											2022-09-21 00:58:04 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/team/teamtest" | 
					
						
							| 
									
										
										
										
											2023-01-26 21:28:11 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							| 
									
										
										
										
											2023-03-08 16:12:02 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/util/errutil" | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) { | 
					
						
							|  |  |  | 	setupRemoteServer := func(fn func(http.ResponseWriter, *http.Request)) *httptest.Server { | 
					
						
							|  |  |  | 		s := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { | 
					
						
							|  |  |  | 			fn(rw, r) | 
					
						
							|  |  |  | 		})) | 
					
						
							|  |  |  | 		t.Cleanup(s.Close) | 
					
						
							|  |  |  | 		return s | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-18 23:01:25 +08:00
										 |  |  | 	sqlmock := dbtest.NewFakeDB() | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 	jsonModel, err := simplejson.NewJson([]byte(`{"id":100}`)) | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-17 21:09:01 +08:00
										 |  |  | 	setUpSnapshotTest := func(t *testing.T, userId int64, deleteUrl string) dashboardsnapshots.Service { | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 		t.Helper() | 
					
						
							| 
									
										
										
										
											2022-06-17 21:09:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		dashSnapSvc := dashboardsnapshots.NewMockService(t) | 
					
						
							|  |  |  | 		dashSnapSvc.On("DeleteDashboardSnapshot", mock.Anything, mock.AnythingOfType("*dashboardsnapshots.DeleteDashboardSnapshotCommand")).Return(nil).Maybe() | 
					
						
							| 
									
										
										
										
											2023-01-25 22:09:44 +08:00
										 |  |  | 		res := &dashboardsnapshots.DashboardSnapshot{ | 
					
						
							|  |  |  | 			ID:        1, | 
					
						
							|  |  |  | 			Key:       "12345", | 
					
						
							|  |  |  | 			DeleteKey: "54321", | 
					
						
							|  |  |  | 			Dashboard: jsonModel, | 
					
						
							|  |  |  | 			Expires:   time.Now().Add(time.Duration(1000) * time.Second), | 
					
						
							|  |  |  | 			UserID:    999999, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if userId != 0 { | 
					
						
							|  |  |  | 			res.UserID = userId | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if deleteUrl != "" { | 
					
						
							|  |  |  | 			res.External = true | 
					
						
							|  |  |  | 			res.ExternalDeleteURL = deleteUrl | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		dashSnapSvc.On("GetDashboardSnapshot", mock.Anything, mock.AnythingOfType("*dashboardsnapshots.GetDashboardSnapshotQuery")).Return(res, nil) | 
					
						
							| 
									
										
										
										
											2022-06-17 21:09:01 +08:00
										 |  |  | 		dashSnapSvc.On("DeleteDashboardSnapshot", mock.Anything, mock.AnythingOfType("*dashboardsnapshots.DeleteDashboardSnapshotCommand")).Return(nil).Maybe() | 
					
						
							|  |  |  | 		return dashSnapSvc | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-12-11 05:40:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 	t.Run("When user has editor role and is not in the ACL", func(t *testing.T) { | 
					
						
							|  |  |  | 		loggedInUserScenarioWithRole(t, "Should not be able to delete snapshot when calling DELETE on", | 
					
						
							| 
									
										
										
										
											2022-08-10 17:56:48 +08:00
										 |  |  | 			"DELETE", "/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) { | 
					
						
							| 
									
										
										
										
											2023-01-26 21:28:11 +08:00
										 |  |  | 				d := setUpSnapshotTest(t, 0, "") | 
					
						
							|  |  |  | 				hs := buildHttpServer(d, true) | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | 				sc.handlerFunc = hs.DeleteDashboardSnapshot | 
					
						
							| 
									
										
										
										
											2022-06-17 21:09:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-21 00:58:04 +08:00
										 |  |  | 				teamSvc := &teamtest.FakeService{} | 
					
						
							| 
									
										
										
										
											2022-06-17 21:09:01 +08:00
										 |  |  | 				dashSvc := dashboards.NewFakeDashboardService(t) | 
					
						
							| 
									
										
										
										
											2023-01-25 17:36:26 +08:00
										 |  |  | 				var qResult *dashboards.Dashboard | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 				dashSvc.On("GetDashboard", mock.Anything, mock.AnythingOfType("*dashboards.GetDashboardQuery")).Run(func(args mock.Arguments) { | 
					
						
							|  |  |  | 					q := args.Get(1).(*dashboards.GetDashboardQuery) | 
					
						
							| 
									
										
										
										
											2023-01-25 17:36:26 +08:00
										 |  |  | 					qResult = &dashboards.Dashboard{ | 
					
						
							| 
									
										
										
										
											2023-01-16 23:33:55 +08:00
										 |  |  | 						ID:  q.ID, | 
					
						
							|  |  |  | 						UID: q.UID, | 
					
						
							| 
									
										
										
										
											2022-12-15 22:34:17 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2023-01-25 17:36:26 +08:00
										 |  |  | 				}).Return(qResult, nil).Maybe() | 
					
						
							|  |  |  | 				dashSvc.On("GetDashboardACLInfoList", mock.Anything, mock.AnythingOfType("*dashboards.GetDashboardACLInfoListQuery")).Return(nil, nil).Maybe() | 
					
						
							| 
									
										
										
										
											2022-12-15 22:34:17 +08:00
										 |  |  | 				hs.DashboardService = dashSvc | 
					
						
							| 
									
										
										
										
											2022-06-17 21:09:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-16 17:54:01 +08:00
										 |  |  | 				guardian.InitLegacyGuardian(setting.NewCfg(), sc.sqlStore, dashSvc, teamSvc) | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"key": "12345"}).exec() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				assert.Equal(t, 403, sc.resp.Code) | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | 			}, sqlmock) | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 	t.Run("When user is anonymous", func(t *testing.T) { | 
					
						
							|  |  |  | 		anonymousUserScenario(t, "Should be able to delete a snapshot when calling GET on", "GET", | 
					
						
							|  |  |  | 			"/api/snapshots-delete/12345", "/api/snapshots-delete/:deleteKey", func(sc *scenarioContext) { | 
					
						
							|  |  |  | 				var externalRequest *http.Request | 
					
						
							|  |  |  | 				ts := setupRemoteServer(func(rw http.ResponseWriter, req *http.Request) { | 
					
						
							|  |  |  | 					rw.WriteHeader(200) | 
					
						
							|  |  |  | 					externalRequest = req | 
					
						
							|  |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2023-01-26 21:28:11 +08:00
										 |  |  | 				d := setUpSnapshotTest(t, 0, ts.URL) | 
					
						
							|  |  |  | 				hs := buildHttpServer(d, true) | 
					
						
							| 
									
										
										
										
											2018-05-24 14:55:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | 				sc.handlerFunc = hs.DeleteDashboardSnapshotByDeleteKey | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				sc.fakeReqWithParams("GET", sc.url, map[string]string{"deleteKey": "12345"}).exec() | 
					
						
							| 
									
										
										
										
											2018-05-24 14:55:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				require.Equal(t, 200, sc.resp.Code) | 
					
						
							|  |  |  | 				respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes()) | 
					
						
							|  |  |  | 				require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2018-12-11 05:40:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				assert.True(t, strings.HasPrefix(respJSON.Get("message").MustString(), "Snapshot deleted")) | 
					
						
							| 
									
										
										
										
											2020-12-04 23:22:58 +08:00
										 |  |  | 				assert.Equal(t, 1, respJSON.Get("id").MustInt()) | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				assert.Equal(t, http.MethodGet, externalRequest.Method) | 
					
						
							|  |  |  | 				assert.Equal(t, ts.URL, fmt.Sprintf("http://%s", externalRequest.Host)) | 
					
						
							|  |  |  | 				assert.Equal(t, "/", externalRequest.URL.EscapedPath()) | 
					
						
							| 
									
										
										
										
											2018-05-24 14:55:16 +08:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2018-05-24 14:55:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 	t.Run("When user is editor and dashboard has default ACL", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2022-09-21 00:58:04 +08:00
										 |  |  | 		teamSvc := &teamtest.FakeService{} | 
					
						
							| 
									
										
										
										
											2022-06-02 02:16:26 +08:00
										 |  |  | 		dashSvc := &dashboards.FakeDashboardService{} | 
					
						
							| 
									
										
										
										
											2023-01-25 17:36:26 +08:00
										 |  |  | 		qResult := []*dashboards.DashboardACLInfoDTO{ | 
					
						
							| 
									
										
										
										
											2023-01-26 21:46:30 +08:00
										 |  |  | 			{Role: &viewerRole, Permission: dashboards.PERMISSION_VIEW}, | 
					
						
							|  |  |  | 			{Role: &editorRole, Permission: dashboards.PERMISSION_EDIT}, | 
					
						
							| 
									
										
										
										
											2023-01-25 17:36:26 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		dashSvc.On("GetDashboardACLInfoList", mock.Anything, mock.AnythingOfType("*dashboards.GetDashboardACLInfoListQuery")).Return(qResult, nil) | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-08 16:12:02 +08:00
										 |  |  | 		loggedInUserScenarioWithRole(t, "Should not be able to delete a snapshot when fetching guardian fails during calling DELETE on", "DELETE", | 
					
						
							|  |  |  | 			"/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 				ts := setupRemoteServer(func(rw http.ResponseWriter, req *http.Request) { | 
					
						
							|  |  |  | 					rw.WriteHeader(200) | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 				dashSvc := dashboards.NewFakeDashboardService(t) | 
					
						
							|  |  |  | 				dashSvc.On("GetDashboard", mock.Anything, mock.AnythingOfType("*dashboards.GetDashboardQuery")).Return(nil, errutil.Error{PublicMessage: "some error"}).Maybe() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-16 17:54:01 +08:00
										 |  |  | 				guardian.InitLegacyGuardian(sc.cfg, sc.sqlStore, dashSvc, teamSvc) | 
					
						
							| 
									
										
										
										
											2023-03-08 16:12:02 +08:00
										 |  |  | 				d := setUpSnapshotTest(t, 0, ts.URL) | 
					
						
							|  |  |  | 				hs := buildHttpServer(d, true) | 
					
						
							|  |  |  | 				hs.DashboardService = dashSvc | 
					
						
							|  |  |  | 				sc.handlerFunc = hs.DeleteDashboardSnapshot | 
					
						
							|  |  |  | 				sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"key": "12345"}).exec() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				assert.Equal(t, http.StatusInternalServerError, sc.resp.Code) | 
					
						
							|  |  |  | 			}, sqlmock) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		loggedInUserScenarioWithRole(t, "Should be able to delete a snapshot from a deleted dashboard when calling DELETE on", "DELETE", | 
					
						
							|  |  |  | 			"/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 				var externalRequest *http.Request | 
					
						
							|  |  |  | 				ts := setupRemoteServer(func(rw http.ResponseWriter, req *http.Request) { | 
					
						
							|  |  |  | 					rw.WriteHeader(200) | 
					
						
							|  |  |  | 					externalRequest = req | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 				dashSvc := dashboards.NewFakeDashboardService(t) | 
					
						
							|  |  |  | 				dashSvc.On("GetDashboard", mock.Anything, mock.AnythingOfType("*dashboards.GetDashboardQuery")).Return(nil, dashboards.ErrDashboardNotFound).Maybe() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-16 17:54:01 +08:00
										 |  |  | 				guardian.InitLegacyGuardian(sc.cfg, sc.sqlStore, dashSvc, teamSvc) | 
					
						
							| 
									
										
										
										
											2023-03-08 16:12:02 +08:00
										 |  |  | 				d := setUpSnapshotTest(t, 0, ts.URL) | 
					
						
							|  |  |  | 				hs := buildHttpServer(d, true) | 
					
						
							|  |  |  | 				hs.DashboardService = dashSvc | 
					
						
							|  |  |  | 				sc.handlerFunc = hs.DeleteDashboardSnapshot | 
					
						
							|  |  |  | 				sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"key": "12345"}).exec() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				assert.Equal(t, 200, sc.resp.Code) | 
					
						
							|  |  |  | 				respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes()) | 
					
						
							|  |  |  | 				require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				assert.True(t, strings.HasPrefix(respJSON.Get("message").MustString(), "Snapshot deleted")) | 
					
						
							|  |  |  | 				assert.Equal(t, 1, respJSON.Get("id").MustInt()) | 
					
						
							|  |  |  | 				assert.Equal(t, ts.URL, fmt.Sprintf("http://%s", externalRequest.Host)) | 
					
						
							|  |  |  | 				assert.Equal(t, "/", externalRequest.URL.EscapedPath()) | 
					
						
							|  |  |  | 			}, sqlmock) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 		loggedInUserScenarioWithRole(t, "Should be able to delete a snapshot when calling DELETE on", "DELETE", | 
					
						
							| 
									
										
										
										
											2022-08-10 17:56:48 +08:00
										 |  |  | 			"/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) { | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				var externalRequest *http.Request | 
					
						
							|  |  |  | 				ts := setupRemoteServer(func(rw http.ResponseWriter, req *http.Request) { | 
					
						
							|  |  |  | 					rw.WriteHeader(200) | 
					
						
							|  |  |  | 					externalRequest = req | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2022-12-15 22:34:17 +08:00
										 |  |  | 				dashSvc := dashboards.NewFakeDashboardService(t) | 
					
						
							| 
									
										
										
										
											2023-01-25 17:36:26 +08:00
										 |  |  | 				qResult := &dashboards.Dashboard{} | 
					
						
							|  |  |  | 				dashSvc.On("GetDashboard", mock.Anything, mock.AnythingOfType("*dashboards.GetDashboardQuery")).Return(qResult, nil).Maybe() | 
					
						
							|  |  |  | 				qResultACL := []*dashboards.DashboardACLInfoDTO{ | 
					
						
							| 
									
										
										
										
											2023-01-26 21:46:30 +08:00
										 |  |  | 					{Role: &viewerRole, Permission: dashboards.PERMISSION_VIEW}, | 
					
						
							|  |  |  | 					{Role: &editorRole, Permission: dashboards.PERMISSION_EDIT}, | 
					
						
							| 
									
										
										
										
											2023-01-25 17:36:26 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				dashSvc.On("GetDashboardACLInfoList", mock.Anything, mock.AnythingOfType("*dashboards.GetDashboardACLInfoListQuery")).Return(qResultACL, nil) | 
					
						
							| 
									
										
										
										
											2023-03-16 17:54:01 +08:00
										 |  |  | 				guardian.InitLegacyGuardian(sc.cfg, sc.sqlStore, dashSvc, teamSvc) | 
					
						
							| 
									
										
										
										
											2023-01-26 21:28:11 +08:00
										 |  |  | 				d := setUpSnapshotTest(t, 0, ts.URL) | 
					
						
							|  |  |  | 				hs := buildHttpServer(d, true) | 
					
						
							|  |  |  | 				hs.DashboardService = dashSvc | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | 				sc.handlerFunc = hs.DeleteDashboardSnapshot | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"key": "12345"}).exec() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				assert.Equal(t, 200, sc.resp.Code) | 
					
						
							|  |  |  | 				respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes()) | 
					
						
							|  |  |  | 				require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				assert.True(t, strings.HasPrefix(respJSON.Get("message").MustString(), "Snapshot deleted")) | 
					
						
							| 
									
										
										
										
											2020-12-04 23:22:58 +08:00
										 |  |  | 				assert.Equal(t, 1, respJSON.Get("id").MustInt()) | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				assert.Equal(t, ts.URL, fmt.Sprintf("http://%s", externalRequest.Host)) | 
					
						
							|  |  |  | 				assert.Equal(t, "/", externalRequest.URL.EscapedPath()) | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | 			}, sqlmock) | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 	t.Run("When user is editor and creator of the snapshot", func(t *testing.T) { | 
					
						
							|  |  |  | 		loggedInUserScenarioWithRole(t, "Should be able to delete a snapshot when calling DELETE on", | 
					
						
							| 
									
										
										
										
											2022-08-10 17:56:48 +08:00
										 |  |  | 			"DELETE", "/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) { | 
					
						
							| 
									
										
										
										
											2022-06-17 21:09:01 +08:00
										 |  |  | 				d := setUpSnapshotTest(t, testUserID, "") | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-15 22:34:17 +08:00
										 |  |  | 				dashSvc := dashboards.NewFakeDashboardService(t) | 
					
						
							| 
									
										
										
										
											2023-01-26 21:28:11 +08:00
										 |  |  | 				hs := buildHttpServer(d, true) | 
					
						
							|  |  |  | 				hs.DashboardService = dashSvc | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | 				sc.handlerFunc = hs.DeleteDashboardSnapshot | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"key": "12345"}).exec() | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				assert.Equal(t, 200, sc.resp.Code) | 
					
						
							|  |  |  | 				respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes()) | 
					
						
							|  |  |  | 				require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				assert.True(t, strings.HasPrefix(respJSON.Get("message").MustString(), "Snapshot deleted")) | 
					
						
							| 
									
										
										
										
											2020-12-04 23:22:58 +08:00
										 |  |  | 				assert.Equal(t, 1, respJSON.Get("id").MustInt()) | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | 			}, sqlmock) | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	t.Run("When deleting an external snapshot", func(t *testing.T) { | 
					
						
							|  |  |  | 		loggedInUserScenarioWithRole(t, | 
					
						
							|  |  |  | 			"Should gracefully delete local snapshot when remote snapshot has already been removed when calling DELETE on", | 
					
						
							| 
									
										
										
										
											2022-08-10 17:56:48 +08:00
										 |  |  | 			"DELETE", "/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) { | 
					
						
							| 
									
										
										
										
											2021-07-15 21:29:52 +08:00
										 |  |  | 				var writeErr error | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				ts := setupRemoteServer(func(rw http.ResponseWriter, req *http.Request) { | 
					
						
							|  |  |  | 					rw.WriteHeader(500) | 
					
						
							| 
									
										
										
										
											2021-07-15 21:29:52 +08:00
										 |  |  | 					_, writeErr = rw.Write([]byte(`{"message":"Failed to get dashboard snapshot"}`)) | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2022-12-15 22:34:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				dashSvc := dashboards.NewFakeDashboardService(t) | 
					
						
							| 
									
										
										
										
											2023-01-26 21:28:11 +08:00
										 |  |  | 				d := setUpSnapshotTest(t, testUserID, ts.URL) | 
					
						
							|  |  |  | 				hs := buildHttpServer(d, true) | 
					
						
							|  |  |  | 				hs.DashboardService = dashSvc | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | 				sc.handlerFunc = hs.DeleteDashboardSnapshot | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"key": "12345"}).exec() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				require.NoError(t, writeErr) | 
					
						
							|  |  |  | 				assert.Equal(t, 200, sc.resp.Code) | 
					
						
							| 
									
										
										
										
											2020-12-04 23:22:58 +08:00
										 |  |  | 				respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes()) | 
					
						
							|  |  |  | 				require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				assert.True(t, strings.HasPrefix(respJSON.Get("message").MustString(), "Snapshot deleted")) | 
					
						
							|  |  |  | 				assert.Equal(t, 1, respJSON.Get("id").MustInt()) | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | 			}, sqlmock) | 
					
						
							| 
									
										
										
										
											2018-12-11 05:40:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 		loggedInUserScenarioWithRole(t, | 
					
						
							|  |  |  | 			"Should fail to delete local snapshot when an unexpected 500 error occurs when calling DELETE on", "DELETE", | 
					
						
							| 
									
										
										
										
											2022-08-10 17:56:48 +08:00
										 |  |  | 			"/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) { | 
					
						
							| 
									
										
										
										
											2019-10-09 00:57:53 +08:00
										 |  |  | 				var writeErr error | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				ts := setupRemoteServer(func(rw http.ResponseWriter, req *http.Request) { | 
					
						
							|  |  |  | 					rw.WriteHeader(500) | 
					
						
							|  |  |  | 					_, writeErr = rw.Write([]byte(`{"message":"Unexpected"}`)) | 
					
						
							| 
									
										
										
										
											2018-12-11 05:40:26 +08:00
										 |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2023-01-26 21:28:11 +08:00
										 |  |  | 				d := setUpSnapshotTest(t, testUserID, ts.URL) | 
					
						
							|  |  |  | 				hs := buildHttpServer(d, true) | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | 				sc.handlerFunc = hs.DeleteDashboardSnapshot | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"key": "12345"}).exec() | 
					
						
							| 
									
										
										
										
											2018-12-11 05:40:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				require.NoError(t, writeErr) | 
					
						
							|  |  |  | 				assert.Equal(t, 500, sc.resp.Code) | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | 			}, sqlmock) | 
					
						
							| 
									
										
										
										
											2018-12-11 05:40:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 		loggedInUserScenarioWithRole(t, | 
					
						
							|  |  |  | 			"Should fail to delete local snapshot when an unexpected remote error occurs when calling DELETE on", | 
					
						
							| 
									
										
										
										
											2022-08-10 17:56:48 +08:00
										 |  |  | 			"DELETE", "/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) { | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				ts := setupRemoteServer(func(rw http.ResponseWriter, req *http.Request) { | 
					
						
							|  |  |  | 					rw.WriteHeader(404) | 
					
						
							| 
									
										
										
										
											2018-12-11 05:40:26 +08:00
										 |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2023-01-26 21:28:11 +08:00
										 |  |  | 				d := setUpSnapshotTest(t, testUserID, ts.URL) | 
					
						
							|  |  |  | 				hs := buildHttpServer(d, true) | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | 				sc.handlerFunc = hs.DeleteDashboardSnapshot | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"key": "12345"}).exec() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				assert.Equal(t, 500, sc.resp.Code) | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | 			}, sqlmock) | 
					
						
							| 
									
										
										
										
											2020-10-13 16:19:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 		loggedInUserScenarioWithRole(t, "Should be able to read a snapshot's unencrypted data when calling GET on", | 
					
						
							| 
									
										
										
										
											2022-08-10 17:56:48 +08:00
										 |  |  | 			"GET", "/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) { | 
					
						
							| 
									
										
										
										
											2023-01-26 21:28:11 +08:00
										 |  |  | 				d := setUpSnapshotTest(t, 0, "") | 
					
						
							|  |  |  | 				hs := buildHttpServer(d, true) | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | 				sc.handlerFunc = hs.GetDashboardSnapshot | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				sc.fakeReqWithParams("GET", sc.url, map[string]string{"key": "12345"}).exec() | 
					
						
							| 
									
										
										
										
											2020-10-13 16:19:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				assert.Equal(t, 200, sc.resp.Code) | 
					
						
							|  |  |  | 				respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes()) | 
					
						
							|  |  |  | 				require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2020-10-13 16:19:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				dashboard := respJSON.Get("dashboard") | 
					
						
							|  |  |  | 				id := dashboard.Get("id") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				assert.Equal(t, int64(100), id.MustInt64()) | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | 			}, sqlmock) | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-08-03 22:31:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestGetDashboardSnapshotNotFound(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2023-01-18 23:01:25 +08:00
										 |  |  | 	sqlmock := dbtest.NewFakeDB() | 
					
						
							| 
									
										
										
										
											2022-08-03 22:31:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	setUpSnapshotTest := func(t *testing.T) dashboardsnapshots.Service { | 
					
						
							|  |  |  | 		t.Helper() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		dashSnapSvc := dashboardsnapshots.NewMockService(t) | 
					
						
							|  |  |  | 		dashSnapSvc. | 
					
						
							|  |  |  | 			On("GetDashboardSnapshot", mock.Anything, mock.AnythingOfType("*dashboardsnapshots.GetDashboardSnapshotQuery")). | 
					
						
							|  |  |  | 			Run(func(args mock.Arguments) {}). | 
					
						
							| 
									
										
										
										
											2023-01-25 22:09:44 +08:00
										 |  |  | 			Return(nil, dashboardsnapshots.ErrBaseNotFound.Errorf("")) | 
					
						
							| 
									
										
										
										
											2022-08-03 22:31:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return dashSnapSvc | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	loggedInUserScenarioWithRole(t, | 
					
						
							|  |  |  | 		"GET /snapshots/{key} should return 404 when the snapshot does not exist", "GET", | 
					
						
							| 
									
										
										
										
											2022-08-10 17:56:48 +08:00
										 |  |  | 		"/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) { | 
					
						
							| 
									
										
										
										
											2022-08-03 22:31:23 +08:00
										 |  |  | 			d := setUpSnapshotTest(t) | 
					
						
							| 
									
										
										
										
											2023-01-26 21:28:11 +08:00
										 |  |  | 			hs := buildHttpServer(d, true) | 
					
						
							| 
									
										
										
										
											2022-08-03 22:31:23 +08:00
										 |  |  | 			sc.handlerFunc = hs.GetDashboardSnapshot | 
					
						
							|  |  |  | 			sc.fakeReqWithParams("GET", sc.url, map[string]string{"key": "12345"}).exec() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert.Equal(t, http.StatusNotFound, sc.resp.Code) | 
					
						
							|  |  |  | 		}, sqlmock) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	loggedInUserScenarioWithRole(t, | 
					
						
							|  |  |  | 		"DELETE /snapshots/{key} should return 404 when the snapshot does not exist", "DELETE", | 
					
						
							| 
									
										
										
										
											2022-08-10 17:56:48 +08:00
										 |  |  | 		"/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) { | 
					
						
							| 
									
										
										
										
											2022-08-03 22:31:23 +08:00
										 |  |  | 			d := setUpSnapshotTest(t) | 
					
						
							| 
									
										
										
										
											2023-01-26 21:28:11 +08:00
										 |  |  | 			hs := buildHttpServer(d, true) | 
					
						
							| 
									
										
										
										
											2022-08-03 22:31:23 +08:00
										 |  |  | 			sc.handlerFunc = hs.DeleteDashboardSnapshot | 
					
						
							|  |  |  | 			sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"key": "12345"}).exec() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert.Equal(t, http.StatusNotFound, sc.resp.Code) | 
					
						
							|  |  |  | 		}, sqlmock) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	loggedInUserScenarioWithRole(t, | 
					
						
							|  |  |  | 		"GET /snapshots-delete/{deleteKey} should return 404 when the snapshot does not exist", "DELETE", | 
					
						
							| 
									
										
										
										
											2022-08-10 17:56:48 +08:00
										 |  |  | 		"/api/snapshots-delete/12345", "/api/snapshots-delete/:deleteKey", org.RoleEditor, func(sc *scenarioContext) { | 
					
						
							| 
									
										
										
										
											2022-08-03 22:31:23 +08:00
										 |  |  | 			d := setUpSnapshotTest(t) | 
					
						
							| 
									
										
										
										
											2023-01-26 21:28:11 +08:00
										 |  |  | 			hs := buildHttpServer(d, true) | 
					
						
							| 
									
										
										
										
											2022-08-03 22:31:23 +08:00
										 |  |  | 			sc.handlerFunc = hs.DeleteDashboardSnapshotByDeleteKey | 
					
						
							|  |  |  | 			sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"deleteKey": "12345"}).exec() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert.Equal(t, http.StatusNotFound, sc.resp.Code) | 
					
						
							|  |  |  | 		}, sqlmock) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestGetDashboardSnapshotFailure(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2023-01-18 23:01:25 +08:00
										 |  |  | 	sqlmock := dbtest.NewFakeDB() | 
					
						
							| 
									
										
										
										
											2022-08-03 22:31:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-26 21:28:11 +08:00
										 |  |  | 	setUpSnapshotTest := func(t *testing.T, shouldMockDashSnapServ bool) dashboardsnapshots.Service { | 
					
						
							| 
									
										
										
										
											2022-08-03 22:31:23 +08:00
										 |  |  | 		t.Helper() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-26 21:28:11 +08:00
										 |  |  | 		if shouldMockDashSnapServ { | 
					
						
							|  |  |  | 			dashSnapSvc := dashboardsnapshots.NewMockService(t) | 
					
						
							|  |  |  | 			dashSnapSvc. | 
					
						
							|  |  |  | 				On("GetDashboardSnapshot", mock.Anything, mock.AnythingOfType("*dashboardsnapshots.GetDashboardSnapshotQuery")). | 
					
						
							|  |  |  | 				Run(func(args mock.Arguments) {}). | 
					
						
							|  |  |  | 				Return(nil, errors.New("something went wrong")) | 
					
						
							|  |  |  | 			return dashSnapSvc | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			return nil | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-08-03 22:31:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	loggedInUserScenarioWithRole(t, | 
					
						
							|  |  |  | 		"GET /snapshots/{key} should return 404 when the snapshot does not exist", "GET", | 
					
						
							| 
									
										
										
										
											2022-08-10 17:56:48 +08:00
										 |  |  | 		"/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) { | 
					
						
							| 
									
										
										
										
											2023-01-26 21:28:11 +08:00
										 |  |  | 			d := setUpSnapshotTest(t, true) | 
					
						
							|  |  |  | 			hs := buildHttpServer(d, true) | 
					
						
							| 
									
										
										
										
											2022-08-03 22:31:23 +08:00
										 |  |  | 			sc.handlerFunc = hs.GetDashboardSnapshot | 
					
						
							|  |  |  | 			sc.fakeReqWithParams("GET", sc.url, map[string]string{"key": "12345"}).exec() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert.Equal(t, http.StatusInternalServerError, sc.resp.Code) | 
					
						
							|  |  |  | 		}, sqlmock) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-26 21:28:11 +08:00
										 |  |  | 	loggedInUserScenarioWithRole(t, | 
					
						
							|  |  |  | 		"GET /snapshots/{key} should return 403 when snapshot is disabled", "GET", | 
					
						
							|  |  |  | 		"/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 			d := setUpSnapshotTest(t, false) | 
					
						
							|  |  |  | 			hs := buildHttpServer(d, false) | 
					
						
							|  |  |  | 			sc.handlerFunc = hs.GetDashboardSnapshot | 
					
						
							|  |  |  | 			sc.fakeReqWithParams("GET", sc.url, map[string]string{"key": "12345"}).exec() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert.Equal(t, http.StatusForbidden, sc.resp.Code) | 
					
						
							|  |  |  | 		}, sqlmock) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-03 22:31:23 +08:00
										 |  |  | 	loggedInUserScenarioWithRole(t, | 
					
						
							|  |  |  | 		"DELETE /snapshots/{key} should return 404 when the snapshot does not exist", "DELETE", | 
					
						
							| 
									
										
										
										
											2022-08-10 17:56:48 +08:00
										 |  |  | 		"/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) { | 
					
						
							| 
									
										
										
										
											2023-01-26 21:28:11 +08:00
										 |  |  | 			d := setUpSnapshotTest(t, true) | 
					
						
							|  |  |  | 			hs := buildHttpServer(d, true) | 
					
						
							| 
									
										
										
										
											2022-08-03 22:31:23 +08:00
										 |  |  | 			sc.handlerFunc = hs.DeleteDashboardSnapshot | 
					
						
							|  |  |  | 			sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"key": "12345"}).exec() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert.Equal(t, http.StatusInternalServerError, sc.resp.Code) | 
					
						
							|  |  |  | 		}, sqlmock) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-26 21:28:11 +08:00
										 |  |  | 	loggedInUserScenarioWithRole(t, | 
					
						
							|  |  |  | 		"DELETE /snapshots/{key} should return 403 when snapshot is disabled", "DELETE", | 
					
						
							|  |  |  | 		"/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 			d := setUpSnapshotTest(t, false) | 
					
						
							|  |  |  | 			hs := buildHttpServer(d, false) | 
					
						
							|  |  |  | 			sc.handlerFunc = hs.DeleteDashboardSnapshot | 
					
						
							|  |  |  | 			sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"key": "12345"}).exec() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert.Equal(t, http.StatusForbidden, sc.resp.Code) | 
					
						
							|  |  |  | 		}, sqlmock) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-03 22:31:23 +08:00
										 |  |  | 	loggedInUserScenarioWithRole(t, | 
					
						
							|  |  |  | 		"GET /snapshots-delete/{deleteKey} should return 404 when the snapshot does not exist", "DELETE", | 
					
						
							| 
									
										
										
										
											2022-08-10 17:56:48 +08:00
										 |  |  | 		"/api/snapshots-delete/12345", "/api/snapshots-delete/:deleteKey", org.RoleEditor, func(sc *scenarioContext) { | 
					
						
							| 
									
										
										
										
											2023-01-26 21:28:11 +08:00
										 |  |  | 			d := setUpSnapshotTest(t, true) | 
					
						
							|  |  |  | 			hs := buildHttpServer(d, true) | 
					
						
							| 
									
										
										
										
											2022-08-03 22:31:23 +08:00
										 |  |  | 			sc.handlerFunc = hs.DeleteDashboardSnapshotByDeleteKey | 
					
						
							|  |  |  | 			sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"deleteKey": "12345"}).exec() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert.Equal(t, http.StatusInternalServerError, sc.resp.Code) | 
					
						
							|  |  |  | 		}, sqlmock) | 
					
						
							| 
									
										
										
										
											2023-01-26 21:28:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	loggedInUserScenarioWithRole(t, | 
					
						
							|  |  |  | 		"GET /snapshots-delete/{deleteKey} should return 403 when snapshot is disabled", "DELETE", | 
					
						
							|  |  |  | 		"/api/snapshots-delete/12345", "/api/snapshots-delete/:deleteKey", org.RoleEditor, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 			d := setUpSnapshotTest(t, false) | 
					
						
							|  |  |  | 			hs := buildHttpServer(d, false) | 
					
						
							|  |  |  | 			sc.handlerFunc = hs.DeleteDashboardSnapshotByDeleteKey | 
					
						
							|  |  |  | 			sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"deleteKey": "12345"}).exec() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert.Equal(t, http.StatusForbidden, sc.resp.Code) | 
					
						
							|  |  |  | 		}, sqlmock) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func buildHttpServer(d dashboardsnapshots.Service, snapshotEnabled bool) *HTTPServer { | 
					
						
							|  |  |  | 	hs := &HTTPServer{ | 
					
						
							|  |  |  | 		dashboardsnapshotsService: d, | 
					
						
							|  |  |  | 		Cfg: &setting.Cfg{ | 
					
						
							|  |  |  | 			SnapshotEnabled: snapshotEnabled, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return hs | 
					
						
							| 
									
										
										
										
											2022-08-03 22:31:23 +08:00
										 |  |  | } |