Test: filesystem/file, filesystem, filesystem/hook, model/file/Create

This commit is contained in:
HFO4
2019-11-20 17:21:30 +08:00
parent 438ce02420
commit 7cb27b2102
11 changed files with 200 additions and 19 deletions

View File

@@ -3,9 +3,12 @@ package filesystem
import "errors"
var (
UnknownPolicyTypeError = errors.New("未知存储策略类型")
FileSizeTooBigError = errors.New("单个文件尺寸太大")
FileExtensionNotAllowedError = errors.New("不允许上传此类型的文件")
InsufficientCapacityError = errors.New("容量空间不足")
IlegalObjectNameError = errors.New("目标名称非法")
ErrUnknownPolicyType = errors.New("未知存储策略类型")
ErrFileSizeTooBig = errors.New("单个文件尺寸太大")
ErrFileExtensionNotAllowed = errors.New("不允许上传此类型的文件")
ErrInsufficientCapacity = errors.New("容量空间不足")
ErrIllegalObjectName = errors.New("目标名称非法")
ErrInsertFileRecord = errors.New("无法插入文件记录")
ErrFileExisted = errors.New("同名文件已存在")
ErrPathNotExist = errors.New("路径不存在")
)

View File

@@ -0,0 +1,52 @@
package filesystem
import (
"context"
"github.com/DATA-DOG/go-sqlmock"
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/filesystem/local"
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
"testing"
)
func TestFileSystem_AddFile(t *testing.T) {
asserts := assert.New(t)
file := local.FileData{
Size: 5,
Name: "1.txt",
}
folder := model.Folder{
Model: gorm.Model{
ID: 1,
},
PositionAbsolute: "/我的文件",
}
fs := FileSystem{
User: &model.User{
Model: gorm.Model{
ID: 1,
},
Policy: model.Policy{
Model: gorm.Model{
ID: 1,
},
},
},
}
ctx := context.WithValue(context.Background(), FileHeaderCtx, file)
ctx = context.WithValue(ctx, SavePathCtx, "/Uploads/1_sad.txt")
_, err := fs.AddFile(ctx, &folder)
asserts.Error(err)
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
f, err := fs.AddFile(ctx, &folder)
asserts.NoError(err)
asserts.Equal("/Uploads/1_sad.txt", f.SourceName)
}

View File

