Chore: Remove convey from dashboards (#39898)

* Remove Convey from dashboards

* Add context for dashboards

* Remove Convey from dashboards

* refactor tests to run setup each time

* Fix last tests

* Adjust after rebase

* Remove print statement

Co-authored-by: Serge Zaitsev <serge.zaitsev@grafana.com>
This commit is contained in:
idafurjes
2021-10-18 14:06:47 +02:00
committed by GitHub
parent 5a087d2708
commit ad79473ca3
2 changed files with 609 additions and 590 deletions

View File

@ -8,441 +8,453 @@ import (
"encoding/json"
"errors"
"fmt"
"testing"
"time"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/search"
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDashboardDataAccess(t *testing.T) {
Convey("Testing DB", t, func() {
sqlStore := InitTestDB(t)
var sqlStore *SQLStore
var savedFolder, savedDash, savedDash2 *models.Dashboard
Convey("Given saved dashboard", func() {
savedFolder := insertTestDashboard(t, sqlStore, "1 test dash folder", 1, 0, true, "prod", "webapp")
savedDash := insertTestDashboard(t, sqlStore, "test dash 23", 1, savedFolder.Id, false, "prod", "webapp")
insertTestDashboard(t, sqlStore, "test dash 45", 1, savedFolder.Id, false, "prod")
savedDash2 := insertTestDashboard(t, sqlStore, "test dash 67", 1, 0, false, "prod")
insertTestRule(t, sqlStore, savedFolder.OrgId, savedFolder.Uid)
setup := func() {
sqlStore = InitTestDB(t)
savedFolder = insertTestDashboard(t, sqlStore, "1 test dash folder", 1, 0, true, "prod", "webapp")
savedDash = insertTestDashboard(t, sqlStore, "test dash 23", 1, savedFolder.Id, false, "prod", "webapp")
insertTestDashboard(t, sqlStore, "test dash 45", 1, savedFolder.Id, false, "prod")
savedDash2 = insertTestDashboard(t, sqlStore, "test dash 67", 1, 0, false, "prod")
insertTestRule(t, sqlStore, savedFolder.OrgId, savedFolder.Uid)
}
Convey("Should return dashboard model", func() {
So(savedDash.Title, ShouldEqual, "test dash 23")
So(savedDash.Slug, ShouldEqual, "test-dash-23")
So(savedDash.Id, ShouldNotEqual, 0)
So(savedDash.IsFolder, ShouldBeFalse)
So(savedDash.FolderId, ShouldBeGreaterThan, 0)
So(len(savedDash.Uid), ShouldBeGreaterThan, 0)
t.Run("Should return dashboard model", func(t *testing.T) {
setup()
require.Equal(t, savedDash.Title, "test dash 23")
require.Equal(t, savedDash.Slug, "test-dash-23")
require.NotEqual(t, savedDash.Id, 0)
require.False(t, savedDash.IsFolder)
require.Positive(t, savedDash.FolderId)
require.Positive(t, len(savedDash.Uid))
So(savedFolder.Title, ShouldEqual, "1 test dash folder")
So(savedFolder.Slug, ShouldEqual, "1-test-dash-folder")
So(savedFolder.Id, ShouldNotEqual, 0)
So(savedFolder.IsFolder, ShouldBeTrue)
So(savedFolder.FolderId, ShouldEqual, 0)
So(len(savedFolder.Uid), ShouldBeGreaterThan, 0)
})
require.Equal(t, savedFolder.Title, "1 test dash folder")
require.Equal(t, savedFolder.Slug, "1-test-dash-folder")
require.NotEqual(t, savedFolder.Id, 0)
require.True(t, savedFolder.IsFolder)
require.EqualValues(t, savedFolder.FolderId, 0)
require.Positive(t, len(savedFolder.Uid))
})
Convey("Should be able to get dashboard by id", func() {
query := models.GetDashboardQuery{
Id: savedDash.Id,
OrgId: 1,
}
t.Run("Should be able to get dashboard by id", func(t *testing.T) {
setup()
query := models.GetDashboardQuery{
Id: savedDash.Id,
OrgId: 1,
}
err := GetDashboard(context.Background(), &query)
So(err, ShouldBeNil)
err := GetDashboard(context.Background(), &query)
require.NoError(t, err)
So(query.Result.Title, ShouldEqual, "test dash 23")
So(query.Result.Slug, ShouldEqual, "test-dash-23")
So(query.Result.Id, ShouldEqual, savedDash.Id)
So(query.Result.Uid, ShouldEqual, savedDash.Uid)
So(query.Result.IsFolder, ShouldBeFalse)
})
require.Equal(t, query.Result.Title, "test dash 23")
require.Equal(t, query.Result.Slug, "test-dash-23")
require.Equal(t, query.Result.Id, savedDash.Id)
require.Equal(t, query.Result.Uid, savedDash.Uid)
require.False(t, query.Result.IsFolder)
})
Convey("Should be able to get dashboard by slug", func() {
query := models.GetDashboardQuery{
Slug: "test-dash-23",
OrgId: 1,
}
t.Run("Should be able to get dashboard by slug", func(t *testing.T) {
setup()
query := models.GetDashboardQuery{
Slug: "test-dash-23",
OrgId: 1,
}
err := GetDashboard(context.Background(), &query)
So(err, ShouldBeNil)
err := GetDashboard(context.Background(), &query)
require.NoError(t, err)
So(query.Result.Title, ShouldEqual, "test dash 23")
So(query.Result.Slug, ShouldEqual, "test-dash-23")
So(query.Result.Id, ShouldEqual, savedDash.Id)
So(query.Result.Uid, ShouldEqual, savedDash.Uid)
So(query.Result.IsFolder, ShouldBeFalse)
})
require.Equal(t, query.Result.Title, "test dash 23")
require.Equal(t, query.Result.Slug, "test-dash-23")
require.Equal(t, query.Result.Id, savedDash.Id)
require.Equal(t, query.Result.Uid, savedDash.Uid)
require.False(t, query.Result.IsFolder)
})
Convey("Should be able to get dashboard by uid", func() {
query := models.GetDashboardQuery{
Uid: savedDash.Uid,
OrgId: 1,
}
t.Run("Should be able to get dashboard by uid", func(t *testing.T) {
setup()
query := models.GetDashboardQuery{
Uid: savedDash.Uid,
OrgId: 1,
}
err := GetDashboard(context.Background(), &query)
So(err, ShouldBeNil)
err := GetDashboard(context.Background(), &query)
require.NoError(t, err)
So(query.Result.Title, ShouldEqual, "test dash 23")
So(query.Result.Slug, ShouldEqual, "test-dash-23")
So(query.Result.Id, ShouldEqual, savedDash.Id)
So(query.Result.Uid, ShouldEqual, savedDash.Uid)
So(query.Result.IsFolder, ShouldBeFalse)
})
require.Equal(t, query.Result.Title, "test dash 23")
require.Equal(t, query.Result.Slug, "test-dash-23")
require.Equal(t, query.Result.Id, savedDash.Id)
require.Equal(t, query.Result.Uid, savedDash.Uid)
require.False(t, query.Result.IsFolder)
})
Convey("Shouldn't be able to get a dashboard with just an OrgID", func() {
query := models.GetDashboardQuery{
OrgId: 1,
}
t.Run("Shouldn't be able to get a dashboard with just an OrgID", func(t *testing.T) {
setup()
query := models.GetDashboardQuery{
OrgId: 1,
}
err := GetDashboard(context.Background(), &query)
So(err, ShouldEqual, models.ErrDashboardIdentifierNotSet)
})
err := GetDashboard(context.Background(), &query)
require.Equal(t, err, models.ErrDashboardIdentifierNotSet)
})
Convey("Should be able to delete dashboard", func() {
dash := insertTestDashboard(t, sqlStore, "delete me", 1, 0, false, "delete this")
t.Run("Should be able to delete dashboard", func(t *testing.T) {
setup()
dash := insertTestDashboard(t, sqlStore, "delete me", 1, 0, false, "delete this")
err := DeleteDashboard(context.Background(), &models.DeleteDashboardCommand{
Id: dash.Id,
OrgId: 1,
})
So(err, ShouldBeNil)
})
Convey("Should retry generation of uid once if it fails.", func() {
timesCalled := 0
generateNewUid = func() string {
timesCalled += 1
if timesCalled <= 2 {
return savedDash.Uid
}
return util.GenerateShortUID()
}
cmd := models.SaveDashboardCommand{
OrgId: 1,
Dashboard: simplejson.NewFromAny(map[string]interface{}{
"title": "new dash 12334",
"tags": []interface{}{},
}),
}
_, err := sqlStore.SaveDashboard(cmd)
So(err, ShouldBeNil)
generateNewUid = util.GenerateShortUID
})
Convey("Should be able to create dashboard", func() {
cmd := models.SaveDashboardCommand{
OrgId: 1,
Dashboard: simplejson.NewFromAny(map[string]interface{}{
"title": "folderId",
"tags": []interface{}{},
}),
UserId: 100,
}
dashboard, err := sqlStore.SaveDashboard(cmd)
So(err, ShouldBeNil)
So(dashboard.CreatedBy, ShouldEqual, 100)
So(dashboard.Created.IsZero(), ShouldBeFalse)
So(dashboard.UpdatedBy, ShouldEqual, 100)
So(dashboard.Updated.IsZero(), ShouldBeFalse)
})
Convey("Should be able to update dashboard by id and remove folderId", func() {
cmd := models.SaveDashboardCommand{
OrgId: 1,
Dashboard: simplejson.NewFromAny(map[string]interface{}{
"id": savedDash.Id,
"title": "folderId",
"tags": []interface{}{},
}),
Overwrite: true,
FolderId: 2,
UserId: 100,
}
dash, err := sqlStore.SaveDashboard(cmd)
So(err, ShouldBeNil)
So(dash.FolderId, ShouldEqual, 2)
cmd = models.SaveDashboardCommand{
OrgId: 1,
Dashboard: simplejson.NewFromAny(map[string]interface{}{
"id": savedDash.Id,
"title": "folderId",
"tags": []interface{}{},
}),
FolderId: 0,
Overwrite: true,
UserId: 100,
}
_, err = sqlStore.SaveDashboard(cmd)
So(err, ShouldBeNil)
query := models.GetDashboardQuery{
Id: savedDash.Id,
OrgId: 1,
}
err = GetDashboard(context.Background(), &query)
So(err, ShouldBeNil)
So(query.Result.FolderId, ShouldEqual, 0)
So(query.Result.CreatedBy, ShouldEqual, savedDash.CreatedBy)
So(query.Result.Created, ShouldHappenWithin, 3*time.Second, savedDash.Created)
So(query.Result.UpdatedBy, ShouldEqual, 100)
So(query.Result.Updated.IsZero(), ShouldBeFalse)
})
Convey("Should be able to delete empty folder", func() {
emptyFolder := insertTestDashboard(t, sqlStore, "2 test dash folder", 1, 0, true, "prod", "webapp")
deleteCmd := &models.DeleteDashboardCommand{Id: emptyFolder.Id}
err := DeleteDashboard(context.Background(), deleteCmd)
So(err, ShouldBeNil)
})
Convey("Should be not able to delete a dashboard if force delete rules is disabled", func() {
deleteCmd := &models.DeleteDashboardCommand{Id: savedFolder.Id, ForceDeleteFolderRules: false}
err := DeleteDashboard(context.Background(), deleteCmd)
So(errors.Is(err, models.ErrFolderContainsAlertRules), ShouldBeTrue)
})
Convey("Should be able to delete a dashboard folder and its children if force delete rules is enabled", func() {
deleteCmd := &models.DeleteDashboardCommand{Id: savedFolder.Id, ForceDeleteFolderRules: true}
err := DeleteDashboard(context.Background(), deleteCmd)
So(err, ShouldBeNil)
query := search.FindPersistedDashboardsQuery{
OrgId: 1,
FolderIds: []int64{savedFolder.Id},
SignedInUser: &models.SignedInUser{},
}
err = SearchDashboards(context.Background(), &query)
So(err, ShouldBeNil)
So(len(query.Result), ShouldEqual, 0)
sqlStore.WithDbSession(context.Background(), func(sess *DBSession) error {
var existingRuleID int64
exists, err := sess.Table("alert_rule").Where("namespace_uid = (SELECT uid FROM dashboard WHERE id = ?)", savedFolder.Id).Cols("id").Get(&existingRuleID)
require.NoError(t, err)
So(exists, ShouldBeFalse)
var existingRuleVersionID int64
exists, err = sess.Table("alert_rule_version").Where("rule_namespace_uid = (SELECT uid FROM dashboard WHERE id = ?)", savedFolder.Id).Cols("id").Get(&existingRuleVersionID)
require.NoError(t, err)
So(exists, ShouldBeFalse)
return nil
})
})
Convey("Should return error if no dashboard is found for update when dashboard id is greater than zero", func() {
cmd := models.SaveDashboardCommand{
OrgId: 1,
Overwrite: true,
Dashboard: simplejson.NewFromAny(map[string]interface{}{
"id": float64(123412321),
"title": "Expect error",
"tags": []interface{}{},
}),
}
_, err := sqlStore.SaveDashboard(cmd)
So(err, ShouldEqual, models.ErrDashboardNotFound)
})
Convey("Should not return error if no dashboard is found for update when dashboard id is zero", func() {
cmd := models.SaveDashboardCommand{
OrgId: 1,
Overwrite: true,
Dashboard: simplejson.NewFromAny(map[string]interface{}{
"id": 0,
"title": "New dash",
"tags": []interface{}{},
}),
}
_, err := sqlStore.SaveDashboard(cmd)
So(err, ShouldBeNil)
})
Convey("Should be able to get dashboard tags", func() {
query := models.GetDashboardTagsQuery{OrgId: 1}
err := GetDashboardTags(context.Background(), &query)
So(err, ShouldBeNil)
So(len(query.Result), ShouldEqual, 2)
})
Convey("Should be able to search for dashboard folder", func() {
query := search.FindPersistedDashboardsQuery{
Title: "1 test dash folder",
OrgId: 1,
SignedInUser: &models.SignedInUser{OrgId: 1, OrgRole: models.ROLE_EDITOR},
}
err := SearchDashboards(context.Background(), &query)
So(err, ShouldBeNil)
So(len(query.Result), ShouldEqual, 1)
hit := query.Result[0]
So(hit.Type, ShouldEqual, search.DashHitFolder)
So(hit.URL, ShouldEqual, fmt.Sprintf("/dashboards/f/%s/%s", savedFolder.Uid, savedFolder.Slug))
So(hit.FolderTitle, ShouldEqual, "")
})
Convey("Should be able to limit search", func() {
query := search.FindPersistedDashboardsQuery{
OrgId: 1,
Limit: 1,
SignedInUser: &models.SignedInUser{OrgId: 1, OrgRole: models.ROLE_EDITOR},
}
err := SearchDashboards(context.Background(), &query)
So(err, ShouldBeNil)
So(len(query.Result), ShouldEqual, 1)
So(query.Result[0].Title, ShouldEqual, "1 test dash folder")
})
Convey("Should be able to search beyond limit using paging", func() {
query := search.FindPersistedDashboardsQuery{
OrgId: 1,
Limit: 1,
Page: 2,
SignedInUser: &models.SignedInUser{OrgId: 1, OrgRole: models.ROLE_EDITOR},
}
err := SearchDashboards(context.Background(), &query)
So(err, ShouldBeNil)
So(len(query.Result), ShouldEqual, 1)
So(query.Result[0].Title, ShouldEqual, "test dash 23")
})
Convey("Should be able to filter by tag and type", func() {
query := search.FindPersistedDashboardsQuery{
OrgId: 1,
Type: "dash-db",
Tags: []string{"prod"},
SignedInUser: &models.SignedInUser{OrgId: 1, OrgRole: models.ROLE_EDITOR},
}
err := SearchDashboards(context.Background(), &query)
So(err, ShouldBeNil)
So(len(query.Result), ShouldEqual, 3)
So(query.Result[0].Title, ShouldEqual, "test dash 23")
})
Convey("Should be able to search for a dashboard folder's children", func() {
query := search.FindPersistedDashboardsQuery{
OrgId: 1,
FolderIds: []int64{savedFolder.Id},
SignedInUser: &models.SignedInUser{OrgId: 1, OrgRole: models.ROLE_EDITOR},
}
err := SearchDashboards(context.Background(), &query)
So(err, ShouldBeNil)
So(len(query.Result), ShouldEqual, 2)
hit := query.Result[0]
So(hit.ID, ShouldEqual, savedDash.Id)
So(hit.URL, ShouldEqual, fmt.Sprintf("/d/%s/%s", savedDash.Uid, savedDash.Slug))
So(hit.FolderID, ShouldEqual, savedFolder.Id)
So(hit.FolderUID, ShouldEqual, savedFolder.Uid)
So(hit.FolderTitle, ShouldEqual, savedFolder.Title)
So(hit.FolderURL, ShouldEqual, fmt.Sprintf("/dashboards/f/%s/%s", savedFolder.Uid, savedFolder.Slug))
})
Convey("Should be able to search for dashboard by dashboard ids", func() {
Convey("should be able to find two dashboards by id", func() {
query := search.FindPersistedDashboardsQuery{
DashboardIds: []int64{savedDash.Id, savedDash2.Id},
SignedInUser: &models.SignedInUser{OrgId: 1, OrgRole: models.ROLE_EDITOR},
}
err := SearchDashboards(context.Background(), &query)
So(err, ShouldBeNil)
So(len(query.Result), ShouldEqual, 2)
hit := query.Result[0]
So(len(hit.Tags), ShouldEqual, 2)
hit2 := query.Result[1]
So(len(hit2.Tags), ShouldEqual, 1)
})
})
Convey("Given two dashboards, one is starred dashboard by user 10, other starred by user 1", func() {
starredDash := insertTestDashboard(t, sqlStore, "starred dash", 1, 0, false)
err := sqlStore.StarDashboard(context.Background(), &models.StarDashboardCommand{
DashboardId: starredDash.Id,
UserId: 10,
})
So(err, ShouldBeNil)
err = sqlStore.StarDashboard(context.Background(), &models.StarDashboardCommand{
DashboardId: savedDash.Id,
UserId: 1,
})
So(err, ShouldBeNil)
Convey("Should be able to search for starred dashboards", func() {
query := search.FindPersistedDashboardsQuery{
SignedInUser: &models.SignedInUser{UserId: 10, OrgId: 1, OrgRole: models.ROLE_EDITOR},
IsStarred: true,
}
err := SearchDashboards(context.Background(), &query)
So(err, ShouldBeNil)
So(len(query.Result), ShouldEqual, 1)
So(query.Result[0].Title, ShouldEqual, "starred dash")
})
})
err := DeleteDashboard(context.Background(), &models.DeleteDashboardCommand{
Id: dash.Id,
OrgId: 1,
})
require.NoError(t, err)
})
Convey("Given a plugin with imported dashboards", func() {
pluginId := "test-app"
t.Run("Should retry generation of uid once if it fails.", func(t *testing.T) {
setup()
timesCalled := 0
generateNewUid = func() string {
timesCalled += 1
if timesCalled <= 2 {
return savedDash.Uid
}
return util.GenerateShortUID()
}
cmd := models.SaveDashboardCommand{
OrgId: 1,
Dashboard: simplejson.NewFromAny(map[string]interface{}{
"title": "new dash 12334",
"tags": []interface{}{},
}),
}
_, err := sqlStore.SaveDashboard(cmd)
require.NoError(t, err)
appFolder := insertTestDashboardForPlugin(t, sqlStore, "app-test", 1, 0, true, pluginId)
insertTestDashboardForPlugin(t, sqlStore, "app-dash1", 1, appFolder.Id, false, pluginId)
insertTestDashboardForPlugin(t, sqlStore, "app-dash2", 1, appFolder.Id, false, pluginId)
generateNewUid = util.GenerateShortUID
})
Convey("Should return imported dashboard", func() {
query := models.GetDashboardsByPluginIdQuery{
PluginId: pluginId,
OrgId: 1,
}
t.Run("Should be able to create dashboard", func(t *testing.T) {
setup()
cmd := models.SaveDashboardCommand{
OrgId: 1,
Dashboard: simplejson.NewFromAny(map[string]interface{}{
"title": "folderId",
"tags": []interface{}{},
}),
UserId: 100,
}
dashboard, err := sqlStore.SaveDashboard(cmd)
require.NoError(t, err)
require.EqualValues(t, dashboard.CreatedBy, 100)
require.False(t, dashboard.Created.IsZero())
require.EqualValues(t, dashboard.UpdatedBy, 100)
require.False(t, dashboard.Updated.IsZero())
})
t.Run("Should be able to update dashboard by id and remove folderId", func(t *testing.T) {
setup()
cmd := models.SaveDashboardCommand{
OrgId: 1,
Dashboard: simplejson.NewFromAny(map[string]interface{}{
"id": savedDash.Id,
"title": "folderId",
"tags": []interface{}{},
}),
Overwrite: true,
FolderId: 2,
UserId: 100,
}
dash, err := sqlStore.SaveDashboard(cmd)
require.NoError(t, err)
require.EqualValues(t, dash.FolderId, 2)
err := GetDashboardsByPluginId(context.Background(), &query)
So(err, ShouldBeNil)
So(len(query.Result), ShouldEqual, 2)
})
cmd = models.SaveDashboardCommand{
OrgId: 1,
Dashboard: simplejson.NewFromAny(map[string]interface{}{
"id": savedDash.Id,
"title": "folderId",
"tags": []interface{}{},
}),
FolderId: 0,
Overwrite: true,
UserId: 100,
}
_, err = sqlStore.SaveDashboard(cmd)
require.NoError(t, err)
query := models.GetDashboardQuery{
Id: savedDash.Id,
OrgId: 1,
}
err = GetDashboard(context.Background(), &query)
require.NoError(t, err)
require.Equal(t, query.Result.FolderId, int64(0))
require.Equal(t, query.Result.CreatedBy, savedDash.CreatedBy)
require.WithinDuration(t, query.Result.Created, savedDash.Created, 3*time.Second)
require.Equal(t, query.Result.UpdatedBy, int64(100))
require.False(t, query.Result.Updated.IsZero())
})
t.Run("Should be able to delete empty folder", func(t *testing.T) {
setup()
emptyFolder := insertTestDashboard(t, sqlStore, "2 test dash folder", 1, 0, true, "prod", "webapp")
deleteCmd := &models.DeleteDashboardCommand{Id: emptyFolder.Id}
err := DeleteDashboard(context.Background(), deleteCmd)
require.NoError(t, err)
})
t.Run("Should be not able to delete a dashboard if force delete rules is disabled", func(t *testing.T) {
setup()
deleteCmd := &models.DeleteDashboardCommand{Id: savedFolder.Id, ForceDeleteFolderRules: false}
err := DeleteDashboard(context.Background(), deleteCmd)
require.True(t, errors.Is(err, models.ErrFolderContainsAlertRules))
})
t.Run("Should be able to delete a dashboard folder and its children if force delete rules is enabled", func(t *testing.T) {
setup()
deleteCmd := &models.DeleteDashboardCommand{Id: savedFolder.Id, ForceDeleteFolderRules: true}
err := DeleteDashboard(context.Background(), deleteCmd)
require.NoError(t, err)
query := search.FindPersistedDashboardsQuery{
OrgId: 1,
FolderIds: []int64{savedFolder.Id},
SignedInUser: &models.SignedInUser{},
}
err = SearchDashboards(context.Background(), &query)
require.NoError(t, err)
require.Equal(t, len(query.Result), 0)
sqlStore.WithDbSession(context.Background(), func(sess *DBSession) error {
var existingRuleID int64
exists, err := sess.Table("alert_rule").Where("namespace_uid = (SELECT uid FROM dashboard WHERE id = ?)", savedFolder.Id).Cols("id").Get(&existingRuleID)
require.NoError(t, err)
require.False(t, exists)
var existingRuleVersionID int64
exists, err = sess.Table("alert_rule_version").Where("rule_namespace_uid = (SELECT uid FROM dashboard WHERE id = ?)", savedFolder.Id).Cols("id").Get(&existingRuleVersionID)
require.NoError(t, err)
require.False(t, exists)
return nil
})
})
t.Run("Should return error if no dashboard is found for update when dashboard id is greater than zero", func(t *testing.T) {
setup()
cmd := models.SaveDashboardCommand{
OrgId: 1,
Overwrite: true,
Dashboard: simplejson.NewFromAny(map[string]interface{}{
"id": float64(123412321),
"title": "Expect error",
"tags": []interface{}{},
}),
}
_, err := sqlStore.SaveDashboard(cmd)
require.Equal(t, err, models.ErrDashboardNotFound)
})
t.Run("Should not return error if no dashboard is found for update when dashboard id is zero", func(t *testing.T) {
setup()
cmd := models.SaveDashboardCommand{
OrgId: 1,
Overwrite: true,
Dashboard: simplejson.NewFromAny(map[string]interface{}{
"id": 0,
"title": "New dash",
"tags": []interface{}{},
}),
}
_, err := sqlStore.SaveDashboard(cmd)
require.NoError(t, err)
})
t.Run("Should be able to get dashboard tags", func(t *testing.T) {
setup()
query := models.GetDashboardTagsQuery{OrgId: 1}
err := GetDashboardTags(context.Background(), &query)
require.NoError(t, err)
require.Equal(t, len(query.Result), 2)
})
t.Run("Should be able to search for dashboard folder", func(t *testing.T) {
setup()
query := search.FindPersistedDashboardsQuery{
Title: "1 test dash folder",
OrgId: 1,
SignedInUser: &models.SignedInUser{OrgId: 1, OrgRole: models.ROLE_EDITOR},
}
err := SearchDashboards(context.Background(), &query)
require.NoError(t, err)
require.Equal(t, len(query.Result), 1)
hit := query.Result[0]
require.Equal(t, hit.Type, search.DashHitFolder)
require.Equal(t, hit.URL, fmt.Sprintf("/dashboards/f/%s/%s", savedFolder.Uid, savedFolder.Slug))
require.Equal(t, hit.FolderTitle, "")
})
t.Run("Should be able to limit search", func(t *testing.T) {
setup()
query := search.FindPersistedDashboardsQuery{
OrgId: 1,
Limit: 1,
SignedInUser: &models.SignedInUser{OrgId: 1, OrgRole: models.ROLE_EDITOR},
}
err := SearchDashboards(context.Background(), &query)
require.NoError(t, err)
require.Equal(t, len(query.Result), 1)
require.EqualValues(t, query.Result[0].Title, "1 test dash folder")
})
t.Run("Should be able to search beyond limit using paging", func(t *testing.T) {
setup()
query := search.FindPersistedDashboardsQuery{
OrgId: 1,
Limit: 1,
Page: 2,
SignedInUser: &models.SignedInUser{OrgId: 1, OrgRole: models.ROLE_EDITOR},
}
err := SearchDashboards(context.Background(), &query)
require.NoError(t, err)
require.Equal(t, len(query.Result), 1)
require.EqualValues(t, query.Result[0].Title, "test dash 23")
})
t.Run("Should be able to filter by tag and type", func(t *testing.T) {
setup()
query := search.FindPersistedDashboardsQuery{
OrgId: 1,
Type: "dash-db",
Tags: []string{"prod"},
SignedInUser: &models.SignedInUser{OrgId: 1, OrgRole: models.ROLE_EDITOR},
}
err := SearchDashboards(context.Background(), &query)
require.NoError(t, err)
require.Equal(t, len(query.Result), 3)
require.Equal(t, query.Result[0].Title, "test dash 23")
})
t.Run("Should be able to search for a dashboard folder's children", func(t *testing.T) {
setup()
query := search.FindPersistedDashboardsQuery{
OrgId: 1,
FolderIds: []int64{savedFolder.Id},
SignedInUser: &models.SignedInUser{OrgId: 1, OrgRole: models.ROLE_EDITOR},
}
err := SearchDashboards(context.Background(), &query)
require.NoError(t, err)
require.Equal(t, len(query.Result), 2)
hit := query.Result[0]
require.Equal(t, hit.ID, savedDash.Id)
require.Equal(t, hit.URL, fmt.Sprintf("/d/%s/%s", savedDash.Uid, savedDash.Slug))
require.Equal(t, hit.FolderID, savedFolder.Id)
require.Equal(t, hit.FolderUID, savedFolder.Uid)
require.Equal(t, hit.FolderTitle, savedFolder.Title)
require.Equal(t, hit.FolderURL, fmt.Sprintf("/dashboards/f/%s/%s", savedFolder.Uid, savedFolder.Slug))
})
t.Run("Should be able to search for dashboard by dashboard ids", func(t *testing.T) {
setup()
query := search.FindPersistedDashboardsQuery{
DashboardIds: []int64{savedDash.Id, savedDash2.Id},
SignedInUser: &models.SignedInUser{OrgId: 1, OrgRole: models.ROLE_EDITOR},
}
err := SearchDashboards(context.Background(), &query)
require.NoError(t, err)
require.Equal(t, len(query.Result), 2)
hit := query.Result[0]
require.Equal(t, len(hit.Tags), 2)
hit2 := query.Result[1]
require.Equal(t, len(hit2.Tags), 1)
})
t.Run("Should be able to search for starred dashboards", func(t *testing.T) {
setup()
starredDash := insertTestDashboard(t, sqlStore, "starred dash", 1, 0, false)
err := sqlStore.StarDashboard(context.Background(), &models.StarDashboardCommand{
DashboardId: starredDash.Id,
UserId: 10,
})
require.NoError(t, err)
err = sqlStore.StarDashboard(context.Background(), &models.StarDashboardCommand{
DashboardId: savedDash.Id,
UserId: 1,
})
require.NoError(t, err)
query := search.FindPersistedDashboardsQuery{
SignedInUser: &models.SignedInUser{UserId: 10, OrgId: 1, OrgRole: models.ROLE_EDITOR},
IsStarred: true,
}
err = SearchDashboards(context.Background(), &query)
require.NoError(t, err)
require.Equal(t, len(query.Result), 1)
require.Equal(t, query.Result[0].Title, "starred dash")
})
}
func TestDashboardDataAccessGivenPluginWithImportedDashboards(t *testing.T) {
sqlStore := InitTestDB(t)
pluginId := "test-app"
appFolder := insertTestDashboardForPlugin(t, sqlStore, "app-test", 1, 0, true, pluginId)
insertTestDashboardForPlugin(t, sqlStore, "app-dash1", 1, appFolder.Id, false, pluginId)
insertTestDashboardForPlugin(t, sqlStore, "app-dash2", 1, appFolder.Id, false, pluginId)
query := models.GetDashboardsByPluginIdQuery{
PluginId: pluginId,
OrgId: 1,
}
err := GetDashboardsByPluginId(context.Background(), &query)
require.NoError(t, err)
require.Equal(t, len(query.Result), 2)
}
func TestDashboard_SortingOptions(t *testing.T) {
// insertTestDashboard uses GoConvey's assertions. Workaround.
Convey("test with multiple sorting options", t, func() {
t.Run("test with multiple sorting options", func(t *testing.T) {
sqlStore := InitTestDB(t)
dashB := insertTestDashboard(t, sqlStore, "Beta", 1, 0, false)
dashA := insertTestDashboard(t, sqlStore, "Alfa", 1, 0, false)
assert.NotZero(t, dashA.Id)
assert.Less(t, dashB.Id, dashA.Id)
q := &search.FindPersistedDashboardsQuery{
SignedInUser: &models.SignedInUser{OrgId: 1, UserId: 1, OrgRole: models.ROLE_ADMIN},
// adding two sorting options (silly no-op example, but it'll complicate the query)
@ -453,17 +465,14 @@ func TestDashboard_SortingOptions(t *testing.T) {
}
dashboards, err := findDashboards(q)
require.NoError(t, err)
require.Len(t, dashboards, 2)
assert.Equal(t, dashA.Id, dashboards[0].ID)
assert.Equal(t, dashB.Id, dashboards[1].ID)
})
}
func insertTestDashboard(t *testing.T, sqlStore *SQLStore, title string, orgId int64,
folderId int64, isFolder bool, tags ...interface{}) *models.Dashboard {
t.Helper()
cmd := models.SaveDashboardCommand{
OrgId: orgId,
FolderId: folderId,
@ -477,22 +486,17 @@ func insertTestDashboard(t *testing.T, sqlStore *SQLStore, title string, orgId i
dash, err := sqlStore.SaveDashboard(cmd)
require.NoError(t, err)
require.NotNil(t, dash)
dash.Data.Set("id", dash.Id)
dash.Data.Set("uid", dash.Uid)
return dash
}
func insertTestRule(t *testing.T, sqlStore *SQLStore, foderOrgID int64, folderUID string) {
sqlStore.WithDbSession(context.Background(), func(sess *DBSession) error {
type alertQuery struct {
RefID string
DatasourceUID string
Model json.RawMessage
}
type alertRule struct {
ID int64 `xorm:"pk autoincr 'id'"`
OrgID int64 `xorm:"org_id"`
@ -504,7 +508,6 @@ func insertTestRule(t *testing.T, sqlStore *SQLStore, foderOrgID int64, folderUI
Condition string
Data []alertQuery
}
rule := alertRule{
OrgID: foderOrgID,
NamespaceUID: folderUID,
@ -525,7 +528,6 @@ func insertTestRule(t *testing.T, sqlStore *SQLStore, foderOrgID int64, folderUI
}
_, err := sess.Insert(&rule)
require.NoError(t, err)
type alertRuleVersion struct {
ID int64 `xorm:"pk autoincr 'id'"`
RuleOrgID int64 `xorm:"rule_org_id"`
@ -541,7 +543,6 @@ func insertTestRule(t *testing.T, sqlStore *SQLStore, foderOrgID int64, folderUI
Data []alertQuery
IntervalSeconds int64
}
ruleVersion := alertRuleVersion{
RuleOrgID: rule.OrgID,
RuleUID: rule.UID,
@ -557,15 +558,12 @@ func insertTestRule(t *testing.T, sqlStore *SQLStore, foderOrgID int64, folderUI
}
_, err = sess.Insert(&ruleVersion)
require.NoError(t, err)
return err
})
}
func insertTestDashboardForPlugin(t *testing.T, sqlStore *SQLStore, title string, orgId int64,
folderId int64, isFolder bool, pluginId string) *models.Dashboard {
t.Helper()
cmd := models.SaveDashboardCommand{
OrgId: orgId,
FolderId: folderId,
@ -578,26 +576,21 @@ func insertTestDashboardForPlugin(t *testing.T, sqlStore *SQLStore, title string
}
dash, err := sqlStore.SaveDashboard(cmd)
So(err, ShouldBeNil)
require.NoError(t, err)
return dash
}
func createUser(t *testing.T, sqlStore *SQLStore, name string, role string, isAdmin bool) models.User {
t.Helper()
setting.AutoAssignOrg = true
setting.AutoAssignOrgId = 1
setting.AutoAssignOrgRole = role
currentUserCmd := models.CreateUserCommand{Login: name, Email: name + "@test.com", Name: "a " + name, IsAdmin: isAdmin}
currentUser, err := sqlStore.CreateUser(context.Background(), currentUserCmd)
require.NoError(t, err)
q1 := models.GetUserOrgListQuery{UserId: currentUser.Id}
err = GetUserOrgList(context.Background(), &q1)
require.NoError(t, err)
require.Equal(t, models.RoleType(role), q1.Result[0].Role)
return *currentUser
}