From 5363b901671088befd93cdd88b9fdcc78bb151d0 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Fri, 21 Feb 2020 11:16:25 +0800 Subject: [PATCH] Test: options / WebDAV middleware --- middleware/auth_test.go | 6 ++++ middleware/{hashid_test.go => option_test.go} | 29 +++++++++++++++++++ models/migration.go | 2 +- pkg/filesystem/driver/onedrive/api.go | 5 +++- pkg/filesystem/driver/remote/handler.go | 1 + 5 files changed, 41 insertions(+), 2 deletions(-) rename middleware/{hashid_test.go => option_test.go} (59%) diff --git a/middleware/auth_test.go b/middleware/auth_test.go index 976215a..e6dfda5 100644 --- a/middleware/auth_test.go +++ b/middleware/auth_test.go @@ -148,6 +148,8 @@ func TestWebDAVAuth(t *testing.T) { WillReturnRows( sqlmock.NewRows([]string{"id", "password", "email", "options"}).AddRow(1, "123", "who@cloudreve.org", "{}"), ) + // 查找密码 + mock.ExpectQuery("SELECT(.+)webdav(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"})) AuthFunc(c) asserts.NoError(mock.ExpectationsWereMet()) asserts.Equal(c.Writer.Status(), http.StatusUnauthorized) @@ -172,6 +174,8 @@ func TestWebDAVAuth(t *testing.T) { ), ) mock.ExpectQuery("SELECT(.+)groups(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "web_dav_enabled"}).AddRow(1, false)) + // 查找密码 + mock.ExpectQuery("SELECT(.+)webdav(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1)) AuthFunc(c) asserts.NoError(mock.ExpectationsWereMet()) asserts.Equal(c.Writer.Status(), http.StatusForbidden) @@ -196,6 +200,8 @@ func TestWebDAVAuth(t *testing.T) { ), ) mock.ExpectQuery("SELECT(.+)groups(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "web_dav_enabled"}).AddRow(1, true)) + // 查找密码 + mock.ExpectQuery("SELECT(.+)webdav(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1)) AuthFunc(c) asserts.NoError(mock.ExpectationsWereMet()) asserts.Equal(c.Writer.Status(), 200) diff --git a/middleware/hashid_test.go b/middleware/option_test.go similarity index 59% rename from middleware/hashid_test.go rename to middleware/option_test.go index 4cdfe83..a0af92d 100644 --- a/middleware/hashid_test.go +++ b/middleware/option_test.go @@ -1,6 +1,7 @@ package middleware import ( + "github.com/HFO4/cloudreve/pkg/cache" "github.com/HFO4/cloudreve/pkg/hashid" "github.com/gin-gonic/gin" "github.com/stretchr/testify/assert" @@ -48,3 +49,31 @@ func TestHashID(t *testing.T) { asserts.False(c.IsAborted()) } } + +func TestIsFunctionEnabled(t *testing.T) { + asserts := assert.New(t) + rec := httptest.NewRecorder() + TestFunc := IsFunctionEnabled("TestIsFunctionEnabled") + + // 未开启 + { + cache.Set("setting_TestIsFunctionEnabled", "0", 0) + c, _ := gin.CreateTestContext(rec) + c.Params = []gin.Param{} + c.Request, _ = http.NewRequest("POST", "/api/v3/file/dellete/1", nil) + TestFunc(c) + asserts.NoError(mock.ExpectationsWereMet()) + asserts.True(c.IsAborted()) + } + // 开启 + { + cache.Set("setting_TestIsFunctionEnabled", "1", 0) + c, _ := gin.CreateTestContext(rec) + c.Params = []gin.Param{} + c.Request, _ = http.NewRequest("POST", "/api/v3/file/dellete/1", nil) + TestFunc(c) + asserts.NoError(mock.ExpectationsWereMet()) + asserts.False(c.IsAborted()) + } + +} diff --git a/models/migration.go b/models/migration.go index 5c3cd6b..7c9fb2c 100644 --- a/models/migration.go +++ b/models/migration.go @@ -150,7 +150,7 @@ Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; verti {Name: "group_sell_data", Value: `[]`, Type: "group_sell"}, {Name: "gravatar_server", Value: `https://gravatar.loli.net/`, Type: "avatar"}, {Name: "defaultTheme", Value: `#3f51b5`, Type: "basic"}, - {Name: "themes", Value: `{"#3f51b5":{"palette":{"primary":{"light":"#7986cb","main":"#3f51b5","dark":"#303f9f","contrastText":"#fff"},"secondary":{"light":"#ff4081","main":"#f50057","dark":"#c51162","contrastText":"#fff"},"error":{"light":"#e57373","main":"#f44336","dark":"#d32f2f","contrastText":"#fff"},"explorer":{"filename":"#474849","icon":"#8f8f8f","bgSelected":"#D5DAF0","emptyIcon":"#e8e8e8"}}}}`, Type: "basic"}, + {Name: "themes", Value: `{"#3f51b5":{"palette":{"primary":{"main":"#3f51b5"},"secondary":{"main":"#f50057"}}},"#2196f3":{"palette":{"primary":{"main":"#2196f3"},"secondary":{"main":"#FFC107"}}},"#673AB7":{"palette":{"primary":{"main":"#673AB7"},"secondary":{"main":"#2196F3"}}},"#E91E63":{"palette":{"primary":{"main":"#E91E63"},"secondary":{"main":"#42A5F5","contrastText":"#fff"}}},"#FF5722":{"palette":{"primary":{"main":"#FF5722"},"secondary":{"main":"#3F51B5"}}},"#FFC107":{"palette":{"primary":{"main":"#FFC107"},"secondary":{"main":"#26C6DA"}}},"#8BC34A":{"palette":{"primary":{"main":"#8BC34A","contrastText":"#fff"},"secondary":{"main":"#FF8A65","contrastText":"#fff"}}},"#009688":{"palette":{"primary":{"main":"#009688"},"secondary":{"main":"#4DD0E1","contrastText":"#fff"}}},"#607D8B":{"palette":{"primary":{"main":"#607D8B"},"secondary":{"main":"#F06292"}}},"#795548":{"palette":{"primary":{"main":"#795548"},"secondary":{"main":"#4CAF50","contrastText":"#fff"}}}}`, Type: "basic"}, {Name: "aria2_token", Value: `your token`, Type: "aria2"}, {Name: "aria2_token", Value: `your token`, Type: "aria2"}, {Name: "aria2_temp_path", Value: ``, Type: "aria2"}, diff --git a/pkg/filesystem/driver/onedrive/api.go b/pkg/filesystem/driver/onedrive/api.go index 8165b52..1a7bb84 100644 --- a/pkg/filesystem/driver/onedrive/api.go +++ b/pkg/filesystem/driver/onedrive/api.go @@ -149,6 +149,7 @@ func (client *Client) UploadChunk(ctx context.Context, uploadURL string, chunk * "Content-Range": {fmt.Sprintf("bytes %d-%d/%d", chunk.Offset, chunk.Offset+chunk.ChunkSize-1, chunk.Total)}, }), request.WithoutHeader([]string{"Authorization", "Content-Type"}), + request.WithTimeout(time.Duration(300)*time.Second), ) if err != nil { // 如果重试次数小于限制,5秒后重试 @@ -253,7 +254,9 @@ func (client *Client) SimpleUpload(ctx context.Context, dst string, body io.Read dst = strings.TrimPrefix(dst, "/") requestURL := client.getRequestURL("me/drive/root:/" + dst + ":/content") - res, err := client.request(ctx, "PUT", requestURL, body, request.WithContentLength(int64(size))) + res, err := client.request(ctx, "PUT", requestURL, body, request.WithContentLength(int64(size)), + request.WithTimeout(time.Duration(150)*time.Second), + ) if err != nil { retried := 0 if v, ok := ctx.Value(fsctx.RetryCtx).(int); ok { diff --git a/pkg/filesystem/driver/remote/handler.go b/pkg/filesystem/driver/remote/handler.go index 8c863b1..90af421 100644 --- a/pkg/filesystem/driver/remote/handler.go +++ b/pkg/filesystem/driver/remote/handler.go @@ -122,6 +122,7 @@ func (handler Driver) Put(ctx context.Context, file io.ReadCloser, dst string, s "X-FileName": {fileName}, }), request.WithContentLength(int64(size)), + request.WithTimeout(time.Duration(0)), ).CheckHTTPResponse(200).DecodeResponse() if err != nil { return err