Cloudreve/pkg/filesystem/local/handller_test.go

187 lines
4.2 KiB
Go
Raw Normal View History

package local
import (
"context"
2019-12-11 12:24:09 +08:00
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/auth"
2019-12-09 12:18:30 +08:00
"github.com/HFO4/cloudreve/pkg/conf"
2019-12-11 12:24:09 +08:00
"github.com/HFO4/cloudreve/pkg/filesystem/fsctx"
"github.com/HFO4/cloudreve/pkg/util"
2019-12-11 12:24:09 +08:00
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
"io"
"io/ioutil"
2019-12-11 12:24:09 +08:00
"net/url"
"os"
"strings"
"testing"
)
func TestHandler_Put(t *testing.T) {
asserts := assert.New(t)
handler := Handler{}
ctx := context.Background()
testCases := []struct {
file io.ReadCloser
dst string
err bool
}{
{
file: ioutil.NopCloser(strings.NewReader("test input file")),
dst: "test/test/txt",
err: false,
},
{
file: ioutil.NopCloser(strings.NewReader("test input file")),
dst: "/notexist:/S.TXT",
err: true,
},
}
for _, testCase := range testCases {
2019-11-26 11:42:26 +08:00
err := handler.Put(ctx, testCase.file, testCase.dst, 15)
if testCase.err {
asserts.Error(err)
} else {
asserts.NoError(err)
asserts.True(util.Exists(testCase.dst))
}
}
}
func TestHandler_Delete(t *testing.T) {
asserts := assert.New(t)
handler := Handler{}
ctx := context.Background()
file, err := os.Create("test.file")
asserts.NoError(err)
_ = file.Close()
list, err := handler.Delete(ctx, []string{"test.file"})
2019-11-30 23:26:02 +08:00
asserts.Equal([]string{}, list)
asserts.NoError(err)
file, err = os.Create("test.file")
asserts.NoError(err)
_ = file.Close()
list, err = handler.Delete(ctx, []string{"test.file", "test.notexist"})
2019-11-30 23:26:02 +08:00
asserts.Equal([]string{"test.notexist"}, list)
asserts.Error(err)
list, err = handler.Delete(ctx, []string{"test.notexist"})
2019-11-30 23:26:02 +08:00
asserts.Equal([]string{"test.notexist"}, list)
asserts.Error(err)
}
2019-11-27 13:54:25 +08:00
func TestHandler_Get(t *testing.T) {
asserts := assert.New(t)
handler := Handler{}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// 成功
file, err := os.Create("TestHandler_Get.txt")
asserts.NoError(err)
_ = file.Close()
rs, err := handler.Get(ctx, "TestHandler_Get.txt")
asserts.NoError(err)
asserts.NotNil(rs)
// 文件不存在
rs, err = handler.Get(ctx, "TestHandler_Get_notExist.txt")
asserts.Error(err)
asserts.Nil(rs)
}
2019-12-09 12:18:30 +08:00
func TestHandler_Thumb(t *testing.T) {
asserts := assert.New(t)
handler := Handler{}
ctx := context.Background()
file, err := os.Create("TestHandler_Thumb" + conf.ThumbConfig.FileSuffix)
asserts.NoError(err)
file.Close()
// 正常
{
thumb, err := handler.Thumb(ctx, "TestHandler_Thumb")
asserts.NoError(err)
asserts.NotNil(thumb.Content)
}
// 不存在
{
_, err := handler.Thumb(ctx, "not_exist")
asserts.Error(err)
}
}
2019-12-11 12:24:09 +08:00
func TestHandler_Source(t *testing.T) {
asserts := assert.New(t)
handler := Handler{}
ctx := context.Background()
auth.General = auth.HMACAuth{SecretKey: []byte("test")}
// 成功
{
file := model.File{
Model: gorm.Model{
ID: 1,
},
Name: "test.jpg",
}
ctx := context.WithValue(ctx, fsctx.FileModelCtx, file)
baseURL, err := url.Parse("https://cloudreve.org")
asserts.NoError(err)
sourceURL, err := handler.Source(ctx, "", *baseURL, 0)
asserts.NoError(err)
asserts.NotEmpty(sourceURL)
asserts.Contains(sourceURL, "sign=")
asserts.Contains(sourceURL, "https://cloudreve.org")
}
// 无法获取上下文
{
baseURL, err := url.Parse("https://cloudreve.org")
asserts.NoError(err)
sourceURL, err := handler.Source(ctx, "", *baseURL, 0)
asserts.Error(err)
asserts.Empty(sourceURL)
}
}
2019-12-14 14:28:01 +08:00
func TestHandler_GetDownloadURL(t *testing.T) {
asserts := assert.New(t)
handler := Handler{}
ctx := context.Background()
auth.General = auth.HMACAuth{SecretKey: []byte("test")}
// 成功
{
file := model.File{
Model: gorm.Model{
ID: 1,
},
Name: "test.jpg",
}
ctx := context.WithValue(ctx, fsctx.FileModelCtx, file)
baseURL, err := url.Parse("https://cloudreve.org")
asserts.NoError(err)
downloadURL, err := handler.GetDownloadURL(ctx, "", *baseURL, 10)
asserts.NoError(err)
asserts.Contains(downloadURL, "sign=")
asserts.Contains(downloadURL, "https://cloudreve.org")
}
// 无法获取上下文
{
baseURL, err := url.Parse("https://cloudreve.org")
asserts.NoError(err)
downloadURL, err := handler.GetDownloadURL(ctx, "", *baseURL, 10)
asserts.Error(err)
asserts.Empty(downloadURL)
}
}