Test: get filesystem from callback context
This commit is contained in:
parent
8672042d61
commit
588514b4e0
3 changed files with 125 additions and 1 deletions
|
@ -212,7 +212,6 @@ func (policy *Policy) GetUploadURL() string {
|
|||
}
|
||||
|
||||
// UpdateAccessKey 更新 AccessKey
|
||||
// TODO 测试
|
||||
func (policy *Policy) UpdateAccessKey(key string) error {
|
||||
policy.AccessKey = key
|
||||
err := DB.Save(policy).Error
|
||||
|
|
|
@ -4,6 +4,7 @@ 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"
|
||||
|
@ -196,3 +197,37 @@ func TestPolicy_IsPathGenerateNeeded(t *testing.T) {
|
|||
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())
|
||||
}
|
||||
|
|
|
@ -3,8 +3,10 @@ package filesystem
|
|||
import (
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/HFO4/cloudreve/pkg/cache"
|
||||
"github.com/HFO4/cloudreve/pkg/filesystem/driver/local"
|
||||
"github.com/HFO4/cloudreve/pkg/filesystem/driver/remote"
|
||||
"github.com/HFO4/cloudreve/pkg/serializer"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"net/http/httptest"
|
||||
|
@ -72,6 +74,94 @@ func TestDispatchHandler(t *testing.T) {
|
|||
fs.Policy = &model.Policy{Type: "unknown"}
|
||||
err = fs.DispatchHandler()
|
||||
asserts.Error(err)
|
||||
|
||||
fs.Policy = &model.Policy{Type: "mock"}
|
||||
err = fs.DispatchHandler()
|
||||
asserts.NoError(err)
|
||||
|
||||
fs.Policy = &model.Policy{Type: "local"}
|
||||
err = fs.DispatchHandler()
|
||||
asserts.NoError(err)
|
||||
|
||||
fs.Policy = &model.Policy{Type: "remote"}
|
||||
err = fs.DispatchHandler()
|
||||
asserts.NoError(err)
|
||||
|
||||
fs.Policy = &model.Policy{Type: "qiniu"}
|
||||
err = fs.DispatchHandler()
|
||||
asserts.NoError(err)
|
||||
|
||||
fs.Policy = &model.Policy{Type: "oss"}
|
||||
err = fs.DispatchHandler()
|
||||
asserts.NoError(err)
|
||||
|
||||
fs.Policy = &model.Policy{Type: "upyun"}
|
||||
err = fs.DispatchHandler()
|
||||
asserts.NoError(err)
|
||||
|
||||
fs.Policy = &model.Policy{Type: "onedrive"}
|
||||
err = fs.DispatchHandler()
|
||||
asserts.NoError(err)
|
||||
}
|
||||
|
||||
func TestNewFileSystemFromCallback(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
|
||||
// 用户上下文不存在
|
||||
{
|
||||
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
fs, err := NewFileSystemFromCallback(c)
|
||||
asserts.Nil(fs)
|
||||
asserts.Error(err)
|
||||
}
|
||||
|
||||
// 找不到回调会话
|
||||
{
|
||||
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
c.Set("user", &model.User{
|
||||
Policy: model.Policy{
|
||||
Type: "local",
|
||||
},
|
||||
})
|
||||
fs, err := NewFileSystemFromCallback(c)
|
||||
asserts.Nil(fs)
|
||||
asserts.Error(err)
|
||||
}
|
||||
|
||||
// 找不到上传策略
|
||||
{
|
||||
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
c.Set("user", &model.User{
|
||||
Policy: model.Policy{
|
||||
Type: "local",
|
||||
},
|
||||
})
|
||||
c.Set("callbackSession", &serializer.UploadSession{PolicyID: 138})
|
||||
cache.Deletes([]string{"138"}, "policy_")
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}))
|
||||
fs, err := NewFileSystemFromCallback(c)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.Nil(fs)
|
||||
asserts.Error(err)
|
||||
}
|
||||
|
||||
// 成功
|
||||
{
|
||||
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
c.Set("user", &model.User{
|
||||
Policy: model.Policy{
|
||||
Type: "local",
|
||||
},
|
||||
})
|
||||
c.Set("callbackSession", &serializer.UploadSession{PolicyID: 138})
|
||||
cache.Deletes([]string{"138"}, "policy_")
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "type", "options"}).AddRow(138, "local", "{}"))
|
||||
fs, err := NewFileSystemFromCallback(c)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.NotNil(fs)
|
||||
asserts.NoError(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestFileSystem_SetTargetFileByIDs(t *testing.T) {
|
||||
|
|
Loading…
Add table
Reference in a new issue