grafana/pkg/infra/filestorage/api_test.go
Artur Wierzbicki e96f67ae2e
Renderer: Add sanitize API (#50936)
* svg fun

* #50597: add proto

* #50597: add sanitizer methods

* #50597: add provider

* #50597: use sanitizer

* #50597: use sanitizer

* update grafana to match new api

* add comments

* add capability check

* add timing

* update sanitize path

* improve log message

* strings.HasPrefix rather than filepath.IsAbs

* filepath.Clean + filepath.ToSlash for windows

* read 404

* remove `path.clean` from `getPathAndScope`

* add resp body close

* remove unneeded prop

* Update pkg/services/rendering/rendering.go

Co-authored-by: Agnès Toulet <35176601+AgnesToulet@users.noreply.github.com>

* remove test files

* filepath.ToSlash correct wrapping

* filepath.ToSlash correct wrapping

* filepath.ToSlash comment

* compilation error

* lint fix

* fix error message

* Update pkg/services/rendering/rendering.go

Co-authored-by: Agnès Toulet <35176601+AgnesToulet@users.noreply.github.com>

* add `image/svg+xml` mime type

* refactored log

* refactored log

Co-authored-by: Agnès Toulet <35176601+AgnesToulet@users.noreply.github.com>
2022-07-07 15:32:18 +04:00

119 lines
2.4 KiB
Go
Raw Blame History

package filestorage
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestFilestorageApi_Join(t *testing.T) {
var tests = []struct {
name string
parts []string
expected string
}{
{
name: "multiple parts",
parts: []string{"prefix", "p1", "p2"},
expected: "/prefix/p1/p2",
},
{
name: "no parts",
parts: []string{},
expected: "/",
},
{
name: "a single part",
parts: []string{"prefix"},
expected: "/prefix",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.expected, Join(tt.parts...))
})
}
}
func pathPart(length int) string {
sb := strings.Builder{}
for i := 0; i < length; i++ {
sb.WriteString("a")
}
return sb.String()
}
func TestFilestorageApi_ValidatePath(t *testing.T) {
var tests = []struct {
path string
expectedError error
}{
{
path: "abc/file.jpg",
expectedError: ErrRelativePath,
},
{
path: "../abc/file.jpg",
expectedError: ErrRelativePath,
},
{
path: "/abc/./file.jpg",
expectedError: ErrNonCanonicalPath,
},
{
path: "/abc/../abc/file.jpg",
expectedError: ErrNonCanonicalPath,
},
{
path: "/abc//folder/file.jpg",
expectedError: ErrNonCanonicalPath,
},
{
path: "/abc/file.jpg/",
expectedError: ErrPathEndsWithDelimiter,
},
{
path: "/abc/folder/ ",
expectedError: ErrEmptyPathPart,
},
{
path: "/abc/ /file.jpg",
expectedError: ErrEmptyPathPart,
},
{
path: "/abc/" + pathPart(260) + "/file.jpg",
expectedError: ErrPathPartTooLong,
},
{
path: "/abc/" + pathPart(1050) + "/file.jpg",
expectedError: ErrPathTooLong,
},
{
path: "/abc/folder🚀/file.jpg",
expectedError: ErrInvalidCharacters,
},
{
path: "/path/with/utf/char/at/the/end.jpg<70>",
expectedError: ErrInvalidCharacters,
},
{
path: "/myFile/file.jpg",
},
{
path: "/file.jpg",
},
}
for _, tt := range tests {
if tt.expectedError == nil {
t.Run("path "+tt.path+" should be valid", func(t *testing.T) {
require.Nil(t, ValidatePath(tt.path))
})
} else {
t.Run("path "+tt.path+" should be invalid: "+tt.expectedError.Error(), func(t *testing.T) {
require.Equal(t, tt.expectedError, ValidatePath(tt.path))
})
}
}
}