You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudreve/models/policy_test.go

234 lines
6.4 KiB

package model
import (
"encoding/json"
"github.com/DATA-DOG/go-sqlmock"
"github.com/HFO4/cloudreve/pkg/cache"
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
"strconv"
"testing"
"time"
)
func TestGetPolicyByID(t *testing.T) {
asserts := assert.New(t)
// 缓存未命中
{
rows := sqlmock.NewRows([]string{"name", "type", "options"}).
AddRow("默认存储策略", "local", "{\"od_redirect\":\"123\"}")
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)
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)
}
}
func TestPolicy_BeforeSave(t *testing.T) {
asserts := assert.New(t)
testPolicy := Policy{
OptionsSerialized: PolicyOption{
OdRedirect: "123",
},
}
expected, _ := json.Marshal(testPolicy.OptionsSerialized)
err := testPolicy.BeforeSave()
asserts.NoError(err)
asserts.Equal(string(expected), testPolicy.Options)
}
func TestPolicy_GeneratePath(t *testing.T) {
asserts := assert.New(t)
testPolicy := Policy{}
testPolicy.DirNameRule = "{randomkey16}"
asserts.Len(testPolicy.GeneratePath(1, "/"), 16)
testPolicy.DirNameRule = "{randomkey8}"
asserts.Len(testPolicy.GeneratePath(1, "/"), 8)
testPolicy.DirNameRule = "{timestamp}"
asserts.Equal(testPolicy.GeneratePath(1, "/"), strconv.FormatInt(time.Now().Unix(), 10))
testPolicy.DirNameRule = "{uid}"
asserts.Equal(testPolicy.GeneratePath(1, "/"), strconv.Itoa(int(1)))
testPolicy.DirNameRule = "{datetime}"
asserts.Len(testPolicy.GeneratePath(1, "/"), 14)
testPolicy.DirNameRule = "{date}"
asserts.Len(testPolicy.GeneratePath(1, "/"), 8)
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"
})
}
func TestPolicy_GenerateFileName(t *testing.T) {
asserts := assert.New(t)
// 重命名关闭
{
testPolicy := Policy{
AutoRename: false,
}
testPolicy.FileNameRule = "{randomkey16}"
asserts.Equal("123.txt", testPolicy.GenerateFileName(1, "123.txt"))
testPolicy.Type = "oss"
asserts.Equal("${filename}", testPolicy.GenerateFileName(1, ""))
}
// 重命名开启
{
testPolicy := Policy{
AutoRename: true,
}
testPolicy.FileNameRule = "{randomkey16}"
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 16)
testPolicy.FileNameRule = "{randomkey8}"
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 8)
testPolicy.FileNameRule = "{timestamp}"
asserts.Equal(testPolicy.GenerateFileName(1, "123.txt"), strconv.FormatInt(time.Now().Unix(), 10))
testPolicy.FileNameRule = "{uid}"
asserts.Equal(testPolicy.GenerateFileName(1, "123.txt"), strconv.Itoa(int(1)))
testPolicy.FileNameRule = "{datetime}"
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 14)
testPolicy.FileNameRule = "{date}"
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 8)
testPolicy.FileNameRule = "123{date}ss{datetime}"
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 27)
// 支持{originname}的策略
testPolicy.Type = "local"
testPolicy.FileNameRule = "123{originname}"
asserts.Equal("123123.txt", testPolicy.GenerateFileName(1, "123.txt"))
testPolicy.Type = "qiniu"
testPolicy.FileNameRule = "{uid}123{originname}"
asserts.Equal("1123123.txt", testPolicy.GenerateFileName(1, "123.txt"))
testPolicy.Type = "oss"
testPolicy.FileNameRule = "{uid}123{originname}"
asserts.Equal("1123${filename}", testPolicy.GenerateFileName(1, ""))
testPolicy.Type = "upyun"
testPolicy.FileNameRule = "{uid}123{originname}"
asserts.Equal("1123{filename}{.suffix}", testPolicy.GenerateFileName(1, ""))
}
}
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_GetUploadURL(t *testing.T) {
asserts := assert.New(t)
// 本地
{
cache.Set("setting_siteURL", "http://127.0.0.1", 0)
policy := Policy{Type: "local", Server: "http://127.0.0.1"}
asserts.Equal("http://127.0.0.1/api/v3/file/upload", policy.GetUploadURL())
}
// 远程
{
policy := Policy{Type: "remote", Server: "http://127.0.0.1"}
asserts.Equal("http://127.0.0.1/api/v3/slave/upload", policy.GetUploadURL())
}
5 years ago
// OSS
{
policy := Policy{Type: "oss", BaseURL: "base", Server: "http://127.0.0.1"}
asserts.Equal("base", policy.GetUploadURL())
}
// 未知
{
policy := Policy{Type: "unknown", Server: "http://127.0.0.1"}
asserts.Equal("http://127.0.0.1", policy.GetUploadURL())
}
}
func TestPolicy_IsPathGenerateNeeded(t *testing.T) {
asserts := assert.New(t)
policy := Policy{Type: "qiniu"}
asserts.True(policy.IsPathGenerateNeeded())
policy.Type = "remote"
asserts.False(policy.IsPathGenerateNeeded())
}
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()
err := policy.UpdateAccessKey("123")
asserts.NoError(mock.ExpectationsWereMet())
asserts.NoError(err)
}
func TestPolicy_Props(t *testing.T) {
asserts := assert.New(t)
policy := Policy{Type: "onedrive"}
asserts.True(policy.IsMockThumbNeeded())
asserts.False(policy.IsThumbGenerateNeeded())
asserts.True(policy.IsPathGenerateNeeded())
asserts.True(policy.IsTransitUpload(4))
asserts.False(policy.IsTransitUpload(5 * 1024 * 1024))
policy.Type = "local"
asserts.False(policy.IsMockThumbNeeded())
asserts.True(policy.IsThumbGenerateNeeded())
asserts.True(policy.IsPathGenerateNeeded())
}