Test: new changes pkg filesystem

pull/1198/head
HFO4 3 years ago
parent 1c0a735df8
commit d117080991

@ -31,7 +31,6 @@ var defaultSettings = []Setting{
{Name: "download_timeout", Value: `60`, Type: "timeout"}, {Name: "download_timeout", Value: `60`, Type: "timeout"},
{Name: "preview_timeout", Value: `60`, Type: "timeout"}, {Name: "preview_timeout", Value: `60`, Type: "timeout"},
{Name: "doc_preview_timeout", Value: `60`, Type: "timeout"}, {Name: "doc_preview_timeout", Value: `60`, Type: "timeout"},
{Name: "upload_credential_timeout", Value: `1800`, Type: "timeout"},
{Name: "upload_session_timeout", Value: `86400`, Type: "timeout"}, {Name: "upload_session_timeout", Value: `86400`, Type: "timeout"},
{Name: "slave_api_timeout", Value: `60`, Type: "timeout"}, {Name: "slave_api_timeout", Value: `60`, Type: "timeout"},
{Name: "slave_node_retry", Value: `3`, Type: "slave"}, {Name: "slave_node_retry", Value: `3`, Type: "slave"},

@ -65,6 +65,7 @@ type cors struct {
var cfg *ini.File var cfg *ini.File
const defaultConf = `[System] const defaultConf = `[System]
Debug = false
Mode = master Mode = master
Listen = :5212 Listen = :5212
SessionSecret = {SessionSecret} SessionSecret = {SessionSecret}

@ -245,6 +245,6 @@ func TestChunkGroup_Process(t *testing.T) {
return errors.New("error") return errors.New("error")
})) }))
a.False(c.Next()) a.False(c.Next())
a.Equal(1, count) a.Equal(4, count)
} }
} }

