2019-11-17 14:33:00 +08:00
|
|
|
package filesystem
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
model "github.com/HFO4/cloudreve/models"
|
|
|
|
"github.com/HFO4/cloudreve/pkg/filesystem/local"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-11-20 17:21:30 +08:00
|
|
|
"os"
|
2019-11-17 14:33:00 +08:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGenericBeforeUpload(t *testing.T) {
|
|
|
|
asserts := assert.New(t)
|
|
|
|
file := local.FileData{
|
|
|
|
Size: 5,
|
|
|
|
Name: "1.txt",
|
|
|
|
}
|
2019-11-19 18:10:18 +08:00
|
|
|
ctx := context.WithValue(context.Background(), FileHeaderCtx, file)
|
2019-11-17 14:33:00 +08:00
|
|
|
fs := FileSystem{
|
|
|
|
User: &model.User{
|
|
|
|
Storage: 0,
|
|
|
|
Group: model.Group{
|
|
|
|
MaxStorage: 11,
|
|
|
|
},
|
|
|
|
Policy: model.Policy{
|
|
|
|
MaxSize: 4,
|
|
|
|
OptionsSerialized: model.PolicyOption{
|
|
|
|
FileType: []string{"txt"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-11-19 18:10:18 +08:00
|
|
|
asserts.Error(GenericBeforeUpload(ctx, &fs))
|
|
|
|
|
2019-11-17 14:33:00 +08:00
|
|
|
file.Size = 1
|
|
|
|
file.Name = "1"
|
2019-11-19 18:10:18 +08:00
|
|
|
ctx = context.WithValue(context.Background(), FileHeaderCtx, file)
|
|
|
|
asserts.Error(GenericBeforeUpload(ctx, &fs))
|
|
|
|
|
2019-11-17 14:33:00 +08:00
|
|
|
file.Name = "1.txt"
|
2019-11-19 18:10:18 +08:00
|
|
|
ctx = context.WithValue(context.Background(), FileHeaderCtx, file)
|
|
|
|
asserts.NoError(GenericBeforeUpload(ctx, &fs))
|
|
|
|
|
2019-11-18 14:06:15 +08:00
|
|
|
file.Name = "1.t/xt"
|
2019-11-19 18:10:18 +08:00
|
|
|
ctx = context.WithValue(context.Background(), FileHeaderCtx, file)
|
|
|
|
asserts.Error(GenericBeforeUpload(ctx, &fs))
|
2019-11-17 14:33:00 +08:00
|
|
|
}
|
2019-11-20 17:21:30 +08:00
|
|
|
|
|
|
|
func TestGenericAfterUploadCanceled(t *testing.T) {
|
|
|
|
asserts := assert.New(t)
|
|
|
|
f, err := os.Create("TestGenericAfterUploadCanceled")
|
|
|
|
asserts.NoError(err)
|
|
|
|
f.Close()
|
|
|
|
file := local.FileStream{
|
|
|
|
Size: 5,
|
|
|
|
Name: "TestGenericAfterUploadCanceled",
|
|
|
|
}
|
|
|
|
ctx := context.WithValue(context.Background(), SavePathCtx, "TestGenericAfterUploadCanceled")
|
|
|
|
ctx = context.WithValue(ctx, FileHeaderCtx, file)
|
|
|
|
fs := FileSystem{
|
|
|
|
User: &model.User{Storage: 5},
|
|
|
|
Handler: local.Handler{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// 成功
|
|
|
|
err = GenericAfterUploadCanceled(ctx, &fs)
|
|
|
|
asserts.NoError(err)
|
|
|
|
asserts.Equal(uint64(0), fs.User.Storage)
|
|
|
|
|
|
|
|
f, err = os.Create("TestGenericAfterUploadCanceled")
|
|
|
|
asserts.NoError(err)
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
// 容量不能再降低
|
|
|
|
err = GenericAfterUploadCanceled(ctx, &fs)
|
|
|
|
asserts.Error(err)
|
|
|
|
|
|
|
|
//文件不存在
|
|
|
|
fs.User.Storage = 5
|
|
|
|
err = GenericAfterUploadCanceled(ctx, &fs)
|
|
|
|
asserts.NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
//func TestGenericAfterUpload(t *testing.T) {
|
|
|
|
// asserts := assert.New(t)
|
|
|
|
// testObj := FileSystem{}
|
|
|
|
// ctx := context.WithValue(context.Background(),FileHeaderCtx,local.FileStream{
|
|
|
|
// VirtualPath: "/我的文件",
|
|
|
|
// Name: "test.txt",
|
|
|
|
// })
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//}
|