Files
grafana/pkg/services/dashboardsnapshots/database/database_test.go
T

220 lines
7.0 KiB
Go
Raw Normal View History

package database
2015-03-21 08:53:16 -04:00
import (
"context"
2015-03-21 08:53:16 -04:00
"testing"
2018-02-20 23:10:59 +01:00
"time"
2015-03-21 08:53:16 -04:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/services/dashboardsnapshots"
"github.com/grafana/grafana/pkg/services/org"
2021-11-04 18:47:21 +02:00
"github.com/grafana/grafana/pkg/services/secrets"
"github.com/grafana/grafana/pkg/services/secrets/fakes"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/user"
2018-02-20 23:10:59 +01:00
"github.com/grafana/grafana/pkg/setting"
2015-03-21 08:53:16 -04:00
)
2022-05-24 05:04:03 -04:00
func TestIntegrationDashboardSnapshotDBAccess(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
sqlstore := sqlstore.InitTestDB(t)
dashStore := ProvideStore(sqlstore)
2015-03-21 08:53:16 -04:00
origSecret := setting.SecretKey
setting.SecretKey = "dashboard_snapshot_testing"
t.Cleanup(func() {
setting.SecretKey = origSecret
})
2021-11-04 18:47:21 +02:00
secretsService := fakes.NewFakeSecretsService()
dashboard := simplejson.NewFromAny(map[string]interface{}{"hello": "mupp"})
t.Run("Given saved snapshot", func(t *testing.T) {
rawDashboard, err := dashboard.Encode()
require.NoError(t, err)
2021-11-04 18:47:21 +02:00
encryptedDashboard, err := secretsService.Encrypt(context.Background(), rawDashboard, secrets.WithoutScope())
require.NoError(t, err)
cmd := dashboardsnapshots.CreateDashboardSnapshotCommand{
Key: "hej",
DashboardEncrypted: encryptedDashboard,
UserId: 1000,
OrgId: 1,
}
err = dashStore.CreateDashboardSnapshot(context.Background(), &cmd)
require.NoError(t, err)
t.Run("Should be able to get snapshot by key", func(t *testing.T) {
query := dashboardsnapshots.GetDashboardSnapshotQuery{Key: "hej"}
err := dashStore.GetDashboardSnapshot(context.Background(), &query)
require.NoError(t, err)
assert.NotNil(t, query.Result)
2021-11-04 18:47:21 +02:00
decryptedDashboard, err := secretsService.Decrypt(
context.Background(),
query.Result.DashboardEncrypted,
)
require.NoError(t, err)
dashboard, err := simplejson.NewJson(decryptedDashboard)
require.NoError(t, err)
assert.Equal(t, "mupp", dashboard.Get("hello").MustString())
})
t.Run("And the user has the admin role", func(t *testing.T) {
query := dashboardsnapshots.GetDashboardSnapshotsQuery{
OrgId: 1,
SignedInUser: &user.SignedInUser{OrgRole: org.RoleAdmin},
}
err := dashStore.SearchDashboardSnapshots(context.Background(), &query)
require.NoError(t, err)
t.Run("Should return all the snapshots", func(t *testing.T) {
assert.NotNil(t, query.Result)
assert.Len(t, query.Result, 1)
})
})
t.Run("And the user has the editor role and has created a snapshot", func(t *testing.T) {
query := dashboardsnapshots.GetDashboardSnapshotsQuery{
OrgId: 1,
SignedInUser: &user.SignedInUser{OrgRole: org.RoleEditor, UserID: 1000},
}
err := dashStore.SearchDashboardSnapshots(context.Background(), &query)
require.NoError(t, err)
t.Run("Should return all the snapshots", func(t *testing.T) {
require.NotNil(t, query.Result)
assert.Len(t, query.Result, 1)
})
})
t.Run("And the user has the editor role and has not created any snapshot", func(t *testing.T) {
query := dashboardsnapshots.GetDashboardSnapshotsQuery{
OrgId: 1,
SignedInUser: &user.SignedInUser{OrgRole: org.RoleEditor, UserID: 2},
}
err := dashStore.SearchDashboardSnapshots(context.Background(), &query)
require.NoError(t, err)
t.Run("Should not return any snapshots", func(t *testing.T) {
require.NotNil(t, query.Result)
assert.Empty(t, query.Result)
})
})
t.Run("And the user is anonymous", func(t *testing.T) {
cmd := dashboardsnapshots.CreateDashboardSnapshotCommand{
Key: "strangesnapshotwithuserid0",
DeleteKey: "adeletekey",
Dashboard: simplejson.NewFromAny(map[string]interface{}{
2015-03-21 08:53:16 -04:00
"hello": "mupp",
}),
UserId: 0,
2018-02-20 23:26:08 +01:00
OrgId: 1,
2015-03-21 08:53:16 -04:00
}
err := dashStore.CreateDashboardSnapshot(context.Background(), &cmd)
require.NoError(t, err)
2015-03-21 08:53:16 -04:00
t.Run("Should not return any snapshots", func(t *testing.T) {
query := dashboardsnapshots.GetDashboardSnapshotsQuery{
OrgId: 1,
SignedInUser: &user.SignedInUser{OrgRole: org.RoleEditor, IsAnonymous: true, UserID: 0},
}
err := dashStore.SearchDashboardSnapshots(context.Background(), &query)
require.NoError(t, err)
2015-03-21 08:53:16 -04:00
require.NotNil(t, query.Result)
assert.Empty(t, query.Result)
2018-02-20 23:26:08 +01:00
})
})
2018-02-20 23:26:08 +01:00
t.Run("Should have encrypted dashboard data", func(t *testing.T) {
2021-11-04 18:47:21 +02:00
decryptedDashboard, err := secretsService.Decrypt(
context.Background(),
cmd.Result.DashboardEncrypted,
)
require.NoError(t, err)
2018-02-20 23:26:08 +01:00
require.Equal(t, decryptedDashboard, rawDashboard)
2015-03-21 08:53:16 -04:00
})
})
}
2018-02-20 23:10:59 +01:00
2022-05-24 05:04:03 -04:00
func TestIntegrationDeleteExpiredSnapshots(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
sqlstore := sqlstore.InitTestDB(t)
dashStore := ProvideStore(sqlstore)
2018-02-20 23:10:59 +01:00
t.Run("Testing dashboard snapshots clean up", func(t *testing.T) {
2018-02-20 23:10:59 +01:00
setting.SnapShotRemoveExpired = true
nonExpiredSnapshot := createTestSnapshot(t, dashStore, "key1", 48000)
createTestSnapshot(t, dashStore, "key2", -1200)
createTestSnapshot(t, dashStore, "key3", -1200)
2018-02-20 23:10:59 +01:00
err := dashStore.DeleteExpiredSnapshots(context.Background(), &dashboardsnapshots.DeleteExpiredSnapshotsCommand{})
require.NoError(t, err)
2018-02-20 23:10:59 +01:00
query := dashboardsnapshots.GetDashboardSnapshotsQuery{
OrgId: 1,
SignedInUser: &user.SignedInUser{OrgRole: org.RoleAdmin},
}
err = dashStore.SearchDashboardSnapshots(context.Background(), &query)
require.NoError(t, err)
2018-02-20 23:10:59 +01:00
assert.Len(t, query.Result, 1)
assert.Equal(t, nonExpiredSnapshot.Key, query.Result[0].Key)
2018-02-20 23:10:59 +01:00
err = dashStore.DeleteExpiredSnapshots(context.Background(), &dashboardsnapshots.DeleteExpiredSnapshotsCommand{})
require.NoError(t, err)
2018-02-20 23:10:59 +01:00
query = dashboardsnapshots.GetDashboardSnapshotsQuery{
OrgId: 1,
SignedInUser: &user.SignedInUser{OrgRole: org.RoleAdmin},
}
err = dashStore.SearchDashboardSnapshots(context.Background(), &query)
require.NoError(t, err)
2018-02-20 23:10:59 +01:00
require.Len(t, query.Result, 1)
require.Equal(t, nonExpiredSnapshot.Key, query.Result[0].Key)
2018-02-20 23:10:59 +01:00
})
}
func createTestSnapshot(t *testing.T, dashStore *DashboardSnapshotStore, key string, expires int64) *dashboardsnapshots.DashboardSnapshot {
cmd := dashboardsnapshots.CreateDashboardSnapshotCommand{
2018-02-20 23:10:59 +01:00
Key: key,
DeleteKey: "delete" + key,
Dashboard: simplejson.NewFromAny(map[string]interface{}{
"hello": "mupp",
}),
UserId: 1000,
OrgId: 1,
Expires: expires,
}
err := dashStore.CreateDashboardSnapshot(context.Background(), &cmd)
require.NoError(t, err)
2018-02-20 23:10:59 +01:00
// Set expiry date manually - to be able to create expired snapshots
if expires < 0 {
expireDate := time.Now().Add(time.Second * time.Duration(expires))
err = dashStore.store.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
_, err := sess.Exec("UPDATE dashboard_snapshot SET expires = ? WHERE id = ?", expireDate, cmd.Result.Id)
return err
})
require.NoError(t, err)
}
2018-02-20 23:10:59 +01:00
return cmd.Result
}