2018-02-20 06:55:43 -06:00
|
|
|
package dashboards
|
|
|
|
|
|
|
|
import (
|
2021-09-14 09:08:04 -05:00
|
|
|
"context"
|
2018-02-20 06:55:43 -06:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2021-03-17 10:06:10 -05:00
|
|
|
"github.com/grafana/grafana/pkg/dashboards"
|
2018-02-20 06:55:43 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2020-11-19 07:47:17 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-07-01 03:40:38 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-02-20 06:55:43 -06:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/services/guardian"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFolderService(t *testing.T) {
|
2021-07-01 03:40:38 -05:00
|
|
|
t.Run("Folder service tests", func(t *testing.T) {
|
2018-02-20 06:55:43 -06:00
|
|
|
service := dashboardServiceImpl{
|
2021-03-17 10:06:10 -05:00
|
|
|
orgId: 1,
|
|
|
|
user: &models.SignedInUser{UserId: 1},
|
|
|
|
dashboardStore: &fakeDashboardStore{},
|
2018-02-20 06:55:43 -06:00
|
|
|
}
|
|
|
|
|
2021-07-01 03:40:38 -05:00
|
|
|
t.Run("Given user has no permissions", func(t *testing.T) {
|
2018-02-20 06:55:43 -06:00
|
|
|
origNewGuardian := guardian.New
|
2018-02-20 11:08:19 -06:00
|
|
|
guardian.MockDashboardGuardian(&guardian.FakeDashboardGuardian{})
|
2018-02-20 06:55:43 -06:00
|
|
|
|
|
|
|
bus.AddHandler("test", func(query *models.GetDashboardQuery) error {
|
|
|
|
query.Result = models.NewDashboardFolder("Folder")
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
origStore := service.dashboardStore
|
|
|
|
t.Cleanup(func() {
|
|
|
|
service.dashboardStore = origStore
|
2018-02-20 06:55:43 -06:00
|
|
|
})
|
2021-03-17 10:06:10 -05:00
|
|
|
service.dashboardStore = &fakeDashboardStore{
|
|
|
|
validationError: models.ErrDashboardUpdateAccessDenied,
|
|
|
|
}
|
2018-02-20 06:55:43 -06:00
|
|
|
|
2021-07-01 03:40:38 -05:00
|
|
|
t.Run("When get folder by id should return access denied error", func(t *testing.T) {
|
2021-09-14 09:08:04 -05:00
|
|
|
_, err := service.GetFolderByID(context.Background(), 1)
|
2021-07-01 03:40:38 -05:00
|
|
|
require.Equal(t, err, models.ErrFolderAccessDenied)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("When get folder by id, with id = 0 should return default folder", func(t *testing.T) {
|
2021-09-14 09:08:04 -05:00
|
|
|
folder, err := service.GetFolderByID(context.Background(), 0)
|
2021-07-01 03:40:38 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, folder, &models.Folder{Id: 0, Title: "General"})
|
2018-02-20 06:55:43 -06:00
|
|
|
})
|
|
|
|
|
2021-07-01 03:40:38 -05:00
|
|
|
t.Run("When get folder by uid should return access denied error", func(t *testing.T) {
|
2021-09-14 09:08:04 -05:00
|
|
|
_, err := service.GetFolderByUID(context.Background(), "uid")
|
2021-07-01 03:40:38 -05:00
|
|
|
require.Equal(t, err, models.ErrFolderAccessDenied)
|
2018-02-20 06:55:43 -06:00
|
|
|
})
|
|
|
|
|
2021-07-01 03:40:38 -05:00
|
|
|
t.Run("When creating folder should return access denied error", func(t *testing.T) {
|
2021-09-14 09:08:04 -05:00
|
|
|
_, err := service.CreateFolder(context.Background(), "Folder", "")
|
2021-07-01 03:40:38 -05:00
|
|
|
require.Equal(t, err, models.ErrFolderAccessDenied)
|
2018-02-20 06:55:43 -06:00
|
|
|
})
|
|
|
|
|
2021-07-01 03:40:38 -05:00
|
|
|
t.Run("When updating folder should return access denied error", func(t *testing.T) {
|
2021-09-14 09:08:04 -05:00
|
|
|
err := service.UpdateFolder(context.Background(), "uid", &models.UpdateFolderCommand{
|
2018-02-20 06:55:43 -06:00
|
|
|
Uid: "uid",
|
|
|
|
Title: "Folder",
|
|
|
|
})
|
2021-07-01 03:40:38 -05:00
|
|
|
require.Equal(t, err, models.ErrFolderAccessDenied)
|
2018-02-20 06:55:43 -06:00
|
|
|
})
|
|
|
|
|
2021-07-01 03:40:38 -05:00
|
|
|
t.Run("When deleting folder by uid should return access denied error", func(t *testing.T) {
|
2021-09-14 09:08:04 -05:00
|
|
|
_, err := service.DeleteFolder(context.Background(), "uid", false)
|
2021-07-01 03:40:38 -05:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, err, models.ErrFolderAccessDenied)
|
2018-02-20 06:55:43 -06:00
|
|
|
})
|
|
|
|
|
2021-07-01 03:40:38 -05:00
|
|
|
t.Cleanup(func() {
|
2018-02-20 06:55:43 -06:00
|
|
|
guardian.New = origNewGuardian
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-07-01 03:40:38 -05:00
|
|
|
t.Run("Given user has permission to save", func(t *testing.T) {
|
2018-02-20 06:55:43 -06:00
|
|
|
origNewGuardian := guardian.New
|
2018-02-20 11:08:19 -06:00
|
|
|
guardian.MockDashboardGuardian(&guardian.FakeDashboardGuardian{CanSaveValue: true})
|
2018-02-20 06:55:43 -06:00
|
|
|
|
|
|
|
dash := models.NewDashboardFolder("Folder")
|
|
|
|
dash.Id = 1
|
|
|
|
|
|
|
|
bus.AddHandler("test", func(query *models.GetDashboardQuery) error {
|
|
|
|
query.Result = dash
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
origUpdateAlerting := UpdateAlerting
|
|
|
|
t.Cleanup(func() {
|
|
|
|
UpdateAlerting = origUpdateAlerting
|
2018-02-20 06:55:43 -06:00
|
|
|
})
|
2021-03-17 10:06:10 -05:00
|
|
|
UpdateAlerting = func(store dashboards.Store, orgID int64, dashboard *models.Dashboard,
|
|
|
|
user *models.SignedInUser) error {
|
2018-02-20 06:55:43 -06:00
|
|
|
return nil
|
2021-03-17 10:06:10 -05:00
|
|
|
}
|
2018-02-20 06:55:43 -06:00
|
|
|
|
|
|
|
bus.AddHandler("test", func(cmd *models.SaveDashboardCommand) error {
|
|
|
|
cmd.Result = dash
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
bus.AddHandler("test", func(cmd *models.DeleteDashboardCommand) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2021-07-01 03:40:38 -05:00
|
|
|
t.Run("When creating folder should not return access denied error", func(t *testing.T) {
|
2021-09-14 09:08:04 -05:00
|
|
|
_, err := service.CreateFolder(context.Background(), "Folder", "")
|
2021-07-01 03:40:38 -05:00
|
|
|
require.NoError(t, err)
|
2018-02-20 06:55:43 -06:00
|
|
|
})
|
|
|
|
|
2021-07-01 03:40:38 -05:00
|
|
|
t.Run("When updating folder should not return access denied error", func(t *testing.T) {
|
2021-09-14 09:08:04 -05:00
|
|
|
err := service.UpdateFolder(context.Background(), "uid", &models.UpdateFolderCommand{
|
2018-02-20 06:55:43 -06:00
|
|
|
Uid: "uid",
|
|
|
|
Title: "Folder",
|
|
|
|
})
|
2021-07-01 03:40:38 -05:00
|
|
|
require.NoError(t, err)
|
2018-02-20 06:55:43 -06:00
|
|
|
})
|
|
|
|
|
2021-07-01 03:40:38 -05:00
|
|
|
t.Run("When deleting folder by uid should not return access denied error", func(t *testing.T) {
|
2021-09-14 09:08:04 -05:00
|
|
|
_, err := service.DeleteFolder(context.Background(), "uid", false)
|
2021-07-01 03:40:38 -05:00
|
|
|
require.NoError(t, err)
|
2018-02-20 06:55:43 -06:00
|
|
|
})
|
|
|
|
|
2021-07-01 03:40:38 -05:00
|
|
|
t.Cleanup(func() {
|
2018-02-20 06:55:43 -06:00
|
|
|
guardian.New = origNewGuardian
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-07-01 03:40:38 -05:00
|
|
|
t.Run("Given user has permission to view", func(t *testing.T) {
|
2018-02-20 06:55:43 -06:00
|
|
|
origNewGuardian := guardian.New
|
2018-02-20 11:08:19 -06:00
|
|
|
guardian.MockDashboardGuardian(&guardian.FakeDashboardGuardian{CanViewValue: true})
|
2018-02-20 06:55:43 -06:00
|
|
|
|
|
|
|
dashFolder := models.NewDashboardFolder("Folder")
|
|
|
|
dashFolder.Id = 1
|
|
|
|
dashFolder.Uid = "uid-abc"
|
|
|
|
|
|
|
|
bus.AddHandler("test", func(query *models.GetDashboardQuery) error {
|
|
|
|
query.Result = dashFolder
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2021-07-01 03:40:38 -05:00
|
|
|
t.Run("When get folder by id should return folder", func(t *testing.T) {
|
2021-09-14 09:08:04 -05:00
|
|
|
f, _ := service.GetFolderByID(context.Background(), 1)
|
2021-07-01 03:40:38 -05:00
|
|
|
require.Equal(t, f.Id, dashFolder.Id)
|
|
|
|
require.Equal(t, f.Uid, dashFolder.Uid)
|
|
|
|
require.Equal(t, f.Title, dashFolder.Title)
|
2018-02-20 06:55:43 -06:00
|
|
|
})
|
|
|
|
|
2021-07-01 03:40:38 -05:00
|
|
|
t.Run("When get folder by uid should return folder", func(t *testing.T) {
|
2021-09-14 09:08:04 -05:00
|
|
|
f, _ := service.GetFolderByUID(context.Background(), "uid")
|
2021-07-01 03:40:38 -05:00
|
|
|
require.Equal(t, f.Id, dashFolder.Id)
|
|
|
|
require.Equal(t, f.Uid, dashFolder.Uid)
|
|
|
|
require.Equal(t, f.Title, dashFolder.Title)
|
2018-02-20 06:55:43 -06:00
|
|
|
})
|
|
|
|
|
2021-07-01 03:40:38 -05:00
|
|
|
t.Cleanup(func() {
|
2018-02-20 06:55:43 -06:00
|
|
|
guardian.New = origNewGuardian
|
|
|
|
})
|
|
|
|
})
|
2018-02-21 04:25:21 -06:00
|
|
|
|
2021-07-01 03:40:38 -05:00
|
|
|
t.Run("Should map errors correct", func(t *testing.T) {
|
2018-02-21 04:25:21 -06:00
|
|
|
testCases := []struct {
|
|
|
|
ActualError error
|
|
|
|
ExpectedError error
|
|
|
|
}{
|
|
|
|
{ActualError: models.ErrDashboardTitleEmpty, ExpectedError: models.ErrFolderTitleEmpty},
|
|
|
|
{ActualError: models.ErrDashboardUpdateAccessDenied, ExpectedError: models.ErrFolderAccessDenied},
|
|
|
|
{ActualError: models.ErrDashboardWithSameNameInFolderExists, ExpectedError: models.ErrFolderSameNameExists},
|
|
|
|
{ActualError: models.ErrDashboardWithSameUIDExists, ExpectedError: models.ErrFolderWithSameUIDExists},
|
|
|
|
{ActualError: models.ErrDashboardVersionMismatch, ExpectedError: models.ErrFolderVersionMismatch},
|
|
|
|
{ActualError: models.ErrDashboardNotFound, ExpectedError: models.ErrFolderNotFound},
|
|
|
|
{ActualError: models.ErrDashboardFailedGenerateUniqueUid, ExpectedError: models.ErrFolderFailedGenerateUniqueUid},
|
|
|
|
{ActualError: models.ErrDashboardInvalidUid, ExpectedError: models.ErrDashboardInvalidUid},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
actualError := toFolderError(tc.ActualError)
|
2020-11-19 07:47:17 -06:00
|
|
|
assert.EqualErrorf(t, actualError, tc.ExpectedError.Error(),
|
|
|
|
"For error '%s' expected error '%s', actual '%s'", tc.ActualError, tc.ExpectedError, actualError)
|
2018-02-21 04:25:21 -06:00
|
|
|
}
|
|
|
|
})
|
2018-02-20 06:55:43 -06:00
|
|
|
})
|
|
|
|
}
|