2018-01-29 06:51:01 -06:00
|
|
|
package api
|
|
|
|
|
2018-02-20 11:11:50 -06:00
|
|
|
import (
|
2021-09-14 09:08:04 -05:00
|
|
|
"context"
|
2018-02-21 04:24:54 -06:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2021-01-15 07:43:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
|
|
"github.com/grafana/grafana/pkg/api/routing"
|
2018-02-21 04:24:54 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2021-03-17 10:06:10 -05:00
|
|
|
dboards "github.com/grafana/grafana/pkg/dashboards"
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2018-03-06 16:59:45 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
2019-03-06 01:09:34 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2020-11-13 02:52:38 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-02-20 11:11:50 -06:00
|
|
|
)
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
func TestFoldersAPIEndpoint(t *testing.T) {
|
|
|
|
t.Run("Given a correct request for creating a folder", func(t *testing.T) {
|
|
|
|
cmd := models.CreateFolderCommand{
|
|
|
|
Uid: "uid",
|
|
|
|
Title: "Folder",
|
|
|
|
}
|
2018-02-21 04:24:54 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
mock := &fakeFolderService{
|
|
|
|
CreateFolderResult: &models.Folder{Id: 1, Uid: "uid", Title: "Folder"},
|
|
|
|
}
|
2018-02-21 04:24:54 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
createFolderScenario(t, "When calling POST on", "/api/folders", "/api/folders", mock, cmd,
|
|
|
|
func(sc *scenarioContext) {
|
2018-02-21 04:24:54 -06:00
|
|
|
callCreateFolder(sc)
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
folder := dtos.Folder{}
|
|
|
|
err := json.NewDecoder(sc.resp.Body).Decode(&folder)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(1), folder.Id)
|
|
|
|
assert.Equal(t, "uid", folder.Uid)
|
|
|
|
assert.Equal(t, "Folder", folder.Title)
|
2018-02-21 04:24:54 -06:00
|
|
|
})
|
2020-11-13 02:52:38 -06:00
|
|
|
})
|
2018-02-21 04:24:54 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
t.Run("Given incorrect requests for creating a folder", func(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
Error error
|
|
|
|
ExpectedStatusCode int
|
|
|
|
}{
|
2021-07-06 04:11:29 -05:00
|
|
|
{Error: models.ErrFolderWithSameUIDExists, ExpectedStatusCode: 409},
|
2020-11-13 02:52:38 -06:00
|
|
|
{Error: models.ErrFolderTitleEmpty, ExpectedStatusCode: 400},
|
2021-07-06 04:11:29 -05:00
|
|
|
{Error: models.ErrFolderSameNameExists, ExpectedStatusCode: 409},
|
2020-11-13 02:52:38 -06:00
|
|
|
{Error: models.ErrDashboardInvalidUid, ExpectedStatusCode: 400},
|
|
|
|
{Error: models.ErrDashboardUidTooLong, ExpectedStatusCode: 400},
|
|
|
|
{Error: models.ErrFolderAccessDenied, ExpectedStatusCode: 403},
|
|
|
|
{Error: models.ErrFolderNotFound, ExpectedStatusCode: 404},
|
|
|
|
{Error: models.ErrFolderVersionMismatch, ExpectedStatusCode: 412},
|
|
|
|
{Error: models.ErrFolderFailedGenerateUniqueUid, ExpectedStatusCode: 500},
|
|
|
|
}
|
2018-02-21 04:24:54 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
cmd := models.CreateFolderCommand{
|
|
|
|
Uid: "uid",
|
|
|
|
Title: "Folder",
|
|
|
|
}
|
2018-02-21 04:24:54 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
for _, tc := range testCases {
|
|
|
|
mock := &fakeFolderService{
|
|
|
|
CreateFolderError: tc.Error,
|
|
|
|
}
|
2018-02-21 04:24:54 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
createFolderScenario(t, fmt.Sprintf("Expect '%s' error when calling POST on", tc.Error.Error()),
|
|
|
|
"/api/folders", "/api/folders", mock, cmd, func(sc *scenarioContext) {
|
2018-02-21 04:24:54 -06:00
|
|
|
callCreateFolder(sc)
|
2020-11-13 02:52:38 -06:00
|
|
|
assert.Equalf(t, tc.ExpectedStatusCode, sc.resp.Code, "Wrong status code for error %s", tc.Error)
|
2018-02-21 04:24:54 -06:00
|
|
|
})
|
2020-11-13 02:52:38 -06:00
|
|
|
}
|
|
|
|
})
|
2018-02-21 04:24:54 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
t.Run("Given a correct request for updating a folder", func(t *testing.T) {
|
|
|
|
cmd := models.UpdateFolderCommand{
|
|
|
|
Title: "Folder upd",
|
|
|
|
}
|
2018-02-21 04:24:54 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
mock := &fakeFolderService{
|
|
|
|
UpdateFolderResult: &models.Folder{Id: 1, Uid: "uid", Title: "Folder upd"},
|
|
|
|
}
|
2018-02-21 04:24:54 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
updateFolderScenario(t, "When calling PUT on", "/api/folders/uid", "/api/folders/:uid", mock, cmd,
|
|
|
|
func(sc *scenarioContext) {
|
2018-02-21 04:24:54 -06:00
|
|
|
callUpdateFolder(sc)
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
folder := dtos.Folder{}
|
|
|
|
err := json.NewDecoder(sc.resp.Body).Decode(&folder)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(1), folder.Id)
|
|
|
|
assert.Equal(t, "uid", folder.Uid)
|
|
|
|
assert.Equal(t, "Folder upd", folder.Title)
|
2018-02-21 04:24:54 -06:00
|
|
|
})
|
2020-11-13 02:52:38 -06:00
|
|
|
})
|
2018-02-21 04:24:54 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
t.Run("Given incorrect requests for updating a folder", func(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
Error error
|
|
|
|
ExpectedStatusCode int
|
|
|
|
}{
|
2021-07-06 04:11:29 -05:00
|
|
|
{Error: models.ErrFolderWithSameUIDExists, ExpectedStatusCode: 409},
|
2020-11-13 02:52:38 -06:00
|
|
|
{Error: models.ErrFolderTitleEmpty, ExpectedStatusCode: 400},
|
2021-07-06 04:11:29 -05:00
|
|
|
{Error: models.ErrFolderSameNameExists, ExpectedStatusCode: 409},
|
2020-11-13 02:52:38 -06:00
|
|
|
{Error: models.ErrDashboardInvalidUid, ExpectedStatusCode: 400},
|
|
|
|
{Error: models.ErrDashboardUidTooLong, ExpectedStatusCode: 400},
|
|
|
|
{Error: models.ErrFolderAccessDenied, ExpectedStatusCode: 403},
|
|
|
|
{Error: models.ErrFolderNotFound, ExpectedStatusCode: 404},
|
|
|
|
{Error: models.ErrFolderVersionMismatch, ExpectedStatusCode: 412},
|
|
|
|
{Error: models.ErrFolderFailedGenerateUniqueUid, ExpectedStatusCode: 500},
|
|
|
|
}
|
2018-02-21 04:24:54 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
cmd := models.UpdateFolderCommand{
|
|
|
|
Title: "Folder upd",
|
|
|
|
}
|
2018-02-21 04:24:54 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
for _, tc := range testCases {
|
|
|
|
mock := &fakeFolderService{
|
|
|
|
UpdateFolderError: tc.Error,
|
|
|
|
}
|
2018-02-21 04:24:54 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
updateFolderScenario(t, fmt.Sprintf("Expect '%s' error when calling PUT on", tc.Error.Error()),
|
|
|
|
"/api/folders/uid", "/api/folders/:uid", mock, cmd, func(sc *scenarioContext) {
|
2018-02-21 04:24:54 -06:00
|
|
|
callUpdateFolder(sc)
|
2020-11-13 02:52:38 -06:00
|
|
|
assert.Equalf(t, tc.ExpectedStatusCode, sc.resp.Code, "Wrong status code for %s", tc.Error)
|
2018-02-21 04:24:54 -06:00
|
|
|
})
|
2020-11-13 02:52:38 -06:00
|
|
|
}
|
2018-02-21 04:24:54 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func callCreateFolder(sc *scenarioContext) {
|
|
|
|
sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
func createFolderScenario(t *testing.T, desc string, url string, routePattern string, mock *fakeFolderService,
|
|
|
|
cmd models.CreateFolderCommand, fn scenarioFunc) {
|
|
|
|
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
|
|
|
|
t.Cleanup(bus.ClearBusHandlers)
|
2018-02-21 04:24:54 -06:00
|
|
|
|
2019-03-06 01:09:34 -06:00
|
|
|
hs := HTTPServer{
|
|
|
|
Bus: bus.GetBus(),
|
2019-03-12 07:59:53 -05:00
|
|
|
Cfg: setting.NewCfg(),
|
2019-03-06 01:09:34 -06:00
|
|
|
}
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
sc := setupScenarioContext(t, url)
|
2021-01-15 07:43:20 -06:00
|
|
|
sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) response.Response {
|
2021-11-29 03:18:01 -06:00
|
|
|
c.Req.Body = mockRequestBody(cmd)
|
2018-02-21 04:24:54 -06:00
|
|
|
sc.context = c
|
2020-11-13 02:52:38 -06:00
|
|
|
sc.context.SignedInUser = &models.SignedInUser{OrgId: testOrgID, UserId: testUserID}
|
2018-02-21 04:24:54 -06:00
|
|
|
|
2021-11-29 03:18:01 -06:00
|
|
|
return hs.CreateFolder(c)
|
2018-02-21 04:24:54 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
origNewFolderService := dashboards.NewFolderService
|
|
|
|
mockFolderService(mock)
|
|
|
|
|
|
|
|
sc.m.Post(routePattern, sc.defaultHandler)
|
2018-02-20 06:57:32 -06:00
|
|
|
|
2018-02-21 04:24:54 -06:00
|
|
|
defer func() {
|
|
|
|
dashboards.NewFolderService = origNewFolderService
|
|
|
|
}()
|
2018-02-20 06:57:32 -06:00
|
|
|
|
2018-02-21 04:24:54 -06:00
|
|
|
fn(sc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func callUpdateFolder(sc *scenarioContext) {
|
|
|
|
sc.fakeReqWithParams("PUT", sc.url, map[string]string{}).exec()
|
|
|
|
}
|
2018-02-20 06:57:32 -06:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
func updateFolderScenario(t *testing.T, desc string, url string, routePattern string, mock *fakeFolderService,
|
|
|
|
cmd models.UpdateFolderCommand, fn scenarioFunc) {
|
|
|
|
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
|
2018-02-21 04:24:54 -06:00
|
|
|
defer bus.ClearBusHandlers()
|
2018-01-29 06:51:01 -06:00
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
hs := HTTPServer{
|
|
|
|
Cfg: setting.NewCfg(),
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
sc := setupScenarioContext(t, url)
|
2021-01-15 07:43:20 -06:00
|
|
|
sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) response.Response {
|
2021-11-29 03:18:01 -06:00
|
|
|
c.Req.Body = mockRequestBody(cmd)
|
2018-02-21 04:24:54 -06:00
|
|
|
sc.context = c
|
2020-11-13 02:52:38 -06:00
|
|
|
sc.context.SignedInUser = &models.SignedInUser{OrgId: testOrgID, UserId: testUserID}
|
2018-02-20 06:57:32 -06:00
|
|
|
|
2021-11-29 03:18:01 -06:00
|
|
|
return hs.UpdateFolder(c)
|
2018-02-21 04:24:54 -06:00
|
|
|
})
|
2018-02-20 06:57:32 -06:00
|
|
|
|
2018-02-21 04:24:54 -06:00
|
|
|
origNewFolderService := dashboards.NewFolderService
|
2020-11-13 02:52:38 -06:00
|
|
|
t.Cleanup(func() {
|
|
|
|
dashboards.NewFolderService = origNewFolderService
|
|
|
|
})
|
2018-02-21 04:24:54 -06:00
|
|
|
mockFolderService(mock)
|
2018-02-20 06:57:32 -06:00
|
|
|
|
2018-02-21 04:24:54 -06:00
|
|
|
sc.m.Put(routePattern, sc.defaultHandler)
|
2018-02-20 06:57:32 -06:00
|
|
|
|
2018-02-21 04:24:54 -06:00
|
|
|
fn(sc)
|
|
|
|
})
|
|
|
|
}
|
2018-02-20 11:11:50 -06:00
|
|
|
|
|
|
|
type fakeFolderService struct {
|
2021-03-17 10:06:10 -05:00
|
|
|
dashboards.FolderService
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
GetFoldersResult []*models.Folder
|
2018-02-20 11:11:50 -06:00
|
|
|
GetFoldersError error
|
2020-03-04 05:57:20 -06:00
|
|
|
GetFolderByUIDResult *models.Folder
|
2018-03-22 16:13:46 -05:00
|
|
|
GetFolderByUIDError error
|
2020-03-04 05:57:20 -06:00
|
|
|
GetFolderByIDResult *models.Folder
|
2018-03-22 16:13:46 -05:00
|
|
|
GetFolderByIDError error
|
2020-03-04 05:57:20 -06:00
|
|
|
CreateFolderResult *models.Folder
|
2018-02-20 11:11:50 -06:00
|
|
|
CreateFolderError error
|
2020-03-04 05:57:20 -06:00
|
|
|
UpdateFolderResult *models.Folder
|
2018-02-20 11:11:50 -06:00
|
|
|
UpdateFolderError error
|
2020-03-04 05:57:20 -06:00
|
|
|
DeleteFolderResult *models.Folder
|
2018-02-20 11:11:50 -06:00
|
|
|
DeleteFolderError error
|
|
|
|
DeletedFolderUids []string
|
|
|
|
}
|
|
|
|
|
2021-09-14 09:08:04 -05:00
|
|
|
func (s *fakeFolderService) GetFolders(ctx context.Context, limit int64, page int64) ([]*models.Folder, error) {
|
2018-02-20 11:11:50 -06:00
|
|
|
return s.GetFoldersResult, s.GetFoldersError
|
|
|
|
}
|
|
|
|
|
2021-09-14 09:08:04 -05:00
|
|
|
func (s *fakeFolderService) GetFolderByID(ctx context.Context, id int64) (*models.Folder, error) {
|
2018-03-22 16:13:46 -05:00
|
|
|
return s.GetFolderByIDResult, s.GetFolderByIDError
|
2018-02-20 11:11:50 -06:00
|
|
|
}
|
|
|
|
|
2021-09-14 09:08:04 -05:00
|
|
|
func (s *fakeFolderService) GetFolderByUID(ctx context.Context, uid string) (*models.Folder, error) {
|
2018-03-22 16:13:46 -05:00
|
|
|
return s.GetFolderByUIDResult, s.GetFolderByUIDError
|
2018-02-20 11:11:50 -06:00
|
|
|
}
|
|
|
|
|
2021-09-14 09:08:04 -05:00
|
|
|
func (s *fakeFolderService) CreateFolder(ctx context.Context, title, uid string) (*models.Folder, error) {
|
2021-03-17 10:06:10 -05:00
|
|
|
return s.CreateFolderResult, s.CreateFolderError
|
2018-02-20 11:11:50 -06:00
|
|
|
}
|
|
|
|
|
2021-09-14 09:08:04 -05:00
|
|
|
func (s *fakeFolderService) UpdateFolder(ctx context.Context, existingUID string, cmd *models.UpdateFolderCommand) error {
|
2018-02-21 04:24:54 -06:00
|
|
|
cmd.Result = s.UpdateFolderResult
|
2018-02-20 11:11:50 -06:00
|
|
|
return s.UpdateFolderError
|
|
|
|
}
|
|
|
|
|
2021-09-14 09:08:04 -05:00
|
|
|
func (s *fakeFolderService) DeleteFolder(ctx context.Context, uid string, forceDeleteRules bool) (*models.Folder, error) {
|
2018-02-20 11:11:50 -06:00
|
|
|
s.DeletedFolderUids = append(s.DeletedFolderUids, uid)
|
|
|
|
return s.DeleteFolderResult, s.DeleteFolderError
|
|
|
|
}
|
|
|
|
|
|
|
|
func mockFolderService(mock *fakeFolderService) {
|
2021-03-17 10:06:10 -05:00
|
|
|
dashboards.NewFolderService = func(orgId int64, user *models.SignedInUser,
|
|
|
|
dashboardStore dboards.Store) dashboards.FolderService {
|
2018-02-20 11:11:50 -06:00
|
|
|
return mock
|
|
|
|
}
|
|
|
|
}
|