|
|
|
@ -3,22 +3,20 @@ package filesystem
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"io"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"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/conf"
|
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/local"
|
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
|
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/mocks/requestmock"
|
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/request"
|
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
model "github.com/cloudreve/Cloudreve/v3/models"
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
testMock "github.com/stretchr/testify/mock"
|
|
|
|
@ -26,78 +24,72 @@ import (
|
|
|
|
|
|
|
|
|
|
func TestGenericBeforeUpload(t *testing.T) {
|
|
|
|
|
asserts := assert.New(t)
|
|
|
|
|
file := fsctx.FileStream{
|
|
|
|
|
file := &fsctx.FileStream{
|
|
|
|
|
Size: 5,
|
|
|
|
|
Name: "1.txt",
|
|
|
|
|
}
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
cache.Set("pack_size_0", uint64(0), 0)
|
|
|
|
|
ctx := context.WithValue(context.Background(), fsctx.FileHeaderCtx, file)
|
|
|
|
|
fs := FileSystem{
|
|
|
|
|
User: &model.User{
|
|
|
|
|
Storage: 0,
|
|
|
|
|
Group: model.Group{
|
|
|
|
|
MaxStorage: 11,
|
|
|
|
|
},
|
|
|
|
|
Policy: model.Policy{
|
|
|
|
|
MaxSize: 4,
|
|
|
|
|
OptionsSerialized: model.PolicyOption{
|
|
|
|
|
FileType: []string{"txt"},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Policy: &model.Policy{
|
|
|
|
|
MaxSize: 4,
|
|
|
|
|
OptionsSerialized: model.PolicyOption{
|
|
|
|
|
FileType: []string{"txt"},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
asserts.Error(HookValidateFile(ctx, &fs))
|
|
|
|
|
asserts.Error(HookValidateFile(ctx, &fs, file))
|
|
|
|
|
|
|
|
|
|
file.Size = 1
|
|
|
|
|
file.Name = "1"
|
|
|
|
|
ctx = context.WithValue(context.Background(), fsctx.FileHeaderCtx, file)
|
|
|
|
|
asserts.Error(HookValidateFile(ctx, &fs))
|
|
|
|
|
asserts.Error(HookValidateFile(ctx, &fs, file))
|
|
|
|
|
|
|
|
|
|
file.Name = "1.txt"
|
|
|
|
|
ctx = context.WithValue(context.Background(), fsctx.FileHeaderCtx, file)
|
|
|
|
|
asserts.NoError(HookValidateFile(ctx, &fs))
|
|
|
|
|
asserts.NoError(HookValidateFile(ctx, &fs, file))
|
|
|
|
|
|
|
|
|
|
file.Name = "1.t/xt"
|
|
|
|
|
ctx = context.WithValue(context.Background(), fsctx.FileHeaderCtx, file)
|
|
|
|
|
asserts.Error(HookValidateFile(ctx, &fs))
|
|
|
|
|
asserts.Error(HookValidateFile(ctx, &fs, file))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGenericAfterUploadCanceled(t *testing.T) {
|
|
|
|
|
asserts := assert.New(t)
|
|
|
|
|
f, err := os.Create("TestGenericAfterUploadCanceled")
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
f.Close()
|
|
|
|
|
file := fsctx.FileStream{
|
|
|
|
|
Size: 5,
|
|
|
|
|
Name: "TestGenericAfterUploadCanceled",
|
|
|
|
|
file := &fsctx.FileStream{
|
|
|
|
|
Size: 5,
|
|
|
|
|
Name: "TestGenericAfterUploadCanceled",
|
|
|
|
|
SavePath: "TestGenericAfterUploadCanceled",
|
|
|
|
|
}
|
|
|
|
|
ctx := context.WithValue(context.Background(), fsctx.SavePathCtx, "TestGenericAfterUploadCanceled")
|
|
|
|
|
ctx = context.WithValue(ctx, fsctx.FileHeaderCtx, file)
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
fs := FileSystem{
|
|
|
|
|
User: &model.User{Storage: 5},
|
|
|
|
|
Handler: local.Driver{},
|
|
|
|
|
User: &model.User{},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 成功
|
|
|
|
|
err = HookDeleteTempFile(ctx, &fs)
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
err = HookGiveBackCapacity(ctx, &fs)
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
asserts.Equal(uint64(0), fs.User.Storage)
|
|
|
|
|
|
|
|
|
|
f, err = os.Create("TestGenericAfterUploadCanceled")
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
f.Close()
|
|
|
|
|
{
|
|
|
|
|
mockHandler := &FileHeaderMock{}
|
|
|
|
|
fs.Handler = mockHandler
|
|
|
|
|
mockHandler.On("Delete", testMock.Anything, testMock.Anything).Return([]string{}, nil)
|
|
|
|
|
err := HookDeleteTempFile(ctx, &fs, file)
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
mockHandler.AssertExpectations(t)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 容量不能再降低
|
|
|
|
|
err = HookGiveBackCapacity(ctx, &fs)
|
|
|
|
|
asserts.Error(err)
|
|
|
|
|
// 失败
|
|
|
|
|
{
|
|
|
|
|
mockHandler := &FileHeaderMock{}
|
|
|
|
|
fs.Handler = mockHandler
|
|
|
|
|
mockHandler.On("Delete", testMock.Anything, testMock.Anything).Return([]string{}, errors.New(""))
|
|
|
|
|
err := HookDeleteTempFile(ctx, &fs, file)
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
mockHandler.AssertExpectations(t)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//文件不存在
|
|
|
|
|
fs.User.Storage = 5
|
|
|
|
|
err = HookDeleteTempFile(ctx, &fs)
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGenericAfterUpload(t *testing.T) {
|
|
|
|
@ -108,13 +100,14 @@ func TestGenericAfterUpload(t *testing.T) {
|
|
|
|
|
ID: 1,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Policy: &model.Policy{},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx := context.WithValue(context.Background(), fsctx.FileHeaderCtx, fsctx.FileStream{
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
file := &fsctx.FileStream{
|
|
|
|
|
VirtualPath: "/我的文件",
|
|
|
|
|
Name: "test.txt",
|
|
|
|
|
})
|
|
|
|
|
ctx = context.WithValue(ctx, fsctx.SavePathCtx, "")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 正常
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)").
|
|
|
|
@ -127,9 +120,10 @@ func TestGenericAfterUpload(t *testing.T) {
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnError(errors.New("not found"))
|
|
|
|
|
mock.ExpectBegin()
|
|
|
|
|
mock.ExpectExec("INSERT(.+)files(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
|
mock.ExpectExec("UPDATE(.+)storage(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
|
mock.ExpectCommit()
|
|
|
|
|
|
|
|
|
|
err := GenericAfterUpload(ctx, &fs)
|
|
|
|
|
err := GenericAfterUpload(ctx, &fs, file)
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
|
|
|
|
|
@ -137,7 +131,7 @@ func TestGenericAfterUpload(t *testing.T) {
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)folders(.+)").WillReturnRows(
|
|
|
|
|
mock.NewRows([]string{"name"}),
|
|
|
|
|
)
|
|
|
|
|
err = GenericAfterUpload(ctx, &fs)
|
|
|
|
|
err = GenericAfterUpload(ctx, &fs, file)
|
|
|
|
|
asserts.Equal(ErrRootProtected, err)
|
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
|
|
|
|
|
@ -152,10 +146,25 @@ func TestGenericAfterUpload(t *testing.T) {
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnRows(
|
|
|
|
|
mock.NewRows([]string{"name"}).AddRow("test.txt"),
|
|
|
|
|
)
|
|
|
|
|
err = GenericAfterUpload(ctx, &fs)
|
|
|
|
|
err = GenericAfterUpload(ctx, &fs, file)
|
|
|
|
|
asserts.Equal(ErrFileExisted, err)
|
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
|
|
|
|
|
|
// 文件已存在, 且为上传占位符
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)").
|
|
|
|
|
WithArgs(1).
|
|
|
|
|
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
|
|
|
|
// 1
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)").
|
|
|
|
|
WithArgs(1, 1, "我的文件").
|
|
|
|
|
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnRows(
|
|
|
|
|
mock.NewRows([]string{"name", "upload_session_id"}).AddRow("test.txt", "1"),
|
|
|
|
|
)
|
|
|
|
|
err = GenericAfterUpload(ctx, &fs, file)
|
|
|
|
|
asserts.Equal(ErrFileUploadSessionExisted, err)
|
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
|
|
|
|
|
|
// 插入失败
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)").
|
|
|
|
|
WithArgs(1).
|
|
|
|
@ -170,7 +179,7 @@ func TestGenericAfterUpload(t *testing.T) {
|
|
|
|
|
mock.ExpectExec("INSERT(.+)files(.+)").WillReturnError(errors.New("error"))
|
|
|
|
|
mock.ExpectRollback()
|
|
|
|
|
|
|
|
|
|
err = GenericAfterUpload(ctx, &fs)
|
|
|
|
|
err = GenericAfterUpload(ctx, &fs, file)
|
|
|
|
|
asserts.Equal(ErrInsertFileRecord, err)
|
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
|
|
|
|
|
@ -180,7 +189,7 @@ func TestFileSystem_Use(t *testing.T) {
|
|
|
|
|
asserts := assert.New(t)
|
|
|
|
|
fs := FileSystem{}
|
|
|
|
|
|
|
|
|
|
hook := func(ctx context.Context, fs *FileSystem) error {
|
|
|
|
|
hook := func(ctx context.Context, fs *FileSystem, fileHeader fsctx.FileHeader) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -215,77 +224,79 @@ func TestFileSystem_Trigger(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
|
|
hook := func(ctx context.Context, fs *FileSystem) error {
|
|
|
|
|
hook := func(ctx context.Context, fs *FileSystem, fileHeader fsctx.FileHeader) error {
|
|
|
|
|
fs.User.Storage++
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 一个
|
|
|
|
|
fs.Use("BeforeUpload", hook)
|
|
|
|
|
err := fs.Trigger(ctx, "BeforeUpload")
|
|
|
|
|
err := fs.Trigger(ctx, "BeforeUpload", nil)
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
asserts.Equal(uint64(1), fs.User.Storage)
|
|
|
|
|
|
|
|
|
|
// 多个
|
|
|
|
|
fs.Use("BeforeUpload", hook)
|
|
|
|
|
fs.Use("BeforeUpload", hook)
|
|
|
|
|
err = fs.Trigger(ctx, "BeforeUpload")
|
|
|
|
|
err = fs.Trigger(ctx, "BeforeUpload", nil)
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
asserts.Equal(uint64(4), fs.User.Storage)
|
|
|
|
|
|
|
|
|
|
// 多个,有失败
|
|
|
|
|
fs.Use("BeforeUpload", func(ctx context.Context, fs *FileSystem, file fsctx.FileHeader) error {
|
|
|
|
|
return errors.New("error")
|
|
|
|
|
})
|
|
|
|
|
fs.Use("BeforeUpload", func(ctx context.Context, fs *FileSystem, file fsctx.FileHeader) error {
|
|
|
|
|
asserts.Fail("following hooks executed")
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
err = fs.Trigger(ctx, "BeforeUpload", nil)
|
|
|
|
|
asserts.Error(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestHookIsFileExist(t *testing.T) {
|
|
|
|
|
func TestHookValidateCapacity(t *testing.T) {
|
|
|
|
|
asserts := assert.New(t)
|
|
|
|
|
cache.Set("pack_size_1", uint64(0), 0)
|
|
|
|
|
fs := &FileSystem{User: &model.User{
|
|
|
|
|
Model: gorm.Model{
|
|
|
|
|
ID: 1,
|
|
|
|
|
Model: gorm.Model{ID: 1},
|
|
|
|
|
Storage: 0,
|
|
|
|
|
Group: model.Group{
|
|
|
|
|
MaxStorage: 11,
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
ctx := context.WithValue(context.Background(), fsctx.PathCtx, "/test.txt")
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
file := &fsctx.FileStream{Size: 11}
|
|
|
|
|
{
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)").
|
|
|
|
|
WithArgs(1).
|
|
|
|
|
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "test.txt").WillReturnRows(
|
|
|
|
|
sqlmock.NewRows([]string{"Name"}).AddRow("s"),
|
|
|
|
|
)
|
|
|
|
|
err := HookIsFileExist(ctx, fs)
|
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
|
err := HookValidateCapacity(ctx, fs, file)
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)").
|
|
|
|
|
WithArgs(1).
|
|
|
|
|
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "test.txt").WillReturnRows(
|
|
|
|
|
sqlmock.NewRows([]string{"Name"}),
|
|
|
|
|
)
|
|
|
|
|
err := HookIsFileExist(ctx, fs)
|
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
|
file.Size = 12
|
|
|
|
|
err := HookValidateCapacity(ctx, fs, file)
|
|
|
|
|
asserts.Error(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestHookValidateCapacity(t *testing.T) {
|
|
|
|
|
asserts := assert.New(t)
|
|
|
|
|
cache.Set("pack_size_1", uint64(0), 0)
|
|
|
|
|
func TestHookValidateCapacityDiff(t *testing.T) {
|
|
|
|
|
a := assert.New(t)
|
|
|
|
|
fs := &FileSystem{User: &model.User{
|
|
|
|
|
Model: gorm.Model{ID: 1},
|
|
|
|
|
Storage: 0,
|
|
|
|
|
Group: model.Group{
|
|
|
|
|
MaxStorage: 11,
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
ctx := context.WithValue(context.Background(), fsctx.FileHeaderCtx, fsctx.FileStream{Size: 10})
|
|
|
|
|
file := model.File{Size: 10}
|
|
|
|
|
ctx := context.WithValue(context.Background(), fsctx.FileModelCtx, file)
|
|
|
|
|
|
|
|
|
|
// 无需操作
|
|
|
|
|
{
|
|
|
|
|
err := HookValidateCapacity(ctx, fs)
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
a.NoError(HookValidateCapacityDiff(ctx, fs, &fsctx.FileStream{Size: 10}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 需要验证
|
|
|
|
|
{
|
|
|
|
|
err := HookValidateCapacity(ctx, fs)
|
|
|
|
|
asserts.Error(err)
|
|
|
|
|
a.Error(HookValidateCapacityDiff(ctx, fs, &fsctx.FileStream{Size: 12}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestHookResetPolicy(t *testing.T) {
|
|
|
|
@ -301,7 +312,7 @@ func TestHookResetPolicy(t *testing.T) {
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)policies(.+)").
|
|
|
|
|
WillReturnRows(sqlmock.NewRows([]string{"id", "type"}).AddRow(2, "local"))
|
|
|
|
|
ctx := context.WithValue(context.Background(), fsctx.FileModelCtx, file)
|
|
|
|
|
err := HookResetPolicy(ctx, fs)
|
|
|
|
|
err := HookResetPolicy(ctx, fs, nil)
|
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
}
|
|
|
|
@ -310,76 +321,22 @@ func TestHookResetPolicy(t *testing.T) {
|
|
|
|
|
{
|
|
|
|
|
cache.Deletes([]string{"2"}, "policy_")
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
err := HookResetPolicy(ctx, fs)
|
|
|
|
|
err := HookResetPolicy(ctx, fs, nil)
|
|
|
|
|
asserts.Error(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestHookChangeCapacity(t *testing.T) {
|
|
|
|
|
asserts := assert.New(t)
|
|
|
|
|
cache.Set("pack_size_1", uint64(0), 0)
|
|
|
|
|
|
|
|
|
|
// 容量增加 失败
|
|
|
|
|
{
|
|
|
|
|
fs := &FileSystem{User: &model.User{
|
|
|
|
|
Model: gorm.Model{ID: 1},
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
newFile := fsctx.FileStream{Size: 10}
|
|
|
|
|
oldFile := model.File{Size: 9}
|
|
|
|
|
ctx := context.WithValue(context.Background(), fsctx.FileModelCtx, oldFile)
|
|
|
|
|
ctx = context.WithValue(ctx, fsctx.FileHeaderCtx, newFile)
|
|
|
|
|
err := HookChangeCapacity(ctx, fs)
|
|
|
|
|
asserts.Equal(ErrInsufficientCapacity, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 容量增加 成功
|
|
|
|
|
{
|
|
|
|
|
fs := &FileSystem{User: &model.User{
|
|
|
|
|
Model: gorm.Model{ID: 1},
|
|
|
|
|
Group: model.Group{MaxStorage: 1},
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
newFile := fsctx.FileStream{Size: 10}
|
|
|
|
|
oldFile := model.File{Size: 9}
|
|
|
|
|
ctx := context.WithValue(context.Background(), fsctx.FileModelCtx, oldFile)
|
|
|
|
|
ctx = context.WithValue(ctx, fsctx.FileHeaderCtx, newFile)
|
|
|
|
|
mock.ExpectBegin()
|
|
|
|
|
mock.ExpectExec("UPDATE(.+)").WithArgs(1, sqlmock.AnyArg(), 1).WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
|
err := HookChangeCapacity(ctx, fs)
|
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
asserts.Equal(uint64(1), fs.User.Storage)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 容量减少
|
|
|
|
|
{
|
|
|
|
|
fs := &FileSystem{User: &model.User{
|
|
|
|
|
Model: gorm.Model{ID: 1},
|
|
|
|
|
Storage: 1,
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
newFile := fsctx.FileStream{Size: 9}
|
|
|
|
|
oldFile := model.File{Size: 10}
|
|
|
|
|
ctx := context.WithValue(context.Background(), fsctx.FileModelCtx, oldFile)
|
|
|
|
|
ctx = context.WithValue(ctx, fsctx.FileHeaderCtx, newFile)
|
|
|
|
|
err := HookChangeCapacity(ctx, fs)
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
asserts.Equal(uint64(0), fs.User.Storage)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestHookCleanFileContent(t *testing.T) {
|
|
|
|
|
asserts := assert.New(t)
|
|
|
|
|
fs := &FileSystem{User: &model.User{
|
|
|
|
|
Model: gorm.Model{ID: 1},
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
ctx := context.WithValue(context.Background(), fsctx.SavePathCtx, "123/123")
|
|
|
|
|
file := &fsctx.FileStream{SavePath: "123/123"}
|
|
|
|
|
handlerMock := FileHeaderMock{}
|
|
|
|
|
handlerMock.On("Put", testMock.Anything, testMock.Anything, "123/123").Return(errors.New("error"))
|
|
|
|
|
handlerMock.On("Put", testMock.Anything, testMock.Anything).Return(errors.New("error"))
|
|
|
|
|
fs.Handler = handlerMock
|
|
|
|
|
err := HookCleanFileContent(ctx, fs)
|
|
|
|
|
err := HookCleanFileContent(context.Background(), fs, file)
|
|
|
|
|
asserts.Error(err)
|
|
|
|
|
handlerMock.AssertExpectations(t)
|
|
|
|
|
}
|
|
|
|
@ -395,14 +352,17 @@ func TestHookClearFileSize(t *testing.T) {
|
|
|
|
|
ctx := context.WithValue(
|
|
|
|
|
context.Background(),
|
|
|
|
|
fsctx.FileModelCtx,
|
|
|
|
|
model.File{Model: gorm.Model{ID: 1}},
|
|
|
|
|
model.File{Model: gorm.Model{ID: 1}, Size: 10},
|
|
|
|
|
)
|
|
|
|
|
mock.ExpectBegin()
|
|
|
|
|
mock.ExpectExec("UPDATE(.+)").
|
|
|
|
|
WithArgs(0, sqlmock.AnyArg(), 1).
|
|
|
|
|
mock.ExpectExec("UPDATE(.+)files(.+)").
|
|
|
|
|
WithArgs(0, sqlmock.AnyArg(), 1, 10).
|
|
|
|
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
|
mock.ExpectExec("UPDATE(.+)users(.+)").
|
|
|
|
|
WithArgs(10, sqlmock.AnyArg()).
|
|
|
|
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
|
mock.ExpectCommit()
|
|
|
|
|
err := HookClearFileSize(ctx, fs)
|
|
|
|
|
err := HookClearFileSize(ctx, fs, nil)
|
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
}
|
|
|
|
@ -410,7 +370,7 @@ func TestHookClearFileSize(t *testing.T) {
|
|
|
|
|
// 上下文对象不存在
|
|
|
|
|
{
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
err := HookClearFileSize(ctx, fs)
|
|
|
|
|
err := HookClearFileSize(ctx, fs, nil)
|
|
|
|
|
asserts.Error(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -432,7 +392,7 @@ func TestHookUpdateSourceName(t *testing.T) {
|
|
|
|
|
mock.ExpectBegin()
|
|
|
|
|
mock.ExpectExec("UPDATE(.+)").WithArgs("new.txt", sqlmock.AnyArg(), 1).WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
|
mock.ExpectCommit()
|
|
|
|
|
err := HookUpdateSourceName(ctx, fs)
|
|
|
|
|
err := HookUpdateSourceName(ctx, fs, nil)
|
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
}
|
|
|
|
@ -440,7 +400,7 @@ func TestHookUpdateSourceName(t *testing.T) {
|
|
|
|
|
// 上下文错误
|
|
|
|
|
{
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
err := HookUpdateSourceName(ctx, fs)
|
|
|
|
|
err := HookUpdateSourceName(ctx, fs, nil)
|
|
|
|
|
asserts.Error(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -457,41 +417,32 @@ func TestGenericAfterUpdate(t *testing.T) {
|
|
|
|
|
Model: gorm.Model{ID: 1},
|
|
|
|
|
PicInfo: "1,1",
|
|
|
|
|
}
|
|
|
|
|
newFile := fsctx.FileStream{Size: 10}
|
|
|
|
|
newFile := &fsctx.FileStream{Size: 10}
|
|
|
|
|
ctx := context.WithValue(context.Background(), fsctx.FileModelCtx, originFile)
|
|
|
|
|
ctx = context.WithValue(ctx, fsctx.FileHeaderCtx, newFile)
|
|
|
|
|
|
|
|
|
|
handlerMock := FileHeaderMock{}
|
|
|
|
|
handlerMock.On("Delete", testMock.Anything, []string{"._thumb"}).Return([]string{}, nil)
|
|
|
|
|
fs.Handler = handlerMock
|
|
|
|
|
mock.ExpectBegin()
|
|
|
|
|
mock.ExpectExec("UPDATE(.+)").
|
|
|
|
|
WithArgs(10, sqlmock.AnyArg(), 1).
|
|
|
|
|
mock.ExpectExec("UPDATE(.+)files(.+)").
|
|
|
|
|
WithArgs(10, sqlmock.AnyArg(), 1, 0).
|
|
|
|
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
|
mock.ExpectExec("UPDATE(.+)users(.+)").
|
|
|
|
|
WithArgs(10, sqlmock.AnyArg()).
|
|
|
|
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
|
mock.ExpectCommit()
|
|
|
|
|
|
|
|
|
|
err := GenericAfterUpdate(ctx, fs)
|
|
|
|
|
err := GenericAfterUpdate(ctx, fs, newFile)
|
|
|
|
|
|
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 新文件上下文不存在
|
|
|
|
|
{
|
|
|
|
|
originFile := model.File{
|
|
|
|
|
Model: gorm.Model{ID: 1},
|
|
|
|
|
PicInfo: "1,1",
|
|
|
|
|
}
|
|
|
|
|
ctx := context.WithValue(context.Background(), fsctx.FileModelCtx, originFile)
|
|
|
|
|
err := GenericAfterUpdate(ctx, fs)
|
|
|
|
|
asserts.Error(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 原始文件上下文不存在
|
|
|
|
|
{
|
|
|
|
|
newFile := fsctx.FileStream{Size: 10}
|
|
|
|
|
ctx := context.WithValue(context.Background(), fsctx.FileHeaderCtx, newFile)
|
|
|
|
|
err := GenericAfterUpdate(ctx, fs)
|
|
|
|
|
newFile := &fsctx.FileStream{Size: 10}
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
err := GenericAfterUpdate(ctx, fs, newFile)
|
|
|
|
|
asserts.Error(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -502,91 +453,41 @@ func TestGenericAfterUpdate(t *testing.T) {
|
|
|
|
|
Model: gorm.Model{ID: 1},
|
|
|
|
|
PicInfo: "1,1",
|
|
|
|
|
}
|
|
|
|
|
newFile := fsctx.FileStream{Size: 10}
|
|
|
|
|
newFile := &fsctx.FileStream{Size: 10}
|
|
|
|
|
ctx := context.WithValue(context.Background(), fsctx.FileModelCtx, originFile)
|
|
|
|
|
ctx = context.WithValue(ctx, fsctx.FileHeaderCtx, newFile)
|
|
|
|
|
|
|
|
|
|
mock.ExpectBegin()
|
|
|
|
|
mock.ExpectExec("UPDATE(.+)").
|
|
|
|
|
WithArgs(10, sqlmock.AnyArg(), 1).
|
|
|
|
|
WithArgs(10, sqlmock.AnyArg(), 1, 0).
|
|
|
|
|
WillReturnError(errors.New("error"))
|
|
|
|
|
mock.ExpectRollback()
|
|
|
|
|
|
|
|
|
|
err := GenericAfterUpdate(ctx, fs)
|
|
|
|
|
err := GenericAfterUpdate(ctx, fs, newFile)
|
|
|
|
|
|
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
|
asserts.Error(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestHookSlaveUploadValidate(t *testing.T) {
|
|
|
|
|
asserts := assert.New(t)
|
|
|
|
|
conf.SystemConfig.Mode = "slave"
|
|
|
|
|
fs, err := NewAnonymousFileSystem()
|
|
|
|
|
conf.SystemConfig.Mode = "master"
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
|
|
|
|
|
// 正常
|
|
|
|
|
{
|
|
|
|
|
policy := serializer.UploadPolicy{
|
|
|
|
|
SavePath: "",
|
|
|
|
|
MaxSize: 10,
|
|
|
|
|
AllowedExtension: nil,
|
|
|
|
|
}
|
|
|
|
|
file := fsctx.FileStream{Name: "1.txt", Size: 10}
|
|
|
|
|
ctx := context.WithValue(context.Background(), fsctx.UploadPolicyCtx, policy)
|
|
|
|
|
ctx = context.WithValue(ctx, fsctx.FileHeaderCtx, file)
|
|
|
|
|
asserts.NoError(HookSlaveUploadValidate(ctx, fs))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 尺寸太大
|
|
|
|
|
{
|
|
|
|
|
policy := serializer.UploadPolicy{
|
|
|
|
|
SavePath: "",
|
|
|
|
|
MaxSize: 10,
|
|
|
|
|
AllowedExtension: nil,
|
|
|
|
|
}
|
|
|
|
|
file := fsctx.FileStream{Name: "1.txt", Size: 11}
|
|
|
|
|
ctx := context.WithValue(context.Background(), fsctx.UploadPolicyCtx, policy)
|
|
|
|
|
ctx = context.WithValue(ctx, fsctx.FileHeaderCtx, file)
|
|
|
|
|
asserts.Equal(ErrFileSizeTooBig, HookSlaveUploadValidate(ctx, fs))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 文件名非法
|
|
|
|
|
{
|
|
|
|
|
policy := serializer.UploadPolicy{
|
|
|
|
|
SavePath: "",
|
|
|
|
|
MaxSize: 10,
|
|
|
|
|
AllowedExtension: nil,
|
|
|
|
|
}
|
|
|
|
|
file := fsctx.FileStream{Name: "/1.txt", Size: 10}
|
|
|
|
|
ctx := context.WithValue(context.Background(), fsctx.UploadPolicyCtx, policy)
|
|
|
|
|
ctx = context.WithValue(ctx, fsctx.FileHeaderCtx, file)
|
|
|
|
|
asserts.Equal(ErrIllegalObjectName, HookSlaveUploadValidate(ctx, fs))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 扩展名非法
|
|
|
|
|
{
|
|
|
|
|
policy := serializer.UploadPolicy{
|
|
|
|
|
SavePath: "",
|
|
|
|
|
MaxSize: 10,
|
|
|
|
|
AllowedExtension: []string{"jpg"},
|
|
|
|
|
}
|
|
|
|
|
file := fsctx.FileStream{Name: "1.txt", Size: 10}
|
|
|
|
|
ctx := context.WithValue(context.Background(), fsctx.UploadPolicyCtx, policy)
|
|
|
|
|
ctx = context.WithValue(ctx, fsctx.FileHeaderCtx, file)
|
|
|
|
|
asserts.Equal(ErrFileExtensionNotAllowed, HookSlaveUploadValidate(ctx, fs))
|
|
|
|
|
func TestHookGenerateThumb(t *testing.T) {
|
|
|
|
|
a := assert.New(t)
|
|
|
|
|
mockHandler := &FileHeaderMock{}
|
|
|
|
|
fs := &FileSystem{
|
|
|
|
|
User: &model.User{
|
|
|
|
|
Model: gorm.Model{ID: 1},
|
|
|
|
|
},
|
|
|
|
|
Handler: mockHandler,
|
|
|
|
|
Policy: &model.Policy{Type: "local"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ClientMock struct {
|
|
|
|
|
testMock.Mock
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m ClientMock) Request(method, target string, body io.Reader, opts ...request.Option) *request.Response {
|
|
|
|
|
args := m.Called(method, target, body, opts)
|
|
|
|
|
return args.Get(0).(*request.Response)
|
|
|
|
|
mockHandler.On("Delete", testMock.Anything, []string{"1.txt._thumb"}).Return([]string{}, nil)
|
|
|
|
|
a.NoError(HookGenerateThumb(context.Background(), fs, &fsctx.FileStream{
|
|
|
|
|
Model: &model.File{
|
|
|
|
|
SourceName: "1.txt",
|
|
|
|
|
},
|
|
|
|
|
}))
|
|
|
|
|
fs.Recycle()
|
|
|
|
|
mockHandler.AssertExpectations(t)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSlaveAfterUpload(t *testing.T) {
|
|
|
|
@ -598,7 +499,7 @@ func TestSlaveAfterUpload(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
// 成功
|
|
|
|
|
{
|
|
|
|
|
clientMock := ClientMock{}
|
|
|
|
|
clientMock := requestmock.RequestMock{}
|
|
|
|
|
clientMock.On(
|
|
|
|
|
"Request",
|
|
|
|
|
"POST",
|
|
|
|
@ -613,19 +514,28 @@ func TestSlaveAfterUpload(t *testing.T) {
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
request.GeneralClient = clientMock
|
|
|
|
|
ctx := context.WithValue(context.Background(), fsctx.FileHeaderCtx, fsctx.FileStream{
|
|
|
|
|
file := &fsctx.FileStream{
|
|
|
|
|
Size: 10,
|
|
|
|
|
VirtualPath: "/my",
|
|
|
|
|
Name: "test.txt",
|
|
|
|
|
})
|
|
|
|
|
ctx = context.WithValue(ctx, fsctx.UploadPolicyCtx, serializer.UploadPolicy{
|
|
|
|
|
CallbackURL: "http://test/callbakc",
|
|
|
|
|
})
|
|
|
|
|
ctx = context.WithValue(ctx, fsctx.SavePathCtx, "/not_exist")
|
|
|
|
|
err := SlaveAfterUpload(ctx, fs)
|
|
|
|
|
SavePath: "/not_exist",
|
|
|
|
|
}
|
|
|
|
|
err := SlaveAfterUpload(&serializer.UploadSession{Callback: "http://test/callbakc"})(context.Background(), fs, file)
|
|
|
|
|
clientMock.AssertExpectations(t)
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 跳过回调
|
|
|
|
|
{
|
|
|
|
|
file := &fsctx.FileStream{
|
|
|
|
|
Size: 10,
|
|
|
|
|
VirtualPath: "/my",
|
|
|
|
|
Name: "test.txt",
|
|
|
|
|
SavePath: "/not_exist",
|
|
|
|
|
}
|
|
|
|
|
err := SlaveAfterUpload(&serializer.UploadSession{})(context.Background(), fs, file)
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestFileSystem_CleanHooks(t *testing.T) {
|
|
|
|
@ -663,7 +573,7 @@ func TestHookCancelContext(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
// empty ctx
|
|
|
|
|
{
|
|
|
|
|
asserts.NoError(HookCancelContext(ctx, fs))
|
|
|
|
|
asserts.NoError(HookCancelContext(ctx, fs, nil))
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
t.Errorf("Channel should not be closed")
|
|
|
|
@ -675,62 +585,99 @@ func TestHookCancelContext(t *testing.T) {
|
|
|
|
|
// with cancel ctx
|
|
|
|
|
{
|
|
|
|
|
ctx = context.WithValue(ctx, fsctx.CancelFuncCtx, cancel)
|
|
|
|
|
asserts.NoError(HookCancelContext(ctx, fs))
|
|
|
|
|
asserts.NoError(HookCancelContext(ctx, fs, nil))
|
|
|
|
|
_, ok := <-ctx.Done()
|
|
|
|
|
asserts.False(ok)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestHookGiveBackCapacity(t *testing.T) {
|
|
|
|
|
asserts := assert.New(t)
|
|
|
|
|
fs := &FileSystem{
|
|
|
|
|
User: &model.User{
|
|
|
|
|
Model: gorm.Model{ID: 1},
|
|
|
|
|
Storage: 10,
|
|
|
|
|
func TestHookClearFileHeaderSize(t *testing.T) {
|
|
|
|
|
a := assert.New(t)
|
|
|
|
|
fs := &FileSystem{}
|
|
|
|
|
file := &fsctx.FileStream{Size: 10}
|
|
|
|
|
a.NoError(HookClearFileHeaderSize(context.Background(), fs, file))
|
|
|
|
|
a.EqualValues(0, file.Size)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestHookTruncateFileTo(t *testing.T) {
|
|
|
|
|
a := assert.New(t)
|
|
|
|
|
fs := &FileSystem{}
|
|
|
|
|
file := &fsctx.FileStream{}
|
|
|
|
|
a.NoError(HookTruncateFileTo(0)(context.Background(), fs, file))
|
|
|
|
|
|
|
|
|
|
fs.Handler = local.Driver{}
|
|
|
|
|
a.Error(HookTruncateFileTo(0)(context.Background(), fs, file))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestHookChunkUploaded(t *testing.T) {
|
|
|
|
|
a := assert.New(t)
|
|
|
|
|
fs := &FileSystem{}
|
|
|
|
|
file := &fsctx.FileStream{
|
|
|
|
|
AppendStart: 10,
|
|
|
|
|
Size: 10,
|
|
|
|
|
Model: &model.File{
|
|
|
|
|
Model: gorm.Model{ID: 1},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
ctx := context.WithValue(context.Background(), fsctx.FileHeaderCtx, fsctx.FileStream{Size: 1})
|
|
|
|
|
|
|
|
|
|
// without once limit
|
|
|
|
|
{
|
|
|
|
|
asserts.NoError(HookGiveBackCapacity(ctx, fs))
|
|
|
|
|
asserts.EqualValues(9, fs.User.Storage)
|
|
|
|
|
asserts.NoError(HookGiveBackCapacity(ctx, fs))
|
|
|
|
|
asserts.EqualValues(8, fs.User.Storage)
|
|
|
|
|
}
|
|
|
|
|
mock.ExpectBegin()
|
|
|
|
|
mock.ExpectExec("UPDATE(.+)files(.+)").WithArgs(20, sqlmock.AnyArg(), 1, 0).WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
|
mock.ExpectExec("UPDATE(.+)users(.+)").
|
|
|
|
|
WithArgs(20, sqlmock.AnyArg()).
|
|
|
|
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
|
mock.ExpectCommit()
|
|
|
|
|
a.NoError(HookChunkUploaded(context.Background(), fs, file))
|
|
|
|
|
a.NoError(mock.ExpectationsWereMet())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// with once limit
|
|
|
|
|
{
|
|
|
|
|
ctx = context.WithValue(ctx, fsctx.ValidateCapacityOnceCtx, &sync.Once{})
|
|
|
|
|
asserts.NoError(HookGiveBackCapacity(ctx, fs))
|
|
|
|
|
asserts.EqualValues(7, fs.User.Storage)
|
|
|
|
|
asserts.NoError(HookGiveBackCapacity(ctx, fs))
|
|
|
|
|
asserts.EqualValues(7, fs.User.Storage)
|
|
|
|
|
func TestHookChunkUploadFailed(t *testing.T) {
|
|
|
|
|
a := assert.New(t)
|
|
|
|
|
fs := &FileSystem{}
|
|
|
|
|
file := &fsctx.FileStream{
|
|
|
|
|
AppendStart: 10,
|
|
|
|
|
Size: 10,
|
|
|
|
|
Model: &model.File{
|
|
|
|
|
Model: gorm.Model{ID: 1},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mock.ExpectBegin()
|
|
|
|
|
mock.ExpectExec("UPDATE(.+)files(.+)").WithArgs(10, sqlmock.AnyArg(), 1, 0).WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
|
mock.ExpectExec("UPDATE(.+)users(.+)").
|
|
|
|
|
WithArgs(10, sqlmock.AnyArg()).
|
|
|
|
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
|
mock.ExpectCommit()
|
|
|
|
|
a.NoError(HookChunkUploadFailed(context.Background(), fs, file))
|
|
|
|
|
a.NoError(mock.ExpectationsWereMet())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestHookValidateCapacityWithoutIncrease(t *testing.T) {
|
|
|
|
|
func TestHookPopPlaceholderToFile(t *testing.T) {
|
|
|
|
|
a := assert.New(t)
|
|
|
|
|
fs := &FileSystem{
|
|
|
|
|
User: &model.User{
|
|
|
|
|
Model: gorm.Model{ID: 1},
|
|
|
|
|
Storage: 10,
|
|
|
|
|
Group: model.Group{},
|
|
|
|
|
fs := &FileSystem{}
|
|
|
|
|
file := &fsctx.FileStream{
|
|
|
|
|
Model: &model.File{
|
|
|
|
|
Model: gorm.Model{ID: 1},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
ctx := context.WithValue(context.Background(), fsctx.FileHeaderCtx, fsctx.FileStream{Size: 1})
|
|
|
|
|
|
|
|
|
|
// not enough
|
|
|
|
|
{
|
|
|
|
|
fs.User.Group.MaxStorage = 10
|
|
|
|
|
a.Error(HookValidateCapacity(ctx, fs))
|
|
|
|
|
a.EqualValues(10, fs.User.Storage)
|
|
|
|
|
}
|
|
|
|
|
mock.ExpectBegin()
|
|
|
|
|
mock.ExpectExec("UPDATE(.+)files(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
|
|
|
|
mock.ExpectCommit()
|
|
|
|
|
a.NoError(HookPopPlaceholderToFile("1,1")(context.Background(), fs, file))
|
|
|
|
|
a.NoError(mock.ExpectationsWereMet())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// enough
|
|
|
|
|
{
|
|
|
|
|
fs.User.Group.MaxStorage = 11
|
|
|
|
|
a.NoError(HookValidateCapacity(ctx, fs))
|
|
|
|
|
a.EqualValues(10, fs.User.Storage)
|
|
|
|
|
func TestHookDeleteUploadSession(t *testing.T) {
|
|
|
|
|
a := assert.New(t)
|
|
|
|
|
fs := &FileSystem{}
|
|
|
|
|
file := &fsctx.FileStream{
|
|
|
|
|
Model: &model.File{
|
|
|
|
|
Model: gorm.Model{ID: 1},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cache.Set(UploadSessionCachePrefix+"TestHookDeleteUploadSession", "", 0)
|
|
|
|
|
a.NoError(HookDeleteUploadSession("TestHookDeleteUploadSession")(context.Background(), fs, file))
|
|
|
|
|
_, ok := cache.Get(UploadSessionCachePrefix + "TestHookDeleteUploadSession")
|
|
|
|
|
a.False(ok)
|
|
|
|
|
}
|
|
|
|
|