From b1344616b82b447129e96abd92034d915aeb1717 Mon Sep 17 00:00:00 2001 From: Aaron Liu <912394456@qq.com> Date: Sat, 27 May 2023 10:44:28 +0800 Subject: [PATCH] test: fix failed ut --- pkg/cache/redis_test.go | 8 +++--- pkg/cluster/controller_test.go | 6 ++-- pkg/filesystem/driver/local/handler_test.go | 32 +++++---------------- pkg/filesystem/driver/oss/handler.go | 1 + pkg/filesystem/file_test.go | 2 -- pkg/filesystem/filesystem.go | 3 +- pkg/filesystem/manage_test.go | 4 +-- pkg/filesystem/upload_test.go | 5 ++-- 8 files changed, 21 insertions(+), 40 deletions(-) diff --git a/pkg/cache/redis_test.go b/pkg/cache/redis_test.go index 609eba1..79dc6fc 100644 --- a/pkg/cache/redis_test.go +++ b/pkg/cache/redis_test.go @@ -16,13 +16,13 @@ func TestNewRedisStore(t *testing.T) { store := NewRedisStore(10, "tcp", "", "", "0") asserts.NotNil(store) - conn, err := store.pool.Dial() - asserts.Nil(conn) - asserts.Error(err) + asserts.Panics(func() { + store.pool.Dial() + }) testConn := redigomock.NewConn() 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 { fmt.Println("Command was not used") return diff --git a/pkg/cluster/controller_test.go b/pkg/cluster/controller_test.go index 22b25ea..42d8362 100644 --- a/pkg/cluster/controller_test.go +++ b/pkg/cluster/controller_test.go @@ -328,7 +328,7 @@ func TestSlaveController_GetOneDriveToken(t *testing.T) { // return none 200 { 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}, }) c := &slaveController{ @@ -345,7 +345,7 @@ func TestSlaveController_GetOneDriveToken(t *testing.T) { // master return error { 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: 200, Body: ioutil.NopCloser(strings.NewReader("{\"code\":1}")), @@ -365,7 +365,7 @@ func TestSlaveController_GetOneDriveToken(t *testing.T) { // success { 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: 200, Body: ioutil.NopCloser(strings.NewReader("{\"data\":\"expected\"}")), diff --git a/pkg/filesystem/driver/local/handler_test.go b/pkg/filesystem/driver/local/handler_test.go index 4769893..b73b564 100644 --- a/pkg/filesystem/driver/local/handler_test.go +++ b/pkg/filesystem/driver/local/handler_test.go @@ -11,7 +11,6 @@ import ( "github.com/jinzhu/gorm" "github.com/stretchr/testify/assert" "io" - "net/url" "os" "strings" "testing" @@ -190,13 +189,10 @@ func TestHandler_Source(t *testing.T) { Name: "test.jpg", } ctx := context.WithValue(ctx, fsctx.FileModelCtx, file) - baseURL, err := url.Parse("https://cloudreve.org") - asserts.NoError(err) - sourceURL, err := handler.Source(ctx, "", *baseURL, 0, false, 0) + sourceURL, err := handler.Source(ctx, "", 0, false, 0) asserts.NoError(err) asserts.NotEmpty(sourceURL) asserts.Contains(sourceURL, "sign=") - asserts.Contains(sourceURL, "https://cloudreve.org") } // 下载 @@ -208,21 +204,16 @@ func TestHandler_Source(t *testing.T) { Name: "test.jpg", } ctx := context.WithValue(ctx, fsctx.FileModelCtx, file) - baseURL, err := url.Parse("https://cloudreve.org") - asserts.NoError(err) - sourceURL, err := handler.Source(ctx, "", *baseURL, 0, true, 0) + sourceURL, err := handler.Source(ctx, "", 0, true, 0) asserts.NoError(err) asserts.NotEmpty(sourceURL) asserts.Contains(sourceURL, "sign=") asserts.Contains(sourceURL, "download") - asserts.Contains(sourceURL, "https://cloudreve.org") } // 无法获取上下文 { - baseURL, err := url.Parse("https://cloudreve.org") - asserts.NoError(err) - sourceURL, err := handler.Source(ctx, "", *baseURL, 0, false, 0) + sourceURL, err := handler.Source(ctx, "", 0, false, 0) asserts.Error(err) asserts.Empty(sourceURL) } @@ -237,9 +228,7 @@ func TestHandler_Source(t *testing.T) { Name: "test.jpg", } ctx := context.WithValue(ctx, fsctx.FileModelCtx, file) - baseURL, err := url.Parse("https://cloudreve.org") - asserts.NoError(err) - sourceURL, err := handler.Source(ctx, "", *baseURL, 0, false, 0) + sourceURL, err := handler.Source(ctx, "", 0, false, 0) asserts.NoError(err) asserts.NotEmpty(sourceURL) asserts.Contains(sourceURL, "sign=") @@ -256,9 +245,7 @@ func TestHandler_Source(t *testing.T) { Name: "test.jpg", } ctx := context.WithValue(ctx, fsctx.FileModelCtx, file) - baseURL, err := url.Parse("https://cloudreve.org") - asserts.NoError(err) - sourceURL, err := handler.Source(ctx, "", *baseURL, 0, false, 0) + sourceURL, err := handler.Source(ctx, "", 0, false, 0) asserts.Error(err) asserts.Empty(sourceURL) } @@ -279,19 +266,14 @@ func TestHandler_GetDownloadURL(t *testing.T) { Name: "test.jpg", } ctx := context.WithValue(ctx, fsctx.FileModelCtx, file) - baseURL, err := url.Parse("https://cloudreve.org") - asserts.NoError(err) - downloadURL, err := handler.Source(ctx, "", *baseURL, 10, true, 0) + downloadURL, err := handler.Source(ctx, "", 10, true, 0) asserts.NoError(err) asserts.Contains(downloadURL, "sign=") - asserts.Contains(downloadURL, "https://cloudreve.org") } // 无法获取上下文 { - baseURL, err := url.Parse("https://cloudreve.org") - asserts.NoError(err) - downloadURL, err := handler.Source(ctx, "", *baseURL, 10, true, 0) + downloadURL, err := handler.Source(ctx, "", 10, true, 0) asserts.Error(err) asserts.Empty(downloadURL) } diff --git a/pkg/filesystem/driver/oss/handler.go b/pkg/filesystem/driver/oss/handler.go index 3a94ca3..8a0f2cb 100644 --- a/pkg/filesystem/driver/oss/handler.go +++ b/pkg/filesystem/driver/oss/handler.go @@ -433,6 +433,7 @@ func (handler *Driver) Token(ctx context.Context, ttl int64, uploadSession *seri options := []oss.Option{ oss.Expires(time.Now().Add(time.Duration(ttl) * time.Second)), oss.ForbidOverWrite(true), + oss.ContentType(fileInfo.DetectMimeType()), } imur, err := handler.bucket.InitiateMultipartUpload(fileInfo.SavePath, options...) if err != nil { diff --git a/pkg/filesystem/file_test.go b/pkg/filesystem/file_test.go index fbc479c..66f3444 100644 --- a/pkg/filesystem/file_test.go +++ b/pkg/filesystem/file_test.go @@ -352,8 +352,6 @@ func TestFileSystem_GetSource(t *testing.T) { sqlmock.NewRows([]string{"id", "type", "is_origin_link_enable"}). 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) asserts.NoError(mock.ExpectationsWereMet()) diff --git a/pkg/filesystem/filesystem.go b/pkg/filesystem/filesystem.go index c7316e6..1e14fa8 100644 --- a/pkg/filesystem/filesystem.go +++ b/pkg/filesystem/filesystem.go @@ -8,6 +8,7 @@ import ( "github.com/cloudreve/Cloudreve/v3/pkg/conf" "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/googledrive" "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/oss" @@ -177,7 +178,7 @@ func (fs *FileSystem) DispatchHandler() error { fs.Handler = handler return err case "googledrive": - handler, err := googledrive.NewDriver(policy) + handler, err := googledrive.NewDriver(currentPolicy) fs.Handler = handler return err default: diff --git a/pkg/filesystem/manage_test.go b/pkg/filesystem/manage_test.go index a239831..1f2cc1a 100644 --- a/pkg/filesystem/manage_test.go +++ b/pkg/filesystem/manage_test.go @@ -682,7 +682,7 @@ func TestFileSystem_Rename(t *testing.T) { WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(10, "old.text")) mock.ExpectBegin() mock.ExpectExec("UPDATE(.+)files(.+)SET(.+)"). - WithArgs("new.txt", 10). + WithArgs(sqlmock.AnyArg(), "new.txt", sqlmock.AnyArg(), 10). WillReturnResult(sqlmock.NewResult(1, 1)) mock.ExpectCommit() 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")) mock.ExpectBegin() mock.ExpectExec("UPDATE(.+)files(.+)SET(.+)"). - WithArgs("new.txt", 10). + WithArgs(sqlmock.AnyArg(), "new.txt", sqlmock.AnyArg(), 10). WillReturnError(errors.New("error")) mock.ExpectRollback() err := fs.Rename(ctx, []uint{}, []uint{10}, "new.txt") diff --git a/pkg/filesystem/upload_test.go b/pkg/filesystem/upload_test.go index 357cb6b..61dad9f 100644 --- a/pkg/filesystem/upload_test.go +++ b/pkg/filesystem/upload_test.go @@ -16,7 +16,6 @@ import ( "io/ioutil" "net/http" "net/http/httptest" - "net/url" "strings" "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) } -func (m FileHeaderMock) Source(ctx context.Context, path string, url url.URL, expires int64, isDownload bool, speed int) (string, error) { - args := m.Called(ctx, path, url, expires, isDownload, speed) +func (m FileHeaderMock) Source(ctx context.Context, path string, expires int64, isDownload bool, speed int) (string, error) { + args := m.Called(ctx, path, expires, isDownload, speed) return args.Get(0).(string), args.Error(1) }