From c74ed10dbda2aafd299eeefb85769b554d08f153 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Thu, 12 Mar 2020 10:12:13 +0800 Subject: [PATCH] Test: aria2 / filesystem.image --- middleware/auth_test.go | 125 +++++++++++++++++++++++++++++++++ models/policy_test.go | 24 ++++++- pkg/aria2/aria2_test.go | 2 +- pkg/aria2/notification_test.go | 51 ++++++++++++++ pkg/filesystem/image_test.go | 39 ++++++++++ 5 files changed, 238 insertions(+), 3 deletions(-) create mode 100644 pkg/aria2/notification_test.go create mode 100644 pkg/filesystem/image_test.go diff --git a/middleware/auth_test.go b/middleware/auth_test.go index e6dfda5..32fdd7f 100644 --- a/middleware/auth_test.go +++ b/middleware/auth_test.go @@ -622,3 +622,128 @@ func TestUpyunCallbackAuth(t *testing.T) { asserts.False(c.IsAborted()) } } + +func TestOneDriveCallbackAuth(t *testing.T) { + asserts := assert.New(t) + rec := httptest.NewRecorder() + AuthFunc := OneDriveCallbackAuth() + + // Callback Key 相关验证失败 + { + c, _ := gin.CreateTestContext(rec) + c.Params = []gin.Param{ + {"key", "testUpyunBackRemote"}, + } + c.Request, _ = http.NewRequest("POST", "/api/v3/callback/upyun/testUpyunBackRemote", nil) + AuthFunc(c) + asserts.True(c.IsAborted()) + } + + // 成功 + { + cache.Set( + "callback_testCallBackUpyun", + serializer.UploadSession{ + UID: 1, + PolicyID: 512, + VirtualPath: "/", + }, + 0, + ) + cache.Deletes([]string{"1"}, "policy_") + mock.ExpectQuery("SELECT(.+)users(.+)"). + WillReturnRows(sqlmock.NewRows([]string{"id", "group_id"}).AddRow(1, 1)) + mock.ExpectQuery("SELECT(.+)groups(.+)"). + WillReturnRows(sqlmock.NewRows([]string{"id", "policies"}).AddRow(1, "[522]")) + mock.ExpectQuery("SELECT(.+)policies(.+)"). + WillReturnRows(sqlmock.NewRows([]string{"id", "access_key", "secret_key"}).AddRow(2, "123", "123")) + c, _ := gin.CreateTestContext(rec) + c.Params = []gin.Param{ + {"key", "testCallBackUpyun"}, + } + c.Request, _ = http.NewRequest("POST", "/api/v3/callback/upyun/testCallBackUpyun", ioutil.NopCloser(strings.NewReader("1"))) + AuthFunc(c) + asserts.NoError(mock.ExpectationsWereMet()) + asserts.False(c.IsAborted()) + } +} + +func TestCOSCallbackAuth(t *testing.T) { + asserts := assert.New(t) + rec := httptest.NewRecorder() + AuthFunc := COSCallbackAuth() + + // Callback Key 相关验证失败 + { + c, _ := gin.CreateTestContext(rec) + c.Params = []gin.Param{ + {"key", "testUpyunBackRemote"}, + } + c.Request, _ = http.NewRequest("POST", "/api/v3/callback/upyun/testUpyunBackRemote", nil) + AuthFunc(c) + asserts.True(c.IsAborted()) + } + + // 成功 + { + cache.Set( + "callback_testCallBackUpyun", + serializer.UploadSession{ + UID: 1, + PolicyID: 512, + VirtualPath: "/", + }, + 0, + ) + cache.Deletes([]string{"1"}, "policy_") + mock.ExpectQuery("SELECT(.+)users(.+)"). + WillReturnRows(sqlmock.NewRows([]string{"id", "group_id"}).AddRow(1, 1)) + mock.ExpectQuery("SELECT(.+)groups(.+)"). + WillReturnRows(sqlmock.NewRows([]string{"id", "policies"}).AddRow(1, "[522]")) + mock.ExpectQuery("SELECT(.+)policies(.+)"). + WillReturnRows(sqlmock.NewRows([]string{"id", "access_key", "secret_key"}).AddRow(2, "123", "123")) + c, _ := gin.CreateTestContext(rec) + c.Params = []gin.Param{ + {"key", "testCallBackUpyun"}, + } + c.Request, _ = http.NewRequest("POST", "/api/v3/callback/upyun/testCallBackUpyun", ioutil.NopCloser(strings.NewReader("1"))) + AuthFunc(c) + asserts.NoError(mock.ExpectationsWereMet()) + asserts.False(c.IsAborted()) + } +} + +func TestIsAdmin(t *testing.T) { + asserts := assert.New(t) + rec := httptest.NewRecorder() + testFunc := IsAdmin() + + // 非管理员 + { + c, _ := gin.CreateTestContext(rec) + c.Set("user", &model.User{}) + testFunc(c) + asserts.True(c.IsAborted()) + } + + // 是管理员 + { + c, _ := gin.CreateTestContext(rec) + user := &model.User{} + user.Group.ID = 1 + c.Set("user", user) + testFunc(c) + asserts.False(c.IsAborted()) + } + + // 初始用户,非管理组 + { + c, _ := gin.CreateTestContext(rec) + user := &model.User{} + user.Group.ID = 2 + user.ID = 1 + c.Set("user", user) + testFunc(c) + asserts.False(c.IsAborted()) + } +} diff --git a/models/policy_test.go b/models/policy_test.go index bf30251..ba92bab 100644 --- a/models/policy_test.go +++ b/models/policy_test.go @@ -149,6 +149,14 @@ func TestPolicy_GenerateFileName(t *testing.T) { testPolicy.Type = "upyun" testPolicy.FileNameRule = "{uid}123{originname}" asserts.Equal("1123{filename}{.suffix}", testPolicy.GenerateFileName(1, "")) + + testPolicy.Type = "qiniu" + testPolicy.FileNameRule = "{uid}123{originname}" + asserts.Equal("1123$(fname)", testPolicy.GenerateFileName(1, "")) + + testPolicy.Type = "local" + testPolicy.FileNameRule = "{uid}123{originname}" + asserts.Equal("1123", testPolicy.GenerateFileName(1, "")) } } @@ -179,8 +187,20 @@ func TestPolicy_GetUploadURL(t *testing.T) { // OSS { - policy := Policy{Type: "oss", BaseURL: "base", Server: "http://127.0.0.1"} - asserts.Equal("base", policy.GetUploadURL()) + policy := Policy{Type: "oss", BucketName: "base", Server: "127.0.0.1"} + asserts.Equal("https://base.127.0.0.1", policy.GetUploadURL()) + } + + // cos + { + policy := Policy{Type: "cos", BaseURL: "base", Server: "http://127.0.0.1"} + asserts.Equal("http://127.0.0.1", policy.GetUploadURL()) + } + + // upyun + { + policy := Policy{Type: "upyun", BucketName: "base", Server: "http://127.0.0.1"} + asserts.Equal("https://v0.api.upyun.com/base", policy.GetUploadURL()) } // 未知 diff --git a/pkg/aria2/aria2_test.go b/pkg/aria2/aria2_test.go index 0cca187..7992556 100644 --- a/pkg/aria2/aria2_test.go +++ b/pkg/aria2/aria2_test.go @@ -67,7 +67,7 @@ func TestInit(t *testing.T) { // 连接失败 { - cache.Set("setting_aria2_options", "[]", 0) + cache.Set("setting_aria2_options", "{}", 0) cache.Set("setting_aria2_rpcurl", "http://127.0.0.1:1234", 0) cache.Set("setting_aria2_call_timeout", "1", 0) cache.Set("setting_aria2_interval", "100", 0) diff --git a/pkg/aria2/notification_test.go b/pkg/aria2/notification_test.go new file mode 100644 index 0000000..5b00019 --- /dev/null +++ b/pkg/aria2/notification_test.go @@ -0,0 +1,51 @@ +package aria2 + +import ( + "github.com/HFO4/cloudreve/pkg/aria2/rpc" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestNotifier_Notify(t *testing.T) { + asserts := assert.New(t) + notifier2 := &Notifier{} + notifyChan := make(chan StatusEvent, 10) + notifier2.Subscribe(notifyChan, "1") + + // 未订阅 + { + notifier2.Notify([]rpc.Event{rpc.Event{Gid: ""}}, 1) + asserts.Len(notifyChan, 0) + } + + // 订阅 + { + notifier2.Notify([]rpc.Event{{Gid: "1"}}, 1) + asserts.Len(notifyChan, 1) + <-notifyChan + + notifier2.OnBtDownloadComplete([]rpc.Event{{Gid: "1"}}) + asserts.Len(notifyChan, 1) + <-notifyChan + + notifier2.OnDownloadStart([]rpc.Event{{Gid: "1"}}) + asserts.Len(notifyChan, 1) + <-notifyChan + + notifier2.OnDownloadPause([]rpc.Event{{Gid: "1"}}) + asserts.Len(notifyChan, 1) + <-notifyChan + + notifier2.OnDownloadStop([]rpc.Event{{Gid: "1"}}) + asserts.Len(notifyChan, 1) + <-notifyChan + + notifier2.OnDownloadComplete([]rpc.Event{{Gid: "1"}}) + asserts.Len(notifyChan, 1) + <-notifyChan + + notifier2.OnDownloadError([]rpc.Event{{Gid: "1"}}) + asserts.Len(notifyChan, 1) + <-notifyChan + } +} diff --git a/pkg/filesystem/image_test.go b/pkg/filesystem/image_test.go new file mode 100644 index 0000000..eb642fd --- /dev/null +++ b/pkg/filesystem/image_test.go @@ -0,0 +1,39 @@ +package filesystem + +import ( + "context" + model "github.com/HFO4/cloudreve/models" + "github.com/HFO4/cloudreve/pkg/cache" + "github.com/HFO4/cloudreve/pkg/filesystem/response" + "github.com/stretchr/testify/assert" + testMock "github.com/stretchr/testify/mock" + "testing" +) + +func TestFileSystem_GetThumb(t *testing.T) { + asserts := assert.New(t) + fs := &FileSystem{User: &model.User{}} + + // 非图像文件 + { + fs.SetTargetFile(&[]model.File{{}}) + _, err := fs.GetThumb(context.Background(), 1) + asserts.Equal(err, ErrObjectNotExist) + } + + // 成功 + { + cache.Set("setting_thumb_width", "10", 0) + cache.Set("setting_thumb_height", "10", 0) + cache.Set("setting_preview_timeout", "50", 0) + testHandller2 := new(FileHeaderMock) + testHandller2.On("Thumb", testMock.Anything, "").Return(&response.ContentResponse{}, nil) + fs.CleanTargets() + fs.SetTargetFile(&[]model.File{{PicInfo: "1,1", Policy: model.Policy{Type: "mock"}}}) + fs.FileTarget[0].Policy.ID = 1 + fs.Handler = testHandller2 + res, err := fs.GetThumb(context.Background(), 1) + asserts.NoError(err) + asserts.EqualValues(50, res.MaxAge) + } +}