| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											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-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-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 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-17 21:09:01 +08:00
										 |  |  | 	sqlmock := mockstore.NewSQLStoreMock() | 
					
						
							|  |  |  | 	sqlmock.ExpectedTeamsByUser = []*models.TeamDTO{} | 
					
						
							| 
									
										
										
										
											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() | 
					
						
							|  |  |  | 		dashSnapSvc.On("GetDashboardSnapshot", mock.Anything, mock.AnythingOfType("*dashboardsnapshots.GetDashboardSnapshotQuery")).Run(func(args mock.Arguments) { | 
					
						
							|  |  |  | 			q := args.Get(1).(*dashboardsnapshots.GetDashboardSnapshotQuery) | 
					
						
							|  |  |  | 			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 | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			q.Result = res | 
					
						
							|  |  |  | 		}).Return(nil) | 
					
						
							|  |  |  | 		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) { | 
					
						
							| 
									
										
										
										
											2022-07-07 21:15:39 +08:00
										 |  |  | 				hs := &HTTPServer{dashboardsnapshotsService: setUpSnapshotTest(t, 0, "")} | 
					
						
							| 
									
										
										
										
											2022-02-09 00:57:59 +08:00
										 |  |  | 				sc.handlerFunc = hs.DeleteDashboardSnapshot | 
					
						
							| 
									
										
										
										
											2022-06-17 21:09:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				dashSvc := dashboards.NewFakeDashboardService(t) | 
					
						
							| 
									
										
										
										
											2022-07-18 21:14:58 +08:00
										 |  |  | 				dashSvc.On("GetDashboardACLInfoList", mock.Anything, mock.AnythingOfType("*models.GetDashboardACLInfoListQuery")).Return(nil).Maybe() | 
					
						
							| 
									
										
										
										
											2022-06-17 21:09:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2022-06-17 21:09:01 +08:00
										 |  |  | 				hs := &HTTPServer{dashboardsnapshotsService: setUpSnapshotTest(t, 0, ts.URL)} | 
					
						
							| 
									
										
										
										
											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-06-02 02:16:26 +08:00
										 |  |  | 		dashSvc := &dashboards.FakeDashboardService{} | 
					
						
							| 
									
										
										
										
											2022-07-18 21:14:58 +08:00
										 |  |  | 		dashSvc.On("GetDashboardACLInfoList", mock.Anything, mock.AnythingOfType("*models.GetDashboardACLInfoListQuery")).Run(func(args mock.Arguments) { | 
					
						
							|  |  |  | 			q := args.Get(1).(*models.GetDashboardACLInfoListQuery) | 
					
						
							|  |  |  | 			q.Result = []*models.DashboardACLInfoDTO{ | 
					
						
							| 
									
										
										
										
											2022-06-02 02:16:26 +08:00
										 |  |  | 				{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", | 
					
						
							| 
									
										
										
										
											2022-08-10 17:56:48 +08:00
										 |  |  | 			"/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) { | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2022-06-17 21:09:01 +08:00
										 |  |  | 				hs := &HTTPServer{dashboardsnapshotsService: setUpSnapshotTest(t, 0, 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", | 
					
						
							| 
									
										
										
										
											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, "") | 
					
						
							|  |  |  | 				hs := &HTTPServer{dashboardsnapshotsService: d} | 
					
						
							| 
									
										
										
										
											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", | 
					
						
							| 
									
										
										
										
											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-06-17 21:09:01 +08:00
										 |  |  | 				hs := &HTTPServer{dashboardsnapshotsService: setUpSnapshotTest(t, testUserID, 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", | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2022-06-17 21:09:01 +08:00
										 |  |  | 				hs := &HTTPServer{dashboardsnapshotsService: setUpSnapshotTest(t, testUserID, 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", | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2022-06-17 21:09:01 +08:00
										 |  |  | 				hs := &HTTPServer{dashboardsnapshotsService: setUpSnapshotTest(t, testUserID, 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", | 
					
						
							| 
									
										
										
										
											2022-08-10 17:56:48 +08:00
										 |  |  | 			"GET", "/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) { | 
					
						
							| 
									
										
										
										
											2022-06-17 21:09:01 +08:00
										 |  |  | 				hs := &HTTPServer{dashboardsnapshotsService: setUpSnapshotTest(t, 0, "")} | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  | 	sqlmock := mockstore.NewSQLStoreMock() | 
					
						
							|  |  |  | 	sqlmock.ExpectedTeamsByUser = []*models.TeamDTO{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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) {}). | 
					
						
							|  |  |  | 			Return(dashboardsnapshots.ErrBaseNotFound.Errorf("")) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		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) | 
					
						
							|  |  |  | 			hs := &HTTPServer{dashboardsnapshotsService: d} | 
					
						
							|  |  |  | 			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) | 
					
						
							|  |  |  | 			hs := &HTTPServer{dashboardsnapshotsService: d} | 
					
						
							|  |  |  | 			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) | 
					
						
							|  |  |  | 			hs := &HTTPServer{dashboardsnapshotsService: d} | 
					
						
							|  |  |  | 			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) { | 
					
						
							|  |  |  | 	sqlmock := mockstore.NewSQLStoreMock() | 
					
						
							|  |  |  | 	sqlmock.ExpectedTeamsByUser = []*models.TeamDTO{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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) {}). | 
					
						
							|  |  |  | 			Return(errors.New("something went wrong")) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		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) | 
					
						
							|  |  |  | 			hs := &HTTPServer{dashboardsnapshotsService: d} | 
					
						
							|  |  |  | 			sc.handlerFunc = hs.GetDashboardSnapshot | 
					
						
							|  |  |  | 			sc.fakeReqWithParams("GET", sc.url, map[string]string{"key": "12345"}).exec() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert.Equal(t, http.StatusInternalServerError, 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) | 
					
						
							|  |  |  | 			hs := &HTTPServer{dashboardsnapshotsService: d} | 
					
						
							|  |  |  | 			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, | 
					
						
							|  |  |  | 		"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) | 
					
						
							|  |  |  | 			hs := &HTTPServer{dashboardsnapshotsService: d} | 
					
						
							|  |  |  | 			sc.handlerFunc = hs.DeleteDashboardSnapshotByDeleteKey | 
					
						
							|  |  |  | 			sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"deleteKey": "12345"}).exec() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert.Equal(t, http.StatusInternalServerError, sc.resp.Code) | 
					
						
							|  |  |  | 		}, sqlmock) | 
					
						
							|  |  |  | } |