Files
gitea/services/context/upload/upload_test.go
T
silverwind 8abda62312 test: consolidate Go tests (#39962)
Consolidate all Go tests. Coverage is identical to before. Go tests are
around 5% faster while being 7% smaller. No non-test code is touched.
2026-10-24 13:48:19 +00:00

46 lines
1.5 KiB
Go

// Copyright 2019 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package upload
import (
"bytes"
"compress/gzip"
"testing"
"github.com/stretchr/testify/assert"
)
func TestUpload(t *testing.T) {
testContent := []byte(`This is a plain text file.`)
var b bytes.Buffer
w := gzip.NewWriter(&b)
w.Write(testContent)
w.Close()
kases := []struct {
data []byte
fileName string
allowedTypes []string
err error
}{
{testContent, "test.txt", []string{""}, nil},
{testContent, "dir/test.txt", []string{""}, nil},
{testContent, "../../../test.txt", []string{""}, nil},
{testContent, "test.txt", []string{"", ",", "|", "*/*", "*/*,", "*/*|", "text/plain"}, nil},
{testContent, "dir/test.txt", []string{"text/plain"}, nil},
{testContent, "/dir.txt/test.js", []string{".js"}, nil},
{testContent, "test.txt", []string{" text/plain ", ".txt", " .txt,.js", " .txt|.js"}, nil},
{testContent, "../../test.txt", []string{" .txt|.js"}, nil},
{testContent, "test.txt", []string{" .txt ,.js ", "text/plain, .txt", "text/*", "text/*,.js"}, nil},
{testContent, "test.txt", []string{"text/**", "application/x-gzip", ".zip", ".zip,.txtx", ".zip|.txtx"}, ErrFileTypeForbidden{"text/plain; charset=utf-8"}},
{b.Bytes(), "test.txt", []string{"application/x-gzip"}, nil},
}
for _, kase := range kases {
for _, allowedTypes := range kase.allowedTypes {
assert.Equal(t, kase.err, Verify(kase.data, kase.fileName, allowedTypes))
}
}
}