@ -81,12 +81,12 @@ func TestHandler_Delete(t *testing.T) {
asserts := assert.New(t) asserts := assert.New(t)
handler := Driver{} handler := Driver{}
ctx := context.Background() ctx := context.Background()
filePath := util.RelativePath("test.file") filePath := util.RelativePath("TestHandler_Delete.file")
file, err := os.Create(filePath) file, err := os.Create(filePath)
asserts.NoError(err) asserts.NoError(err)
_ = file.Close() _ = file.Close()
list, err := handler.Delete(ctx, []string{"test.file"}) list, err := handler.Delete(ctx, []string{"TestHandler_Delete.file"})
asserts.Equal([]string{}, list) asserts.Equal([]string{}, list)
asserts.NoError(err) asserts.NoError(err)
@ -94,7 +94,7 @@ func TestHandler_Delete(t *testing.T) {
_ = file.Close() _ = file.Close()
file, _ = os.OpenFile(filePath, os.O_RDWR, os.FileMode(0)) file, _ = os.OpenFile(filePath, os.O_RDWR, os.FileMode(0))
asserts.NoError(err) asserts.NoError(err)
list, err = handler.Delete(ctx, []string{"test.file", "test.notexist"}) list, err = handler.Delete(ctx, []string{"TestHandler_Delete.file", "test.notexist"})
file.Close() file.Close()
asserts.Equal([]string{}, list) asserts.Equal([]string{}, list)
asserts.NoError(err) asserts.NoError(err)
@ -105,7 +105,7 @@ func TestHandler_Delete(t *testing.T) {
file, err = os.Create(filePath) file, err = os.Create(filePath)
asserts.NoError(err) asserts.NoError(err)
list, err = handler.Delete(ctx, []string{"test.file"}) list, err = handler.Delete(ctx, []string{"TestHandler_Delete.file"})
_ = file.Close() _ = file.Close()
asserts.Equal([]string{}, list) asserts.Equal([]string{}, list)
asserts.NoError(err) asserts.NoError(err)

@ -229,7 +229,7 @@ func (handler *Driver) Put(ctx context.Context, file fsctx.FileHeader) error {
fileInfo := file.Info() fileInfo := file.Info()
// 凭证有效期 // 凭证有效期
credentialTTL := model.GetIntSetting("upload_credential_timeout", 3600) credentialTTL := model.GetIntSetting("upload_session_timeout", 3600)
// 是否允许覆盖 // 是否允许覆盖
overwrite := fileInfo.Mode&fsctx.Overwrite == fsctx.Overwrite overwrite := fileInfo.Mode&fsctx.Overwrite == fsctx.Overwrite

@ -156,7 +156,7 @@ func (handler *Driver) Put(ctx context.Context, file fsctx.FileHeader) error {
defer file.Close() defer file.Close()
// 凭证有效期 // 凭证有效期
credentialTTL := model.GetIntSetting("upload_credential_timeout", 3600) credentialTTL := model.GetIntSetting("upload_session_timeout", 3600)
// 生成上传策略 // 生成上传策略
fileInfo := file.Info() fileInfo := file.Info()

@ -1,38 +1,45 @@
package filesystem package filesystem
import ( import (
"context"
"errors"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/cache"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/response"
"github.com/cloudreve/Cloudreve/v3/pkg/request"
testMock "github.com/stretchr/testify/mock"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
//func TestFileSystem_GetThumb(t *testing.T) { func TestFileSystem_GetThumb(t *testing.T) {
// asserts := assert.New(t) asserts := assert.New(t)
// fs := &FileSystem{User: &model.User{}} fs := &FileSystem{User: &model.User{}}
//
// // 非图像文件 // 非图像文件
// { {
// fs.SetTargetFile(&[]model.File{{}}) fs.SetTargetFile(&[]model.File{{}})
// _, err := fs.GetThumb(context.Background(), 1) _, err := fs.GetThumb(context.Background(), 1)
// asserts.Equal(err, ErrObjectNotExist) asserts.Equal(err, ErrObjectNotExist)
// } }
//
// // 成功 // 成功
// { {
// cache.Set("setting_thumb_width", "10", 0) cache.Set("setting_thumb_width", "10", 0)
// cache.Set("setting_thumb_height", "10", 0) cache.Set("setting_thumb_height", "10", 0)
// cache.Set("setting_preview_timeout", "50", 0) cache.Set("setting_preview_timeout", "50", 0)
// testHandller2 := new(FileHeaderMock) testHandller2 := new(FileHeaderMock)
// testHandller2.On("Thumb", testMock.Anything, "").Return(&response.ContentResponse{}, nil) testHandller2.On("Thumb", testMock.Anything, "").Return(&response.ContentResponse{}, nil)
// fs.CleanTargets() fs.CleanTargets()
// fs.SetTargetFile(&[]model.File{{PicInfo: "1,1", Policy: model.Policy{Type: "mock"}}}) fs.SetTargetFile(&[]model.File{{PicInfo: "1,1", Policy: model.Policy{Type: "mock"}}})
// fs.FileTarget[0].Policy.ID = 1 fs.FileTarget[0].Policy.ID = 1
// fs.Handler = testHandller2 fs.Handler = testHandller2
// res, err := fs.GetThumb(context.Background(), 1) res, err := fs.GetThumb(context.Background(), 1)
// asserts.NoError(err) asserts.NoError(err)
// asserts.EqualValues(50, res.MaxAge) asserts.EqualValues(50, res.MaxAge)
// } }
//} }
func TestFileSystem_ThumbWorker(t *testing.T) { func TestFileSystem_ThumbWorker(t *testing.T) {
asserts := assert.New(t) asserts := assert.New(t)
@ -42,3 +49,22 @@ func TestFileSystem_ThumbWorker(t *testing.T) {
getThumbWorker().releaseWorker() getThumbWorker().releaseWorker()
}) })
} }
func TestFileSystem_GenerateThumbnail(t *testing.T) {
fs := &FileSystem{User: &model.User{}}
// 无法生成缩略图
{
fs.SetTargetFile(&[]model.File{{}})
fs.GenerateThumbnail(context.Background(), &model.File{})
}
// 无法获取文件数据
{
testHandller := new(FileHeaderMock)
testHandller.On("Get", testMock.Anything, "").Return(request.NopRSCloser{}, errors.New("error"))
fs.Handler = testHandller
fs.GenerateThumbnail(context.Background(), &model.File{Name: "test.png"})
testHandller.AssertExpectations(t)
}
}

@ -3,6 +3,8 @@ package filesystem
import ( import (
"context" "context"
"errors" "errors"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/response"
testMock "github.com/stretchr/testify/mock"
"os" "os"
"testing" "testing"
@ -17,51 +19,51 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
//func TestFileSystem_ListPhysical(t *testing.T) { func TestFileSystem_ListPhysical(t *testing.T) {
// asserts := assert.New(t) asserts := assert.New(t)
// fs := &FileSystem{ fs := &FileSystem{
// User: &model.User{ User: &model.User{
// Model: gorm.Model{ Model: gorm.Model{
// ID: 1, ID: 1,
// }, },
// }, },
// Policy: &model.Policy{Type: "mock"}, Policy: &model.Policy{Type: "mock"},
// } }
// ctx := context.Background() ctx := context.Background()
//
// // 未知存储策略 // 未知存储策略
// { {
// fs.Policy.Type = "unknown" fs.Policy.Type = "unknown"
// res, err := fs.ListPhysical(ctx, "/") res, err := fs.ListPhysical(ctx, "/")
// asserts.Equal(ErrUnknownPolicyType, err) asserts.Equal(ErrUnknownPolicyType, err)
// asserts.Empty(res) asserts.Empty(res)
// fs.Policy.Type = "mock" fs.Policy.Type = "mock"
// } }
//
// // 无法列取目录 // 无法列取目录
// { {
// testHandler := new(FileHeaderMock) testHandler := new(FileHeaderMock)
// testHandler.On("List", testMock.Anything, "/", testMock.Anything).Return([]response.Object{}, errors.New("error")) testHandler.On("List", testMock.Anything, "/", testMock.Anything).Return([]response.Object{}, errors.New("error"))
// fs.Handler = testHandler fs.Handler = testHandler
// res, err := fs.ListPhysical(ctx, "/") res, err := fs.ListPhysical(ctx, "/")
// asserts.EqualError(err, "error") asserts.EqualError(err, "error")
// asserts.Empty(res) asserts.Empty(res)
// } }
//
// // 成功 // 成功
// { {
// testHandler := new(FileHeaderMock) testHandler := new(FileHeaderMock)
// testHandler.On("List", testMock.Anything, "/", testMock.Anything).Return( testHandler.On("List", testMock.Anything, "/", testMock.Anything).Return(
// []response.Object{{IsDir: true, Name: "1"}, {IsDir: false, Name: "2"}}, []response.Object{{IsDir: true, Name: "1"}, {IsDir: false, Name: "2"}},
// nil, nil,
// ) )
// fs.Handler = testHandler fs.Handler = testHandler
// res, err := fs.ListPhysical(ctx, "/") res, err := fs.ListPhysical(ctx, "/")
// asserts.NoError(err) asserts.NoError(err)
// asserts.Len(res, 1) asserts.Len(res, 1)
// asserts.Equal("1", res[0].Name) asserts.Equal("1", res[0].Name)
// } }
//} }
func TestFileSystem_List(t *testing.T) { func TestFileSystem_List(t *testing.T) {
asserts := assert.New(t) asserts := assert.New(t)
@ -294,12 +296,12 @@ func TestFileSystem_ListDeleteDirs(t *testing.T) {
{ {
mock.ExpectQuery("SELECT(.+)"). mock.ExpectQuery("SELECT(.+)").
WillReturnRows( WillReturnRows(
sqlmock.NewRows([]string{"id"}). sqlmock.NewRows([]string{"id", "parent_id"}).
AddRow(1). AddRow(1, 0).
AddRow(2). AddRow(2, 0).
AddRow(3), AddRow(3, 0),
) )
mock.ExpectQuery("SELECT(.+)"). mock.ExpectQuery("SELECT(.+)files(.+)").
WithArgs(1, 2, 3). WithArgs(1, 2, 3).
WillReturnRows( WillReturnRows(
sqlmock.NewRows([]string{"id", "name"}). sqlmock.NewRows([]string{"id", "name"}).
@ -314,21 +316,47 @@ func TestFileSystem_ListDeleteDirs(t *testing.T) {
asserts.NoError(mock.ExpectationsWereMet()) asserts.NoError(mock.ExpectationsWereMet())
} }
// 成功,忽略根目录
{
mock.ExpectQuery("SELECT(.+)").
WillReturnRows(
sqlmock.NewRows([]string{"id", "parent_id"}).
AddRow(1, 0).
AddRow(2, nil).
AddRow(3, 0),
)
mock.ExpectQuery("SELECT(.+)files(.+)").
WithArgs(1, 3).
WillReturnRows(
sqlmock.NewRows([]string{"id", "name"}).
AddRow(4, "1.txt").
AddRow(5, "2.txt").
AddRow(6, "3.txt"),
)
fs.CleanTargets()
err := fs.ListDeleteDirs(context.Background(), []uint{1})
asserts.NoError(err)
asserts.Len(fs.FileTarget, 3)
asserts.Len(fs.DirTarget, 2)
asserts.NoError(mock.ExpectationsWereMet())
}
// 检索文件发生错误 // 检索文件发生错误
{ {
mock.ExpectQuery("SELECT(.+)"). mock.ExpectQuery("SELECT(.+)").
WillReturnRows( WillReturnRows(
sqlmock.NewRows([]string{"id"}). sqlmock.NewRows([]string{"id", "parent_id"}).
AddRow(1). AddRow(1, 0).
AddRow(2). AddRow(2, 0).
AddRow(3), AddRow(3, 0),
) )
mock.ExpectQuery("SELECT(.+)"). mock.ExpectQuery("SELECT(.+)").
WithArgs(1, 2, 3). WithArgs(1, 2, 3).
WillReturnError(errors.New("error")) WillReturnError(errors.New("error"))
fs.CleanTargets()
err := fs.ListDeleteDirs(context.Background(), []uint{1}) err := fs.ListDeleteDirs(context.Background(), []uint{1})
asserts.Error(err) asserts.Error(err)
asserts.Len(fs.DirTarget, 6) asserts.Len(fs.DirTarget, 3)
asserts.NoError(mock.ExpectationsWereMet()) asserts.NoError(mock.ExpectationsWereMet())
} }
// 检索目录发生错误 // 检索目录发生错误
@ -347,7 +375,7 @@ func TestFileSystem_Delete(t *testing.T) {
cache.Set("pack_size_1", uint64(0), 0) cache.Set("pack_size_1", uint64(0), 0)
fs := &FileSystem{User: &model.User{ fs := &FileSystem{User: &model.User{
Model: gorm.Model{ Model: gorm.Model{
ID: 1, ID: 0,
}, },
Storage: 3, Storage: 3,
Group: model.Group{MaxStorage: 3}, Group: model.Group{MaxStorage: 3},
@ -359,10 +387,10 @@ func TestFileSystem_Delete(t *testing.T) {
fs.CleanTargets() fs.CleanTargets()
mock.ExpectQuery("SELECT(.+)"). mock.ExpectQuery("SELECT(.+)").
WillReturnRows( WillReturnRows(
sqlmock.NewRows([]string{"id"}). sqlmock.NewRows([]string{"id", "parent_id"}).
AddRow(1). AddRow(1, 0).
AddRow(2). AddRow(2, 0).
AddRow(3), AddRow(3, 0),
) )
mock.ExpectQuery("SELECT(.+)"). mock.ExpectQuery("SELECT(.+)").
WithArgs(1, 2, 3). WithArgs(1, 2, 3).
@ -378,18 +406,16 @@ func TestFileSystem_Delete(t *testing.T) {
// 删除文件记录 // 删除文件记录
mock.ExpectBegin() mock.ExpectBegin()
mock.ExpectExec("DELETE(.+)"). mock.ExpectExec("DELETE(.+)").
WillReturnResult(sqlmock.NewResult(0, 3)) WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectExec("DELETE(.+)").
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectExec("UPDATE(.+)users(.+)storage(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit() mock.ExpectCommit()
// 删除对应分享 // 删除对应分享
mock.ExpectBegin() mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)shares"). mock.ExpectExec("UPDATE(.+)shares").
WillReturnResult(sqlmock.NewResult(0, 3)) WillReturnResult(sqlmock.NewResult(0, 3))
mock.ExpectCommit() mock.ExpectCommit()
// 归还容量
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").
WillReturnResult(sqlmock.NewResult(0, 3))
mock.ExpectCommit()
// 删除目录 // 删除目录
mock.ExpectBegin() mock.ExpectBegin()
mock.ExpectExec("DELETE(.+)"). mock.ExpectExec("DELETE(.+)").
@ -405,7 +431,6 @@ func TestFileSystem_Delete(t *testing.T) {
fs.DirTarget = []model.Folder{} fs.DirTarget = []model.Folder{}
err := fs.Delete(ctx, []uint{1}, []uint{1}, true) err := fs.Delete(ctx, []uint{1}, []uint{1}, true)
asserts.NoError(err) asserts.NoError(err)
asserts.Equal(uint64(0), fs.User.Storage)
} }
//全部成功 //全部成功
{ {
@ -417,10 +442,10 @@ func TestFileSystem_Delete(t *testing.T) {
asserts.NoError(err) asserts.NoError(err)
mock.ExpectQuery("SELECT(.+)"). mock.ExpectQuery("SELECT(.+)").
WillReturnRows( WillReturnRows(
sqlmock.NewRows([]string{"id"}). sqlmock.NewRows([]string{"id", "parent_id"}).
AddRow(1). AddRow(1, 0).
AddRow(2). AddRow(2, 0).
AddRow(3), AddRow(3, 0),
) )
mock.ExpectQuery("SELECT(.+)"). mock.ExpectQuery("SELECT(.+)").
WithArgs(1, 2, 3). WithArgs(1, 2, 3).
@ -436,18 +461,16 @@ func TestFileSystem_Delete(t *testing.T) {
// 删除文件记录 // 删除文件记录
mock.ExpectBegin() mock.ExpectBegin()
mock.ExpectExec("DELETE(.+)"). mock.ExpectExec("DELETE(.+)").
WillReturnResult(sqlmock.NewResult(0, 3)) WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectExec("DELETE(.+)").
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectExec("UPDATE(.+)users(.+)storage(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit() mock.ExpectCommit()
// 删除对应分享 // 删除对应分享
mock.ExpectBegin() mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)shares"). mock.ExpectExec("UPDATE(.+)shares").
WillReturnResult(sqlmock.NewResult(0, 3)) WillReturnResult(sqlmock.NewResult(0, 3))
mock.ExpectCommit() mock.ExpectCommit()
// 归还容量
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").
WillReturnResult(sqlmock.NewResult(0, 3))
mock.ExpectCommit()
// 删除目录 // 删除目录
mock.ExpectBegin() mock.ExpectBegin()
mock.ExpectExec("DELETE(.+)"). mock.ExpectExec("DELETE(.+)").
@ -463,7 +486,6 @@ func TestFileSystem_Delete(t *testing.T) {
fs.DirTarget = []model.Folder{} fs.DirTarget = []model.Folder{}
err = fs.Delete(ctx, []uint{1}, []uint{1}, false) err = fs.Delete(ctx, []uint{1}, []uint{1}, false)
asserts.NoError(err) asserts.NoError(err)
asserts.Equal(uint64(0), fs.User.Storage)
} }
} }
@ -571,7 +593,9 @@ func TestFileSystem_Rename(t *testing.T) {
Model: gorm.Model{ Model: gorm.Model{
ID: 1, ID: 1,
}, },
}} },
Policy: &model.Policy{},
}
ctx := context.Background() ctx := context.Background()
// 重命名文件 成功 // 重命名文件 成功
@ -681,7 +705,7 @@ func TestFileSystem_Rename(t *testing.T) {
// 新名字是文件,扩展名不合法 // 新名字是文件,扩展名不合法
{ {
fs.User.Policy.OptionsSerialized.FileType = []string{"txt"} fs.Policy.OptionsSerialized.FileType = []string{"txt"}
err := fs.Rename(ctx, []uint{}, []uint{10}, "1.jpg") err := fs.Rename(ctx, []uint{}, []uint{10}, "1.jpg")
asserts.Error(err) asserts.Error(err)
asserts.Equal(ErrIllegalObjectName, err) asserts.Equal(ErrIllegalObjectName, err)
@ -689,7 +713,7 @@ func TestFileSystem_Rename(t *testing.T) {
// 新名字是目录,不应该检测扩展名 // 新名字是目录,不应该检测扩展名
{ {
fs.User.Policy.OptionsSerialized.FileType = []string{"txt"} fs.Policy.OptionsSerialized.FileType = []string{"txt"}
mock.ExpectQuery("SELECT(.+)folders(.+)"). mock.ExpectQuery("SELECT(.+)folders(.+)").
WithArgs(10, 1). WithArgs(10, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "name"})) WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))

@ -2,11 +2,23 @@ package filesystem
import ( import (
"context" "context"
"errors"
"github.com/DATA-DOG/go-sqlmock"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/cache"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/response" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/response"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer" "github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
testMock "github.com/stretchr/testify/mock" testMock "github.com/stretchr/testify/mock"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url" "net/url"
"strings"
"testing"
) )
type FileHeaderMock struct { type FileHeaderMock struct {
@ -53,177 +65,200 @@ func (m FileHeaderMock) Source(ctx context.Context, path string, url url.URL, ex
return args.Get(0).(string), args.Error(1) return args.Get(0).(string), args.Error(1)
} }
//func TestFileSystem_Upload(t *testing.T) { func TestFileSystem_Upload(t *testing.T) {
// asserts := assert.New(t) asserts := assert.New(t)
//
// // 正常 // 正常
// testHandler := new(FileHeaderMock) testHandler := new(FileHeaderMock)
// testHandler.On("Put", testMock.Anything, testMock.Anything, testMock.Anything).Return(nil) testHandler.On("Put", testMock.Anything, testMock.Anything, testMock.Anything).Return(nil)
// fs := &FileSystem{ fs := &FileSystem{
// Handler: testHandler, Handler: testHandler,
// User: &model.User{ User: &model.User{
// Model: gorm.Model{ Model: gorm.Model{
// ID: 1, ID: 1,
// }, },
// Policy: model.Policy{ },
// AutoRename: false, Policy: &model.Policy{
// DirNameRule: "{path}", AutoRename: false,
// }, DirNameRule: "{path}",
// }, },
// } }
// ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
// c, _ := gin.CreateTestContext(httptest.NewRecorder()) c, _ := gin.CreateTestContext(httptest.NewRecorder())
// c.Request, _ = http.NewRequest("POST", "/", nil) c.Request, _ = http.NewRequest("POST", "/", nil)
// ctx = context.WithValue(ctx, fsctx.GinCtx, c) ctx = context.WithValue(ctx, fsctx.GinCtx, c)
// cancel() cancel()
// file := fsctx.FileStream{ file := &fsctx.FileStream{
// Size: 5, Size: 5,
// VirtualPath: "/", VirtualPath: "/",
// Name: "1.txt", Name: "1.txt",
// } }
// err := fs.Upload(ctx, file) err := fs.Upload(ctx, file)
// asserts.NoError(err) asserts.NoError(err)
//
// // 正常,上下文已指定源文件 // 正常,上下文已指定源文件
// testHandler = new(FileHeaderMock) testHandler = new(FileHeaderMock)
// testHandler.On("Put", testMock.Anything, testMock.Anything, "123/123.txt").Return(nil) testHandler.On("Put", testMock.Anything, testMock.Anything).Return(nil)
// fs = &FileSystem{ fs = &FileSystem{
// Handler: testHandler, Handler: testHandler,
// User: &model.User{ User: &model.User{
// Model: gorm.Model{ Model: gorm.Model{
// ID: 1, ID: 1,
// }, },
// Policy: model.Policy{ },
// AutoRename: false, Policy: &model.Policy{
// DirNameRule: "{path}", AutoRename: false,
// }, DirNameRule: "{path}",
// }, },
// } }
// ctx, cancel = context.WithCancel(context.Background()) ctx, cancel = context.WithCancel(context.Background())
// c, _ = gin.CreateTestContext(httptest.NewRecorder()) c, _ = gin.CreateTestContext(httptest.NewRecorder())
// c.Request, _ = http.NewRequest("POST", "/", nil) c.Request, _ = http.NewRequest("POST", "/", nil)
// ctx = context.WithValue(ctx, fsctx.GinCtx, c) ctx = context.WithValue(ctx, fsctx.GinCtx, c)
// ctx = context.WithValue(ctx, fsctx.FileModelCtx, model.File{SourceName: "123/123.txt"}) ctx = context.WithValue(ctx, fsctx.FileModelCtx, model.File{SourceName: "123/123.txt"})
// cancel() cancel()
// file = fsctx.FileStream{ file = &fsctx.FileStream{
// Size: 5, Size: 5,
// VirtualPath: "/", VirtualPath: "/",
// Name: "1.txt", Name: "1.txt",
// File: ioutil.NopCloser(strings.NewReader("")), File: ioutil.NopCloser(strings.NewReader("")),
// } }
// err = fs.Upload(ctx, file) err = fs.Upload(ctx, file)
// asserts.NoError(err) asserts.NoError(err)
//
// // BeforeUpload 返回错误 // BeforeUpload 返回错误
// fs.Use("BeforeUpload", func(ctx context.Context, fs *FileSystem) error { fs.Use("BeforeUpload", func(ctx context.Context, fs *FileSystem, file fsctx.FileHeader) error {
// return errors.New("error") return errors.New("error")
// }) })
// err = fs.Upload(ctx, file) err = fs.Upload(ctx, file)
// asserts.Error(err) asserts.Error(err)
// fs.Hooks["BeforeUpload"] = nil fs.Hooks["BeforeUpload"] = nil
// testHandler.AssertExpectations(t) testHandler.AssertExpectations(t)
//
// // 上传文件失败 // 上传文件失败
// testHandler2 := new(FileHeaderMock) testHandler2 := new(FileHeaderMock)
// testHandler2.On("Put", testMock.Anything, testMock.Anything, testMock.Anything).Return(errors.New("error")) testHandler2.On("Put", testMock.Anything, testMock.Anything).Return(errors.New("error"))
// fs.Handler = testHandler2 fs.Handler = testHandler2
// err = fs.Upload(ctx, file) err = fs.Upload(ctx, file)
// asserts.Error(err) asserts.Error(err)
// testHandler2.AssertExpectations(t) testHandler2.AssertExpectations(t)
//
// // AfterUpload失败 // AfterUpload失败
// testHandler3 := new(FileHeaderMock) testHandler3 := new(FileHeaderMock)
// testHandler3.On("Put", testMock.Anything, testMock.Anything, testMock.Anything).Return(nil) testHandler3.On("Put", testMock.Anything, testMock.Anything).Return(nil)
// fs.Handler = testHandler3 fs.Handler = testHandler3
// fs.Use("AfterUpload", func(ctx context.Context, fs *FileSystem) error { fs.Use("AfterUpload", func(ctx context.Context, fs *FileSystem, file fsctx.FileHeader) error {
// return errors.New("error") return errors.New("error")
// }) })
// fs.Use("AfterValidateFailed", func(ctx context.Context, fs *FileSystem) error { fs.Use("AfterValidateFailed", func(ctx context.Context, fs *FileSystem, file fsctx.FileHeader) error {
// return errors.New("error") return errors.New("error")
// }) })
// err = fs.Upload(ctx, file) err = fs.Upload(ctx, file)
// asserts.Error(err) asserts.Error(err)
// testHandler2.AssertExpectations(t) testHandler2.AssertExpectations(t)
//
//} }
//
//func TestFileSystem_GenerateSavePath_Anonymous(t *testing.T) { func TestFileSystem_GetUploadToken(t *testing.T) {
// asserts := assert.New(t) asserts := assert.New(t)
// fs := FileSystem{User: &model.User{}} fs := FileSystem{
// ctx := context.WithValue( User: &model.User{Model: gorm.Model{ID: 1}},
// context.Background(), Policy: &model.Policy{},
// fsctx.UploadPolicyCtx, }
// serializer.UploadPolicy{ ctx := context.Background()
// SavePath: "{randomkey16}",
// AutoRename: false, // 成功
// }, {
// ) cache.SetSettings(map[string]string{
// "upload_session_timeout": "10",
// savePath := fs.GenerateSavePath(ctx, fsctx.FileStream{ }, "setting_")
// Name: "test.test", testHandler := new(FileHeaderMock)
// }) testHandler.On("Token", testMock.Anything, int64(10), testMock.Anything, testMock.Anything).Return(&serializer.UploadCredential{Credential: "test"}, nil)
// asserts.Len(savePath, 26) fs.Handler = testHandler
// asserts.Contains(savePath, "test.test") mock.ExpectQuery("SELECT(.+)").
//} WithArgs(1).
// WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
//func TestFileSystem_GetUploadToken(t *testing.T) { mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnError(errors.New("not found"))
// asserts := assert.New(t) mock.ExpectBegin()
// fs := FileSystem{User: &model.User{Model: gorm.Model{ID: 1}}} mock.ExpectExec("INSERT(.+)files(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
// ctx := context.Background() mock.ExpectExec("UPDATE(.+)storage(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
// mock.ExpectCommit()
// // 成功 res, err := fs.CreateUploadSession(ctx, &fsctx.FileStream{
// { Size: 0,
// cache.SetSettings(map[string]string{ Name: "file",
// "upload_credential_timeout": "10", VirtualPath: "/",
// "upload_session_timeout": "10", })
// }, "setting_") asserts.NoError(mock.ExpectationsWereMet())
// testHandler := new(FileHeaderMock) testHandler.AssertExpectations(t)
// testHandler.On("Token", testMock.Anything, int64(10), testMock.Anything).Return(serializer.UploadCredential{Token: "test"}, nil) asserts.NoError(err)
// fs.Handler = testHandler asserts.Equal("test", res.Credential)
// res, err := fs.CreateUploadSession(ctx, "/", 10, "123") }
// testHandler.AssertExpectations(t)
// asserts.NoError(err) // 无法获取上传凭证
// asserts.Equal("test", res.Token) {
// } cache.SetSettings(map[string]string{
// "upload_credential_timeout": "10",
// // 无法获取上传凭证 "upload_session_timeout": "10",
// { }, "setting_")
// cache.SetSettings(map[string]string{ testHandler := new(FileHeaderMock)
// "upload_credential_timeout": "10", testHandler.On("Token", testMock.Anything, int64(10), testMock.Anything, testMock.Anything).Return(&serializer.UploadCredential{}, errors.New("error"))
// "upload_session_timeout": "10", fs.Handler = testHandler
// }, "setting_") mock.ExpectQuery("SELECT(.+)").
// testHandler := new(FileHeaderMock) WithArgs(1).
// testHandler.On("Token", testMock.Anything, int64(10), testMock.Anything).Return(serializer.UploadCredential{}, errors.New("error")) WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
// fs.Handler = testHandler mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnError(errors.New("not found"))
// _, err := fs.CreateUploadSession(ctx, "/", 10, "123") mock.ExpectBegin()
// testHandler.AssertExpectations(t) mock.ExpectExec("INSERT(.+)files(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
// asserts.Error(err) mock.ExpectExec("UPDATE(.+)storage(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
// } mock.ExpectCommit()
//} _, err := fs.CreateUploadSession(ctx, &fsctx.FileStream{
// Size: 0,
//func TestFileSystem_UploadFromStream(t *testing.T) { Name: "file",
// asserts := assert.New(t) VirtualPath: "/",
// fs := FileSystem{User: &model.User{Model: gorm.Model{ID: 1}}} })
// ctx := context.Background() asserts.NoError(mock.ExpectationsWereMet())
// testHandler.AssertExpectations(t)
// err := fs.UploadFromStream(ctx, ioutil.NopCloser(strings.NewReader("123")), "/1.txt", 1) asserts.Error(err)
// asserts.Error(err) }
//} }
//
//func TestFileSystem_UploadFromPath(t *testing.T) { func TestFileSystem_UploadFromStream(t *testing.T) {
// asserts := assert.New(t) asserts := assert.New(t)
// fs := FileSystem{User: &model.User{Policy: model.Policy{Type: "mock"}, Model: gorm.Model{ID: 1}}} fs := FileSystem{
// ctx := context.Background() User: &model.User{
// Model: gorm.Model{ID: 1},
// // 文件不存在 Policy: model.Policy{Type: "mock"},
// { },
// err := fs.UploadFromPath(ctx, "test/not_exist", "/", true) Policy: &model.Policy{Type: "mock"},
// asserts.Error(err) }
// } ctx := context.Background()
//
// // 文存在,上传失败 err := fs.UploadFromStream(ctx, &fsctx.FileStream{
// { File: ioutil.NopCloser(strings.NewReader("123")),
// err := fs.UploadFromPath(ctx, "tests/test.zip", "/", true) }, true)
// asserts.Error(err) asserts.Error(err)
// } }
//}
func TestFileSystem_UploadFromPath(t *testing.T) {
asserts := assert.New(t)
fs := FileSystem{
User: &model.User{
Model: gorm.Model{ID: 1},
Policy: model.Policy{Type: "mock"},
},
Policy: &model.Policy{Type: "mock"},
}
ctx := context.Background()
// 文件不存在
{
err := fs.UploadFromPath(ctx, "test/not_exist", "/", fsctx.Overwrite)
asserts.Error(err)
}
// 文存在,上传失败
{
err := fs.UploadFromPath(ctx, "tests/test.zip", "/", fsctx.Overwrite)
asserts.Error(err)
}
}

@ -68,10 +68,9 @@ func TestFileSystem_ValidateFileSize(t *testing.T) {
asserts := assert.New(t) asserts := assert.New(t)
ctx := context.Background() ctx := context.Background()
fs := FileSystem{ fs := FileSystem{
User: &model.User{ User: &model.User{},
Policy: model.Policy{ Policy: &model.Policy{
MaxSize: 10, MaxSize: 10,
},
}, },
} }
@ -80,7 +79,7 @@ func TestFileSystem_ValidateFileSize(t *testing.T) {
asserts.False(fs.ValidateFileSize(ctx, 11)) asserts.False(fs.ValidateFileSize(ctx, 11))
// 无限制 // 无限制
fs.User.Policy.MaxSize = 0 fs.Policy.MaxSize = 0
asserts.True(fs.ValidateFileSize(ctx, 11)) asserts.True(fs.ValidateFileSize(ctx, 11))
} }
@ -88,11 +87,10 @@ func TestFileSystem_ValidateExtension(t *testing.T) {
asserts := assert.New(t) asserts := assert.New(t)
ctx := context.Background() ctx := context.Background()
fs := FileSystem{ fs := FileSystem{
User: &model.User{ User: &model.User{},
Policy: model.Policy{ Policy: &model.Policy{
OptionsSerialized: model.PolicyOption{ OptionsSerialized: model.PolicyOption{
FileType: nil, FileType: nil,
},
}, },
}, },
} }
@ -100,11 +98,11 @@ func TestFileSystem_ValidateExtension(t *testing.T) {
asserts.True(fs.ValidateExtension(ctx, "1")) asserts.True(fs.ValidateExtension(ctx, "1"))
asserts.True(fs.ValidateExtension(ctx, "1.txt")) asserts.True(fs.ValidateExtension(ctx, "1.txt"))
fs.User.Policy.OptionsSerialized.FileType = []string{} fs.Policy.OptionsSerialized.FileType = []string{}
asserts.True(fs.ValidateExtension(ctx, "1")) asserts.True(fs.ValidateExtension(ctx, "1"))
asserts.True(fs.ValidateExtension(ctx, "1.txt")) asserts.True(fs.ValidateExtension(ctx, "1.txt"))
fs.User.Policy.OptionsSerialized.FileType = []string{"txt", "jpg"} fs.Policy.OptionsSerialized.FileType = []string{"txt", "jpg"}
asserts.False(fs.ValidateExtension(ctx, "1")) asserts.False(fs.ValidateExtension(ctx, "1"))
asserts.False(fs.ValidateExtension(ctx, "1.jpg.png")) asserts.False(fs.ValidateExtension(ctx, "1.jpg.png"))
asserts.True(fs.ValidateExtension(ctx, "1.txt")) asserts.True(fs.ValidateExtension(ctx, "1.txt"))

Loading…
Cancel
Save