test: fix failed ut

pull/1741/head
Aaron Liu 1 year ago
parent 89ee147961
commit b1344616b8

@ -16,13 +16,13 @@ func TestNewRedisStore(t *testing.T) {
store := NewRedisStore(10, "tcp", "", "", "0") store := NewRedisStore(10, "tcp", "", "", "0")
asserts.NotNil(store) asserts.NotNil(store)
conn, err := store.pool.Dial() asserts.Panics(func() {
asserts.Nil(conn) store.pool.Dial()
asserts.Error(err) })
testConn := redigomock.NewConn() testConn := redigomock.NewConn()
cmd := testConn.Command("PING").Expect("PONG") cmd := testConn.Command("PING").Expect("PONG")
err = store.pool.TestOnBorrow(testConn, time.Now()) err := store.pool.TestOnBorrow(testConn, time.Now())
if testConn.Stats(cmd) != 1 { if testConn.Stats(cmd) != 1 {
fmt.Println("Command was not used") fmt.Println("Command was not used")
return return

@ -328,7 +328,7 @@ func TestSlaveController_GetOneDriveToken(t *testing.T) {
// return none 200 // return none 200
{ {
mockRequest := &requestMock{} mockRequest := &requestMock{}
mockRequest.On("Request", "GET", "/api/v3/slave/credential/onedrive/1", testMock.Anything, testMock.Anything).Return(&request.Response{ mockRequest.On("Request", "GET", "/api/v3/slave/credential/1", testMock.Anything, testMock.Anything).Return(&request.Response{
Response: &http.Response{StatusCode: http.StatusConflict}, Response: &http.Response{StatusCode: http.StatusConflict},
}) })
c := &slaveController{ c := &slaveController{
@ -345,7 +345,7 @@ func TestSlaveController_GetOneDriveToken(t *testing.T) {
// master return error // master return error
{ {
mockRequest := &requestMock{} mockRequest := &requestMock{}
mockRequest.On("Request", "GET", "/api/v3/slave/credential/onedrive/1", testMock.Anything, testMock.Anything).Return(&request.Response{ mockRequest.On("Request", "GET", "/api/v3/slave/credential/1", testMock.Anything, testMock.Anything).Return(&request.Response{
Response: &http.Response{ Response: &http.Response{
StatusCode: 200, StatusCode: 200,
Body: ioutil.NopCloser(strings.NewReader("{\"code\":1}")), Body: ioutil.NopCloser(strings.NewReader("{\"code\":1}")),
@ -365,7 +365,7 @@ func TestSlaveController_GetOneDriveToken(t *testing.T) {
// success // success
{ {
mockRequest := &requestMock{} mockRequest := &requestMock{}
mockRequest.On("Request", "GET", "/api/v3/slave/credential/onedrive/1", testMock.Anything, testMock.Anything).Return(&request.Response{ mockRequest.On("Request", "GET", "/api/v3/slave/credential/1", testMock.Anything, testMock.Anything).Return(&request.Response{
Response: &http.Response{ Response: &http.Response{
StatusCode: 200, StatusCode: 200,
Body: ioutil.NopCloser(strings.NewReader("{\"data\":\"expected\"}")), Body: ioutil.NopCloser(strings.NewReader("{\"data\":\"expected\"}")),

@ -11,7 +11,6 @@ import (
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"io" "io"
"net/url"
"os" "os"
"strings" "strings"
"testing" "testing"
@ -190,13 +189,10 @@ func TestHandler_Source(t *testing.T) {
Name: "test.jpg", Name: "test.jpg",
} }
ctx := context.WithValue(ctx, fsctx.FileModelCtx, file) ctx := context.WithValue(ctx, fsctx.FileModelCtx, file)
baseURL, err := url.Parse("https://cloudreve.org") sourceURL, err := handler.Source(ctx, "", 0, false, 0)
asserts.NoError(err)
sourceURL, err := handler.Source(ctx, "", *baseURL, 0, false, 0)
asserts.NoError(err) asserts.NoError(err)
asserts.NotEmpty(sourceURL) asserts.NotEmpty(sourceURL)
asserts.Contains(sourceURL, "sign=") asserts.Contains(sourceURL, "sign=")
asserts.Contains(sourceURL, "https://cloudreve.org")
} }
// 下载 // 下载
@ -208,21 +204,16 @@ func TestHandler_Source(t *testing.T) {
Name: "test.jpg", Name: "test.jpg",
} }
ctx := context.WithValue(ctx, fsctx.FileModelCtx, file) ctx := context.WithValue(ctx, fsctx.FileModelCtx, file)
baseURL, err := url.Parse("https://cloudreve.org") sourceURL, err := handler.Source(ctx, "", 0, true, 0)
asserts.NoError(err)
sourceURL, err := handler.Source(ctx, "", *baseURL, 0, true, 0)
asserts.NoError(err) asserts.NoError(err)
asserts.NotEmpty(sourceURL) asserts.NotEmpty(sourceURL)
asserts.Contains(sourceURL, "sign=") asserts.Contains(sourceURL, "sign=")
asserts.Contains(sourceURL, "download") asserts.Contains(sourceURL, "download")
asserts.Contains(sourceURL, "https://cloudreve.org")
} }
// 无法获取上下文 // 无法获取上下文
{ {
baseURL, err := url.Parse("https://cloudreve.org") sourceURL, err := handler.Source(ctx, "", 0, false, 0)
asserts.NoError(err)
sourceURL, err := handler.Source(ctx, "", *baseURL, 0, false, 0)
asserts.Error(err) asserts.Error(err)
asserts.Empty(sourceURL) asserts.Empty(sourceURL)
} }
@ -237,9 +228,7 @@ func TestHandler_Source(t *testing.T) {
Name: "test.jpg", Name: "test.jpg",
} }
ctx := context.WithValue(ctx, fsctx.FileModelCtx, file) ctx := context.WithValue(ctx, fsctx.FileModelCtx, file)
baseURL, err := url.Parse("https://cloudreve.org") sourceURL, err := handler.Source(ctx, "", 0, false, 0)
asserts.NoError(err)
sourceURL, err := handler.Source(ctx, "", *baseURL, 0, false, 0)
asserts.NoError(err) asserts.NoError(err)
asserts.NotEmpty(sourceURL) asserts.NotEmpty(sourceURL)
asserts.Contains(sourceURL, "sign=") asserts.Contains(sourceURL, "sign=")
@ -256,9 +245,7 @@ func TestHandler_Source(t *testing.T) {
Name: "test.jpg", Name: "test.jpg",
} }
ctx := context.WithValue(ctx, fsctx.FileModelCtx, file) ctx := context.WithValue(ctx, fsctx.FileModelCtx, file)
baseURL, err := url.Parse("https://cloudreve.org") sourceURL, err := handler.Source(ctx, "", 0, false, 0)
asserts.NoError(err)
sourceURL, err := handler.Source(ctx, "", *baseURL, 0, false, 0)
asserts.Error(err) asserts.Error(err)
asserts.Empty(sourceURL) asserts.Empty(sourceURL)
} }
@ -279,19 +266,14 @@ func TestHandler_GetDownloadURL(t *testing.T) {
Name: "test.jpg", Name: "test.jpg",
} }
ctx := context.WithValue(ctx, fsctx.FileModelCtx, file) ctx := context.WithValue(ctx, fsctx.FileModelCtx, file)
baseURL, err := url.Parse("https://cloudreve.org") downloadURL, err := handler.Source(ctx, "", 10, true, 0)
asserts.NoError(err)
downloadURL, err := handler.Source(ctx, "", *baseURL, 10, true, 0)
asserts.NoError(err) asserts.NoError(err)
asserts.Contains(downloadURL, "sign=") asserts.Contains(downloadURL, "sign=")
asserts.Contains(downloadURL, "https://cloudreve.org")
} }
// 无法获取上下文 // 无法获取上下文
{ {
baseURL, err := url.Parse("https://cloudreve.org") downloadURL, err := handler.Source(ctx, "", 10, true, 0)
asserts.NoError(err)
downloadURL, err := handler.Source(ctx, "", *baseURL, 10, true, 0)
asserts.Error(err) asserts.Error(err)
asserts.Empty(downloadURL) asserts.Empty(downloadURL)
} }

@ -433,6 +433,7 @@ func (handler *Driver) Token(ctx context.Context, ttl int64, uploadSession *seri
options := []oss.Option{ options := []oss.Option{
oss.Expires(time.Now().Add(time.Duration(ttl) * time.Second)), oss.Expires(time.Now().Add(time.Duration(ttl) * time.Second)),
oss.ForbidOverWrite(true), oss.ForbidOverWrite(true),
oss.ContentType(fileInfo.DetectMimeType()),
} }
imur, err := handler.bucket.InitiateMultipartUpload(fileInfo.SavePath, options...) imur, err := handler.bucket.InitiateMultipartUpload(fileInfo.SavePath, options...)
if err != nil { if err != nil {

@ -352,8 +352,6 @@ func TestFileSystem_GetSource(t *testing.T) {
sqlmock.NewRows([]string{"id", "type", "is_origin_link_enable"}). sqlmock.NewRows([]string{"id", "type", "is_origin_link_enable"}).
AddRow(35, "local", true), AddRow(35, "local", true),
) )
// 查找站点URL
mock.ExpectQuery("SELECT(.+)").WithArgs("siteURL").WillReturnRows(sqlmock.NewRows([]string{"id", "value"}).AddRow(1, "https://cloudreve.org"))
sourceURL, err := fs.GetSource(ctx, 2) sourceURL, err := fs.GetSource(ctx, 2)
asserts.NoError(mock.ExpectationsWereMet()) asserts.NoError(mock.ExpectationsWereMet())

@ -8,6 +8,7 @@ import (
"github.com/cloudreve/Cloudreve/v3/pkg/conf" "github.com/cloudreve/Cloudreve/v3/pkg/conf"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/cos" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/cos"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/googledrive"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/local" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/local"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/onedrive" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/onedrive"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/oss" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/oss"
@ -177,7 +178,7 @@ func (fs *FileSystem) DispatchHandler() error {
fs.Handler = handler fs.Handler = handler
return err return err
case "googledrive": case "googledrive":
handler, err := googledrive.NewDriver(policy) handler, err := googledrive.NewDriver(currentPolicy)
fs.Handler = handler fs.Handler = handler
return err return err
default: default:

@ -682,7 +682,7 @@ func TestFileSystem_Rename(t *testing.T) {
WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(10, "old.text")) WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(10, "old.text"))
mock.ExpectBegin() mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)files(.+)SET(.+)"). mock.ExpectExec("UPDATE(.+)files(.+)SET(.+)").
WithArgs("new.txt", 10). WithArgs(sqlmock.AnyArg(), "new.txt", sqlmock.AnyArg(), 10).
WillReturnResult(sqlmock.NewResult(1, 1)) WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit() mock.ExpectCommit()
err := fs.Rename(ctx, []uint{}, []uint{10}, "new.txt") err := fs.Rename(ctx, []uint{}, []uint{10}, "new.txt")
@ -708,7 +708,7 @@ func TestFileSystem_Rename(t *testing.T) {
WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(10, "old.text")) WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(10, "old.text"))
mock.ExpectBegin() mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)files(.+)SET(.+)"). mock.ExpectExec("UPDATE(.+)files(.+)SET(.+)").
WithArgs("new.txt", 10). WithArgs(sqlmock.AnyArg(), "new.txt", sqlmock.AnyArg(), 10).
WillReturnError(errors.New("error")) WillReturnError(errors.New("error"))
mock.ExpectRollback() mock.ExpectRollback()
err := fs.Rename(ctx, []uint{}, []uint{10}, "new.txt") err := fs.Rename(ctx, []uint{}, []uint{10}, "new.txt")

@ -16,7 +16,6 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url"
"strings" "strings"
"testing" "testing"
) )
@ -60,8 +59,8 @@ func (m FileHeaderMock) Thumb(ctx context.Context, files *model.File) (*response
return args.Get(0).(*response.ContentResponse), args.Error(1) return args.Get(0).(*response.ContentResponse), args.Error(1)
} }
func (m FileHeaderMock) Source(ctx context.Context, path string, url url.URL, expires int64, isDownload bool, speed int) (string, error) { func (m FileHeaderMock) Source(ctx context.Context, path string, expires int64, isDownload bool, speed int) (string, error) {
args := m.Called(ctx, path, url, expires, isDownload, speed) args := m.Called(ctx, path, expires, isDownload, speed)
return args.Get(0).(string), args.Error(1) return args.Get(0).(string), args.Error(1)
} }

Loading…
Cancel
Save