| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2022-06-02 02:16:26 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/dashboards" | 
					
						
							| 
									
										
										
										
											2022-02-17 16:31:26 +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-02-03 16:20:20 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/sqlstore/mockstore" | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	jsonModel, err := simplejson.NewJson([]byte(`{"id":100}`)) | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | 	sqlmock := mockstore.NewSQLStoreMock() | 
					
						
							| 
									
										
										
										
											2022-06-02 02:16:26 +08:00
										 |  |  | 	dashSvc := &dashboards.FakeDashboardService{} | 
					
						
							|  |  |  | 	dashSvc.On("GetDashboardAclInfoList", mock.Anything, mock.AnythingOfType("*models.GetDashboardAclInfoListQuery")).Return(nil) | 
					
						
							| 
									
										
										
										
											2022-02-17 16:31:26 +08:00
										 |  |  | 	hs := &HTTPServer{DashboardsnapshotsService: &dashboardsnapshots.Service{SQLStore: sqlmock}} | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	setUpSnapshotTest := func(t *testing.T) *models.DashboardSnapshot { | 
					
						
							|  |  |  | 		t.Helper() | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 		mockSnapshotResult := &models.DashboardSnapshot{ | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 			Id:        1, | 
					
						
							| 
									
										
										
										
											2018-12-11 05:40:26 +08:00
										 |  |  | 			Key:       "12345", | 
					
						
							|  |  |  | 			DeleteKey: "54321", | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 			Dashboard: jsonModel, | 
					
						
							|  |  |  | 			Expires:   time.Now().Add(time.Duration(1000) * time.Second), | 
					
						
							|  |  |  | 			UserId:    999999, | 
					
						
							| 
									
										
										
										
											2018-12-11 05:40:26 +08:00
										 |  |  | 			External:  true, | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | 		sqlmock.ExpectedDashboardSnapshot = mockSnapshotResult | 
					
						
							|  |  |  | 		sqlmock.ExpectedTeamsByUser = []*models.TeamDTO{} | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 		return mockSnapshotResult | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											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", | 
					
						
							|  |  |  | 			"DELETE", "/api/snapshots/12345", "/api/snapshots/:key", models.ROLE_EDITOR, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 				var externalRequest *http.Request | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | 				mockSnapshotResult := setUpSnapshotTest(t) | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				ts := setupRemoteServer(func(rw http.ResponseWriter, req *http.Request) { | 
					
						
							|  |  |  | 					externalRequest = req | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				mockSnapshotResult.ExternalDeleteUrl = ts.URL | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | 				sc.handlerFunc = hs.DeleteDashboardSnapshot | 
					
						
							| 
									
										
										
										
											2022-06-02 02:16:26 +08:00
										 |  |  | 				guardian.InitLegacyGuardian(sc.sqlStore, dashSvc) | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | 				require.Nil(t, externalRequest) | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  | 				mockSnapshotResult := setUpSnapshotTest(t) | 
					
						
							| 
									
										
										
										
											2018-12-11 05:40:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-05-24 14:55:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				mockSnapshotResult.ExternalDeleteUrl = ts.URL | 
					
						
							| 
									
										
										
										
											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-06-02 02:16:26 +08:00
										 |  |  | 		dashSvc := &dashboards.FakeDashboardService{} | 
					
						
							|  |  |  | 		dashSvc.On("GetDashboardAclInfoList", mock.Anything, mock.AnythingOfType("*models.GetDashboardAclInfoListQuery")).Run(func(args mock.Arguments) { | 
					
						
							|  |  |  | 			q := args.Get(1).(*models.GetDashboardAclInfoListQuery) | 
					
						
							|  |  |  | 			q.Result = []*models.DashboardAclInfoDTO{ | 
					
						
							|  |  |  | 				{Role: &viewerRole, Permission: models.PERMISSION_VIEW}, | 
					
						
							|  |  |  | 				{Role: &editorRole, Permission: models.PERMISSION_EDIT}, | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}).Return(nil) | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 		loggedInUserScenarioWithRole(t, "Should be able to delete a snapshot when calling DELETE on", "DELETE", | 
					
						
							|  |  |  | 			"/api/snapshots/12345", "/api/snapshots/:key", models.ROLE_EDITOR, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 				mockSnapshotResult := setUpSnapshotTest(t) | 
					
						
							| 
									
										
										
										
											2022-06-02 02:16:26 +08:00
										 |  |  | 				guardian.InitLegacyGuardian(sc.sqlStore, dashSvc) | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				mockSnapshotResult.ExternalDeleteUrl = ts.URL | 
					
						
							| 
									
										
										
										
											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", | 
					
						
							|  |  |  | 			"DELETE", "/api/snapshots/12345", "/api/snapshots/:key", models.ROLE_EDITOR, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 				mockSnapshotResult := setUpSnapshotTest(t) | 
					
						
							|  |  |  | 				mockSnapshotResult.UserId = testUserID | 
					
						
							|  |  |  | 				mockSnapshotResult.External = false | 
					
						
							| 
									
										
										
										
											2018-02-21 06:26:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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", | 
					
						
							|  |  |  | 			"DELETE", "/api/snapshots/12345", "/api/snapshots/:key", models.ROLE_EDITOR, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 				mockSnapshotResult := setUpSnapshotTest(t) | 
					
						
							|  |  |  | 				mockSnapshotResult.UserId = testUserID | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				mockSnapshotResult.ExternalDeleteUrl = ts.URL | 
					
						
							| 
									
										
										
										
											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", | 
					
						
							|  |  |  | 			"/api/snapshots/12345", "/api/snapshots/:key", models.ROLE_EDITOR, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 				mockSnapshotResult := setUpSnapshotTest(t) | 
					
						
							|  |  |  | 				mockSnapshotResult.UserId = testUserID | 
					
						
							| 
									
										
										
										
											2018-12-11 05:40:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 				}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 				t.Log("Setting external delete URL", "url", ts.URL) | 
					
						
							|  |  |  | 				mockSnapshotResult.ExternalDeleteUrl = ts.URL | 
					
						
							| 
									
										
										
										
											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", | 
					
						
							|  |  |  | 			"DELETE", "/api/snapshots/12345", "/api/snapshots/:key", models.ROLE_EDITOR, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 				mockSnapshotResult := setUpSnapshotTest(t) | 
					
						
							|  |  |  | 				mockSnapshotResult.UserId = testUserID | 
					
						
							| 
									
										
										
										
											2018-12-11 05:40:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				mockSnapshotResult.ExternalDeleteUrl = ts.URL | 
					
						
							| 
									
										
										
										
											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", | 
					
						
							|  |  |  | 			"GET", "/api/snapshots/12345", "/api/snapshots/:key", models.ROLE_EDITOR, func(sc *scenarioContext) { | 
					
						
							|  |  |  | 				setUpSnapshotTest(t) | 
					
						
							| 
									
										
										
										
											2020-10-13 16:19:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	}) | 
					
						
							|  |  |  | } |