2018-02-20 16:26:08 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2022-08-03 09:31:23 -05:00
|
|
|
"errors"
|
2018-12-10 15:40:26 -06:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2020-11-13 02:52:38 -06:00
|
|
|
"strings"
|
2018-02-20 16:26:08 -06:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-06-01 13:16:26 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2018-02-20 16:26:08 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2022-06-01 13:16:26 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
2022-06-17 08:09:01 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/dashboardsnapshots"
|
2022-03-21 04:49:49 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/guardian"
|
2022-08-10 04:56:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/org"
|
2022-02-03 02:20:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
|
2022-09-20 11:58:04 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/team/teamtest"
|
2018-02-20 16:26:08 -06:00
|
|
|
)
|
|
|
|
|
2020-11-13 02:52:38 -06: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 08:09:01 -05:00
|
|
|
sqlmock := mockstore.NewSQLStoreMock()
|
|
|
|
sqlmock.ExpectedTeamsByUser = []*models.TeamDTO{}
|
2020-11-13 02:52:38 -06:00
|
|
|
jsonModel, err := simplejson.NewJson([]byte(`{"id":100}`))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-06-17 08:09:01 -05:00
|
|
|
setUpSnapshotTest := func(t *testing.T, userId int64, deleteUrl string) dashboardsnapshots.Service {
|
2020-11-13 02:52:38 -06:00
|
|
|
t.Helper()
|
2022-06-17 08:09:01 -05: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 02:52:38 -06:00
|
|
|
}
|
2018-12-10 15:40:26 -06:00
|
|
|
|
2020-11-13 02:52:38 -06: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 04:56:48 -05:00
|
|
|
"DELETE", "/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) {
|
2022-07-07 08:15:39 -05:00
|
|
|
hs := &HTTPServer{dashboardsnapshotsService: setUpSnapshotTest(t, 0, "")}
|
2022-02-08 10:57:59 -06:00
|
|
|
sc.handlerFunc = hs.DeleteDashboardSnapshot
|
2022-06-17 08:09:01 -05:00
|
|
|
|
2022-09-20 11:58:04 -05:00
|
|
|
teamSvc := &teamtest.FakeService{}
|
2022-06-17 08:09:01 -05:00
|
|
|
dashSvc := dashboards.NewFakeDashboardService(t)
|
2022-12-15 08:34:17 -06:00
|
|
|
dashSvc.On("GetDashboard", mock.Anything, mock.AnythingOfType("*models.GetDashboardQuery")).Run(func(args mock.Arguments) {
|
|
|
|
q := args.Get(1).(*models.GetDashboardQuery)
|
|
|
|
q.Result = &models.Dashboard{
|
|
|
|
Id: q.Id,
|
|
|
|
Uid: q.Uid,
|
|
|
|
}
|
|
|
|
}).Return(nil).Maybe()
|
2022-07-18 08:14:58 -05:00
|
|
|
dashSvc.On("GetDashboardACLInfoList", mock.Anything, mock.AnythingOfType("*models.GetDashboardACLInfoListQuery")).Return(nil).Maybe()
|
2022-12-15 08:34:17 -06:00
|
|
|
hs.DashboardService = dashSvc
|
2022-06-17 08:09:01 -05:00
|
|
|
|
2022-09-20 11:58:04 -05:00
|
|
|
guardian.InitLegacyGuardian(sc.sqlStore, dashSvc, teamSvc)
|
2020-11-13 02:52:38 -06:00
|
|
|
sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"key": "12345"}).exec()
|
|
|
|
|
|
|
|
assert.Equal(t, 403, sc.resp.Code)
|
2022-02-08 10:57:59 -06:00
|
|
|
}, sqlmock)
|
2020-11-13 02:52:38 -06:00
|
|
|
})
|
2018-02-20 16:26:08 -06:00
|
|
|
|
2020-11-13 02:52:38 -06: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 08:09:01 -05:00
|
|
|
hs := &HTTPServer{dashboardsnapshotsService: setUpSnapshotTest(t, 0, ts.URL)}
|
2018-05-24 01:55:16 -05:00
|
|
|
|
2022-02-08 10:57:59 -06:00
|
|
|
sc.handlerFunc = hs.DeleteDashboardSnapshotByDeleteKey
|
2020-11-13 02:52:38 -06:00
|
|
|
sc.fakeReqWithParams("GET", sc.url, map[string]string{"deleteKey": "12345"}).exec()
|
2018-05-24 01:55:16 -05:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
require.Equal(t, 200, sc.resp.Code)
|
|
|
|
respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
|
|
|
|
require.NoError(t, err)
|
2018-12-10 15:40:26 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
assert.True(t, strings.HasPrefix(respJSON.Get("message").MustString(), "Snapshot deleted"))
|
2020-12-04 09:22:58 -06:00
|
|
|
assert.Equal(t, 1, respJSON.Get("id").MustInt())
|
2020-11-13 02:52:38 -06: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 01:55:16 -05:00
|
|
|
})
|
2020-11-13 02:52:38 -06:00
|
|
|
})
|
2018-05-24 01:55:16 -05:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
t.Run("When user is editor and dashboard has default ACL", func(t *testing.T) {
|
2022-09-20 11:58:04 -05:00
|
|
|
teamSvc := &teamtest.FakeService{}
|
2022-06-01 13:16:26 -05:00
|
|
|
dashSvc := &dashboards.FakeDashboardService{}
|
2022-07-18 08:14:58 -05: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-01 13:16:26 -05:00
|
|
|
{Role: &viewerRole, Permission: models.PERMISSION_VIEW},
|
|
|
|
{Role: &editorRole, Permission: models.PERMISSION_EDIT},
|
|
|
|
}
|
|
|
|
}).Return(nil)
|
2022-02-08 10:57:59 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
loggedInUserScenarioWithRole(t, "Should be able to delete a snapshot when calling DELETE on", "DELETE",
|
2022-08-10 04:56:48 -05:00
|
|
|
"/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) {
|
2020-11-13 02:52:38 -06:00
|
|
|
var externalRequest *http.Request
|
|
|
|
ts := setupRemoteServer(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
rw.WriteHeader(200)
|
|
|
|
externalRequest = req
|
2018-02-20 16:26:08 -06:00
|
|
|
})
|
2022-12-15 08:34:17 -06:00
|
|
|
dashSvc := dashboards.NewFakeDashboardService(t)
|
|
|
|
dashSvc.On("GetDashboard", mock.Anything, mock.AnythingOfType("*models.GetDashboardQuery")).Run(func(args mock.Arguments) {
|
|
|
|
q := args.Get(1).(*models.GetDashboardQuery)
|
|
|
|
q.Result = &models.Dashboard{
|
|
|
|
Id: q.Id,
|
|
|
|
OrgId: q.OrgId,
|
|
|
|
}
|
|
|
|
}).Return(nil).Maybe()
|
|
|
|
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)
|
|
|
|
guardian.InitLegacyGuardian(sc.sqlStore, dashSvc, teamSvc)
|
|
|
|
hs := &HTTPServer{dashboardsnapshotsService: setUpSnapshotTest(t, 0, ts.URL), DashboardService: dashSvc}
|
2022-02-08 10:57:59 -06:00
|
|
|
sc.handlerFunc = hs.DeleteDashboardSnapshot
|
2020-11-13 02:52:38 -06: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 09:22:58 -06:00
|
|
|
assert.Equal(t, 1, respJSON.Get("id").MustInt())
|
2020-11-13 02:52:38 -06:00
|
|
|
assert.Equal(t, ts.URL, fmt.Sprintf("http://%s", externalRequest.Host))
|
|
|
|
assert.Equal(t, "/", externalRequest.URL.EscapedPath())
|
2022-02-08 10:57:59 -06:00
|
|
|
}, sqlmock)
|
2020-11-13 02:52:38 -06:00
|
|
|
})
|
2018-02-20 16:26:08 -06:00
|
|
|
|
2020-11-13 02:52:38 -06: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 04:56:48 -05:00
|
|
|
"DELETE", "/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) {
|
2022-06-17 08:09:01 -05:00
|
|
|
d := setUpSnapshotTest(t, testUserID, "")
|
2018-02-20 16:26:08 -06:00
|
|
|
|
2022-12-15 08:34:17 -06:00
|
|
|
dashSvc := dashboards.NewFakeDashboardService(t)
|
|
|
|
hs := &HTTPServer{dashboardsnapshotsService: d, DashboardService: dashSvc}
|
2022-02-08 10:57:59 -06:00
|
|
|
sc.handlerFunc = hs.DeleteDashboardSnapshot
|
2020-11-13 02:52:38 -06:00
|
|
|
sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"key": "12345"}).exec()
|
2018-02-20 16:26:08 -06:00
|
|
|
|
2020-11-13 02:52:38 -06: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 09:22:58 -06:00
|
|
|
assert.Equal(t, 1, respJSON.Get("id").MustInt())
|
2022-02-08 10:57:59 -06:00
|
|
|
}, sqlmock)
|
2020-11-13 02:52:38 -06: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 04:56:48 -05:00
|
|
|
"DELETE", "/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) {
|
2021-07-15 08:29:52 -05:00
|
|
|
var writeErr error
|
2020-11-13 02:52:38 -06:00
|
|
|
ts := setupRemoteServer(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
rw.WriteHeader(500)
|
2021-07-15 08:29:52 -05:00
|
|
|
_, writeErr = rw.Write([]byte(`{"message":"Failed to get dashboard snapshot"}`))
|
2018-02-20 16:26:08 -06:00
|
|
|
})
|
2022-12-15 08:34:17 -06:00
|
|
|
|
|
|
|
dashSvc := dashboards.NewFakeDashboardService(t)
|
|
|
|
hs := &HTTPServer{dashboardsnapshotsService: setUpSnapshotTest(t, testUserID, ts.URL), DashboardService: dashSvc}
|
2022-02-08 10:57:59 -06:00
|
|
|
sc.handlerFunc = hs.DeleteDashboardSnapshot
|
2020-11-13 02:52:38 -06: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 09:22:58 -06: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-08 10:57:59 -06:00
|
|
|
}, sqlmock)
|
2018-12-10 15:40:26 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
loggedInUserScenarioWithRole(t,
|
|
|
|
"Should fail to delete local snapshot when an unexpected 500 error occurs when calling DELETE on", "DELETE",
|
2022-08-10 04:56:48 -05:00
|
|
|
"/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) {
|
2019-10-08 11:57:53 -05:00
|
|
|
var writeErr error
|
2020-11-13 02:52:38 -06:00
|
|
|
ts := setupRemoteServer(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
rw.WriteHeader(500)
|
|
|
|
_, writeErr = rw.Write([]byte(`{"message":"Unexpected"}`))
|
2018-12-10 15:40:26 -06:00
|
|
|
})
|
2022-06-17 08:09:01 -05:00
|
|
|
hs := &HTTPServer{dashboardsnapshotsService: setUpSnapshotTest(t, testUserID, ts.URL)}
|
2022-02-08 10:57:59 -06:00
|
|
|
sc.handlerFunc = hs.DeleteDashboardSnapshot
|
2020-11-13 02:52:38 -06:00
|
|
|
sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"key": "12345"}).exec()
|
2018-12-10 15:40:26 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
require.NoError(t, writeErr)
|
|
|
|
assert.Equal(t, 500, sc.resp.Code)
|
2022-02-08 10:57:59 -06:00
|
|
|
}, sqlmock)
|
2018-12-10 15:40:26 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
loggedInUserScenarioWithRole(t,
|
|
|
|
"Should fail to delete local snapshot when an unexpected remote error occurs when calling DELETE on",
|
2022-08-10 04:56:48 -05:00
|
|
|
"DELETE", "/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) {
|
2020-11-13 02:52:38 -06:00
|
|
|
ts := setupRemoteServer(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
rw.WriteHeader(404)
|
2018-12-10 15:40:26 -06:00
|
|
|
})
|
2022-06-17 08:09:01 -05:00
|
|
|
hs := &HTTPServer{dashboardsnapshotsService: setUpSnapshotTest(t, testUserID, ts.URL)}
|
2022-02-08 10:57:59 -06:00
|
|
|
sc.handlerFunc = hs.DeleteDashboardSnapshot
|
2020-11-13 02:52:38 -06:00
|
|
|
sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"key": "12345"}).exec()
|
|
|
|
|
|
|
|
assert.Equal(t, 500, sc.resp.Code)
|
2022-02-08 10:57:59 -06:00
|
|
|
}, sqlmock)
|
2020-10-13 03:19:42 -05:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
loggedInUserScenarioWithRole(t, "Should be able to read a snapshot's unencrypted data when calling GET on",
|
2022-08-10 04:56:48 -05:00
|
|
|
"GET", "/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) {
|
2022-06-17 08:09:01 -05:00
|
|
|
hs := &HTTPServer{dashboardsnapshotsService: setUpSnapshotTest(t, 0, "")}
|
2022-02-08 10:57:59 -06:00
|
|
|
sc.handlerFunc = hs.GetDashboardSnapshot
|
2020-11-13 02:52:38 -06:00
|
|
|
sc.fakeReqWithParams("GET", sc.url, map[string]string{"key": "12345"}).exec()
|
2020-10-13 03:19:42 -05:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
assert.Equal(t, 200, sc.resp.Code)
|
|
|
|
respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
|
|
|
|
require.NoError(t, err)
|
2020-10-13 03:19:42 -05:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
dashboard := respJSON.Get("dashboard")
|
|
|
|
id := dashboard.Get("id")
|
|
|
|
|
|
|
|
assert.Equal(t, int64(100), id.MustInt64())
|
2022-02-08 10:57:59 -06:00
|
|
|
}, sqlmock)
|
2018-02-20 16:26:08 -06:00
|
|
|
})
|
|
|
|
}
|
2022-08-03 09:31:23 -05: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 04:56:48 -05:00
|
|
|
"/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) {
|
2022-08-03 09:31:23 -05: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 04:56:48 -05:00
|
|
|
"/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) {
|
2022-08-03 09:31:23 -05: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 04:56:48 -05:00
|
|
|
"/api/snapshots-delete/12345", "/api/snapshots-delete/:deleteKey", org.RoleEditor, func(sc *scenarioContext) {
|
2022-08-03 09:31:23 -05: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 04:56:48 -05:00
|
|
|
"/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) {
|
2022-08-03 09:31:23 -05: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 04:56:48 -05:00
|
|
|
"/api/snapshots/12345", "/api/snapshots/:key", org.RoleEditor, func(sc *scenarioContext) {
|
2022-08-03 09:31:23 -05: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 04:56:48 -05:00
|
|
|
"/api/snapshots-delete/12345", "/api/snapshots-delete/:deleteKey", org.RoleEditor, func(sc *scenarioContext) {
|
2022-08-03 09:31:23 -05: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)
|
|
|
|
}
|