test(thumb): new changes in filesystem pkg

This commit is contained in:
Aaron Liu
2023-04-13 19:39:12 +08:00
parent 408733a974
commit 8e2fc1a8f6
10 changed files with 195 additions and 212 deletions

View File

@@ -4,6 +4,7 @@ import (
"context"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/auth"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
@@ -141,17 +142,34 @@ func TestHandler_Thumb(t *testing.T) {
asserts.NoError(err)
file.Close()
f := &model.File{
SourceName: "TestHandler_Thumb",
MetadataSerialized: map[string]string{
model.ThumbStatusMetadataKey: model.ThumbStatusExist,
},
}
// 正常
{
thumb, err := handler.Thumb(ctx, "TestHandler_Thumb")
thumb, err := handler.Thumb(ctx, f)
asserts.NoError(err)
asserts.NotNil(thumb.Content)
}
// 不存在
// file 不存在
{
_, err := handler.Thumb(ctx, "not_exist")
f.SourceName = "not_exist"
_, err := handler.Thumb(ctx, f)
asserts.Error(err)
asserts.ErrorIs(err, driver.ErrorThumbNotExist)
}
// thumb not exist
{
f.MetadataSerialized[model.ThumbStatusMetadataKey] = model.ThumbStatusNotExist
_, err := handler.Thumb(ctx, f)
asserts.Error(err)
asserts.ErrorIs(err, driver.ErrorThumbNotExist)
}
}

View File

@@ -246,19 +246,19 @@ func TestDriver_Thumb(t *testing.T) {
}
handler.Client, _ = NewClient(&model.Policy{})
handler.Client.Credential.ExpiresIn = time.Now().Add(time.Duration(100) * time.Hour).Unix()
file := &model.File{PicInfo: "1,1", Model: gorm.Model{ID: 1}}
// 失败
{
ctx := context.WithValue(context.Background(), fsctx.ThumbSizeCtx, [2]uint{10, 20})
ctx = context.WithValue(ctx, fsctx.FileModelCtx, model.File{PicInfo: "1,1", Model: gorm.Model{ID: 1}})
res, err := handler.Thumb(ctx, "123.jpg")
res, err := handler.Thumb(ctx, file)
asserts.Error(err)
asserts.Empty(res.URL)
}
// 上下文错误
{
_, err := handler.Thumb(context.Background(), "123.jpg")
_, err := handler.Thumb(context.Background(), file)
asserts.Error(err)
}
}

View File

@@ -3,6 +3,7 @@ package remote
import (
"context"
"errors"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver"
"github.com/cloudreve/Cloudreve/v3/pkg/mocks/remoteclientmock"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"io"
@@ -373,14 +374,33 @@ func TestHandler_Thumb(t *testing.T) {
Type: "remote",
SecretKey: "test",
Server: "http://test.com",
OptionsSerialized: model.PolicyOption{
ThumbExts: []string{"txt"},
},
},
AuthInstance: auth.HMACAuth{},
}
file := &model.File{
Name: "1.txt",
SourceName: "1.txt",
}
ctx := context.Background()
asserts.NoError(cache.Set("setting_preview_timeout", "60", 0))
resp, err := handler.Thumb(ctx, "/1.txt")
asserts.NoError(err)
asserts.True(resp.Redirect)
// no error
{
resp, err := handler.Thumb(ctx, file)
asserts.NoError(err)
asserts.True(resp.Redirect)
}
// ext not support
{
file.Name = "1.jpg"
resp, err := handler.Thumb(ctx, file)
asserts.ErrorIs(err, driver.ErrorThumbNotSupported)
asserts.Nil(resp)
}
}
func TestHandler_Token(t *testing.T) {