mirror of https://github.com/cloudreve/Cloudreve
Fix: failed unit test due to missing mock.ExpectWereMatch()
parent
6eff13c80a
commit
a445da5286
|
@ -26,24 +26,37 @@ type Object struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rename 重命名对象
|
// Rename 重命名对象
|
||||||
func (fs *FileSystem) Rename(ctx context.Context, src, new string) (err error) {
|
func (fs *FileSystem) Rename(ctx context.Context, dir, file []uint, new string) (err error) {
|
||||||
// 验证新名字
|
// 验证新名字
|
||||||
if !fs.ValidateLegalName(ctx, new) || !fs.ValidateExtension(ctx, new) {
|
if !fs.ValidateLegalName(ctx, new) || !fs.ValidateExtension(ctx, new) {
|
||||||
return ErrIllegalObjectName
|
return ErrIllegalObjectName
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果源对象是文件
|
// 如果源对象是文件
|
||||||
fileExist, file := fs.IsFileExist(src)
|
if len(file) > 0 {
|
||||||
if fileExist {
|
fileObject, err := model.GetFilesByIDs([]uint{file[0]}, fs.User.ID)
|
||||||
err = file.Rename(new)
|
if err != nil || len(fileObject) == 0 {
|
||||||
return err
|
return ErrPathNotExist
|
||||||
|
}
|
||||||
|
|
||||||
|
err = fileObject[0].Rename(new)
|
||||||
|
if err != nil {
|
||||||
|
return ErrFileExisted
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 源对象是目录
|
if len(dir) > 0 {
|
||||||
folderExist, folder := fs.IsPathExist(src)
|
folderObject, err := model.GetFoldersByIDs([]uint{dir[0]}, fs.User.ID)
|
||||||
if folderExist {
|
if err != nil || len(folderObject) == 0 {
|
||||||
err = folder.Rename(new)
|
return ErrPathNotExist
|
||||||
return err
|
}
|
||||||
|
|
||||||
|
err = folderObject[0].Rename(new)
|
||||||
|
if err != nil {
|
||||||
|
return ErrFileExisted
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return ErrPathNotExist
|
return ErrPathNotExist
|
||||||
|
|
|
@ -411,7 +411,7 @@ func TestFileSystem_Copy(t *testing.T) {
|
||||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
||||||
// 1
|
// 1
|
||||||
mock.ExpectQuery("SELECT(.+)").
|
mock.ExpectQuery("SELECT(.+)").
|
||||||
WithArgs(1, 1, "src").
|
WithArgs(1, 1, "dst").
|
||||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
|
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
|
||||||
// 根目录
|
// 根目录
|
||||||
mock.ExpectQuery("SELECT(.+)").
|
mock.ExpectQuery("SELECT(.+)").
|
||||||
|
@ -419,17 +419,17 @@ func TestFileSystem_Copy(t *testing.T) {
|
||||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
||||||
// 1
|
// 1
|
||||||
mock.ExpectQuery("SELECT(.+)").
|
mock.ExpectQuery("SELECT(.+)").
|
||||||
WithArgs(1, 1, "dst").
|
WithArgs(1, 1, "src").
|
||||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
|
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
|
||||||
|
|
||||||
err := fs.Copy(ctx, []uint{1}, []uint{}, "/src", "/dst")
|
err := fs.Copy(ctx, []uint{1}, []uint{}, "/src", "/dst")
|
||||||
asserts.Error(err)
|
asserts.Error(err)
|
||||||
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFileSystem_Move(t *testing.T) {
|
func TestFileSystem_Move(t *testing.T) {
|
||||||
conf.DatabaseConfig.Type = "mysql"
|
|
||||||
asserts := assert.New(t)
|
asserts := assert.New(t)
|
||||||
fs := &FileSystem{User: &model.User{
|
fs := &FileSystem{User: &model.User{
|
||||||
Model: gorm.Model{
|
Model: gorm.Model{
|
||||||
|
@ -458,7 +458,7 @@ func TestFileSystem_Move(t *testing.T) {
|
||||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
||||||
// 1
|
// 1
|
||||||
mock.ExpectQuery("SELECT(.+)").
|
mock.ExpectQuery("SELECT(.+)").
|
||||||
WithArgs(1, 1, "src").
|
WithArgs(1, 1, "dst").
|
||||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
|
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
|
||||||
// 根目录
|
// 根目录
|
||||||
mock.ExpectQuery("SELECT(.+)").
|
mock.ExpectQuery("SELECT(.+)").
|
||||||
|
@ -466,9 +466,10 @@ func TestFileSystem_Move(t *testing.T) {
|
||||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
||||||
// 1
|
// 1
|
||||||
mock.ExpectQuery("SELECT(.+)").
|
mock.ExpectQuery("SELECT(.+)").
|
||||||
WithArgs(1, 1, "dst").
|
WithArgs(1, 1, "src").
|
||||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
|
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
|
||||||
err := fs.Move(ctx, []uint{1}, []uint{}, "/src", "/dst")
|
err := fs.Move(ctx, []uint{1}, []uint{}, "/src", "/dst")
|
||||||
asserts.Error(err)
|
asserts.Error(err)
|
||||||
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"github.com/HFO4/cloudreve/pkg/serializer"
|
"github.com/HFO4/cloudreve/pkg/serializer"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// FileDownloadService 文件下载服务,path为文件完整路径
|
// FileDownloadService 文件下载服务,path为文件完整路径
|
||||||
|
@ -32,7 +31,7 @@ func (service *FileDownloadService) Download(ctx context.Context, c *gin.Context
|
||||||
// 设置文件名
|
// 设置文件名
|
||||||
c.Header("Content-Disposition", "attachment; filename=\""+fs.FileTarget[0].Name+"\"")
|
c.Header("Content-Disposition", "attachment; filename=\""+fs.FileTarget[0].Name+"\"")
|
||||||
// 发送文件
|
// 发送文件
|
||||||
http.ServeContent(c.Writer, c.Request, "", time.Time{}, rs)
|
http.ServeContent(c.Writer, c.Request, "", fs.FileTarget[0].UpdatedAt, rs)
|
||||||
|
|
||||||
return serializer.Response{
|
return serializer.Response{
|
||||||
Code: 0,
|
Code: 0,
|
||||||
|
|
|
@ -16,8 +16,8 @@ type ItemMoveService struct {
|
||||||
|
|
||||||
// ItemRenameService 处理多文件/目录重命名
|
// ItemRenameService 处理多文件/目录重命名
|
||||||
type ItemRenameService struct {
|
type ItemRenameService struct {
|
||||||
Src string `json:"src" binding:"required,min=1,max=65535,ne=/"`
|
Src ItemService `json:"src" binding:"exists"`
|
||||||
NewName string `json:"new_name" binding:"required,min=1,max=255"`
|
NewName string `json:"new_name" binding:"required,min=1,max=255"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ItemService 处理多文件/目录相关服务
|
// ItemService 处理多文件/目录相关服务
|
||||||
|
@ -93,6 +93,11 @@ func (service *ItemMoveService) Copy(ctx context.Context, c *gin.Context) serial
|
||||||
|
|
||||||
// Rename 重命名对象
|
// Rename 重命名对象
|
||||||
func (service *ItemRenameService) Rename(ctx context.Context, c *gin.Context) serializer.Response {
|
func (service *ItemRenameService) Rename(ctx context.Context, c *gin.Context) serializer.Response {
|
||||||
|
// 重命名作只能对一个目录或文件对象进行操作
|
||||||
|
if len(service.Src.Items)+len(service.Src.Dirs) > 1 {
|
||||||
|
return serializer.ParamErr("只能操作一个对象", nil)
|
||||||
|
}
|
||||||
|
|
||||||
// 创建文件系统
|
// 创建文件系统
|
||||||
fs, err := filesystem.NewFileSystemFromContext(c)
|
fs, err := filesystem.NewFileSystemFromContext(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -100,7 +105,7 @@ func (service *ItemRenameService) Rename(ctx context.Context, c *gin.Context) se
|
||||||
}
|
}
|
||||||
|
|
||||||
// 重命名对象
|
// 重命名对象
|
||||||
err = fs.Rename(ctx, service.Src, service.NewName)
|
err = fs.Rename(ctx, service.Src.Dirs, service.Src.Items, service.NewName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return serializer.Err(serializer.CodeNotSet, err.Error(), err)
|
return serializer.Err(serializer.CodeNotSet, err.Error(), err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue