Cloudreve/models/policy_test.go

280 lines
7.1 KiB
Go
Raw Normal View History

2019-11-14 01:45:06 -05:00
package model
import (
"encoding/json"
2019-11-17 00:50:14 -05:00
"strconv"
2019-11-14 01:45:06 -05:00
"testing"
2019-11-17 00:50:14 -05:00
"time"
"github.com/DATA-DOG/go-sqlmock"
"github.com/cloudreve/Cloudreve/v3/pkg/cache"
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
2019-11-14 01:45:06 -05:00
)
func TestGetPolicyByID(t *testing.T) {
asserts := assert.New(t)
cache.Deletes([]string{"22", "23"}, "policy_")
2019-12-05 04:01:14 -05:00
// 缓存未命中
{
rows := sqlmock.NewRows([]string{"name", "type", "options"}).
AddRow("默认存储策略", "local", "{\"od_redirect\":\"123\"}")
2019-12-05 04:01:14 -05:00
mock.ExpectQuery("^SELECT(.+)").WillReturnRows(rows)
policy, err := GetPolicyByID(uint(22))
asserts.NoError(err)
asserts.NoError(mock.ExpectationsWereMet())
asserts.Equal("默认存储策略", policy.Name)
asserts.Equal("123", policy.OptionsSerialized.OdRedirect)
2019-12-05 04:01:14 -05:00
rows = sqlmock.NewRows([]string{"name", "type", "options"})
mock.ExpectQuery("^SELECT(.+)").WillReturnRows(rows)
policy, err = GetPolicyByID(uint(23))
asserts.NoError(mock.ExpectationsWereMet())
asserts.Error(err)
}
// 命中
{
policy, err := GetPolicyByID(uint(22))
asserts.NoError(err)
asserts.Equal("默认存储策略", policy.Name)
asserts.Equal("123", policy.OptionsSerialized.OdRedirect)
2019-12-05 04:01:14 -05:00
}
2019-11-14 01:45:06 -05:00
}
func TestPolicy_BeforeSave(t *testing.T) {
asserts := assert.New(t)
testPolicy := Policy{
OptionsSerialized: PolicyOption{
OdRedirect: "123",
2019-11-14 01:45:06 -05:00
},
}
expected, _ := json.Marshal(testPolicy.OptionsSerialized)
err := testPolicy.BeforeSave()
asserts.NoError(err)
asserts.Equal(string(expected), testPolicy.Options)
}
2019-11-17 00:50:14 -05:00
func TestPolicy_GeneratePath(t *testing.T) {
asserts := assert.New(t)
testPolicy := Policy{}
testPolicy.DirNameRule = "{randomkey16}"
asserts.Len(testPolicy.GeneratePath(1, "/"), 16)
2019-11-17 00:50:14 -05:00
testPolicy.DirNameRule = "{randomkey8}"
asserts.Len(testPolicy.GeneratePath(1, "/"), 8)
2019-11-17 00:50:14 -05:00
testPolicy.DirNameRule = "{timestamp}"
asserts.Equal(testPolicy.GeneratePath(1, "/"), strconv.FormatInt(time.Now().Unix(), 10))
2019-11-17 00:50:14 -05:00
testPolicy.DirNameRule = "{uid}"
asserts.Equal(testPolicy.GeneratePath(1, "/"), strconv.Itoa(int(1)))
2019-11-17 00:50:14 -05:00
testPolicy.DirNameRule = "{datetime}"
asserts.Len(testPolicy.GeneratePath(1, "/"), 14)
2019-11-17 00:50:14 -05:00
testPolicy.DirNameRule = "{date}"
asserts.Len(testPolicy.GeneratePath(1, "/"), 8)
2019-11-17 00:50:14 -05:00
testPolicy.DirNameRule = "123{date}ss{datetime}"
asserts.Len(testPolicy.GeneratePath(1, "/"), 27)
testPolicy.DirNameRule = "/1/{path}/456"
asserts.Condition(func() (success bool) {
res := testPolicy.GeneratePath(1, "/23")
return res == "/1/23/456" || res == "\\1\\23\\456"
})
2019-11-17 00:50:14 -05:00
}
func TestPolicy_GenerateFileName(t *testing.T) {
asserts := assert.New(t)
2020-01-16 00:36:13 -05:00
// 重命名关闭
{
testPolicy := Policy{
AutoRename: false,
}
testPolicy.FileNameRule = "{randomkey16}"
asserts.Equal("123.txt", testPolicy.GenerateFileName(1, "123.txt"))
testPolicy.Type = "oss"
2022-03-24 07:07:56 -05:00
asserts.Equal("origin", testPolicy.GenerateFileName(1, "origin"))
}
2019-11-17 00:50:14 -05:00
2020-01-16 00:36:13 -05:00
// 重命名开启
{
testPolicy := Policy{
AutoRename: true,
}
2019-11-17 00:50:14 -05:00
2020-01-16 00:36:13 -05:00
testPolicy.FileNameRule = "{randomkey16}"
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 16)
2019-11-17 00:50:14 -05:00
2020-01-16 00:36:13 -05:00
testPolicy.FileNameRule = "{randomkey8}"
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 8)
2019-11-17 00:50:14 -05:00
2020-01-16 00:36:13 -05:00
testPolicy.FileNameRule = "{timestamp}"
asserts.Equal(testPolicy.GenerateFileName(1, "123.txt"), strconv.FormatInt(time.Now().Unix(), 10))
2019-11-17 00:50:14 -05:00
2020-01-16 00:36:13 -05:00
testPolicy.FileNameRule = "{uid}"
asserts.Equal(testPolicy.GenerateFileName(1, "123.txt"), strconv.Itoa(int(1)))
2019-11-17 00:50:14 -05:00
2020-01-16 00:36:13 -05:00
testPolicy.FileNameRule = "{datetime}"
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 14)
2019-11-17 00:50:14 -05:00
2020-01-16 00:36:13 -05:00
testPolicy.FileNameRule = "{date}"
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 8)
2019-11-17 00:50:14 -05:00
2020-01-16 00:36:13 -05:00
testPolicy.FileNameRule = "123{date}ss{datetime}"
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 27)
2019-11-17 00:50:14 -05:00
2020-01-16 00:36:13 -05:00
// 支持{originname}的策略
testPolicy.Type = "local"
testPolicy.FileNameRule = "123{originname}"
asserts.Equal("123123.txt", testPolicy.GenerateFileName(1, "123.txt"))
2019-11-17 00:50:14 -05:00
2020-01-16 00:36:13 -05:00
testPolicy.Type = "qiniu"
testPolicy.FileNameRule = "{uid}123{originname}"
asserts.Equal("1123123.txt", testPolicy.GenerateFileName(1, "123.txt"))
2019-11-17 00:50:14 -05:00
2020-01-16 00:36:13 -05:00
testPolicy.Type = "oss"
testPolicy.FileNameRule = "{uid}123{originname}"
2022-03-24 07:07:56 -05:00
asserts.Equal("1123123321", testPolicy.GenerateFileName(1, "123321"))
2020-01-16 00:36:13 -05:00
testPolicy.Type = "upyun"
testPolicy.FileNameRule = "{uid}123{originname}"
2022-03-24 07:07:56 -05:00
asserts.Equal("1123123321", testPolicy.GenerateFileName(1, "123321"))
2020-03-11 21:12:13 -05:00
testPolicy.Type = "qiniu"
testPolicy.FileNameRule = "{uid}123{originname}"
2022-03-24 07:07:56 -05:00
asserts.Equal("1123123321", testPolicy.GenerateFileName(1, "123321"))
2020-03-11 21:12:13 -05:00
testPolicy.Type = "local"
testPolicy.FileNameRule = "{uid}123{originname}"
asserts.Equal("1123", testPolicy.GenerateFileName(1, ""))
2022-03-24 07:07:56 -05:00
testPolicy.Type = "local"
testPolicy.FileNameRule = "{ext}123{uuid}"
asserts.Contains(testPolicy.GenerateFileName(1, "123.txt"), ".txt123")
2020-01-16 00:36:13 -05:00
}
2020-01-15 03:03:23 -05:00
2019-11-17 00:50:14 -05:00
}
2019-12-31 00:31:20 -05:00
func TestPolicy_IsDirectlyPreview(t *testing.T) {
asserts := assert.New(t)
policy := Policy{Type: "local"}
asserts.True(policy.IsDirectlyPreview())
policy.Type = "remote"
asserts.False(policy.IsDirectlyPreview())
}
func TestPolicy_ClearCache(t *testing.T) {
asserts := assert.New(t)
cache.Set("policy_202", 1, 0)
policy := Policy{Model: gorm.Model{ID: 202}}
policy.ClearCache()
_, ok := cache.Get("policy_202")
asserts.False(ok)
}
func TestPolicy_UpdateAccessKey(t *testing.T) {
asserts := assert.New(t)
policy := Policy{Model: gorm.Model{ID: 202}}
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
policy.AccessKey = "123"
err := policy.SaveAndClearCache()
asserts.NoError(mock.ExpectationsWereMet())
asserts.NoError(err)
}
func TestPolicy_Props(t *testing.T) {
asserts := assert.New(t)
policy := Policy{Type: "onedrive"}
2022-03-24 07:07:56 -05:00
policy.OptionsSerialized.PlaceholderWithSize = true
asserts.False(policy.IsThumbGenerateNeeded())
2022-03-24 07:07:56 -05:00
asserts.False(policy.IsTransitUpload(4))
asserts.False(policy.IsTransitUpload(5 * 1024 * 1024))
asserts.True(policy.CanStructureBeListed())
2022-03-24 07:07:56 -05:00
asserts.True(policy.IsUploadPlaceholderWithSize())
policy.Type = "local"
asserts.True(policy.IsThumbGenerateNeeded())
asserts.False(policy.CanStructureBeListed())
2022-03-24 07:07:56 -05:00
asserts.False(policy.IsUploadPlaceholderWithSize())
policy.Type = "remote"
asserts.True(policy.IsUploadPlaceholderWithSize())
}
func TestPolicy_IsThumbExist(t *testing.T) {
asserts := assert.New(t)
testCases := []struct {
name string
expect bool
policy string
}{
{
"1.png",
false,
"unknown",
},
{
"1.png",
false,
"local",
},
{
"1.png",
true,
"cos",
},
{
"1",
false,
"cos",
},
{
"1.txt.png",
true,
"cos",
},
{
"1.png.txt",
false,
"cos",
},
{
"1",
true,
"onedrive",
},
}
for _, testCase := range testCases {
policy := Policy{Type: testCase.policy}
asserts.Equal(testCase.expect, policy.IsThumbExist(testCase.name))
}
}
2021-11-09 06:29:56 -05:00
func TestPolicy_UpdateAccessKeyAndClearCache(t *testing.T) {
a := assert.New(t)
cache.Set("policy_1331", Policy{}, 3600)
p := &Policy{}
p.ID = 1331
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").WithArgs("ak", sqlmock.AnyArg()).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
a.NoError(p.UpdateAccessKeyAndClearCache("ak"))
a.NoError(mock.ExpectationsWereMet())
_, ok := cache.Get("policy_1331")
a.False(ok)
}