Test: share related operation in filesystem

pull/247/head
HFO4 5 years ago
parent ab739ea07c
commit 8bb2c2b0c5

@ -41,7 +41,12 @@ TablePrefix = v3_`
// TestInitNoPanic 日志路径存在且合法时 // TestInitNoPanic 日志路径存在且合法时
func TestInitNoPanic(t *testing.T) { func TestInitNoPanic(t *testing.T) {
asserts := assert.New(t) asserts := assert.New(t)
testCase := `[Database] testCase := `
[System]
Listen = 3000
HashIDSalt = 1
[Database]
Type = mysql Type = mysql
User = root User = root
Password = root Password = root
@ -62,7 +67,12 @@ func TestMapSection(t *testing.T) {
asserts := assert.New(t) asserts := assert.New(t)
//正常情况 //正常情况
testCase := `[Database] testCase := `
[System]
Listen = 3000
HashIDSalt = 1
[Database]
Type = mysql Type = mysql
User = root User = root
Password:root Password:root

@ -5,6 +5,7 @@ import (
"github.com/DATA-DOG/go-sqlmock" "github.com/DATA-DOG/go-sqlmock"
model "github.com/HFO4/cloudreve/models" model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/cache" "github.com/HFO4/cloudreve/pkg/cache"
"github.com/HFO4/cloudreve/pkg/filesystem/fsctx"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"testing" "testing"
@ -79,4 +80,30 @@ func TestFileSystem_Compress(t *testing.T) {
asserts.Error(err) asserts.Error(err)
asserts.Empty(zipFile) 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) 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创建回调用文件系统 // NewFileSystemFromCallback 从gin.Context创建回调用文件系统
// TODO 测试
func NewFileSystemFromCallback(c *gin.Context) (*FileSystem, error) { func NewFileSystemFromCallback(c *gin.Context) (*FileSystem, error) {
fs, err := NewFileSystemFromContext(c) fs, err := NewFileSystemFromContext(c)
if err != nil { if err != nil {

@ -238,3 +238,22 @@ func TestFileSystem_Recycle(t *testing.T) {
t.Error("指针不一致") 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" model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/cache" "github.com/HFO4/cloudreve/pkg/cache"
"github.com/HFO4/cloudreve/pkg/conf" "github.com/HFO4/cloudreve/pkg/conf"
"github.com/HFO4/cloudreve/pkg/filesystem/fsctx"
"github.com/HFO4/cloudreve/pkg/serializer" "github.com/HFO4/cloudreve/pkg/serializer"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -40,6 +41,25 @@ func TestFileSystem_List(t *testing.T) {
asserts.NoError(err) asserts.NoError(err)
asserts.NoError(mock.ExpectationsWereMet()) 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(.+)"). mock.ExpectQuery("SELECT(.+)").
WithArgs(1). WithArgs(1).
@ -585,3 +605,49 @@ func TestFileSystem_Rename(t *testing.T) {
asserts.Equal(ErrIllegalObjectName, err) 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) 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" path := "/1/2/3"

@ -54,7 +54,6 @@ func HashID(id uint, t int) string {
} }
// DecodeHashID 计算HashID对应的数据库ID // DecodeHashID 计算HashID对应的数据库ID
// TODO 测试
func DecodeHashID(id string, t int) (uint, error) { func DecodeHashID(id string, t int) (uint, error) {
v, _ := HashDecode(id) v, _ := HashDecode(id)
if len(v) != 2 || v[1] != t { if len(v) != 2 || v[1] != t {

@ -30,3 +30,40 @@ func TestHashID(t *testing.T) {
asserts.NotEmpty(res) asserts.NotEmpty(res)
} }
} }
func TestHashDecode(t *testing.T) {
asserts := assert.New(t)
// 正常
{
res, _ := HashEncode([]int{1, 2, 3})
decodeRes, err := HashDecode(res)
asserts.NoError(err)
asserts.Equal([]int{1, 2, 3}, decodeRes)
}
// 出错
{
decodeRes, err := HashDecode("233")
asserts.Error(err)
asserts.Len(decodeRes, 0)
}
}
func TestDecodeHashID(t *testing.T) {
asserts := assert.New(t)
// 成功
{
uid, err := DecodeHashID(HashID(1, ShareID), ShareID)
asserts.NoError(err)
asserts.EqualValues(1, uid)
}
// 类型不匹配
{
uid, err := DecodeHashID(HashID(1, ShareID), UserID)
asserts.Error(err)
asserts.EqualValues(0, uid)
}
}

@ -0,0 +1,64 @@
package serializer
import (
model "github.com/HFO4/cloudreve/models"
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestBuildShareResponse(t *testing.T) {
asserts := assert.New(t)
// 未解锁
{
share := &model.Share{
User: model.User{Model: gorm.Model{ID: 1}},
Downloads: 1,
}
res := BuildShareResponse(share, false)
asserts.EqualValues(0, res.Downloads)
asserts.True(res.Locked)
asserts.NotNil(res.Creator)
}
// 已解锁,非目录
{
expires := time.Now().Add(time.Duration(10) * time.Second)
share := &model.Share{
User: model.User{Model: gorm.Model{ID: 1}},
Downloads: 1,
Expires: &expires,
File: model.File{
Model: gorm.Model{ID: 1},
},
}
res := BuildShareResponse(share, true)
asserts.EqualValues(1, res.Downloads)
asserts.False(res.Locked)
asserts.NotEmpty(res.Expire)
asserts.NotNil(res.Creator)
asserts.NotNil(res.Score)
}
// 已解锁,是目录
{
expires := time.Now().Add(time.Duration(10) * time.Second)
share := &model.Share{
User: model.User{Model: gorm.Model{ID: 1}},
Downloads: 1,
Expires: &expires,
Folder: model.Folder{
Model: gorm.Model{ID: 1},
},
IsDir: true,
}
res := BuildShareResponse(share, true)
asserts.EqualValues(1, res.Downloads)
asserts.False(res.Locked)
asserts.NotEmpty(res.Expire)
asserts.NotNil(res.Creator)
asserts.NotNil(res.Score)
}
}
Loading…
Cancel
Save