grafana/pkg/services/thumbs/datasources_lookup_test.go
Artur Wierzbicki 18daa6754c
Previews: datasource permissions (#52747)
* Previews: datasource permissions

* lint

* simplify - force non-null `ds_uids`

* add `canBeDisabled` to search service

* add `IncludeThumbnailsWithEmptyDsUids`

* remove force refresh migration

* refactor main preview service

* add safeguard

* revert ticker interval

* update testdata

* fix test

* add mock search service

* add datasources lookup test

* update migration

* extract ds lookup to its own package to avoid cyclic imports

* lint

* fix dashbaord extract, use the real datasource lookup in tests. IS IT BULLETPROOF YET?!

* fix dashbaord extract, use the real datasource lookup in tests. IS IT BULLETPROOF YET?!

* remove stale log

* consistent casing

* pass context to `createServiceAccount`

* filter out the special grafana ds
2022-07-28 16:40:26 +04:00

58 lines
1.8 KiB
Go

package thumbs
import (
"context"
_ "embed"
"testing"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/searchV2"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
var (
//go:embed testdata/search_response_frame.json
exampleListFrameJSON string
exampleListFrame = &data.Frame{}
_ = exampleListFrame.UnmarshalJSON([]byte(exampleListFrameJSON))
)
func TestShouldParseUidFromSearchResponseFrame(t *testing.T) {
searchService := &searchV2.MockSearchService{}
dsLookup := &dsUidsLookup{
searchService: searchService,
crawlerAuth: &crawlerAuth{},
features: featuremgmt.WithFeatures(featuremgmt.FlagPanelTitleSearch),
}
dashboardUid := "abc"
searchService.On("IsDisabled").Return(false)
searchService.On("DoDashboardQuery", mock.Anything, mock.Anything, mock.Anything, searchV2.DashboardQuery{
UIDs: []string{dashboardUid},
}).Return(&backend.DataResponse{
Frames: []*data.Frame{exampleListFrame},
})
uids, err := dsLookup.getDatasourceUidsForDashboard(context.Background(), dashboardUid, 1)
require.NoError(t, err)
require.Equal(t, []string{"datasource-2", "datasource-3", "datasource-4"}, uids)
}
func TestShouldReturnNullIfSearchServiceIsDisabled(t *testing.T) {
searchService := &searchV2.MockSearchService{}
dsLookup := &dsUidsLookup{
searchService: searchService,
crawlerAuth: &crawlerAuth{},
features: featuremgmt.WithFeatures(featuremgmt.FlagPanelTitleSearch),
}
dashboardUid := "abc"
searchService.On("IsDisabled").Return(true)
uids, err := dsLookup.getDatasourceUidsForDashboard(context.Background(), dashboardUid, 1)
require.NoError(t, err)
require.Nil(t, uids)
}