2018-02-20 06:55:43 -06:00
|
|
|
package dashboards
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2020-11-19 07:47:17 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-02-20 06:55:43 -06:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/services/guardian"
|
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFolderService(t *testing.T) {
|
|
|
|
Convey("Folder service tests", t, func() {
|
|
|
|
service := dashboardServiceImpl{
|
|
|
|
orgId: 1,
|
|
|
|
user: &models.SignedInUser{UserId: 1},
|
|
|
|
}
|
|
|
|
|
|
|
|
Convey("Given user has no permissions", func() {
|
|
|
|
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
|
|
|
|
})
|
|
|
|
|
|
|
|
bus.AddHandler("test", func(cmd *models.ValidateDashboardAlertsCommand) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
bus.AddHandler("test", func(cmd *models.ValidateDashboardBeforeSaveCommand) error {
|
2018-04-10 17:23:50 -05:00
|
|
|
cmd.Result = &models.ValidateDashboardBeforeSaveResult{}
|
2018-02-20 06:55:43 -06:00
|
|
|
return models.ErrDashboardUpdateAccessDenied
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When get folder by id should return access denied error", func() {
|
2018-03-22 16:13:46 -05:00
|
|
|
_, err := service.GetFolderByID(1)
|
2018-02-20 06:55:43 -06:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(err, ShouldEqual, models.ErrFolderAccessDenied)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When get folder by uid should return access denied error", func() {
|
2018-03-22 16:13:46 -05:00
|
|
|
_, err := service.GetFolderByUID("uid")
|
2018-02-20 06:55:43 -06:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(err, ShouldEqual, models.ErrFolderAccessDenied)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When creating folder should return access denied error", func() {
|
|
|
|
err := service.CreateFolder(&models.CreateFolderCommand{
|
|
|
|
Title: "Folder",
|
|
|
|
})
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(err, ShouldEqual, models.ErrFolderAccessDenied)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When updating folder should return access denied error", func() {
|
|
|
|
err := service.UpdateFolder("uid", &models.UpdateFolderCommand{
|
|
|
|
Uid: "uid",
|
|
|
|
Title: "Folder",
|
|
|
|
})
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(err, ShouldEqual, models.ErrFolderAccessDenied)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When deleting folder by uid should return access denied error", func() {
|
|
|
|
_, err := service.DeleteFolder("uid")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(err, ShouldEqual, models.ErrFolderAccessDenied)
|
|
|
|
})
|
|
|
|
|
|
|
|
Reset(func() {
|
|
|
|
guardian.New = origNewGuardian
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Given user has permission to save", func() {
|
|
|
|
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
|
|
|
|
})
|
|
|
|
|
|
|
|
bus.AddHandler("test", func(cmd *models.ValidateDashboardAlertsCommand) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
bus.AddHandler("test", func(cmd *models.ValidateDashboardBeforeSaveCommand) error {
|
2018-04-10 17:23:50 -05:00
|
|
|
cmd.Result = &models.ValidateDashboardBeforeSaveResult{}
|
2018-02-20 06:55:43 -06:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
bus.AddHandler("test", func(cmd *models.UpdateDashboardAlertsCommand) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
bus.AddHandler("test", func(cmd *models.SaveDashboardCommand) error {
|
|
|
|
cmd.Result = dash
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
bus.AddHandler("test", func(cmd *models.DeleteDashboardCommand) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2018-04-13 10:45:32 -05:00
|
|
|
provisioningValidated := false
|
|
|
|
|
2019-04-30 06:32:18 -05:00
|
|
|
bus.AddHandler("test", func(query *models.GetProvisionedDashboardDataByIdQuery) error {
|
2018-04-13 10:45:32 -05:00
|
|
|
provisioningValidated = true
|
2019-04-30 06:32:18 -05:00
|
|
|
query.Result = nil
|
2018-04-13 10:45:32 -05:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2018-02-20 06:55:43 -06:00
|
|
|
Convey("When creating folder should not return access denied error", func() {
|
|
|
|
err := service.CreateFolder(&models.CreateFolderCommand{
|
|
|
|
Title: "Folder",
|
|
|
|
})
|
|
|
|
So(err, ShouldBeNil)
|
2018-04-13 10:45:32 -05:00
|
|
|
So(provisioningValidated, ShouldBeFalse)
|
2018-02-20 06:55:43 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When updating folder should not return access denied error", func() {
|
|
|
|
err := service.UpdateFolder("uid", &models.UpdateFolderCommand{
|
|
|
|
Uid: "uid",
|
|
|
|
Title: "Folder",
|
|
|
|
})
|
|
|
|
So(err, ShouldBeNil)
|
2018-04-13 10:45:32 -05:00
|
|
|
So(provisioningValidated, ShouldBeFalse)
|
2018-02-20 06:55:43 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When deleting folder by uid should not return access denied error", func() {
|
|
|
|
_, err := service.DeleteFolder("uid")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Reset(func() {
|
|
|
|
guardian.New = origNewGuardian
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Given user has permission to view", func() {
|
|
|
|
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
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When get folder by id should return folder", func() {
|
2018-03-22 16:13:46 -05:00
|
|
|
f, _ := service.GetFolderByID(1)
|
2018-02-20 06:55:43 -06:00
|
|
|
So(f.Id, ShouldEqual, dashFolder.Id)
|
|
|
|
So(f.Uid, ShouldEqual, dashFolder.Uid)
|
|
|
|
So(f.Title, ShouldEqual, dashFolder.Title)
|
|
|
|
})
|
|
|
|
|
2018-02-20 11:08:19 -06:00
|
|
|
Convey("When get folder by uid should return folder", func() {
|
2018-03-22 16:13:46 -05:00
|
|
|
f, _ := service.GetFolderByUID("uid")
|
2018-02-20 06:55:43 -06:00
|
|
|
So(f.Id, ShouldEqual, dashFolder.Id)
|
|
|
|
So(f.Uid, ShouldEqual, dashFolder.Uid)
|
|
|
|
So(f.Title, ShouldEqual, dashFolder.Title)
|
|
|
|
})
|
|
|
|
|
|
|
|
Reset(func() {
|
|
|
|
guardian.New = origNewGuardian
|
|
|
|
})
|
|
|
|
})
|
2018-02-21 04:25:21 -06:00
|
|
|
|
|
|
|
Convey("Should map errors correct", func() {
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|