2021-09-01 09:38:56 -05:00
|
|
|
//go:build integration
|
2020-10-16 02:46:14 -05:00
|
|
|
// +build integration
|
|
|
|
|
2015-03-21 07:53:16 -05:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2018-02-20 16:10:59 -06:00
|
|
|
"time"
|
2015-03-21 07:53:16 -05:00
|
|
|
|
2016-03-12 03:13:49 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2020-02-29 06:35:15 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2021-09-01 06:05:15 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/encryption/ossencryption"
|
2018-02-20 16:10:59 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2021-09-01 06:05:15 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2015-03-21 07:53:16 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDashboardSnapshotDBAccess(t *testing.T) {
|
2021-09-01 06:05:15 -05:00
|
|
|
sqlstore := InitTestDB(t)
|
2015-03-21 07:53:16 -05:00
|
|
|
|
2020-10-13 03:19:42 -05:00
|
|
|
origSecret := setting.SecretKey
|
|
|
|
setting.SecretKey = "dashboard_snapshot_testing"
|
|
|
|
t.Cleanup(func() {
|
|
|
|
setting.SecretKey = origSecret
|
|
|
|
})
|
|
|
|
|
2021-09-01 06:05:15 -05:00
|
|
|
dashboard := simplejson.NewFromAny(map[string]interface{}{"hello": "mupp"})
|
|
|
|
|
2020-10-13 03:19:42 -05:00
|
|
|
t.Run("Given saved snapshot", func(t *testing.T) {
|
2021-09-01 06:05:15 -05:00
|
|
|
rawDashboard, err := dashboard.Encode()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
encryptedDashboard, err := ossencryption.ProvideService().Encrypt(rawDashboard, setting.SecretKey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-13 03:19:42 -05:00
|
|
|
cmd := models.CreateDashboardSnapshotCommand{
|
2021-09-01 06:05:15 -05:00
|
|
|
Key: "hej",
|
|
|
|
DashboardEncrypted: encryptedDashboard,
|
|
|
|
UserId: 1000,
|
|
|
|
OrgId: 1,
|
2020-10-13 03:19:42 -05:00
|
|
|
}
|
2021-09-01 06:05:15 -05:00
|
|
|
|
|
|
|
err = sqlstore.CreateDashboardSnapshot(&cmd)
|
2020-10-13 03:19:42 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
t.Run("Should be able to get snapshot by key", func(t *testing.T) {
|
|
|
|
query := models.GetDashboardSnapshotQuery{Key: "hej"}
|
2021-09-01 06:05:15 -05:00
|
|
|
err := sqlstore.GetDashboardSnapshot(&query)
|
2020-10-13 03:19:42 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.NotNil(t, query.Result)
|
|
|
|
|
2021-09-01 06:05:15 -05:00
|
|
|
decryptedDashboard, err := ossencryption.ProvideService().Decrypt(
|
|
|
|
query.Result.DashboardEncrypted,
|
|
|
|
setting.SecretKey,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
dashboard, err := simplejson.NewJson(decryptedDashboard)
|
2020-10-13 03:19:42 -05:00
|
|
|
require.NoError(t, err)
|
2021-09-01 06:05:15 -05:00
|
|
|
|
2020-10-13 03:19:42 -05:00
|
|
|
assert.Equal(t, "mupp", dashboard.Get("hello").MustString())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("And the user has the admin role", func(t *testing.T) {
|
|
|
|
query := models.GetDashboardSnapshotsQuery{
|
|
|
|
OrgId: 1,
|
|
|
|
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_ADMIN},
|
|
|
|
}
|
2021-09-01 06:05:15 -05:00
|
|
|
err := sqlstore.SearchDashboardSnapshots(&query)
|
2020-10-13 03:19:42 -05:00
|
|
|
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 := models.GetDashboardSnapshotsQuery{
|
|
|
|
OrgId: 1,
|
|
|
|
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_EDITOR, UserId: 1000},
|
|
|
|
}
|
2021-09-01 06:05:15 -05:00
|
|
|
err := sqlstore.SearchDashboardSnapshots(&query)
|
2020-10-13 03:19:42 -05:00
|
|
|
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 := models.GetDashboardSnapshotsQuery{
|
|
|
|
OrgId: 1,
|
|
|
|
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_EDITOR, UserId: 2},
|
|
|
|
}
|
2021-09-01 06:05:15 -05:00
|
|
|
err := sqlstore.SearchDashboardSnapshots(&query)
|
2020-10-13 03:19:42 -05:00
|
|
|
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) {
|
2020-02-29 06:35:15 -06:00
|
|
|
cmd := models.CreateDashboardSnapshotCommand{
|
2020-10-13 03:19:42 -05:00
|
|
|
Key: "strangesnapshotwithuserid0",
|
|
|
|
DeleteKey: "adeletekey",
|
2016-03-12 03:13:49 -06:00
|
|
|
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
2015-03-21 07:53:16 -05:00
|
|
|
"hello": "mupp",
|
2016-03-12 03:13:49 -06:00
|
|
|
}),
|
2020-10-13 03:19:42 -05:00
|
|
|
UserId: 0,
|
2018-02-20 16:26:08 -06:00
|
|
|
OrgId: 1,
|
2015-03-21 07:53:16 -05:00
|
|
|
}
|
2021-09-01 06:05:15 -05:00
|
|
|
err := sqlstore.CreateDashboardSnapshot(&cmd)
|
2020-10-13 03:19:42 -05:00
|
|
|
require.NoError(t, err)
|
2015-03-21 07:53:16 -05:00
|
|
|
|
2020-10-13 03:19:42 -05:00
|
|
|
t.Run("Should not return any snapshots", func(t *testing.T) {
|
|
|
|
query := models.GetDashboardSnapshotsQuery{
|
|
|
|
OrgId: 1,
|
|
|
|
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_EDITOR, IsAnonymous: true, UserId: 0},
|
|
|
|
}
|
2021-09-01 06:05:15 -05:00
|
|
|
err := sqlstore.SearchDashboardSnapshots(&query)
|
2020-10-13 03:19:42 -05:00
|
|
|
require.NoError(t, err)
|
2015-03-21 07:53:16 -05:00
|
|
|
|
2020-10-13 03:19:42 -05:00
|
|
|
require.NotNil(t, query.Result)
|
|
|
|
assert.Empty(t, query.Result)
|
2018-02-20 16:26:08 -06:00
|
|
|
})
|
2020-10-13 03:19:42 -05:00
|
|
|
})
|
2018-02-20 16:26:08 -06:00
|
|
|
|
2020-10-13 03:19:42 -05:00
|
|
|
t.Run("Should have encrypted dashboard data", func(t *testing.T) {
|
2021-09-01 06:05:15 -05:00
|
|
|
decryptedDashboard, err := ossencryption.ProvideService().Decrypt(
|
|
|
|
cmd.Result.DashboardEncrypted,
|
|
|
|
setting.SecretKey,
|
|
|
|
)
|
2020-10-13 03:19:42 -05:00
|
|
|
require.NoError(t, err)
|
2018-02-20 16:26:08 -06:00
|
|
|
|
2021-09-01 06:05:15 -05:00
|
|
|
require.Equal(t, decryptedDashboard, rawDashboard)
|
2015-03-21 07:53:16 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2018-02-20 16:10:59 -06:00
|
|
|
|
|
|
|
func TestDeleteExpiredSnapshots(t *testing.T) {
|
2018-05-18 04:10:10 -05:00
|
|
|
sqlstore := InitTestDB(t)
|
2018-02-20 16:10:59 -06:00
|
|
|
|
2020-10-13 03:19:42 -05:00
|
|
|
t.Run("Testing dashboard snapshots clean up", func(t *testing.T) {
|
2018-02-20 16:10:59 -06:00
|
|
|
setting.SnapShotRemoveExpired = true
|
|
|
|
|
2020-10-13 03:19:42 -05:00
|
|
|
nonExpiredSnapshot := createTestSnapshot(t, sqlstore, "key1", 48000)
|
|
|
|
createTestSnapshot(t, sqlstore, "key2", -1200)
|
|
|
|
createTestSnapshot(t, sqlstore, "key3", -1200)
|
2018-02-20 16:10:59 -06:00
|
|
|
|
2021-09-01 06:05:15 -05:00
|
|
|
err := sqlstore.DeleteExpiredSnapshots(&models.DeleteExpiredSnapshotsCommand{})
|
2020-10-13 03:19:42 -05:00
|
|
|
require.NoError(t, err)
|
2018-02-20 16:10:59 -06:00
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
query := models.GetDashboardSnapshotsQuery{
|
2018-05-10 09:54:21 -05:00
|
|
|
OrgId: 1,
|
2020-02-29 06:35:15 -06:00
|
|
|
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_ADMIN},
|
2018-05-10 09:54:21 -05:00
|
|
|
}
|
2021-09-01 06:05:15 -05:00
|
|
|
err = sqlstore.SearchDashboardSnapshots(&query)
|
2020-10-13 03:19:42 -05:00
|
|
|
require.NoError(t, err)
|
2018-02-20 16:10:59 -06:00
|
|
|
|
2020-10-13 03:19:42 -05:00
|
|
|
assert.Len(t, query.Result, 1)
|
|
|
|
assert.Equal(t, nonExpiredSnapshot.Key, query.Result[0].Key)
|
2018-02-20 16:10:59 -06:00
|
|
|
|
2021-09-01 06:05:15 -05:00
|
|
|
err = sqlstore.DeleteExpiredSnapshots(&models.DeleteExpiredSnapshotsCommand{})
|
2020-10-13 03:19:42 -05:00
|
|
|
require.NoError(t, err)
|
2018-02-20 16:10:59 -06:00
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
query = models.GetDashboardSnapshotsQuery{
|
2018-05-10 09:54:21 -05:00
|
|
|
OrgId: 1,
|
2020-02-29 06:35:15 -06:00
|
|
|
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_ADMIN},
|
2018-05-10 09:54:21 -05:00
|
|
|
}
|
2021-09-01 06:05:15 -05:00
|
|
|
err = sqlstore.SearchDashboardSnapshots(&query)
|
2020-10-13 03:19:42 -05:00
|
|
|
require.NoError(t, err)
|
2018-02-20 16:10:59 -06:00
|
|
|
|
2020-10-13 03:19:42 -05:00
|
|
|
require.Len(t, query.Result, 1)
|
|
|
|
require.Equal(t, nonExpiredSnapshot.Key, query.Result[0].Key)
|
2018-02-20 16:10:59 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-10 23:21:08 -06:00
|
|
|
func createTestSnapshot(t *testing.T, sqlstore *SQLStore, key string, expires int64) *models.DashboardSnapshot {
|
2020-02-29 06:35:15 -06:00
|
|
|
cmd := models.CreateDashboardSnapshotCommand{
|
2018-02-20 16:10:59 -06:00
|
|
|
Key: key,
|
|
|
|
DeleteKey: "delete" + key,
|
|
|
|
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
|
|
|
"hello": "mupp",
|
|
|
|
}),
|
|
|
|
UserId: 1000,
|
|
|
|
OrgId: 1,
|
|
|
|
Expires: expires,
|
|
|
|
}
|
2021-09-01 06:05:15 -05:00
|
|
|
err := sqlstore.CreateDashboardSnapshot(&cmd)
|
2020-10-13 03:19:42 -05:00
|
|
|
require.NoError(t, err)
|
2018-02-20 16:10:59 -06:00
|
|
|
|
|
|
|
// Set expiry date manually - to be able to create expired snapshots
|
2018-05-10 09:54:21 -05:00
|
|
|
if expires < 0 {
|
|
|
|
expireDate := time.Now().Add(time.Second * time.Duration(expires))
|
2018-05-18 04:10:10 -05:00
|
|
|
_, err = sqlstore.engine.Exec("UPDATE dashboard_snapshot SET expires = ? WHERE id = ?", expireDate, cmd.Result.Id)
|
2020-10-13 03:19:42 -05:00
|
|
|
require.NoError(t, err)
|
2018-05-10 09:54:21 -05:00
|
|
|
}
|
2018-02-20 16:10:59 -06:00
|
|
|
|
|
|
|
return cmd.Result
|
|
|
|
}
|