@@ -4,6 +4,7 @@ import (
"context"
"github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/filesystem/local"
testMock "github.com/stretchr/testify/mock"
"io"
)
@@ -27,6 +28,10 @@ type Handler interface {
// FileSystem 管理文件的文件系统
type FileSystem struct {
/*
测试用
*/
testMock.Mock
/*
文件系统所有者
*/
@@ -59,7 +64,7 @@ func NewFileSystem(user *model.User) (*FileSystem, error) {
case "local":
handler = local.Handler{}
default:
return nil, UnknownPolicyTypeError
return nil, ErrUnknownPolicyType
}
// TODO 分配默认钩子

View File

@@ -0,0 +1,24 @@
package filesystem
import (
model "github.com/HFO4/cloudreve/models"
"github.com/stretchr/testify/assert"
"testing"
)
func TestNewFileSystem(t *testing.T) {
asserts := assert.New(t)
user := model.User{
Policy: model.Policy{
Type: "local",
},
}
fs, err := NewFileSystem(&user)
asserts.NoError(err)
asserts.NotNil(fs.Handler)
user.Policy.Type = "unknown"
fs, err = NewFileSystem(&user)
asserts.Error(err)
}

View File

@@ -13,22 +13,22 @@ func GenericBeforeUpload(ctx context.Context, fs *FileSystem) error {
// 验证单文件尺寸
if !fs.ValidateFileSize(ctx, file.GetSize()) {
return FileSizeTooBigError
return ErrFileSizeTooBig
}
// 验证文件名
if !fs.ValidateLegalName(ctx, file.GetFileName()) {
return IlegalObjectNameError
return ErrIllegalObjectName
}
// 验证扩展名
if !fs.ValidateExtension(ctx, file.GetFileName()) {
return FileExtensionNotAllowedError
return ErrFileExtensionNotAllowed
}
// 验证并扣除容量
if !fs.ValidateCapacity(ctx, file.GetSize()) {
return InsufficientCapacityError
return ErrInsufficientCapacity
}
return nil
}
@@ -61,7 +61,7 @@ func GenericAfterUpload(ctx context.Context, fs *FileSystem) error {
// 检查路径是否存在
isExist, folder := fs.IsPathExist(virtualPath)
if !isExist {
return errors.New("路径\"" + virtualPath + "\"不存在")
return ErrPathNotExist
}
// 检查文件是否存在
@@ -69,13 +69,13 @@ func GenericAfterUpload(ctx context.Context, fs *FileSystem) error {
virtualPath,
ctx.Value(FileHeaderCtx).(FileHeader).GetFileName(),
)) {
return errors.New("同名文件已存在")
return ErrFileExisted
}
// 向数据库中插入记录
file, err := fs.AddFile(ctx, &folder)
file, err := fs.AddFile(ctx, folder)
if err != nil {
return errors.New("无法插入文件记录")
return ErrInsertFileRecord
}
// TODO 是否需要立即获取图像大小?

View File

@@ -5,6 +5,7 @@ import (
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/filesystem/local"
"github.com/stretchr/testify/assert"
"os"
"testing"
)
@@ -45,3 +46,49 @@ func TestGenericBeforeUpload(t *testing.T) {
ctx = context.WithValue(context.Background(), FileHeaderCtx, file)
asserts.Error(GenericBeforeUpload(ctx, &fs))
}
func TestGenericAfterUploadCanceled(t *testing.T) {
asserts := assert.New(t)
f, err := os.Create("TestGenericAfterUploadCanceled")
asserts.NoError(err)
f.Close()
file := local.FileStream{
Size: 5,
Name: "TestGenericAfterUploadCanceled",
}
ctx := context.WithValue(context.Background(), SavePathCtx, "TestGenericAfterUploadCanceled")
ctx = context.WithValue(ctx, FileHeaderCtx, file)
fs := FileSystem{
User: &model.User{Storage: 5},
Handler: local.Handler{},
}
// 成功
err = GenericAfterUploadCanceled(ctx, &fs)
asserts.NoError(err)
asserts.Equal(uint64(0), fs.User.Storage)
f, err = os.Create("TestGenericAfterUploadCanceled")
asserts.NoError(err)
f.Close()
// 容量不能再降低
err = GenericAfterUploadCanceled(ctx, &fs)
asserts.Error(err)
//文件不存在
fs.User.Storage = 5
err = GenericAfterUploadCanceled(ctx, &fs)
asserts.NoError(err)
}
//func TestGenericAfterUpload(t *testing.T) {
// asserts := assert.New(t)
// testObj := FileSystem{}
// ctx := context.WithValue(context.Background(),FileHeaderCtx,local.FileStream{
// VirtualPath: "/我的文件",
// Name: "test.txt",
// })
//
//
//}

View File

@@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/assert"
"io"
"io/ioutil"
"os"
"strings"
"testing"
)
@@ -42,3 +43,27 @@ func TestHandler_Put(t *testing.T) {
}
}
}
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"})
asserts.Equal([]string{"test.file"}, 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"})
asserts.Equal([]string{"test.file"}, list)
asserts.Error(err)
list, err = handler.Delete(ctx, []string{"test.notexist"})
asserts.Equal([]string{}, list)
asserts.Error(err)
}

View File

@@ -12,9 +12,9 @@ import (
// IsPathExist 返回给定目录是否存在
// 如果存在就返回目录
func (fs *FileSystem) IsPathExist(path string) (bool, model.Folder) {
func (fs *FileSystem) IsPathExist(path string) (bool, *model.Folder) {
folder, err := model.GetFolderByPath(path, fs.User.ID)
return err == nil, folder
return err == nil, &folder
}
// IsFileExist 返回给定路径的文件是否存在