2020-10-16 02:46:14 -05:00
|
|
|
// +build integration
|
|
|
|
|
2015-01-05 10:04:29 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2018-06-15 14:23:57 -05:00
|
|
|
"context"
|
2018-01-30 08:24:14 -06:00
|
|
|
"fmt"
|
2015-01-05 10:04:29 -06:00
|
|
|
"testing"
|
2018-02-22 04:54:28 -06:00
|
|
|
"time"
|
2015-01-05 10:04:29 -06: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"
|
2015-06-05 04:08:19 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/search"
|
2020-05-06 04:42:52 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
|
2017-06-16 19:33:53 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2018-01-31 10:27:28 -06:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2020-05-06 04:42:52 -05:00
|
|
|
|
2018-01-31 10:27:28 -06:00
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2020-05-06 04:42:52 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2015-01-05 10:04:29 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDashboardDataAccess(t *testing.T) {
|
|
|
|
Convey("Testing DB", t, func() {
|
2021-03-17 10:06:10 -05:00
|
|
|
sqlStore := InitTestDB(t)
|
2015-01-05 10:04:29 -06:00
|
|
|
|
|
|
|
Convey("Given saved dashboard", func() {
|
2021-03-17 10:06:10 -05:00
|
|
|
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")
|
2021-04-21 03:22:46 -05:00
|
|
|
savedDash2 := insertTestDashboard(t, sqlStore, "test dash 67", 1, 0, false, "prod")
|
2015-01-05 10:04:29 -06:00
|
|
|
|
|
|
|
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)
|
2017-03-27 07:36:28 -05:00
|
|
|
So(savedDash.IsFolder, ShouldBeFalse)
|
2017-06-23 15:00:26 -05:00
|
|
|
So(savedDash.FolderId, ShouldBeGreaterThan, 0)
|
2018-01-29 14:23:07 -06:00
|
|
|
So(len(savedDash.Uid), ShouldBeGreaterThan, 0)
|
2017-03-27 07:36:28 -05:00
|
|
|
|
|
|
|
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)
|
2017-06-23 15:00:26 -05:00
|
|
|
So(savedFolder.FolderId, ShouldEqual, 0)
|
2018-01-29 14:23:07 -06:00
|
|
|
So(len(savedFolder.Uid), ShouldBeGreaterThan, 0)
|
2015-01-05 10:04:29 -06:00
|
|
|
})
|
|
|
|
|
2018-01-29 14:23:07 -06:00
|
|
|
Convey("Should be able to get dashboard by id", func() {
|
2020-02-29 06:35:15 -06:00
|
|
|
query := models.GetDashboardQuery{
|
2018-01-29 14:23:07 -06:00
|
|
|
Id: savedDash.Id,
|
|
|
|
OrgId: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := GetDashboard(&query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should be able to get dashboard by slug", func() {
|
2020-02-29 06:35:15 -06:00
|
|
|
query := models.GetDashboardQuery{
|
2015-02-23 13:07:49 -06:00
|
|
|
Slug: "test-dash-23",
|
|
|
|
OrgId: 1,
|
2015-01-05 10:04:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
err := GetDashboard(&query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(query.Result.Title, ShouldEqual, "test dash 23")
|
|
|
|
So(query.Result.Slug, ShouldEqual, "test-dash-23")
|
2018-01-29 14:23:07 -06:00
|
|
|
So(query.Result.Id, ShouldEqual, savedDash.Id)
|
|
|
|
So(query.Result.Uid, ShouldEqual, savedDash.Uid)
|
|
|
|
So(query.Result.IsFolder, ShouldBeFalse)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should be able to get dashboard by uid", func() {
|
2020-02-29 06:35:15 -06:00
|
|
|
query := models.GetDashboardQuery{
|
2018-01-29 14:23:07 -06:00
|
|
|
Uid: savedDash.Uid,
|
|
|
|
OrgId: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := GetDashboard(&query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
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)
|
2017-03-27 07:36:28 -05:00
|
|
|
So(query.Result.IsFolder, ShouldBeFalse)
|
2015-01-05 10:04:29 -06:00
|
|
|
})
|
|
|
|
|
2020-02-17 08:32:20 -06:00
|
|
|
Convey("Shouldn't be able to get a dashboard with just an OrgID", func() {
|
2020-02-29 06:35:15 -06:00
|
|
|
query := models.GetDashboardQuery{
|
2020-02-17 08:32:20 -06:00
|
|
|
OrgId: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := GetDashboard(&query)
|
2020-02-29 06:35:15 -06:00
|
|
|
So(err, ShouldEqual, models.ErrDashboardIdentifierNotSet)
|
2020-02-17 08:32:20 -06:00
|
|
|
})
|
|
|
|
|
2016-04-25 01:46:15 -05:00
|
|
|
Convey("Should be able to delete dashboard", func() {
|
2021-03-17 10:06:10 -05:00
|
|
|
dash := insertTestDashboard(t, sqlStore, "delete me", 1, 0, false, "delete this")
|
2016-04-25 01:46:15 -05:00
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
err := DeleteDashboard(&models.DeleteDashboardCommand{
|
2017-06-17 17:24:38 -05:00
|
|
|
Id: dash.Id,
|
2016-04-25 01:46:15 -05:00
|
|
|
OrgId: 1,
|
|
|
|
})
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
2018-01-31 10:27:28 -06:00
|
|
|
Convey("Should retry generation of uid once if it fails.", func() {
|
|
|
|
timesCalled := 0
|
|
|
|
generateNewUid = func() string {
|
|
|
|
timesCalled += 1
|
|
|
|
if timesCalled <= 2 {
|
|
|
|
return savedDash.Uid
|
|
|
|
}
|
2019-01-28 15:37:44 -06:00
|
|
|
return util.GenerateShortUID()
|
2018-01-31 10:27:28 -06:00
|
|
|
}
|
2020-02-29 06:35:15 -06:00
|
|
|
cmd := models.SaveDashboardCommand{
|
2018-01-31 10:27:28 -06:00
|
|
|
OrgId: 1,
|
|
|
|
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
|
|
|
"title": "new dash 12334",
|
|
|
|
"tags": []interface{}{},
|
|
|
|
}),
|
|
|
|
}
|
2021-03-17 10:06:10 -05:00
|
|
|
_, err := sqlStore.SaveDashboard(cmd)
|
2018-01-31 10:27:28 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2019-01-28 15:37:44 -06:00
|
|
|
generateNewUid = util.GenerateShortUID
|
2018-01-31 10:27:28 -06:00
|
|
|
})
|
|
|
|
|
2018-02-22 04:54:28 -06:00
|
|
|
Convey("Should be able to create dashboard", func() {
|
2020-02-29 06:35:15 -06:00
|
|
|
cmd := models.SaveDashboardCommand{
|
2018-02-22 04:54:28 -06:00
|
|
|
OrgId: 1,
|
|
|
|
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
|
|
|
"title": "folderId",
|
|
|
|
"tags": []interface{}{},
|
|
|
|
}),
|
|
|
|
UserId: 100,
|
|
|
|
}
|
2021-03-17 10:06:10 -05:00
|
|
|
dashboard, err := sqlStore.SaveDashboard(cmd)
|
2018-02-22 04:54:28 -06:00
|
|
|
So(err, ShouldBeNil)
|
2021-03-17 10:06:10 -05:00
|
|
|
So(dashboard.CreatedBy, ShouldEqual, 100)
|
|
|
|
So(dashboard.Created.IsZero(), ShouldBeFalse)
|
|
|
|
So(dashboard.UpdatedBy, ShouldEqual, 100)
|
|
|
|
So(dashboard.Updated.IsZero(), ShouldBeFalse)
|
2018-02-22 04:54:28 -06:00
|
|
|
})
|
|
|
|
|
2018-02-08 05:48:38 -06:00
|
|
|
Convey("Should be able to update dashboard by id and remove folderId", func() {
|
2020-02-29 06:35:15 -06:00
|
|
|
cmd := models.SaveDashboardCommand{
|
2017-05-31 09:53:12 -05:00
|
|
|
OrgId: 1,
|
|
|
|
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
2018-02-08 05:48:38 -06:00
|
|
|
"id": savedDash.Id,
|
2017-06-23 15:00:26 -05:00
|
|
|
"title": "folderId",
|
2017-05-31 09:53:12 -05:00
|
|
|
"tags": []interface{}{},
|
|
|
|
}),
|
|
|
|
Overwrite: true,
|
2017-06-23 15:00:26 -05:00
|
|
|
FolderId: 2,
|
2018-02-22 04:54:28 -06:00
|
|
|
UserId: 100,
|
2017-05-31 09:53:12 -05:00
|
|
|
}
|
2021-03-17 10:06:10 -05:00
|
|
|
dash, err := sqlStore.SaveDashboard(cmd)
|
2017-05-31 09:53:12 -05:00
|
|
|
So(err, ShouldBeNil)
|
2021-03-17 10:06:10 -05:00
|
|
|
So(dash.FolderId, ShouldEqual, 2)
|
2017-05-31 09:53:12 -05:00
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
cmd = models.SaveDashboardCommand{
|
2017-05-31 09:53:12 -05:00
|
|
|
OrgId: 1,
|
|
|
|
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
2018-02-08 05:48:38 -06:00
|
|
|
"id": savedDash.Id,
|
2017-06-23 15:00:26 -05:00
|
|
|
"title": "folderId",
|
2017-05-31 09:53:12 -05:00
|
|
|
"tags": []interface{}{},
|
|
|
|
}),
|
2017-06-23 15:00:26 -05:00
|
|
|
FolderId: 0,
|
2017-05-31 09:53:12 -05:00
|
|
|
Overwrite: true,
|
2018-02-22 04:54:28 -06:00
|
|
|
UserId: 100,
|
2017-05-31 09:53:12 -05:00
|
|
|
}
|
2021-03-17 10:06:10 -05:00
|
|
|
_, err = sqlStore.SaveDashboard(cmd)
|
2017-05-31 09:53:12 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
query := models.GetDashboardQuery{
|
2018-02-08 05:48:38 -06:00
|
|
|
Id: savedDash.Id,
|
2017-05-31 09:53:12 -05:00
|
|
|
OrgId: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = GetDashboard(&query)
|
|
|
|
So(err, ShouldBeNil)
|
2017-06-23 15:00:26 -05:00
|
|
|
So(query.Result.FolderId, ShouldEqual, 0)
|
2018-02-22 04:54:28 -06:00
|
|
|
So(query.Result.CreatedBy, ShouldEqual, savedDash.CreatedBy)
|
2018-07-24 12:05:09 -05:00
|
|
|
So(query.Result.Created, ShouldHappenWithin, 3*time.Second, savedDash.Created)
|
2018-02-22 04:54:28 -06:00
|
|
|
So(query.Result.UpdatedBy, ShouldEqual, 100)
|
|
|
|
So(query.Result.Updated.IsZero(), ShouldBeFalse)
|
2017-05-31 09:53:12 -05:00
|
|
|
})
|
|
|
|
|
2020-11-06 02:02:31 -06:00
|
|
|
Convey("Should be able to delete empty folder", func() {
|
2021-03-17 10:06:10 -05:00
|
|
|
emptyFolder := insertTestDashboard(t, sqlStore, "2 test dash folder", 1, 0, true, "prod", "webapp")
|
2020-11-06 02:02:31 -06:00
|
|
|
|
|
|
|
deleteCmd := &models.DeleteDashboardCommand{Id: emptyFolder.Id}
|
|
|
|
err := DeleteDashboard(deleteCmd)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
2017-06-16 14:14:42 -05:00
|
|
|
Convey("Should be able to delete a dashboard folder and its children", func() {
|
2020-02-29 06:35:15 -06:00
|
|
|
deleteCmd := &models.DeleteDashboardCommand{Id: savedFolder.Id}
|
2017-06-16 14:14:42 -05:00
|
|
|
err := DeleteDashboard(deleteCmd)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
query := search.FindPersistedDashboardsQuery{
|
2017-06-16 19:33:53 -05:00
|
|
|
OrgId: 1,
|
2017-11-21 04:53:56 -06:00
|
|
|
FolderIds: []int64{savedFolder.Id},
|
2020-02-29 06:35:15 -06:00
|
|
|
SignedInUser: &models.SignedInUser{},
|
2017-06-16 14:14:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
err = SearchDashboards(&query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(len(query.Result), ShouldEqual, 0)
|
|
|
|
})
|
|
|
|
|
2018-02-19 04:12:56 -06:00
|
|
|
Convey("Should return error if no dashboard is found for update when dashboard id is greater than zero", func() {
|
2020-02-29 06:35:15 -06:00
|
|
|
cmd := models.SaveDashboardCommand{
|
2018-02-19 04:12:56 -06:00
|
|
|
OrgId: 1,
|
|
|
|
Overwrite: true,
|
|
|
|
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
|
|
|
"id": float64(123412321),
|
|
|
|
"title": "Expect error",
|
|
|
|
"tags": []interface{}{},
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
_, err := sqlStore.SaveDashboard(cmd)
|
2020-02-29 06:35:15 -06:00
|
|
|
So(err, ShouldEqual, models.ErrDashboardNotFound)
|
2018-02-19 04:12:56 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should not return error if no dashboard is found for update when dashboard id is zero", func() {
|
2020-02-29 06:35:15 -06:00
|
|
|
cmd := models.SaveDashboardCommand{
|
2018-02-19 04:12:56 -06:00
|
|
|
OrgId: 1,
|
|
|
|
Overwrite: true,
|
|
|
|
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
|
|
|
"id": 0,
|
|
|
|
"title": "New dash",
|
|
|
|
"tags": []interface{}{},
|
|
|
|
}),
|
|
|
|
}
|
2021-03-17 10:06:10 -05:00
|
|
|
_, err := sqlStore.SaveDashboard(cmd)
|
2018-02-19 04:12:56 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
2015-01-07 05:37:24 -06:00
|
|
|
Convey("Should be able to get dashboard tags", func() {
|
2020-02-29 06:35:15 -06:00
|
|
|
query := models.GetDashboardTagsQuery{OrgId: 1}
|
2015-01-05 10:04:29 -06:00
|
|
|
|
2015-01-07 05:37:24 -06:00
|
|
|
err := GetDashboardTags(&query)
|
|
|
|
So(err, ShouldBeNil)
|
2015-01-05 10:04:29 -06:00
|
|
|
|
2015-01-20 07:15:48 -06:00
|
|
|
So(len(query.Result), ShouldEqual, 2)
|
2015-01-07 05:37:24 -06:00
|
|
|
})
|
2015-02-04 04:35:59 -06:00
|
|
|
|
2018-02-08 05:48:38 -06:00
|
|
|
Convey("Should be able to search for dashboard folder", func() {
|
|
|
|
query := search.FindPersistedDashboardsQuery{
|
|
|
|
Title: "1 test dash folder",
|
|
|
|
OrgId: 1,
|
2020-02-29 06:35:15 -06:00
|
|
|
SignedInUser: &models.SignedInUser{OrgId: 1, OrgRole: models.ROLE_EDITOR},
|
2018-02-08 05:48:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
err := SearchDashboards(&query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(len(query.Result), ShouldEqual, 1)
|
|
|
|
hit := query.Result[0]
|
|
|
|
So(hit.Type, ShouldEqual, search.DashHitFolder)
|
2021-02-11 01:49:16 -06:00
|
|
|
So(hit.URL, ShouldEqual, fmt.Sprintf("/dashboards/f/%s/%s", savedFolder.Uid, savedFolder.Slug))
|
2018-02-08 05:48:38 -06:00
|
|
|
So(hit.FolderTitle, ShouldEqual, "")
|
|
|
|
})
|
|
|
|
|
2019-04-17 06:07:50 -05:00
|
|
|
Convey("Should be able to limit search", func() {
|
|
|
|
query := search.FindPersistedDashboardsQuery{
|
|
|
|
OrgId: 1,
|
|
|
|
Limit: 1,
|
2020-02-29 06:35:15 -06:00
|
|
|
SignedInUser: &models.SignedInUser{OrgId: 1, OrgRole: models.ROLE_EDITOR},
|
2019-04-17 06:07:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
err := SearchDashboards(&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,
|
2020-02-29 06:35:15 -06:00
|
|
|
SignedInUser: &models.SignedInUser{OrgId: 1, OrgRole: models.ROLE_EDITOR},
|
2019-04-17 06:07:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
err := SearchDashboards(&query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(len(query.Result), ShouldEqual, 1)
|
|
|
|
So(query.Result[0].Title, ShouldEqual, "test dash 23")
|
|
|
|
})
|
|
|
|
|
2019-04-17 12:07:13 -05:00
|
|
|
Convey("Should be able to filter by tag and type", func() {
|
|
|
|
query := search.FindPersistedDashboardsQuery{
|
|
|
|
OrgId: 1,
|
|
|
|
Type: "dash-db",
|
|
|
|
Tags: []string{"prod"},
|
2020-02-29 06:35:15 -06:00
|
|
|
SignedInUser: &models.SignedInUser{OrgId: 1, OrgRole: models.ROLE_EDITOR},
|
2019-04-17 12:07:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
err := SearchDashboards(&query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(len(query.Result), ShouldEqual, 3)
|
|
|
|
So(query.Result[0].Title, ShouldEqual, "test dash 23")
|
|
|
|
})
|
|
|
|
|
2018-02-08 05:48:38 -06:00
|
|
|
Convey("Should be able to search for a dashboard folder's children", func() {
|
|
|
|
query := search.FindPersistedDashboardsQuery{
|
|
|
|
OrgId: 1,
|
|
|
|
FolderIds: []int64{savedFolder.Id},
|
2020-02-29 06:35:15 -06:00
|
|
|
SignedInUser: &models.SignedInUser{OrgId: 1, OrgRole: models.ROLE_EDITOR},
|
2018-02-08 05:48:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
err := SearchDashboards(&query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(len(query.Result), ShouldEqual, 2)
|
|
|
|
hit := query.Result[0]
|
2021-02-11 01:49:16 -06:00
|
|
|
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)
|
2018-02-08 05:48:38 -06:00
|
|
|
So(hit.FolderTitle, ShouldEqual, savedFolder.Title)
|
2021-02-11 01:49:16 -06:00
|
|
|
So(hit.FolderURL, ShouldEqual, fmt.Sprintf("/dashboards/f/%s/%s", savedFolder.Uid, savedFolder.Slug))
|
2018-02-08 05:48:38 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
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{
|
2021-04-21 03:22:46 -05:00
|
|
|
DashboardIds: []int64{savedDash.Id, savedDash2.Id},
|
2020-02-29 06:35:15 -06:00
|
|
|
SignedInUser: &models.SignedInUser{OrgId: 1, OrgRole: models.ROLE_EDITOR},
|
2018-02-08 05:48:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
err := SearchDashboards(&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)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-04 04:35:59 -06:00
|
|
|
Convey("Given two dashboards, one is starred dashboard by user 10, other starred by user 1", func() {
|
2021-03-17 10:06:10 -05:00
|
|
|
starredDash := insertTestDashboard(t, sqlStore, "starred dash", 1, 0, false)
|
2020-02-29 06:35:15 -06:00
|
|
|
err := StarDashboard(&models.StarDashboardCommand{
|
2015-02-04 04:35:59 -06:00
|
|
|
DashboardId: starredDash.Id,
|
|
|
|
UserId: 10,
|
|
|
|
})
|
2019-10-22 07:08:18 -05:00
|
|
|
So(err, ShouldBeNil)
|
2015-02-04 04:35:59 -06:00
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
err = StarDashboard(&models.StarDashboardCommand{
|
2015-02-04 04:35:59 -06:00
|
|
|
DashboardId: savedDash.Id,
|
|
|
|
UserId: 1,
|
|
|
|
})
|
2019-10-22 07:08:18 -05:00
|
|
|
So(err, ShouldBeNil)
|
2015-02-04 04:35:59 -06:00
|
|
|
|
|
|
|
Convey("Should be able to search for starred dashboards", func() {
|
2018-02-13 09:49:00 -06:00
|
|
|
query := search.FindPersistedDashboardsQuery{
|
2020-02-29 06:35:15 -06:00
|
|
|
SignedInUser: &models.SignedInUser{UserId: 10, OrgId: 1, OrgRole: models.ROLE_EDITOR},
|
2018-02-13 09:49:00 -06:00
|
|
|
IsStarred: true,
|
|
|
|
}
|
2015-02-04 04:35:59 -06:00
|
|
|
err := SearchDashboards(&query)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(query.Result), ShouldEqual, 1)
|
|
|
|
So(query.Result[0].Title, ShouldEqual, "starred dash")
|
|
|
|
})
|
|
|
|
})
|
2015-01-07 05:37:24 -06:00
|
|
|
})
|
2017-06-16 19:33:53 -05:00
|
|
|
|
2017-11-27 10:08:39 -06:00
|
|
|
Convey("Given a plugin with imported dashboards", func() {
|
|
|
|
pluginId := "test-app"
|
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
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)
|
2017-11-27 10:08:39 -06:00
|
|
|
|
|
|
|
Convey("Should return imported dashboard", func() {
|
2020-02-29 06:35:15 -06:00
|
|
|
query := models.GetDashboardsByPluginIdQuery{
|
2017-11-27 10:08:39 -06:00
|
|
|
PluginId: pluginId,
|
|
|
|
OrgId: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := GetDashboardsByPluginId(&query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(query.Result), ShouldEqual, 2)
|
|
|
|
})
|
|
|
|
})
|
2015-01-07 05:37:24 -06:00
|
|
|
})
|
2015-01-05 10:04:29 -06:00
|
|
|
}
|
2017-06-16 19:33:53 -05:00
|
|
|
|
2020-05-06 04:42:52 -05:00
|
|
|
func TestDashboard_SortingOptions(t *testing.T) {
|
|
|
|
// insertTestDashboard uses GoConvey's assertions. Workaround.
|
|
|
|
Convey("test with multiple sorting options", t, func() {
|
2021-03-17 10:06:10 -05:00
|
|
|
sqlStore := InitTestDB(t)
|
|
|
|
dashB := insertTestDashboard(t, sqlStore, "Beta", 1, 0, false)
|
|
|
|
dashA := insertTestDashboard(t, sqlStore, "Alfa", 1, 0, false)
|
2020-05-06 04:42:52 -05:00
|
|
|
|
|
|
|
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)
|
|
|
|
Filters: []interface{}{
|
|
|
|
searchstore.TitleSorter{},
|
|
|
|
searchstore.TitleSorter{Descending: true},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
dashboards, err := findDashboards(q)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Len(t, dashboards, 2)
|
2021-02-11 01:49:16 -06:00
|
|
|
assert.Equal(t, dashA.Id, dashboards[0].ID)
|
|
|
|
assert.Equal(t, dashB.Id, dashboards[1].ID)
|
2020-05-06 04:42:52 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
func insertTestDashboard(t *testing.T, sqlStore *SQLStore, title string, orgId int64,
|
|
|
|
folderId int64, isFolder bool, tags ...interface{}) *models.Dashboard {
|
2020-11-11 14:33:32 -06:00
|
|
|
t.Helper()
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
cmd := models.SaveDashboardCommand{
|
2017-06-23 16:22:09 -05:00
|
|
|
OrgId: orgId,
|
|
|
|
FolderId: folderId,
|
|
|
|
IsFolder: isFolder,
|
|
|
|
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
|
|
|
"id": nil,
|
|
|
|
"title": title,
|
|
|
|
"tags": tags,
|
|
|
|
}),
|
|
|
|
}
|
2021-03-17 10:06:10 -05:00
|
|
|
dash, err := sqlStore.SaveDashboard(cmd)
|
2020-11-11 14:33:32 -06:00
|
|
|
require.NoError(t, err)
|
2021-03-17 10:06:10 -05:00
|
|
|
require.NotNil(t, dash)
|
2017-06-23 16:22:09 -05:00
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
dash.Data.Set("id", dash.Id)
|
|
|
|
dash.Data.Set("uid", dash.Uid)
|
2018-02-19 04:12:56 -06:00
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
return dash
|
2017-06-23 16:22:09 -05:00
|
|
|
}
|
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
func insertTestDashboardForPlugin(t *testing.T, sqlStore *SQLStore, title string, orgId int64,
|
|
|
|
folderId int64, isFolder bool, pluginId string) *models.Dashboard {
|
|
|
|
t.Helper()
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
cmd := models.SaveDashboardCommand{
|
2017-11-27 10:08:39 -06:00
|
|
|
OrgId: orgId,
|
|
|
|
FolderId: folderId,
|
|
|
|
IsFolder: isFolder,
|
|
|
|
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
|
|
|
"id": nil,
|
|
|
|
"title": title,
|
|
|
|
}),
|
|
|
|
PluginId: pluginId,
|
|
|
|
}
|
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
dash, err := sqlStore.SaveDashboard(cmd)
|
2017-11-27 10:08:39 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
return dash
|
2017-11-27 10:08:39 -06:00
|
|
|
}
|
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
func createUser(t *testing.T, sqlStore *SQLStore, name string, role string, isAdmin bool) models.User {
|
2020-11-11 14:33:32 -06:00
|
|
|
t.Helper()
|
|
|
|
|
2017-06-16 19:33:53 -05:00
|
|
|
setting.AutoAssignOrg = true
|
2018-07-13 14:14:40 -05:00
|
|
|
setting.AutoAssignOrgId = 1
|
2017-06-16 19:33:53 -05:00
|
|
|
setting.AutoAssignOrgRole = role
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
currentUserCmd := models.CreateUserCommand{Login: name, Email: name + "@test.com", Name: "a " + name, IsAdmin: isAdmin}
|
2021-03-17 10:06:10 -05:00
|
|
|
currentUser, err := sqlStore.CreateUser(context.Background(), currentUserCmd)
|
2020-11-11 14:33:32 -06:00
|
|
|
require.NoError(t, err)
|
2017-06-16 19:33:53 -05:00
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
q1 := models.GetUserOrgListQuery{UserId: currentUser.Id}
|
2019-10-22 07:08:18 -05:00
|
|
|
err = GetUserOrgList(&q1)
|
2020-11-11 14:33:32 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, models.RoleType(role), q1.Result[0].Role)
|
2017-06-16 19:33:53 -05:00
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
return *currentUser
|
2017-06-23 16:22:09 -05:00
|
|
|
}
|