2022-02-16 07:15:44 -06:00
|
|
|
package service
|
2018-02-19 04:12:56 -06:00
|
|
|
|
|
|
|
import (
|
2021-11-03 08:10:39 -05:00
|
|
|
"context"
|
2022-02-16 07:15:44 -06:00
|
|
|
"errors"
|
2020-06-29 07:08:32 -05:00
|
|
|
"testing"
|
|
|
|
|
2022-03-10 11:19:50 -06:00
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-07-13 04:33:28 -05:00
|
|
|
"github.com/xorcare/pointer"
|
2022-03-10 11:19:50 -06:00
|
|
|
|
2022-02-16 07:15:44 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2022-11-02 08:15:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/appcontext"
|
2022-02-16 07:15:44 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2022-06-30 08:31:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
2022-11-11 07:28:24 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/folder"
|
2023-02-01 07:43:21 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/folder/foldertest"
|
2019-04-10 06:29:10 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/guardian"
|
2022-08-10 04:56:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2022-02-16 07:15:44 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2018-02-19 04:12:56 -06:00
|
|
|
)
|
|
|
|
|
2022-07-15 11:06:44 -05:00
|
|
|
func TestDashboardService(t *testing.T) {
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Dashboard service tests", func(t *testing.T) {
|
2022-06-30 08:31:54 -05:00
|
|
|
fakeStore := dashboards.FakeDashboardStore{}
|
2022-02-16 07:15:44 -06:00
|
|
|
defer fakeStore.AssertExpectations(t)
|
2022-07-13 04:33:28 -05:00
|
|
|
|
2023-02-01 07:43:21 -06:00
|
|
|
folderStore := foldertest.NewFakeFolderStore(t)
|
2023-01-19 10:38:07 -06:00
|
|
|
|
2022-02-16 07:15:44 -06:00
|
|
|
service := &DashboardServiceImpl{
|
2022-07-13 04:33:28 -05:00
|
|
|
cfg: setting.NewCfg(),
|
2022-02-28 02:54:56 -06:00
|
|
|
log: log.New("test.logger"),
|
|
|
|
dashboardStore: &fakeStore,
|
2023-01-19 10:38:07 -06:00
|
|
|
folderStore: folderStore,
|
2022-02-28 02:54:56 -06:00
|
|
|
dashAlertExtractor: &dummyDashAlertExtractor{},
|
2020-02-28 07:32:01 -06:00
|
|
|
}
|
2018-02-19 04:12:56 -06:00
|
|
|
|
|
|
|
origNewDashboardGuardian := guardian.New
|
2021-10-27 08:17:50 -05:00
|
|
|
defer func() { guardian.New = origNewDashboardGuardian }()
|
2018-02-20 11:08:19 -06:00
|
|
|
guardian.MockDashboardGuardian(&guardian.FakeDashboardGuardian{CanSaveValue: true})
|
2018-02-19 04:12:56 -06:00
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Save dashboard validation", func(t *testing.T) {
|
2022-06-30 08:31:54 -05:00
|
|
|
dto := &dashboards.SaveDashboardDTO{}
|
2018-02-19 04:12:56 -06:00
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("When saving a dashboard with empty title it should return error", func(t *testing.T) {
|
2018-02-19 04:12:56 -06:00
|
|
|
titles := []string{"", " ", " \t "}
|
|
|
|
|
|
|
|
for _, title := range titles {
|
2023-01-16 09:33:55 -06:00
|
|
|
dto.Dashboard = dashboards.NewDashboard(title)
|
2021-11-03 08:10:39 -05:00
|
|
|
_, err := service.SaveDashboard(context.Background(), dto, false)
|
2022-06-30 08:31:54 -05:00
|
|
|
require.Equal(t, err, dashboards.ErrDashboardTitleEmpty)
|
2018-02-19 04:12:56 -06:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Should return validation error if it's a folder and have a folder id", func(t *testing.T) {
|
2023-01-16 09:33:55 -06:00
|
|
|
dto.Dashboard = dashboards.NewDashboardFolder("Folder")
|
|
|
|
dto.Dashboard.FolderID = 1
|
2021-11-03 08:10:39 -05:00
|
|
|
_, err := service.SaveDashboard(context.Background(), dto, false)
|
2022-06-30 08:31:54 -05:00
|
|
|
require.Equal(t, err, dashboards.ErrDashboardFolderCannotHaveParent)
|
2018-02-19 04:12:56 -06:00
|
|
|
})
|
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Should return validation error if folder is named General", func(t *testing.T) {
|
2023-01-16 09:33:55 -06:00
|
|
|
dto.Dashboard = dashboards.NewDashboardFolder("General")
|
2021-11-03 08:10:39 -05:00
|
|
|
_, err := service.SaveDashboard(context.Background(), dto, false)
|
2022-06-30 08:31:54 -05:00
|
|
|
require.Equal(t, err, dashboards.ErrDashboardFolderNameExists)
|
2018-02-19 04:12:56 -06:00
|
|
|
})
|
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("When saving a dashboard should validate uid", func(t *testing.T) {
|
2018-02-19 04:12:56 -06:00
|
|
|
testCases := []struct {
|
|
|
|
Uid string
|
|
|
|
Error error
|
|
|
|
}{
|
|
|
|
{Uid: "", Error: nil},
|
|
|
|
{Uid: " ", Error: nil},
|
|
|
|
{Uid: " \t ", Error: nil},
|
|
|
|
{Uid: "asdf90_-", Error: nil},
|
2022-06-30 08:31:54 -05:00
|
|
|
{Uid: "asdf/90", Error: dashboards.ErrDashboardInvalidUid},
|
2018-02-19 04:12:56 -06:00
|
|
|
{Uid: " asdfghjklqwertyuiopzxcvbnmasdfghjklqwer ", Error: nil},
|
2022-06-30 08:31:54 -05:00
|
|
|
{Uid: "asdfghjklqwertyuiopzxcvbnmasdfghjklqwertyuiopzxcvbnmasdfghjklqwertyuiopzxcvbnm", Error: dashboards.ErrDashboardUidTooLong},
|
2018-02-19 04:12:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2023-01-16 09:33:55 -06:00
|
|
|
dto.Dashboard = dashboards.NewDashboard("title")
|
|
|
|
dto.Dashboard.SetUID(tc.Uid)
|
2022-08-10 04:56:48 -05:00
|
|
|
dto.User = &user.SignedInUser{}
|
2018-02-19 04:12:56 -06:00
|
|
|
|
2022-02-16 07:15:44 -06:00
|
|
|
if tc.Error == nil {
|
2022-11-02 08:15:50 -05:00
|
|
|
fakeStore.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything, mock.AnythingOfType("bool")).Return(true, nil).Once()
|
2022-02-16 07:15:44 -06:00
|
|
|
}
|
|
|
|
_, err := service.BuildSaveDashboardCommand(context.Background(), dto, true, false)
|
2021-10-27 08:17:50 -05:00
|
|
|
require.Equal(t, err, tc.Error)
|
2018-02-19 04:12:56 -06:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Should return validation error if dashboard is provisioned", func(t *testing.T) {
|
2022-11-02 08:15:50 -05:00
|
|
|
fakeStore.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything, mock.AnythingOfType("bool")).Return(true, nil).Once()
|
2023-01-18 06:52:41 -06:00
|
|
|
fakeStore.On("GetProvisionedDataByDashboardID", mock.Anything, mock.AnythingOfType("int64")).Return(&dashboards.DashboardProvisioning{}, nil).Once()
|
2018-03-27 08:12:47 -05:00
|
|
|
|
2023-01-16 09:33:55 -06:00
|
|
|
dto.Dashboard = dashboards.NewDashboard("Dash")
|
|
|
|
dto.Dashboard.SetID(3)
|
2022-08-11 06:28:55 -05:00
|
|
|
dto.User = &user.SignedInUser{UserID: 1}
|
2021-11-03 08:10:39 -05:00
|
|
|
_, err := service.SaveDashboard(context.Background(), dto, false)
|
2022-06-30 08:31:54 -05:00
|
|
|
require.Equal(t, err, dashboards.ErrDashboardCannotSaveProvisionedDashboard)
|
2018-03-27 08:12:47 -05:00
|
|
|
})
|
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Should not return validation error if dashboard is provisioned but UI updates allowed", func(t *testing.T) {
|
2022-11-02 08:15:50 -05:00
|
|
|
fakeStore.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything, mock.AnythingOfType("bool")).Return(true, nil).Once()
|
2023-01-16 09:33:55 -06:00
|
|
|
fakeStore.On("SaveDashboard", mock.Anything, mock.AnythingOfType("dashboards.SaveDashboardCommand")).Return(&dashboards.Dashboard{Data: simplejson.New()}, nil).Once()
|
2019-10-31 08:27:31 -05:00
|
|
|
|
2023-01-16 09:33:55 -06:00
|
|
|
dto.Dashboard = dashboards.NewDashboard("Dash")
|
|
|
|
dto.Dashboard.SetID(3)
|
2022-08-11 06:28:55 -05:00
|
|
|
dto.User = &user.SignedInUser{UserID: 1}
|
2021-11-03 08:10:39 -05:00
|
|
|
_, err := service.SaveDashboard(context.Background(), dto, true)
|
2021-10-27 08:17:50 -05:00
|
|
|
require.NoError(t, err)
|
2019-10-31 08:27:31 -05:00
|
|
|
})
|
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Should return validation error if alert data is invalid", func(t *testing.T) {
|
2022-07-13 04:33:28 -05:00
|
|
|
origAlertingEnabledSet := setting.AlertingEnabled != nil
|
|
|
|
origAlertingEnabledVal := false
|
|
|
|
if origAlertingEnabledSet {
|
|
|
|
origAlertingEnabledVal = *setting.AlertingEnabled
|
|
|
|
}
|
|
|
|
setting.AlertingEnabled = pointer.Bool(true)
|
|
|
|
t.Cleanup(func() {
|
|
|
|
if !origAlertingEnabledSet {
|
|
|
|
setting.AlertingEnabled = nil
|
|
|
|
} else {
|
|
|
|
setting.AlertingEnabled = &origAlertingEnabledVal
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-11-02 08:15:50 -05:00
|
|
|
fakeStore.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything, mock.AnythingOfType("bool")).Return(true, nil).Once()
|
|
|
|
fakeStore.On("GetProvisionedDataByDashboardID", mock.Anything, mock.AnythingOfType("int64")).Return(nil, nil).Once()
|
2023-01-16 09:33:55 -06:00
|
|
|
fakeStore.On("SaveDashboard", mock.Anything, mock.AnythingOfType("dashboards.SaveDashboardCommand")).Return(&dashboards.Dashboard{Data: simplejson.New()}, nil).Once()
|
2022-02-16 07:15:44 -06:00
|
|
|
fakeStore.On("SaveAlerts", mock.Anything, mock.Anything, mock.Anything).Return(errors.New("alert validation error")).Once()
|
2018-02-19 04:12:56 -06:00
|
|
|
|
2023-01-16 09:33:55 -06:00
|
|
|
dto.Dashboard = dashboards.NewDashboard("Dash")
|
2022-08-11 06:28:55 -05:00
|
|
|
dto.User = &user.SignedInUser{UserID: 1}
|
2021-11-03 08:10:39 -05:00
|
|
|
_, err := service.SaveDashboard(context.Background(), dto, false)
|
2022-07-13 04:33:28 -05:00
|
|
|
require.Error(t, err)
|
2021-10-27 08:17:50 -05:00
|
|
|
require.Equal(t, err.Error(), "alert validation error")
|
2018-02-19 04:12:56 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Save provisioned dashboard validation", func(t *testing.T) {
|
2022-06-30 08:31:54 -05:00
|
|
|
dto := &dashboards.SaveDashboardDTO{}
|
2018-04-13 10:45:32 -05:00
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Should not return validation error if dashboard is provisioned", func(t *testing.T) {
|
2022-11-02 08:15:50 -05:00
|
|
|
fakeStore.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything, mock.AnythingOfType("bool")).Return(true, nil).Once()
|
2023-01-16 09:33:55 -06:00
|
|
|
fakeStore.On("SaveProvisionedDashboard", mock.Anything, mock.AnythingOfType("dashboards.SaveDashboardCommand"), mock.AnythingOfType("*dashboards.DashboardProvisioning")).Return(&dashboards.Dashboard{Data: simplejson.New()}, nil).Once()
|
2018-04-13 10:45:32 -05:00
|
|
|
|
2023-01-16 09:33:55 -06:00
|
|
|
dto.Dashboard = dashboards.NewDashboard("Dash")
|
|
|
|
dto.Dashboard.SetID(3)
|
2022-08-11 06:28:55 -05:00
|
|
|
dto.User = &user.SignedInUser{UserID: 1}
|
2021-11-03 08:10:39 -05:00
|
|
|
_, err := service.SaveProvisionedDashboard(context.Background(), dto, nil)
|
2021-10-27 08:17:50 -05:00
|
|
|
require.NoError(t, err)
|
2018-04-13 10:45:32 -05:00
|
|
|
})
|
2020-02-28 07:32:01 -06:00
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Should override invalid refresh interval if dashboard is provisioned", func(t *testing.T) {
|
2022-11-02 08:15:50 -05:00
|
|
|
fakeStore.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything, mock.AnythingOfType("bool")).Return(true, nil).Once()
|
2023-01-16 09:33:55 -06:00
|
|
|
fakeStore.On("SaveProvisionedDashboard", mock.Anything, mock.AnythingOfType("dashboards.SaveDashboardCommand"), mock.AnythingOfType("*dashboards.DashboardProvisioning")).Return(&dashboards.Dashboard{Data: simplejson.New()}, nil).Once()
|
2022-02-16 07:15:44 -06:00
|
|
|
|
2020-02-28 07:32:01 -06:00
|
|
|
oldRefreshInterval := setting.MinRefreshInterval
|
|
|
|
setting.MinRefreshInterval = "5m"
|
|
|
|
defer func() { setting.MinRefreshInterval = oldRefreshInterval }()
|
|
|
|
|
2023-01-16 09:33:55 -06:00
|
|
|
dto.Dashboard = dashboards.NewDashboard("Dash")
|
|
|
|
dto.Dashboard.SetID(3)
|
2022-08-11 06:28:55 -05:00
|
|
|
dto.User = &user.SignedInUser{UserID: 1}
|
2020-02-28 07:32:01 -06:00
|
|
|
dto.Dashboard.Data.Set("refresh", "1s")
|
2021-11-03 08:10:39 -05:00
|
|
|
_, err := service.SaveProvisionedDashboard(context.Background(), dto, nil)
|
2021-10-27 08:17:50 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, dto.Dashboard.Data.Get("refresh").MustString(), "5m")
|
2020-02-28 07:32:01 -06:00
|
|
|
})
|
2018-04-13 10:45:32 -05:00
|
|
|
})
|
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Import dashboard validation", func(t *testing.T) {
|
2022-06-30 08:31:54 -05:00
|
|
|
dto := &dashboards.SaveDashboardDTO{}
|
2018-04-13 10:54:41 -05:00
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Should return validation error if dashboard is provisioned", func(t *testing.T) {
|
2022-11-02 08:15:50 -05:00
|
|
|
fakeStore.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything, mock.AnythingOfType("bool")).Return(true, nil).Once()
|
2023-01-18 06:52:41 -06:00
|
|
|
fakeStore.On("GetProvisionedDataByDashboardID", mock.Anything, mock.AnythingOfType("int64")).Return(&dashboards.DashboardProvisioning{}, nil).Once()
|
2018-04-13 10:54:41 -05:00
|
|
|
|
2023-01-16 09:33:55 -06:00
|
|
|
dto.Dashboard = dashboards.NewDashboard("Dash")
|
|
|
|
dto.Dashboard.SetID(3)
|
2022-08-11 06:28:55 -05:00
|
|
|
dto.User = &user.SignedInUser{UserID: 1}
|
2021-11-12 07:35:38 -06:00
|
|
|
_, err := service.ImportDashboard(context.Background(), dto)
|
2022-06-30 08:31:54 -05:00
|
|
|
require.Equal(t, err, dashboards.ErrDashboardCannotSaveProvisionedDashboard)
|
2018-04-13 10:54:41 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Given provisioned dashboard", func(t *testing.T) {
|
|
|
|
t.Run("DeleteProvisionedDashboard should delete it", func(t *testing.T) {
|
2023-01-18 06:52:41 -06:00
|
|
|
args := &dashboards.DeleteDashboardCommand{OrgID: 1, ID: 1}
|
2022-03-22 08:36:50 -05:00
|
|
|
fakeStore.On("DeleteDashboard", mock.Anything, args).Return(nil).Once()
|
2021-11-19 07:32:14 -06:00
|
|
|
err := service.DeleteProvisionedDashboard(context.Background(), 1, 1)
|
2021-10-27 08:17:50 -05:00
|
|
|
require.NoError(t, err)
|
2019-04-10 06:29:10 -05:00
|
|
|
})
|
|
|
|
|
2022-03-22 08:36:50 -05:00
|
|
|
t.Run("DeleteDashboard should fail to delete it when provisioning information is missing", func(t *testing.T) {
|
2023-01-18 06:52:41 -06:00
|
|
|
fakeStore.On("GetProvisionedDataByDashboardID", mock.Anything, mock.AnythingOfType("int64")).Return(&dashboards.DashboardProvisioning{}, nil).Once()
|
2021-11-19 07:32:14 -06:00
|
|
|
err := service.DeleteDashboard(context.Background(), 1, 1)
|
2022-06-30 08:31:54 -05:00
|
|
|
require.Equal(t, err, dashboards.ErrDashboardCannotDeleteProvisionedDashboard)
|
2019-04-10 06:29:10 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("Given non provisioned dashboard", func(t *testing.T) {
|
2022-03-22 08:36:50 -05:00
|
|
|
t.Run("DeleteProvisionedDashboard should delete the dashboard", func(t *testing.T) {
|
2023-01-18 06:52:41 -06:00
|
|
|
args := &dashboards.DeleteDashboardCommand{OrgID: 1, ID: 1, ForceDeleteFolderRules: false}
|
2022-03-22 08:36:50 -05:00
|
|
|
fakeStore.On("DeleteDashboard", mock.Anything, args).Return(nil).Once()
|
2021-11-19 07:32:14 -06:00
|
|
|
err := service.DeleteProvisionedDashboard(context.Background(), 1, 1)
|
2021-10-27 08:17:50 -05:00
|
|
|
require.NoError(t, err)
|
2019-04-10 06:29:10 -05:00
|
|
|
})
|
|
|
|
|
2021-10-27 08:17:50 -05:00
|
|
|
t.Run("DeleteDashboard should delete it", func(t *testing.T) {
|
2023-01-18 06:52:41 -06:00
|
|
|
args := &dashboards.DeleteDashboardCommand{OrgID: 1, ID: 1}
|
2022-03-22 08:36:50 -05:00
|
|
|
fakeStore.On("DeleteDashboard", mock.Anything, args).Return(nil).Once()
|
2022-11-02 08:15:50 -05:00
|
|
|
fakeStore.On("GetProvisionedDataByDashboardID", mock.Anything, mock.AnythingOfType("int64")).Return(nil, nil).Once()
|
2021-11-19 07:32:14 -06:00
|
|
|
err := service.DeleteDashboard(context.Background(), 1, 1)
|
2021-10-27 08:17:50 -05:00
|
|
|
require.NoError(t, err)
|
2019-04-10 06:29:10 -05:00
|
|
|
})
|
2022-07-15 11:06:44 -05:00
|
|
|
|
|
|
|
// t.Run("Delete ACL by user", func(t *testing.T) {
|
|
|
|
// fakeStore := dashboards.FakeDashboardStore{}
|
|
|
|
// args := 1
|
|
|
|
// fakeStore.On("DeleteACLByUser", mock.Anything, args).Return(nil).Once()
|
|
|
|
// err := service.DeleteACLByUser(context.Background(), 1)
|
|
|
|
// require.NoError(t, err)
|
|
|
|
// })
|
2019-04-10 06:29:10 -05:00
|
|
|
})
|
2022-11-02 08:15:50 -05:00
|
|
|
|
|
|
|
t.Run("Count dashboards in folder", func(t *testing.T) {
|
2023-01-19 10:38:07 -06:00
|
|
|
folderStore.On("GetFolderByUID", mock.Anything, mock.AnythingOfType("int64"), mock.AnythingOfType("string")).Return(&folder.Folder{}, nil)
|
2022-11-02 08:15:50 -05:00
|
|
|
fakeStore.On("CountDashboardsInFolder", mock.Anything, mock.AnythingOfType("*dashboards.CountDashboardsInFolderRequest")).Return(int64(3), nil)
|
|
|
|
|
|
|
|
// set up a ctx with signed in user
|
|
|
|
ctx := context.Background()
|
|
|
|
usr := &user.SignedInUser{UserID: 1}
|
|
|
|
ctx = appcontext.WithUser(ctx, usr)
|
|
|
|
|
|
|
|
count, err := service.CountDashboardsInFolder(ctx, &dashboards.CountDashboardsInFolderQuery{FolderUID: "i am a folder"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, int64(3), count)
|
|
|
|
})
|
2018-02-19 04:12:56 -06:00
|
|
|
})
|
2022-07-19 09:01:05 -05:00
|
|
|
|
|
|
|
t.Run("Delete user by acl", func(t *testing.T) {
|
|
|
|
fakeStore := dashboards.FakeDashboardStore{}
|
2022-11-02 08:15:50 -05:00
|
|
|
fakeStore.On("DeleteACLByUser", mock.Anything, mock.AnythingOfType("int64")).Return(nil)
|
2022-07-19 09:01:05 -05:00
|
|
|
defer fakeStore.AssertExpectations(t)
|
|
|
|
|
|
|
|
service := &DashboardServiceImpl{
|
|
|
|
cfg: setting.NewCfg(),
|
|
|
|
log: log.New("test.logger"),
|
|
|
|
dashboardStore: &fakeStore,
|
|
|
|
dashAlertExtractor: &dummyDashAlertExtractor{},
|
|
|
|
}
|
|
|
|
err := service.DeleteACLByUser(context.Background(), 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
2022-11-28 07:59:40 -06:00
|
|
|
|
|
|
|
t.Run("When org user is deleted", func(t *testing.T) {
|
|
|
|
fakeStore := dashboards.FakeDashboardStore{}
|
2023-01-25 03:36:26 -06:00
|
|
|
fakeStore.On("GetDashboardACLInfoList", mock.Anything, mock.AnythingOfType("*dashboards.GetDashboardACLInfoListQuery")).Return(nil, nil)
|
2022-11-28 07:59:40 -06:00
|
|
|
t.Run("Should remove dependent permissions for deleted org user", func(t *testing.T) {
|
2023-01-25 03:36:26 -06:00
|
|
|
permQuery := &dashboards.GetDashboardACLInfoListQuery{DashboardID: 1, OrgID: 1}
|
2022-11-28 07:59:40 -06:00
|
|
|
|
2023-01-25 03:36:26 -06:00
|
|
|
permQueryResult, err := fakeStore.GetDashboardACLInfoList(context.Background(), permQuery)
|
2022-11-28 07:59:40 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-01-25 03:36:26 -06:00
|
|
|
require.Equal(t, len(permQueryResult), 0)
|
2022-11-28 07:59:40 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Should not remove dashboard permissions for same user in another org", func(t *testing.T) {
|
|
|
|
fakeStore := dashboards.FakeDashboardStore{}
|
2023-01-25 03:36:26 -06:00
|
|
|
fakeStore.On("GetDashboardACLInfoList", mock.Anything, mock.AnythingOfType("*dashboards.GetDashboardACLInfoListQuery")).Return(nil, nil)
|
2023-01-20 07:58:47 -06:00
|
|
|
permQuery := &dashboards.GetDashboardACLInfoListQuery{DashboardID: 2, OrgID: 3}
|
2022-11-28 07:59:40 -06:00
|
|
|
|
2023-01-25 03:36:26 -06:00
|
|
|
_, err := fakeStore.GetDashboardACLInfoList(context.Background(), permQuery)
|
2022-11-28 07:59:40 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
})
|
2018-02-19 04:12:56 -06:00
|
|
|
}
|