From 79daf92896e1e3f0c07ea0fbfaaad868078c0e78 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Fri, 1 May 2020 09:22:27 +0800 Subject: [PATCH] Test: List files in OneDrive --- assets | 2 +- pkg/filesystem/driver/onedrive/api_test.go | 60 ++++++++++++++++ .../driver/onedrive/handler_test.go | 72 +++++++++++++++++++ 3 files changed, 133 insertions(+), 1 deletion(-) diff --git a/assets b/assets index d752e26..1b9186c 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit d752e266d52230b3334b88b22facd9f5d37eda9e +Subproject commit 1b9186c3c7aab6a619fc7e872ada9f65ca08a34a diff --git a/pkg/filesystem/driver/onedrive/api_test.go b/pkg/filesystem/driver/onedrive/api_test.go index d06469e..e80c618 100644 --- a/pkg/filesystem/driver/onedrive/api_test.go +++ b/pkg/filesystem/driver/onedrive/api_test.go @@ -752,6 +752,66 @@ func TestClient_Delete(t *testing.T) { } } +func TestClient_ListChildren(t *testing.T) { + asserts := assert.New(t) + client, _ := NewClient(&model.Policy{}) + client.Credential.AccessToken = "AccessToken" + + // 根目录,请求失败,重测试 + { + client.Credential.ExpiresIn = 0 + res, err := client.ListChildren(context.Background(), "/") + asserts.Error(err) + asserts.Empty(res) + } + + // 非根目录,未知响应 + { + client.Credential.ExpiresIn = time.Now().Add(time.Duration(100) * time.Hour).Unix() + clientMock := ClientMock{} + clientMock.On( + "Request", + "GET", + testMock.Anything, + testMock.Anything, + testMock.Anything, + ).Return(&request.Response{ + Err: nil, + Response: &http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(strings.NewReader(`???`)), + }, + }) + client.Request = clientMock + res, err := client.ListChildren(context.Background(), "/uploads") + asserts.Error(err) + asserts.Empty(res) + } + + // 非根目录,成功 + { + client.Credential.ExpiresIn = time.Now().Add(time.Duration(100) * time.Hour).Unix() + clientMock := ClientMock{} + clientMock.On( + "Request", + "GET", + testMock.Anything, + testMock.Anything, + testMock.Anything, + ).Return(&request.Response{ + Err: nil, + Response: &http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(strings.NewReader(`{"value":[{}]}`)), + }, + }) + client.Request = clientMock + res, err := client.ListChildren(context.Background(), "/uploads") + asserts.NoError(err) + asserts.Len(res, 1) + } +} + func TestClient_GetThumbURL(t *testing.T) { asserts := assert.New(t) client, _ := NewClient(&model.Policy{}) diff --git a/pkg/filesystem/driver/onedrive/handler_test.go b/pkg/filesystem/driver/onedrive/handler_test.go index a6f7e42..ccb5414 100644 --- a/pkg/filesystem/driver/onedrive/handler_test.go +++ b/pkg/filesystem/driver/onedrive/handler_test.go @@ -174,6 +174,78 @@ func TestDriver_Source(t *testing.T) { } } +func TestDriver_List(t *testing.T) { + asserts := assert.New(t) + handler := Driver{ + Policy: &model.Policy{ + AccessKey: "ak", + SecretKey: "sk", + BucketName: "test", + Server: "test.com", + }, + } + handler.Client, _ = NewClient(&model.Policy{}) + handler.Client.Credential.AccessToken = "AccessToken" + handler.Client.Credential.ExpiresIn = time.Now().Add(time.Duration(100) * time.Hour).Unix() + + // 非递归 + { + clientMock := ClientMock{} + clientMock.On( + "Request", + "GET", + testMock.Anything, + testMock.Anything, + testMock.Anything, + ).Return(&request.Response{ + Err: nil, + Response: &http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(strings.NewReader(`{"value":[{}]}`)), + }, + }) + handler.Client.Request = clientMock + res, err := handler.List(context.Background(), "/", false) + asserts.NoError(err) + asserts.Len(res, 1) + } + + // 递归一次 + { + clientMock := ClientMock{} + clientMock.On( + "Request", + "GET", + "me/drive/root/children?$top=999999999", + testMock.Anything, + testMock.Anything, + ).Return(&request.Response{ + Err: nil, + Response: &http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(strings.NewReader(`{"value":[{"name":"1","folder":{}}]}`)), + }, + }) + clientMock.On( + "Request", + "GET", + "me/drive/root:/1:/children?$top=999999999", + testMock.Anything, + testMock.Anything, + ).Return(&request.Response{ + Err: nil, + Response: &http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(strings.NewReader(`{"value":[{"name":"2"}]}`)), + }, + }) + handler.Client.Request = clientMock + res, err := handler.List(context.Background(), "/", true) + asserts.NoError(err) + asserts.Len(res, 2) + } +} + func TestDriver_Thumb(t *testing.T) { asserts := assert.New(t) handler := Driver{