grafana/pkg/services/store/service_test.go
Artur Wierzbicki cc4473faf3
Storage: validation and sanitization stubs (#50523)
* add `IsPathValidationError` util to fs api

* refactor storage.Upload method

* remove unused struct

* extract `RootUpload` constant

* move file validation outside of the service

* Make UploadErrorToStatusCode exported

* validation/sanitization

* refactor pathValidationError check

* refactor, rename sanitize to transform

* add a todo

* refactor

* transform -> sanitize

* lint fix

* #50608: fix jpg/jpeg

Co-authored-by: Tania B <yalyna.ts@gmail.com>
Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
2022-06-15 12:32:29 +04:00

72 lines
2.0 KiB
Go

package store
import (
"bytes"
"context"
"os"
"path/filepath"
"testing"
"github.com/grafana/grafana-plugin-sdk-go/experimental"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tsdb/testdatasource"
"github.com/stretchr/testify/require"
)
var (
dummyUser = &models.SignedInUser{OrgId: 1}
)
func TestListFiles(t *testing.T) {
publicRoot, err := filepath.Abs("../../../public")
require.NoError(t, err)
roots := []storageRuntime{
newDiskStorage("public", "Public static files", &StorageLocalDiskConfig{
Path: publicRoot,
Roots: []string{
"/testdata/",
"/img/icons/",
"/img/bg/",
"/gazetteer/",
"/maps/",
"/upload/",
},
}).setReadOnly(true).setBuiltin(true),
}
store := newStandardStorageService(roots, func(orgId int64) []storageRuntime {
return make([]storageRuntime, 0)
})
frame, err := store.List(context.Background(), dummyUser, "public/testdata")
require.NoError(t, err)
experimental.CheckGoldenJSONFrame(t, "testdata", "public_testdata.golden", frame, true)
file, err := store.Read(context.Background(), dummyUser, "public/testdata/js_libraries.csv")
require.NoError(t, err)
require.NotNil(t, file)
frame, err = testdatasource.LoadCsvContent(bytes.NewReader(file.Contents), file.Name)
require.NoError(t, err)
experimental.CheckGoldenJSONFrame(t, "testdata", "public_testdata_js_libraries.golden", frame, true)
}
func TestUpload(t *testing.T) {
features := featuremgmt.WithFeatures(featuremgmt.FlagStorageLocalUpload)
path, err := os.Getwd()
require.NoError(t, err)
cfg := &setting.Cfg{AppURL: "http://localhost:3000/", DataPath: path}
s := ProvideService(sqlstore.InitTestDB(t), features, cfg)
request := UploadRequest{
EntityType: EntityTypeImage,
Contents: make([]byte, 0),
Path: "upload/myFile.jpg",
MimeType: "image/jpg",
}
err = s.Upload(context.Background(), dummyUser, &request)
require.NoError(t, err)
}