From a445da5286ded33c5e5b3388c9bda324aae96c86 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Sun, 8 Dec 2019 13:03:08 +0800 Subject: [PATCH] Fix: failed unit test due to missing mock.ExpectWereMatch() --- pkg/filesystem/manage.go | 33 +++++++++++++++++++++++---------- pkg/filesystem/manage_test.go | 11 ++++++----- service/explorer/file.go | 3 +-- service/explorer/objects.go | 11 ++++++++--- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/pkg/filesystem/manage.go b/pkg/filesystem/manage.go index f0cbe1e..697fc01 100644 --- a/pkg/filesystem/manage.go +++ b/pkg/filesystem/manage.go @@ -26,24 +26,37 @@ type Object struct { } // 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) { return ErrIllegalObjectName } // 如果源对象是文件 - fileExist, file := fs.IsFileExist(src) - if fileExist { - err = file.Rename(new) - return err + if len(file) > 0 { + fileObject, err := model.GetFilesByIDs([]uint{file[0]}, fs.User.ID) + if err != nil || len(fileObject) == 0 { + return ErrPathNotExist + } + + err = fileObject[0].Rename(new) + if err != nil { + return ErrFileExisted + } + return nil } - // 源对象是目录 - folderExist, folder := fs.IsPathExist(src) - if folderExist { - err = folder.Rename(new) - return err + if len(dir) > 0 { + folderObject, err := model.GetFoldersByIDs([]uint{dir[0]}, fs.User.ID) + if err != nil || len(folderObject) == 0 { + return ErrPathNotExist + } + + err = folderObject[0].Rename(new) + if err != nil { + return ErrFileExisted + } + return nil } return ErrPathNotExist diff --git a/pkg/filesystem/manage_test.go b/pkg/filesystem/manage_test.go index f9bd6ec..dc2399b 100644 --- a/pkg/filesystem/manage_test.go +++ b/pkg/filesystem/manage_test.go @@ -411,7 +411,7 @@ func TestFileSystem_Copy(t *testing.T) { WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1)) // 1 mock.ExpectQuery("SELECT(.+)"). - WithArgs(1, 1, "src"). + WithArgs(1, 1, "dst"). WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1)) // 根目录 mock.ExpectQuery("SELECT(.+)"). @@ -419,17 +419,17 @@ func TestFileSystem_Copy(t *testing.T) { WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1)) // 1 mock.ExpectQuery("SELECT(.+)"). - WithArgs(1, 1, "dst"). + WithArgs(1, 1, "src"). WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1)) err := fs.Copy(ctx, []uint{1}, []uint{}, "/src", "/dst") asserts.Error(err) + asserts.NoError(mock.ExpectationsWereMet()) } } func TestFileSystem_Move(t *testing.T) { - conf.DatabaseConfig.Type = "mysql" asserts := assert.New(t) fs := &FileSystem{User: &model.User{ Model: gorm.Model{ @@ -458,7 +458,7 @@ func TestFileSystem_Move(t *testing.T) { WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1)) // 1 mock.ExpectQuery("SELECT(.+)"). - WithArgs(1, 1, "src"). + WithArgs(1, 1, "dst"). WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1)) // 根目录 mock.ExpectQuery("SELECT(.+)"). @@ -466,9 +466,10 @@ func TestFileSystem_Move(t *testing.T) { WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1)) // 1 mock.ExpectQuery("SELECT(.+)"). - WithArgs(1, 1, "dst"). + WithArgs(1, 1, "src"). WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1)) err := fs.Move(ctx, []uint{1}, []uint{}, "/src", "/dst") asserts.Error(err) + asserts.NoError(mock.ExpectationsWereMet()) } } diff --git a/service/explorer/file.go b/service/explorer/file.go index fd4f2b5..ecdd32d 100644 --- a/service/explorer/file.go +++ b/service/explorer/file.go @@ -6,7 +6,6 @@ import ( "github.com/HFO4/cloudreve/pkg/serializer" "github.com/gin-gonic/gin" "net/http" - "time" ) // 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+"\"") // 发送文件 - http.ServeContent(c.Writer, c.Request, "", time.Time{}, rs) + http.ServeContent(c.Writer, c.Request, "", fs.FileTarget[0].UpdatedAt, rs) return serializer.Response{ Code: 0, diff --git a/service/explorer/objects.go b/service/explorer/objects.go index 9ea0cba..d5f1222 100644 --- a/service/explorer/objects.go +++ b/service/explorer/objects.go @@ -16,8 +16,8 @@ type ItemMoveService struct { // ItemRenameService 处理多文件/目录重命名 type ItemRenameService struct { - Src string `json:"src" binding:"required,min=1,max=65535,ne=/"` - NewName string `json:"new_name" binding:"required,min=1,max=255"` + Src ItemService `json:"src" binding:"exists"` + NewName string `json:"new_name" binding:"required,min=1,max=255"` } // ItemService 处理多文件/目录相关服务 @@ -93,6 +93,11 @@ func (service *ItemMoveService) Copy(ctx context.Context, c *gin.Context) serial // Rename 重命名对象 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) 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 { return serializer.Err(serializer.CodeNotSet, err.Error(), err) }