API: Fix snapshot responses (#52998)
* API: Fix response status when snapshots are not found * API: Fix response status when snapshot key is empty * Apply suggestions from code review
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -223,3 +224,103 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
|
||||
}, sqlmock)
|
||||
})
|
||||
}
|
||||
|
||||
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",
|
||||
"/api/snapshots/12345", "/api/snapshots/:key", models.ROLE_EDITOR, func(sc *scenarioContext) {
|
||||
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",
|
||||
"/api/snapshots/12345", "/api/snapshots/:key", models.ROLE_EDITOR, func(sc *scenarioContext) {
|
||||
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",
|
||||
"/api/snapshots-delete/12345", "/api/snapshots-delete/:deleteKey", models.ROLE_EDITOR, func(sc *scenarioContext) {
|
||||
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",
|
||||
"/api/snapshots/12345", "/api/snapshots/:key", models.ROLE_EDITOR, func(sc *scenarioContext) {
|
||||
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",
|
||||
"/api/snapshots/12345", "/api/snapshots/:key", models.ROLE_EDITOR, func(sc *scenarioContext) {
|
||||
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",
|
||||
"/api/snapshots-delete/12345", "/api/snapshots-delete/:deleteKey", models.ROLE_EDITOR, func(sc *scenarioContext) {
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user