grafana/pkg/services/dashboardsnapshots/service/service_test.go
Lucy Chen e8625ecd4a
Snapshots: Prevent creation of snapshot of nonexistent dashboard (#86806)
* Add validation to check for existence of dashboard before creation

* add service mock for FindDashboard

* update tests

* reorder function

* update logic change to bool

* update naming validate dashboard

* add tests

* update return val

* remove bool return val

* simplify return statement

* update test

* remove extra spacing

* update mock

* remove unncessary space
2024-05-23 15:17:55 -04:00

135 lines
4.4 KiB
Go

package service
import (
"context"
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/require"
common "github.com/grafana/grafana/pkg/apimachinery/apis/common/v0alpha1"
dashboardsnapshot "github.com/grafana/grafana/pkg/apis/dashboardsnapshot/v0alpha1"
"github.com/grafana/grafana/pkg/infra/db"
acmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock"
"github.com/grafana/grafana/pkg/services/dashboards"
dashdb "github.com/grafana/grafana/pkg/services/dashboards/database"
dashsvc "github.com/grafana/grafana/pkg/services/dashboards/service"
"github.com/grafana/grafana/pkg/services/dashboardsnapshots"
dashsnapdb "github.com/grafana/grafana/pkg/services/dashboardsnapshots/database"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/folder/folderimpl"
"github.com/grafana/grafana/pkg/services/folder/foldertest"
"github.com/grafana/grafana/pkg/services/quota/quotatest"
"github.com/grafana/grafana/pkg/services/secrets/database"
secretsManager "github.com/grafana/grafana/pkg/services/secrets/manager"
"github.com/grafana/grafana/pkg/services/tag/tagimpl"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tests/testsuite"
)
func TestMain(m *testing.M) {
testsuite.Run(m)
}
func TestDashboardSnapshotsService(t *testing.T) {
sqlStore := db.InitTestDB(t)
cfg := setting.NewCfg()
dsStore := dashsnapdb.ProvideStore(sqlStore, cfg)
fakeDashboardService := &dashboards.FakeDashboardService{}
secretsService := secretsManager.SetupTestService(t, database.ProvideSecretsStore(sqlStore))
s := ProvideService(dsStore, secretsService, fakeDashboardService)
origSecret := cfg.SecretKey
cfg.SecretKey = "dashboard_snapshot_service_test"
t.Cleanup(func() {
cfg.SecretKey = origSecret
})
dashboardKey := "12345"
dashboard := &common.Unstructured{}
rawDashboard := []byte(`{"id":123}`)
err := json.Unmarshal(rawDashboard, dashboard)
require.NoError(t, err)
t.Run("create dashboard snapshot should encrypt the dashboard", func(t *testing.T) {
ctx := context.Background()
cmd := dashboardsnapshots.CreateDashboardSnapshotCommand{
Key: dashboardKey,
DeleteKey: dashboardKey,
DashboardCreateCommand: dashboardsnapshot.DashboardCreateCommand{
Dashboard: dashboard,
},
}
result, err := s.CreateDashboardSnapshot(ctx, &cmd)
require.NoError(t, err)
decrypted, err := s.secretsService.Decrypt(ctx, result.DashboardEncrypted)
require.NoError(t, err)
require.Equal(t, rawDashboard, decrypted)
})
t.Run("get dashboard snapshot should return the dashboard decrypted", func(t *testing.T) {
ctx := context.Background()
query := dashboardsnapshots.GetDashboardSnapshotQuery{
Key: dashboardKey,
DeleteKey: dashboardKey,
}
queryResult, err := s.GetDashboardSnapshot(ctx, &query)
require.NoError(t, err)
decrypted, err := queryResult.Dashboard.Encode()
require.NoError(t, err)
require.Equal(t, rawDashboard, decrypted)
})
}
func TestValidateDashboardExists(t *testing.T) {
sqlStore := db.InitTestDB(t)
cfg := setting.NewCfg()
dsStore := dashsnapdb.ProvideStore(sqlStore, cfg)
secretsService := secretsManager.SetupTestService(t, database.ProvideSecretsStore(sqlStore))
dashboardStore, err := dashdb.ProvideDashboardStore(sqlStore, cfg, featuremgmt.WithFeatures(), tagimpl.ProvideService(sqlStore), quotatest.New(false, nil))
require.NoError(t, err)
dashSvc, err := dashsvc.ProvideDashboardServiceImpl(cfg, dashboardStore, folderimpl.ProvideDashboardFolderStore(sqlStore), nil, nil, nil, acmock.New(), foldertest.NewFakeService(), nil)
require.NoError(t, err)
s := ProvideService(dsStore, secretsService, dashSvc)
ctx := context.Background()
t.Run("returns false when dashboard does not exist", func(t *testing.T) {
err := s.ValidateDashboardExists(ctx, 1, "test")
require.Error(t, err)
require.Equal(t, dashboards.ErrDashboardNotFound, err)
})
t.Run("returns true when dashboard exists", func(t *testing.T) {
err := createDashboard(sqlStore)
require.NoError(t, err)
err = s.ValidateDashboardExists(ctx, 1, "test")
require.NoError(t, err)
})
}
func createDashboard(store db.DB) error {
return store.WithDbSession(context.Background(), func(sess *db.Session) error {
dashboard := &dashboards.Dashboard{
ID: 1,
UID: "test",
OrgID: 1,
Created: time.Now(),
Updated: time.Now(),
}
_, err := sess.Insert(dashboard)
return err
})
}