mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Test: share related operation in filesystem
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/HFO4/cloudreve/pkg/cache"
|
||||
"github.com/HFO4/cloudreve/pkg/filesystem/fsctx"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
@@ -79,4 +80,30 @@ func TestFileSystem_Compress(t *testing.T) {
|
||||
asserts.Error(err)
|
||||
asserts.Empty(zipFile)
|
||||
}
|
||||
|
||||
// 限制父目录
|
||||
{
|
||||
ctx := context.WithValue(context.Background(), fsctx.LimitParentCtx, &model.Folder{
|
||||
Model: gorm.Model{ID: 3},
|
||||
})
|
||||
// 查找压缩父目录
|
||||
mock.ExpectQuery("SELECT(.+)folders(.+)").
|
||||
WithArgs(1, 1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "name", "parent_id"}).AddRow(1, "parent", 3))
|
||||
// 查找顶级待压缩文件
|
||||
mock.ExpectQuery("SELECT(.+)files(.+)").
|
||||
WithArgs(1, 1).
|
||||
WillReturnRows(
|
||||
sqlmock.NewRows(
|
||||
[]string{"id", "name", "source_name", "policy_id"}).
|
||||
AddRow(1, "1.txt", "tests/file1.txt", 1),
|
||||
)
|
||||
asserts.NoError(cache.Set("setting_temp_path", "tests", -1))
|
||||
|
||||
zipFile, err := fs.Compress(ctx, []uint{1}, []uint{1})
|
||||
asserts.Error(err)
|
||||
asserts.Equal(ErrObjectNotExist, err)
|
||||
asserts.Empty(zipFile)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -601,3 +601,16 @@ func TestFileSystem_Preview(t *testing.T) {
|
||||
asserts.Nil(resp)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileSystem_ResetFileIDIfNotExist(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
ctx := context.WithValue(context.Background(), fsctx.LimitParentCtx, &model.Folder{Model: gorm.Model{ID: 1}})
|
||||
fs := FileSystem{
|
||||
FileTarget: []model.File{
|
||||
{
|
||||
FolderID: 2,
|
||||
},
|
||||
},
|
||||
}
|
||||
asserts.Equal(ErrObjectNotExist, fs.resetFileIDIfNotExist(ctx, 1))
|
||||
}
|
||||
|
||||
@@ -227,7 +227,6 @@ func NewFileSystemFromContext(c *gin.Context) (*FileSystem, error) {
|
||||
}
|
||||
|
||||
// NewFileSystemFromCallback 从gin.Context创建回调用文件系统
|
||||
// TODO 测试
|
||||
func NewFileSystemFromCallback(c *gin.Context) (*FileSystem, error) {
|
||||
fs, err := NewFileSystemFromContext(c)
|
||||
if err != nil {
|
||||
|
||||
@@ -238,3 +238,22 @@ func TestFileSystem_Recycle(t *testing.T) {
|
||||
t.Error("指针不一致")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileSystem_SetTargetByInterface(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
fs := FileSystem{}
|
||||
|
||||
// 目录
|
||||
{
|
||||
asserts.NoError(fs.SetTargetByInterface(&model.Folder{}))
|
||||
asserts.Len(fs.DirTarget, 1)
|
||||
asserts.Len(fs.FileTarget, 0)
|
||||
}
|
||||
|
||||
// 文件
|
||||
{
|
||||
asserts.NoError(fs.SetTargetByInterface(&model.File{}))
|
||||
asserts.Len(fs.DirTarget, 1)
|
||||
asserts.Len(fs.FileTarget, 1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/HFO4/cloudreve/pkg/cache"
|
||||
"github.com/HFO4/cloudreve/pkg/conf"
|
||||
"github.com/HFO4/cloudreve/pkg/filesystem/fsctx"
|
||||
"github.com/HFO4/cloudreve/pkg/serializer"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -40,6 +41,25 @@ func TestFileSystem_List(t *testing.T) {
|
||||
asserts.NoError(err)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 成功,子目录包含文件和路径,不使用路径处理钩子,包含分享key
|
||||
// 根目录
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "name", "owner_id"}).AddRow(1, "/", 1))
|
||||
// folder
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1, 1, "folder").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "name", "owner_id"}).AddRow(5, "folder", 1))
|
||||
|
||||
mock.ExpectQuery("SELECT(.+)folder(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(6, "sub_folder1").AddRow(7, "sub_folder2"))
|
||||
mock.ExpectQuery("SELECT(.+)file(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(6, "sub_file1.txt").AddRow(7, "sub_file2.txt"))
|
||||
ctxWithKey := context.WithValue(ctx, fsctx.ShareKeyCtx, "share")
|
||||
objects, err = fs.List(ctxWithKey, "/folder", nil)
|
||||
asserts.Len(objects, 4)
|
||||
asserts.Equal("share", objects[3].Key)
|
||||
asserts.NoError(err)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 成功,子目录包含文件和路径,使用路径处理钩子
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
@@ -585,3 +605,49 @@ func TestFileSystem_Rename(t *testing.T) {
|
||||
asserts.Equal(ErrIllegalObjectName, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileSystem_SaveTo(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
fs := &FileSystem{User: &model.User{
|
||||
Model: gorm.Model{
|
||||
ID: 1,
|
||||
},
|
||||
}}
|
||||
ctx := context.Background()
|
||||
|
||||
// 单文件 失败
|
||||
{
|
||||
// 根目录
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnError(errors.New("error"))
|
||||
fs.SetTargetFile(&[]model.File{{Name: "test.txt"}})
|
||||
err := fs.SaveTo(ctx, "/")
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.Error(err)
|
||||
}
|
||||
// 目录 成功
|
||||
{
|
||||
// 根目录
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnError(errors.New("error"))
|
||||
fs.SetTargetDir(&[]model.Folder{{Name: "folder"}})
|
||||
err := fs.SaveTo(ctx, "/")
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.Error(err)
|
||||
}
|
||||
// 父目录不存在
|
||||
{
|
||||
// 根目录
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
|
||||
fs.SetTargetDir(&[]model.Folder{{Name: "folder"}})
|
||||
err := fs.SaveTo(ctx, "/")
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,6 +102,25 @@ func TestFileSystem_IsPathExist(t *testing.T) {
|
||||
asserts.Equal(uint(4), folder.ID)
|
||||
}
|
||||
|
||||
// 深层路径 重设根目录为/1
|
||||
{
|
||||
path := "/2/3"
|
||||
fs.Root = &model.Folder{Name: "1", Model: gorm.Model{ID: 2}, OwnerID: 1}
|
||||
// 2
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(2, 1, "2").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(3, 1))
|
||||
// 3
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(3, 1, "3").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(4, 1))
|
||||
exist, folder := fs.IsPathExist(path)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.True(exist)
|
||||
asserts.Equal(uint(4), folder.ID)
|
||||
fs.Root = nil
|
||||
}
|
||||
|
||||
// 深层 不存在
|
||||
{
|
||||
path := "/1/2/3"
|
||||
|
||||
Reference in New Issue
Block a user