backend/services: Move GetDashboard from sqlstore to dashboard service (#48971)

* rename folder to match package name
* backend/sqlstore: move GetDashboard into DashboardService

This is a stepping-stone commit which copies the GetDashboard function - which lets us remove the sqlstore from the interfaces in dashboards - without changing any other callers.
* checkpoint: moving GetDashboard calls into dashboard service
* finish refactoring api tests for dashboardService.GetDashboard
This commit is contained in:
Kristin Laemmert
2022-05-17 14:52:22 -04:00
committed by GitHub
parent 9af30f6570
commit 1df340ff28
47 changed files with 376 additions and 269 deletions
+3 -3
View File
@@ -53,7 +53,7 @@ func (hs *HTTPServer) GetAnnotations(c *models.ReqContext) response.Response {
item.DashboardUID = val
} else {
query := models.GetDashboardQuery{Id: item.DashboardId, OrgId: c.OrgId}
err := hs.SQLStore.GetDashboard(c.Req.Context(), &query)
err := hs.dashboardService.GetDashboard(c.Req.Context(), &query)
if err == nil && query.Result != nil {
item.DashboardUID = &query.Result.Uid
dashboardCache[item.DashboardId] = &query.Result.Uid
@@ -82,7 +82,7 @@ func (hs *HTTPServer) PostAnnotation(c *models.ReqContext) response.Response {
// overwrite dashboardId when dashboardUID is not empty
if cmd.DashboardUID != "" {
query := models.GetDashboardQuery{OrgId: c.OrgId, Uid: cmd.DashboardUID}
err := hs.SQLStore.GetDashboard(c.Req.Context(), &query)
err := hs.dashboardService.GetDashboard(c.Req.Context(), &query)
if err == nil {
cmd.DashboardId = query.Result.Id
}
@@ -291,7 +291,7 @@ func (hs *HTTPServer) MassDeleteAnnotations(c *models.ReqContext) response.Respo
if cmd.DashboardUID != "" {
query := models.GetDashboardQuery{OrgId: c.OrgId, Uid: cmd.DashboardUID}
err := hs.SQLStore.GetDashboard(c.Req.Context(), &query)
err := hs.dashboardService.GetDashboard(c.Req.Context(), &query)
if err == nil {
cmd.DashboardId = query.Result.Id
}
+3 -4
View File
@@ -16,6 +16,7 @@ import (
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/annotations"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/guardian"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
@@ -221,10 +222,6 @@ func TestAnnotationsAPIEndpoint(t *testing.T) {
role := models.ROLE_ADMIN
mock := mockstore.NewSQLStoreMock()
mock.ExpectedDashboard = &models.Dashboard{
Id: 1,
Uid: "home",
}
t.Run("Should be able to do anything", func(t *testing.T) {
postAnnotationScenario(t, "When calling POST on", "/api/annotations", "/api/annotations", role, cmd, store, func(sc *scenarioContext) {
@@ -327,6 +324,7 @@ func postAnnotationScenario(t *testing.T, desc string, url string, routePattern
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
hs := setupSimpleHTTPServer(nil)
hs.SQLStore = store
hs.dashboardService = &dashboards.FakeDashboardService{}
sc := setupScenarioContext(t, url)
sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) response.Response {
@@ -411,6 +409,7 @@ func deleteAnnotationsScenario(t *testing.T, desc string, url string, routePatte
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
hs := setupSimpleHTTPServer(nil)
hs.SQLStore = store
hs.dashboardService = &dashboards.FakeDashboardService{}
sc := setupScenarioContext(t, url)
sc.defaultHandler = routing.Wrap(func(c *models.ReqContext) response.Response {
+3 -2
View File
@@ -11,6 +11,8 @@ import (
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/api/routing"
"github.com/grafana/grafana/pkg/infra/fs"
@@ -27,7 +29,7 @@ import (
"github.com/grafana/grafana/pkg/services/contexthandler/authproxy"
"github.com/grafana/grafana/pkg/services/dashboards"
dashboardsstore "github.com/grafana/grafana/pkg/services/dashboards/database"
dashboardservice "github.com/grafana/grafana/pkg/services/dashboards/manager"
dashboardservice "github.com/grafana/grafana/pkg/services/dashboards/service"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/ldap"
"github.com/grafana/grafana/pkg/services/login/loginservice"
@@ -41,7 +43,6 @@ import (
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/web"
"github.com/grafana/grafana/pkg/web/webtest"
"github.com/stretchr/testify/require"
)
func loggedInUserScenario(t *testing.T, desc string, url string, routePattern string, fn scenarioFunc, sqlStore sqlstore.Store) {
+4 -4
View File
@@ -151,7 +151,7 @@ func (hs *HTTPServer) GetDashboard(c *models.ReqContext) response.Response {
// lookup folder title
if dash.FolderId > 0 {
query := models.GetDashboardQuery{Id: dash.FolderId, OrgId: c.OrgId}
if err := hs.SQLStore.GetDashboard(c.Req.Context(), &query); err != nil {
if err := hs.dashboardService.GetDashboard(c.Req.Context(), &query); err != nil {
if errors.Is(err, models.ErrFolderNotFound) {
return response.Error(404, "Folder not found", err)
}
@@ -242,7 +242,7 @@ func (hs *HTTPServer) getDashboardHelper(ctx context.Context, orgID int64, id in
query = models.GetDashboardQuery{Id: id, OrgId: orgID}
}
if err := hs.SQLStore.GetDashboard(ctx, &query); err != nil {
if err := hs.dashboardService.GetDashboard(ctx, &query); err != nil {
return nil, response.Error(404, "Dashboard not found", err)
}
@@ -531,7 +531,7 @@ func (hs *HTTPServer) GetDashboardVersions(c *models.ReqContext) response.Respon
OrgId: c.SignedInUser.OrgId,
Uid: dashUID,
}
if err := hs.SQLStore.GetDashboard(c.Req.Context(), &q); err != nil {
if err := hs.dashboardService.GetDashboard(c.Req.Context(), &q); err != nil {
return response.Error(http.StatusBadRequest, "failed to get dashboard by UID", err)
}
dashID = q.Result.Id
@@ -589,7 +589,7 @@ func (hs *HTTPServer) GetDashboardVersion(c *models.ReqContext) response.Respons
OrgId: c.SignedInUser.OrgId,
Uid: dashUID,
}
if err := hs.SQLStore.GetDashboard(c.Req.Context(), &q); err != nil {
if err := hs.dashboardService.GetDashboard(c.Req.Context(), &q); err != nil {
return response.Error(http.StatusBadRequest, "failed to get dashboard by UID", err)
}
dashID = q.Result.Id
+2 -1
View File
@@ -15,7 +15,7 @@ import (
"github.com/grafana/grafana/pkg/models"
accesscontrolmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock"
"github.com/grafana/grafana/pkg/services/dashboards"
dashboardservice "github.com/grafana/grafana/pkg/services/dashboards/manager"
dashboardservice "github.com/grafana/grafana/pkg/services/dashboards/service"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/guardian"
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
@@ -26,6 +26,7 @@ func TestDashboardPermissionAPIEndpoint(t *testing.T) {
t.Run("Dashboard permissions test", func(t *testing.T) {
settings := setting.NewCfg()
dashboardStore := &dashboards.FakeDashboardStore{}
dashboardStore.On("GetDashboard", mock.Anything, mock.AnythingOfType("*models.GetDashboardQuery")).Return(nil)
defer dashboardStore.AssertExpectations(t)
features := featuremgmt.WithFeatures()
+68 -49
View File
@@ -23,7 +23,7 @@ import (
"github.com/grafana/grafana/pkg/services/alerting"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/dashboards/database"
service "github.com/grafana/grafana/pkg/services/dashboards/manager"
service "github.com/grafana/grafana/pkg/services/dashboards/service"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/guardian"
"github.com/grafana/grafana/pkg/services/libraryelements"
@@ -101,7 +101,7 @@ func newTestLive(t *testing.T, store *sqlstore.SQLStore) *live.GrafanaLive {
nil,
&usagestats.UsageStatsMock{T: t},
nil,
features, accesscontrolmock.New())
features, accesscontrolmock.New(), &dashboards.FakeDashboardService{})
require.NoError(t, err)
return gLive
}
@@ -118,18 +118,22 @@ func TestDashboardAPIEndpoint(t *testing.T) {
fakeDash.Id = 1
fakeDash.FolderId = 1
fakeDash.HasAcl = false
dashboardService := &dashboards.FakeDashboardService{}
dashboardService.GetDashboardFn = func(ctx context.Context, cmd *models.GetDashboardQuery) error {
cmd.Result = fakeDash
return nil
}
mockSQLStore := mockstore.NewSQLStoreMock()
mockSQLStore.ExpectedDashboard = fakeDash
hs := &HTTPServer{
Cfg: setting.NewCfg(),
pluginStore: &fakePluginStore{},
SQLStore: mockSQLStore,
AccessControl: accesscontrolmock.New(),
Features: featuremgmt.WithFeatures(),
Cfg: setting.NewCfg(),
pluginStore: &fakePluginStore{},
SQLStore: mockSQLStore,
AccessControl: accesscontrolmock.New(),
Features: featuremgmt.WithFeatures(),
dashboardService: dashboardService,
}
hs.SQLStore = mockSQLStore
setUp := func() {
viewerRole := models.ROLE_VIEWER
@@ -153,7 +157,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
"/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
setUp()
sc.sqlStore = mockSQLStore
dash := getDashboardShouldReturn200(t, sc)
dash := getDashboardShouldReturn200WithConfig(t, sc, nil, nil, dashboardService)
assert.False(t, dash.Meta.CanEdit)
assert.False(t, dash.Meta.CanSave)
@@ -185,7 +189,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
"/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
setUp()
sc.sqlStore = mockSQLStore
dash := getDashboardShouldReturn200(t, sc)
dash := getDashboardShouldReturn200WithConfig(t, sc, nil, nil, dashboardService)
assert.True(t, dash.Meta.CanEdit)
assert.True(t, dash.Meta.CanSave)
@@ -216,14 +220,16 @@ func TestDashboardAPIEndpoint(t *testing.T) {
fakeDash.Id = 1
fakeDash.FolderId = 1
fakeDash.HasAcl = true
dashboardService := &dashboards.FakeDashboardService{}
dashboardService.GetDashboardFn = func(ctx context.Context, cmd *models.GetDashboardQuery) error {
cmd.Result = fakeDash
return nil
}
mockSQLStore := mockstore.NewSQLStoreMock()
mockSQLStore.ExpectedDashboard = fakeDash
cfg := setting.NewCfg()
features := featuremgmt.WithFeatures()
sql := sqlstore.InitTestDB(t)
dashboardStore := database.ProvideDashboardStore(sql)
hs := &HTTPServer{
Cfg: cfg,
Live: newTestLive(t, sql),
@@ -231,12 +237,8 @@ func TestDashboardAPIEndpoint(t *testing.T) {
LibraryElementService: &mockLibraryElementService{},
SQLStore: mockSQLStore,
AccessControl: accesscontrolmock.New(),
dashboardService: service.ProvideDashboardService(
cfg, dashboardStore, nil, features,
accesscontrolmock.NewMockedPermissionsService(), accesscontrolmock.NewMockedPermissionsService(),
),
dashboardService: dashboardService,
}
hs.SQLStore = mockSQLStore
setUp := func() {
origCanEdit := setting.ViewersCanEdit
@@ -281,7 +283,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
"/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
setUp()
sc.sqlStore = mockSQLStore
hs.callDeleteDashboardByUID(t, sc, nil)
hs.callDeleteDashboardByUID(t, sc, &dashboards.FakeDashboardService{})
assert.Equal(t, 403, sc.resp.Code)
}, mockSQLStore)
@@ -319,7 +321,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
loggedInUserScenarioWithRole(t, "When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi",
"/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
setUp()
hs.callDeleteDashboardByUID(t, sc, nil)
hs.callDeleteDashboardByUID(t, sc, &dashboards.FakeDashboardService{})
assert.Equal(t, 403, sc.resp.Code)
}, mockSQLStore)
@@ -357,7 +359,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
"/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
setUpInner()
sc.sqlStore = mockSQLStore
dash := getDashboardShouldReturn200(t, sc)
dash := getDashboardShouldReturn200WithConfig(t, sc, nil, nil, dashboardService)
assert.True(t, dash.Meta.CanEdit)
assert.True(t, dash.Meta.CanSave)
@@ -420,7 +422,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
require.True(t, setting.ViewersCanEdit)
sc.sqlStore = mockSQLStore
dash := getDashboardShouldReturn200(t, sc)
dash := getDashboardShouldReturn200WithConfig(t, sc, nil, nil, dashboardService)
assert.True(t, dash.Meta.CanEdit)
assert.False(t, dash.Meta.CanSave)
@@ -430,7 +432,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
loggedInUserScenarioWithRole(t, "When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
setUpInner()
hs.callDeleteDashboardByUID(t, sc, nil)
hs.callDeleteDashboardByUID(t, sc, dashboardService)
assert.Equal(t, 403, sc.resp.Code)
}, mockSQLStore)
})
@@ -450,7 +452,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
loggedInUserScenarioWithRole(t, "When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
setUpInner()
sc.sqlStore = mockSQLStore
dash := getDashboardShouldReturn200(t, sc)
dash := getDashboardShouldReturn200WithConfig(t, sc, nil, nil, dashboardService)
assert.True(t, dash.Meta.CanEdit)
assert.True(t, dash.Meta.CanSave)
@@ -495,7 +497,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
loggedInUserScenarioWithRole(t, "When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
setUpInner()
sc.sqlStore = mockSQLStore
dash := getDashboardShouldReturn200(t, sc)
dash := getDashboardShouldReturn200WithConfig(t, sc, nil, nil, dashboardService)
assert.False(t, dash.Meta.CanEdit)
assert.False(t, dash.Meta.CanSave)
@@ -503,7 +505,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
loggedInUserScenarioWithRole(t, "When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
setUpInner()
hs.callDeleteDashboardByUID(t, sc, nil)
hs.callDeleteDashboardByUID(t, sc, dashboardService)
assert.Equal(t, 403, sc.resp.Code)
}, mockSQLStore)
@@ -780,13 +782,19 @@ func TestDashboardAPIEndpoint(t *testing.T) {
fakeDash.FolderId = folderID
fakeDash.HasAcl = false
saved := &models.Dashboard{
Id: 2,
Uid: "uid",
Title: "Dash",
Slug: "dash",
Version: 1,
}
mock := &dashboards.FakeDashboardService{
SaveDashboardResult: &models.Dashboard{
Id: 2,
Uid: "uid",
Title: "Dash",
Slug: "dash",
Version: 1,
SaveDashboardResult: saved,
GetDashboardFn: func(ctx context.Context, cmd *models.GetDashboardQuery) error {
cmd.Result = fakeDash
return nil
},
}
@@ -794,7 +802,6 @@ func TestDashboardAPIEndpoint(t *testing.T) {
Version: 1,
}
mockSQLStore := mockstore.NewSQLStoreMock()
mockSQLStore.ExpectedDashboard = fakeDash
mockSQLStore.ExpectedDashboardVersions = []*models.DashboardVersion{
{
DashboardId: 2,
@@ -818,13 +825,19 @@ func TestDashboardAPIEndpoint(t *testing.T) {
fakeDash.Id = 2
fakeDash.HasAcl = false
saved := &models.Dashboard{
Id: 2,
Uid: "uid",
Title: "Dash",
Slug: "dash",
Version: 1,
}
mock := &dashboards.FakeDashboardService{
SaveDashboardResult: &models.Dashboard{
Id: 2,
Uid: "uid",
Title: "Dash",
Slug: "dash",
Version: 1,
SaveDashboardResult: saved,
GetDashboardFn: func(ctx context.Context, cmd *models.GetDashboardQuery) error {
cmd.Result = fakeDash
return nil
},
}
@@ -832,7 +845,6 @@ func TestDashboardAPIEndpoint(t *testing.T) {
Version: 1,
}
mockSQLStore := mockstore.NewSQLStoreMock()
mockSQLStore.ExpectedDashboard = fakeDash
mockSQLStore.ExpectedDashboardVersions = []*models.DashboardVersion{
{
DashboardId: 2,
@@ -861,7 +873,12 @@ func TestDashboardAPIEndpoint(t *testing.T) {
dataValue, err := simplejson.NewJson([]byte(`{"id": 1, "editable": true, "style": "dark"}`))
require.NoError(t, err)
mockSQLStore.ExpectedDashboard = &models.Dashboard{Id: 1, Data: dataValue}
dashboardService := &dashboards.FakeDashboardService{
GetDashboardFn: func(ctx context.Context, cmd *models.GetDashboardQuery) error {
cmd.Result = &models.Dashboard{Id: 1, Data: dataValue}
return nil
},
}
loggedInUserScenarioWithRole(t, "When calling GET on", "GET", "/api/dashboards/uid/dash", "/api/dashboards/uid/:uid", models.ROLE_EDITOR, func(sc *scenarioContext) {
setUp()
@@ -875,7 +892,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
dashboardStore.On("GetProvisionedDataByDashboardID", mock.Anything).Return(&models.DashboardProvisioning{ExternalId: "/dashboard1.json"}, nil).Once()
dash := getDashboardShouldReturn200WithConfig(t, sc, fakeProvisioningService, dashboardStore)
dash := getDashboardShouldReturn200WithConfig(t, sc, fakeProvisioningService, dashboardStore, dashboardService)
assert.Equal(t, "../../../dashboard1.json", dash.Meta.ProvisionedExternalId, mockSQLStore)
}, mockSQLStore)
@@ -899,6 +916,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
dashboardProvisioningService: mockDashboardProvisioningService{},
SQLStore: mockSQLStore,
AccessControl: accesscontrolmock.New(),
dashboardService: dashboardService,
}
hs.callGetDashboard(sc)
@@ -913,7 +931,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
})
}
func getDashboardShouldReturn200WithConfig(t *testing.T, sc *scenarioContext, provisioningService provisioning.ProvisioningService, dashboardStore dashboards.Store) dtos.DashboardFullWithMeta {
func getDashboardShouldReturn200WithConfig(t *testing.T, sc *scenarioContext, provisioningService provisioning.ProvisioningService, dashboardStore dashboards.Store, dashboardService dashboards.DashboardService) dtos.DashboardFullWithMeta {
t.Helper()
if provisioningService == nil {
@@ -925,6 +943,10 @@ func getDashboardShouldReturn200WithConfig(t *testing.T, sc *scenarioContext, pr
dashboardStore = database.ProvideDashboardStore(sql)
}
if dashboardService == nil {
dashboardService = &dashboards.FakeDashboardService{}
}
libraryPanelsService := mockLibraryPanelService{}
libraryElementsService := mockLibraryElementService{}
cfg := setting.NewCfg()
@@ -941,6 +963,7 @@ func getDashboardShouldReturn200WithConfig(t *testing.T, sc *scenarioContext, pr
cfg, dashboardStore, nil, features,
accesscontrolmock.NewMockedPermissionsService(), accesscontrolmock.NewMockedPermissionsService(),
),
dashboardService: dashboardService,
}
hs.callGetDashboard(sc)
@@ -954,10 +977,6 @@ func getDashboardShouldReturn200WithConfig(t *testing.T, sc *scenarioContext, pr
return dash
}
func getDashboardShouldReturn200(t *testing.T, sc *scenarioContext) dtos.DashboardFullWithMeta {
return getDashboardShouldReturn200WithConfig(t, sc, nil, nil)
}
func (hs *HTTPServer) callGetDashboard(sc *scenarioContext) {
sc.handlerFunc = hs.GetDashboard
sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
+1 -1
View File
@@ -15,7 +15,7 @@ import (
"github.com/grafana/grafana/pkg/models"
accesscontrolmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock"
"github.com/grafana/grafana/pkg/services/dashboards"
service "github.com/grafana/grafana/pkg/services/dashboards/manager"
service "github.com/grafana/grafana/pkg/services/dashboards/service"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/guardian"
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
+1
View File
@@ -5,6 +5,7 @@ import (
"net/http"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/models"
+4 -5
View File
@@ -7,16 +7,15 @@ import (
"strings"
"testing"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/web/webtest"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/stretchr/testify/require"
"golang.org/x/oauth2"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana/pkg/models"
fakeDatasources "github.com/grafana/grafana/pkg/services/datasources/fakes"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/query"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/web/webtest"
)
var queryDatasourceInput = `{
+4 -4
View File
@@ -31,7 +31,7 @@ func (hs *HTTPServer) SetHomeDashboard(c *models.ReqContext) response.Response {
dashboardID := cmd.HomeDashboardID
if cmd.HomeDashboardUID != nil {
query := models.GetDashboardQuery{Uid: *cmd.HomeDashboardUID}
err := hs.SQLStore.GetDashboard(c.Req.Context(), &query)
err := hs.dashboardService.GetDashboard(c.Req.Context(), &query)
if err != nil {
return response.Error(404, "Dashboard not found", err)
}
@@ -65,7 +65,7 @@ func (hs *HTTPServer) getPreferencesFor(ctx context.Context, orgID, userID, team
// when homedashboardID is 0, that means it is the default home dashboard, no UID would be returned in the response
if preference.HomeDashboardID != 0 {
query := models.GetDashboardQuery{Id: preference.HomeDashboardID, OrgId: orgID}
err = hs.SQLStore.GetDashboard(ctx, &query)
err = hs.dashboardService.GetDashboard(ctx, &query)
if err == nil {
dashboardUID = query.Result.Uid
}
@@ -104,7 +104,7 @@ func (hs *HTTPServer) updatePreferencesFor(ctx context.Context, orgID, userID, t
dashboardID := dtoCmd.HomeDashboardID
if dtoCmd.HomeDashboardUID != nil {
query := models.GetDashboardQuery{Uid: *dtoCmd.HomeDashboardUID, OrgId: orgID}
err := hs.SQLStore.GetDashboard(ctx, &query)
err := hs.dashboardService.GetDashboard(ctx, &query)
if err != nil {
return response.Error(404, "Dashboard not found", err)
}
@@ -147,7 +147,7 @@ func (hs *HTTPServer) patchPreferencesFor(ctx context.Context, orgID, userID, te
dashboardID := dtoCmd.HomeDashboardID
if dtoCmd.HomeDashboardUID != nil {
query := models.GetDashboardQuery{Uid: *dtoCmd.HomeDashboardUID, OrgId: orgID}
err := hs.SQLStore.GetDashboard(ctx, &query)
err := hs.dashboardService.GetDashboard(ctx, &query)
if err != nil {
return response.Error(404, "Dashboard not found", err)
}
+15 -23
View File
@@ -1,19 +1,21 @@
package api
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"testing"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/accesscontrol"
pref "github.com/grafana/grafana/pkg/services/preference"
"github.com/grafana/grafana/pkg/services/preference/preftest"
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/dashboards"
pref "github.com/grafana/grafana/pkg/services/preference"
"github.com/grafana/grafana/pkg/services/preference/preftest"
)
var (
@@ -32,12 +34,13 @@ var (
func TestAPIEndpoint_GetCurrentOrgPreferences_LegacyAccessControl(t *testing.T) {
sc := setupHTTPServer(t, true, false)
sqlstore := mockstore.NewSQLStoreMock()
sqlstore.ExpectedDashboard = &models.Dashboard{
Uid: "home",
Id: 1,
dashSvc := &dashboards.FakeDashboardService{
GetDashboardFn: func(ctx context.Context, cmd *models.GetDashboardQuery) error {
cmd.Result = &models.Dashboard{Uid: "home", Id: 1}
return nil
},
}
sc.hs.SQLStore = sqlstore
sc.hs.dashboardService = dashSvc
prefService := preftest.NewPreferenceServiceFake()
prefService.ExpectedPreference = &pref.Preference{HomeDashboardID: 1, Theme: "dark"}
@@ -68,13 +71,6 @@ func TestAPIEndpoint_GetCurrentOrgPreferences_AccessControl(t *testing.T) {
sc := setupHTTPServer(t, true, true)
setInitCtxSignedInViewer(sc.initCtx)
sqlstore := mockstore.NewSQLStoreMock()
sqlstore.ExpectedDashboard = &models.Dashboard{
Uid: "home",
Id: 1,
}
sc.hs.SQLStore = sqlstore
prefService := preftest.NewPreferenceServiceFake()
prefService.ExpectedPreference = &pref.Preference{HomeDashboardID: 1, Theme: "dark"}
sc.hs.preferenceService = prefService
@@ -168,12 +164,8 @@ func TestAPIEndpoint_PatchUserPreferences(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, response.Code)
})
input = strings.NewReader(testUpdateOrgPreferencesWithHomeDashboardUIDCmd)
sqlstore := mockstore.NewSQLStoreMock()
sqlstore.ExpectedDashboard = &models.Dashboard{
Uid: "home",
Id: 1,
}
sc.hs.SQLStore = sqlstore
dashSvc := &dashboards.FakeDashboardService{}
sc.hs.dashboardService = dashSvc
t.Run("Returns 200 on success", func(t *testing.T) {
response := callAPI(sc.server, http.MethodPatch, patchUserPreferencesUrl, input, t)
assert.Equal(t, http.StatusOK, response.Code)
+1 -1
View File
@@ -26,7 +26,7 @@ func (hs *HTTPServer) GetStars(c *models.ReqContext) response.Response {
Id: dashboardId,
OrgId: c.OrgId,
}
err := hs.SQLStore.GetDashboard(c.Req.Context(), query)
err := hs.dashboardService.GetDashboard(c.Req.Context(), query)
if err != nil {
return response.Error(500, "Failed to get dashboard", err)
}
-4
View File
@@ -369,10 +369,6 @@ func TestTeamAPIEndpoint_GetTeamPreferences_RBAC(t *testing.T) {
_, err := sc.db.CreateTeam("team1", "", 1)
sqlstore := mockstore.NewSQLStoreMock()
sqlstore.ExpectedDashboard = &models.Dashboard{
Uid: "home",
Id: 1,
}
sc.hs.SQLStore = sqlstore
prefService := preftest.NewPreferenceServiceFake()