|
|
@ -2,11 +2,23 @@ package filesystem
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
|
|
|
|
|
|
model "github.com/cloudreve/Cloudreve/v3/models"
|
|
|
|
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/cache"
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/response"
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/response"
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
testMock "github.com/stretchr/testify/mock"
|
|
|
|
testMock "github.com/stretchr/testify/mock"
|
|
|
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type FileHeaderMock struct {
|
|
|
|
type FileHeaderMock struct {
|
|
|
@ -53,177 +65,200 @@ func (m FileHeaderMock) Source(ctx context.Context, path string, url url.URL, ex
|
|
|
|
return args.Get(0).(string), args.Error(1)
|
|
|
|
return args.Get(0).(string), args.Error(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//func TestFileSystem_Upload(t *testing.T) {
|
|
|
|
func TestFileSystem_Upload(t *testing.T) {
|
|
|
|
// asserts := assert.New(t)
|
|
|
|
asserts := assert.New(t)
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 正常
|
|
|
|
// 正常
|
|
|
|
// testHandler := new(FileHeaderMock)
|
|
|
|
testHandler := new(FileHeaderMock)
|
|
|
|
// testHandler.On("Put", testMock.Anything, testMock.Anything, testMock.Anything).Return(nil)
|
|
|
|
testHandler.On("Put", testMock.Anything, testMock.Anything, testMock.Anything).Return(nil)
|
|
|
|
// fs := &FileSystem{
|
|
|
|
fs := &FileSystem{
|
|
|
|
// Handler: testHandler,
|
|
|
|
Handler: testHandler,
|
|
|
|
// User: &model.User{
|
|
|
|
User: &model.User{
|
|
|
|
// Model: gorm.Model{
|
|
|
|
Model: gorm.Model{
|
|
|
|
// ID: 1,
|
|
|
|
ID: 1,
|
|
|
|
// },
|
|
|
|
},
|
|
|
|
// Policy: model.Policy{
|
|
|
|
},
|
|
|
|
// AutoRename: false,
|
|
|
|
Policy: &model.Policy{
|
|
|
|
// DirNameRule: "{path}",
|
|
|
|
AutoRename: false,
|
|
|
|
// },
|
|
|
|
DirNameRule: "{path}",
|
|
|
|
// },
|
|
|
|
},
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
// c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
|
|
|
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
|
|
|
// c.Request, _ = http.NewRequest("POST", "/", nil)
|
|
|
|
c.Request, _ = http.NewRequest("POST", "/", nil)
|
|
|
|
// ctx = context.WithValue(ctx, fsctx.GinCtx, c)
|
|
|
|
ctx = context.WithValue(ctx, fsctx.GinCtx, c)
|
|
|
|
// cancel()
|
|
|
|
cancel()
|
|
|
|
// file := fsctx.FileStream{
|
|
|
|
file := &fsctx.FileStream{
|
|
|
|
// Size: 5,
|
|
|
|
Size: 5,
|
|
|
|
// VirtualPath: "/",
|
|
|
|
VirtualPath: "/",
|
|
|
|
// Name: "1.txt",
|
|
|
|
Name: "1.txt",
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// err := fs.Upload(ctx, file)
|
|
|
|
err := fs.Upload(ctx, file)
|
|
|
|
// asserts.NoError(err)
|
|
|
|
asserts.NoError(err)
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 正常,上下文已指定源文件
|
|
|
|
// 正常,上下文已指定源文件
|
|
|
|
// testHandler = new(FileHeaderMock)
|
|
|
|
testHandler = new(FileHeaderMock)
|
|
|
|
// testHandler.On("Put", testMock.Anything, testMock.Anything, "123/123.txt").Return(nil)
|
|
|
|
testHandler.On("Put", testMock.Anything, testMock.Anything).Return(nil)
|
|
|
|
// fs = &FileSystem{
|
|
|
|
fs = &FileSystem{
|
|
|
|
// Handler: testHandler,
|
|
|
|
Handler: testHandler,
|
|
|
|
// User: &model.User{
|
|
|
|
User: &model.User{
|
|
|
|
// Model: gorm.Model{
|
|
|
|
Model: gorm.Model{
|
|
|
|
// ID: 1,
|
|
|
|
ID: 1,
|
|
|
|
// },
|
|
|
|
},
|
|
|
|
// Policy: model.Policy{
|
|
|
|
},
|
|
|
|
// AutoRename: false,
|
|
|
|
Policy: &model.Policy{
|
|
|
|
// DirNameRule: "{path}",
|
|
|
|
AutoRename: false,
|
|
|
|
// },
|
|
|
|
DirNameRule: "{path}",
|
|
|
|
// },
|
|
|
|
},
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// ctx, cancel = context.WithCancel(context.Background())
|
|
|
|
ctx, cancel = context.WithCancel(context.Background())
|
|
|
|
// c, _ = gin.CreateTestContext(httptest.NewRecorder())
|
|
|
|
c, _ = gin.CreateTestContext(httptest.NewRecorder())
|
|
|
|
// c.Request, _ = http.NewRequest("POST", "/", nil)
|
|
|
|
c.Request, _ = http.NewRequest("POST", "/", nil)
|
|
|
|
// ctx = context.WithValue(ctx, fsctx.GinCtx, c)
|
|
|
|
ctx = context.WithValue(ctx, fsctx.GinCtx, c)
|
|
|
|
// ctx = context.WithValue(ctx, fsctx.FileModelCtx, model.File{SourceName: "123/123.txt"})
|
|
|
|
ctx = context.WithValue(ctx, fsctx.FileModelCtx, model.File{SourceName: "123/123.txt"})
|
|
|
|
// cancel()
|
|
|
|
cancel()
|
|
|
|
// file = fsctx.FileStream{
|
|
|
|
file = &fsctx.FileStream{
|
|
|
|
// Size: 5,
|
|
|
|
Size: 5,
|
|
|
|
// VirtualPath: "/",
|
|
|
|
VirtualPath: "/",
|
|
|
|
// Name: "1.txt",
|
|
|
|
Name: "1.txt",
|
|
|
|
// File: ioutil.NopCloser(strings.NewReader("")),
|
|
|
|
File: ioutil.NopCloser(strings.NewReader("")),
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// err = fs.Upload(ctx, file)
|
|
|
|
err = fs.Upload(ctx, file)
|
|
|
|
// asserts.NoError(err)
|
|
|
|
asserts.NoError(err)
|
|
|
|
//
|
|
|
|
|
|
|
|
// // BeforeUpload 返回错误
|
|
|
|
// BeforeUpload 返回错误
|
|
|
|
// fs.Use("BeforeUpload", func(ctx context.Context, fs *FileSystem) error {
|
|
|
|
fs.Use("BeforeUpload", func(ctx context.Context, fs *FileSystem, file fsctx.FileHeader) error {
|
|
|
|
// return errors.New("error")
|
|
|
|
return errors.New("error")
|
|
|
|
// })
|
|
|
|
})
|
|
|
|
// err = fs.Upload(ctx, file)
|
|
|
|
err = fs.Upload(ctx, file)
|
|
|
|
// asserts.Error(err)
|
|
|
|
asserts.Error(err)
|
|
|
|
// fs.Hooks["BeforeUpload"] = nil
|
|
|
|
fs.Hooks["BeforeUpload"] = nil
|
|
|
|
// testHandler.AssertExpectations(t)
|
|
|
|
testHandler.AssertExpectations(t)
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 上传文件失败
|
|
|
|
// 上传文件失败
|
|
|
|
// testHandler2 := new(FileHeaderMock)
|
|
|
|
testHandler2 := new(FileHeaderMock)
|
|
|
|
// testHandler2.On("Put", testMock.Anything, testMock.Anything, testMock.Anything).Return(errors.New("error"))
|
|
|
|
testHandler2.On("Put", testMock.Anything, testMock.Anything).Return(errors.New("error"))
|
|
|
|
// fs.Handler = testHandler2
|
|
|
|
fs.Handler = testHandler2
|
|
|
|
// err = fs.Upload(ctx, file)
|
|
|
|
err = fs.Upload(ctx, file)
|
|
|
|
// asserts.Error(err)
|
|
|
|
asserts.Error(err)
|
|
|
|
// testHandler2.AssertExpectations(t)
|
|
|
|
testHandler2.AssertExpectations(t)
|
|
|
|
//
|
|
|
|
|
|
|
|
// // AfterUpload失败
|
|
|
|
// AfterUpload失败
|
|
|
|
// testHandler3 := new(FileHeaderMock)
|
|
|
|
testHandler3 := new(FileHeaderMock)
|
|
|
|
// testHandler3.On("Put", testMock.Anything, testMock.Anything, testMock.Anything).Return(nil)
|
|
|
|
testHandler3.On("Put", testMock.Anything, testMock.Anything).Return(nil)
|
|
|
|
// fs.Handler = testHandler3
|
|
|
|
fs.Handler = testHandler3
|
|
|
|
// fs.Use("AfterUpload", func(ctx context.Context, fs *FileSystem) error {
|
|
|
|
fs.Use("AfterUpload", func(ctx context.Context, fs *FileSystem, file fsctx.FileHeader) error {
|
|
|
|
// return errors.New("error")
|
|
|
|
return errors.New("error")
|
|
|
|
// })
|
|
|
|
})
|
|
|
|
// fs.Use("AfterValidateFailed", func(ctx context.Context, fs *FileSystem) error {
|
|
|
|
fs.Use("AfterValidateFailed", func(ctx context.Context, fs *FileSystem, file fsctx.FileHeader) error {
|
|
|
|
// return errors.New("error")
|
|
|
|
return errors.New("error")
|
|
|
|
// })
|
|
|
|
})
|
|
|
|
// err = fs.Upload(ctx, file)
|
|
|
|
err = fs.Upload(ctx, file)
|
|
|
|
// asserts.Error(err)
|
|
|
|
asserts.Error(err)
|
|
|
|
// testHandler2.AssertExpectations(t)
|
|
|
|
testHandler2.AssertExpectations(t)
|
|
|
|
//
|
|
|
|
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
//
|
|
|
|
|
|
|
|
//func TestFileSystem_GenerateSavePath_Anonymous(t *testing.T) {
|
|
|
|
func TestFileSystem_GetUploadToken(t *testing.T) {
|
|
|
|
// asserts := assert.New(t)
|
|
|
|
asserts := assert.New(t)
|
|
|
|
// fs := FileSystem{User: &model.User{}}
|
|
|
|
fs := FileSystem{
|
|
|
|
// ctx := context.WithValue(
|
|
|
|
User: &model.User{Model: gorm.Model{ID: 1}},
|
|
|
|
// context.Background(),
|
|
|
|
Policy: &model.Policy{},
|
|
|
|
// fsctx.UploadPolicyCtx,
|
|
|
|
}
|
|
|
|
// serializer.UploadPolicy{
|
|
|
|
ctx := context.Background()
|
|
|
|
// SavePath: "{randomkey16}",
|
|
|
|
|
|
|
|
// AutoRename: false,
|
|
|
|
// 成功
|
|
|
|
// },
|
|
|
|
{
|
|
|
|
// )
|
|
|
|
cache.SetSettings(map[string]string{
|
|
|
|
//
|
|
|
|
"upload_session_timeout": "10",
|
|
|
|
// savePath := fs.GenerateSavePath(ctx, fsctx.FileStream{
|
|
|
|
}, "setting_")
|
|
|
|
// Name: "test.test",
|
|
|
|
testHandler := new(FileHeaderMock)
|
|
|
|
// })
|
|
|
|
testHandler.On("Token", testMock.Anything, int64(10), testMock.Anything, testMock.Anything).Return(&serializer.UploadCredential{Credential: "test"}, nil)
|
|
|
|
// asserts.Len(savePath, 26)
|
|
|
|
fs.Handler = testHandler
|
|
|
|
// asserts.Contains(savePath, "test.test")
|
|
|
|
mock.ExpectQuery("SELECT(.+)").
|
|
|
|
//}
|
|
|
|
WithArgs(1).
|
|
|
|
//
|
|
|
|
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
|
|
|
//func TestFileSystem_GetUploadToken(t *testing.T) {
|
|
|
|
mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnError(errors.New("not found"))
|
|
|
|
// asserts := assert.New(t)
|
|
|
|
mock.ExpectBegin()
|
|
|
|
// fs := FileSystem{User: &model.User{Model: gorm.Model{ID: 1}}}
|
|
|
|
mock.ExpectExec("INSERT(.+)files(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
// ctx := context.Background()
|
|
|
|
mock.ExpectExec("UPDATE(.+)storage(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
//
|
|
|
|
mock.ExpectCommit()
|
|
|
|
// // 成功
|
|
|
|
res, err := fs.CreateUploadSession(ctx, &fsctx.FileStream{
|
|
|
|
// {
|
|
|
|
Size: 0,
|
|
|
|
// cache.SetSettings(map[string]string{
|
|
|
|
Name: "file",
|
|
|
|
// "upload_credential_timeout": "10",
|
|
|
|
VirtualPath: "/",
|
|
|
|
// "upload_session_timeout": "10",
|
|
|
|
})
|
|
|
|
// }, "setting_")
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
// testHandler := new(FileHeaderMock)
|
|
|
|
testHandler.AssertExpectations(t)
|
|
|
|
// testHandler.On("Token", testMock.Anything, int64(10), testMock.Anything).Return(serializer.UploadCredential{Token: "test"}, nil)
|
|
|
|
asserts.NoError(err)
|
|
|
|
// fs.Handler = testHandler
|
|
|
|
asserts.Equal("test", res.Credential)
|
|
|
|
// res, err := fs.CreateUploadSession(ctx, "/", 10, "123")
|
|
|
|
}
|
|
|
|
// testHandler.AssertExpectations(t)
|
|
|
|
|
|
|
|
// asserts.NoError(err)
|
|
|
|
// 无法获取上传凭证
|
|
|
|
// asserts.Equal("test", res.Token)
|
|
|
|
{
|
|
|
|
// }
|
|
|
|
cache.SetSettings(map[string]string{
|
|
|
|
//
|
|
|
|
"upload_credential_timeout": "10",
|
|
|
|
// // 无法获取上传凭证
|
|
|
|
"upload_session_timeout": "10",
|
|
|
|
// {
|
|
|
|
}, "setting_")
|
|
|
|
// cache.SetSettings(map[string]string{
|
|
|
|
testHandler := new(FileHeaderMock)
|
|
|
|
// "upload_credential_timeout": "10",
|
|
|
|
testHandler.On("Token", testMock.Anything, int64(10), testMock.Anything, testMock.Anything).Return(&serializer.UploadCredential{}, errors.New("error"))
|
|
|
|
// "upload_session_timeout": "10",
|
|
|
|
fs.Handler = testHandler
|
|
|
|
// }, "setting_")
|
|
|
|
mock.ExpectQuery("SELECT(.+)").
|
|
|
|
// testHandler := new(FileHeaderMock)
|
|
|
|
WithArgs(1).
|
|
|
|
// testHandler.On("Token", testMock.Anything, int64(10), testMock.Anything).Return(serializer.UploadCredential{}, errors.New("error"))
|
|
|
|
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
|
|
|
// fs.Handler = testHandler
|
|
|
|
mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnError(errors.New("not found"))
|
|
|
|
// _, err := fs.CreateUploadSession(ctx, "/", 10, "123")
|
|
|
|
mock.ExpectBegin()
|
|
|
|
// testHandler.AssertExpectations(t)
|
|
|
|
mock.ExpectExec("INSERT(.+)files(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
// asserts.Error(err)
|
|
|
|
mock.ExpectExec("UPDATE(.+)storage(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
// }
|
|
|
|
mock.ExpectCommit()
|
|
|
|
//}
|
|
|
|
_, err := fs.CreateUploadSession(ctx, &fsctx.FileStream{
|
|
|
|
//
|
|
|
|
Size: 0,
|
|
|
|
//func TestFileSystem_UploadFromStream(t *testing.T) {
|
|
|
|
Name: "file",
|
|
|
|
// asserts := assert.New(t)
|
|
|
|
VirtualPath: "/",
|
|
|
|
// fs := FileSystem{User: &model.User{Model: gorm.Model{ID: 1}}}
|
|
|
|
})
|
|
|
|
// ctx := context.Background()
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
//
|
|
|
|
testHandler.AssertExpectations(t)
|
|
|
|
// err := fs.UploadFromStream(ctx, ioutil.NopCloser(strings.NewReader("123")), "/1.txt", 1)
|
|
|
|
asserts.Error(err)
|
|
|
|
// asserts.Error(err)
|
|
|
|
}
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
//
|
|
|
|
|
|
|
|
//func TestFileSystem_UploadFromPath(t *testing.T) {
|
|
|
|
func TestFileSystem_UploadFromStream(t *testing.T) {
|
|
|
|
// asserts := assert.New(t)
|
|
|
|
asserts := assert.New(t)
|
|
|
|
// fs := FileSystem{User: &model.User{Policy: model.Policy{Type: "mock"}, Model: gorm.Model{ID: 1}}}
|
|
|
|
fs := FileSystem{
|
|
|
|
// ctx := context.Background()
|
|
|
|
User: &model.User{
|
|
|
|
//
|
|
|
|
Model: gorm.Model{ID: 1},
|
|
|
|
// // 文件不存在
|
|
|
|
Policy: model.Policy{Type: "mock"},
|
|
|
|
// {
|
|
|
|
},
|
|
|
|
// err := fs.UploadFromPath(ctx, "test/not_exist", "/", true)
|
|
|
|
Policy: &model.Policy{Type: "mock"},
|
|
|
|
// asserts.Error(err)
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
ctx := context.Background()
|
|
|
|
//
|
|
|
|
|
|
|
|
// // 文存在,上传失败
|
|
|
|
err := fs.UploadFromStream(ctx, &fsctx.FileStream{
|
|
|
|
// {
|
|
|
|
File: ioutil.NopCloser(strings.NewReader("123")),
|
|
|
|
// err := fs.UploadFromPath(ctx, "tests/test.zip", "/", true)
|
|
|
|
}, true)
|
|
|
|
// asserts.Error(err)
|
|
|
|
asserts.Error(err)
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
func TestFileSystem_UploadFromPath(t *testing.T) {
|
|
|
|
|
|
|
|
asserts := assert.New(t)
|
|
|
|
|
|
|
|
fs := FileSystem{
|
|
|
|
|
|
|
|
User: &model.User{
|
|
|
|
|
|
|
|
Model: gorm.Model{ID: 1},
|
|
|
|
|
|
|
|
Policy: model.Policy{Type: "mock"},
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
Policy: &model.Policy{Type: "mock"},
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 文件不存在
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
err := fs.UploadFromPath(ctx, "test/not_exist", "/", fsctx.Overwrite)
|
|
|
|
|
|
|
|
asserts.Error(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 文存在,上传失败
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
err := fs.UploadFromPath(ctx, "tests/test.zip", "/", fsctx.Overwrite)
|
|
|
|
|
|
|
|
asserts.Error(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|