mirror of https://github.com/cloudreve/Cloudreve
Test: onedrive related
parent
0d2e3cc717
commit
8672042d61
|
@ -393,7 +393,7 @@ func (client *Client) MonitorUpload(uploadURL, callbackKey, path string, size ui
|
||||||
cache.Deletes([]string{callbackKey}, "callback_")
|
cache.Deletes([]string{callbackKey}, "callback_")
|
||||||
_, err = client.Delete(context.Background(), []string{path})
|
_, err = client.Delete(context.Background(), []string{path})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.Log().Warning("无法删除未回掉的文件,%s", err)
|
util.Log().Warning("无法删除未回调的文件,%s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
@ -486,7 +486,7 @@ func (client *Client) request(ctx context.Context, method string, url string, bo
|
||||||
decodeErr error
|
decodeErr error
|
||||||
)
|
)
|
||||||
// 如果有错误
|
// 如果有错误
|
||||||
if res.Response.StatusCode < 200 && res.Response.StatusCode >= 300 {
|
if res.Response.StatusCode < 200 || res.Response.StatusCode >= 300 {
|
||||||
decodeErr = json.Unmarshal([]byte(respBody), &errResp)
|
decodeErr = json.Unmarshal([]byte(respBody), &errResp)
|
||||||
if decodeErr != nil {
|
if decodeErr != nil {
|
||||||
return "", sysError(decodeErr)
|
return "", sysError(decodeErr)
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,7 +1,31 @@
|
||||||
package onedrive
|
package onedrive
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
model "github.com/HFO4/cloudreve/models"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func TestNewClient(t *testing.T) {
|
func TestNewClient(t *testing.T) {
|
||||||
|
asserts := assert.New(t)
|
||||||
|
// getOAuthEndpoint失败
|
||||||
|
{
|
||||||
|
policy := model.Policy{
|
||||||
|
BaseURL: string([]byte{0x7f}),
|
||||||
|
}
|
||||||
|
res, err := NewClient(&policy)
|
||||||
|
asserts.Error(err)
|
||||||
|
asserts.Nil(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 成功
|
||||||
|
{
|
||||||
|
policy := model.Policy{}
|
||||||
|
res, err := NewClient(&policy)
|
||||||
|
asserts.NoError(err)
|
||||||
|
asserts.NotNil(res)
|
||||||
|
asserts.NotNil(res.Credential)
|
||||||
|
asserts.NotNil(res.Endpoints)
|
||||||
|
asserts.NotNil(res.Endpoints.OAuthEndpoints)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,303 @@
|
||||||
|
package onedrive
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"github.com/DATA-DOG/go-sqlmock"
|
||||||
|
model "github.com/HFO4/cloudreve/models"
|
||||||
|
"github.com/HFO4/cloudreve/pkg/cache"
|
||||||
|
"github.com/HFO4/cloudreve/pkg/filesystem/fsctx"
|
||||||
|
"github.com/HFO4/cloudreve/pkg/request"
|
||||||
|
"github.com/HFO4/cloudreve/pkg/serializer"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
testMock "github.com/stretchr/testify/mock"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDriver_Token(t *testing.T) {
|
||||||
|
asserts := assert.New(t)
|
||||||
|
handler := Driver{
|
||||||
|
Policy: &model.Policy{
|
||||||
|
AccessKey: "ak",
|
||||||
|
SecretKey: "sk",
|
||||||
|
BucketName: "test",
|
||||||
|
Server: "test.com",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// 无法获取文件路径
|
||||||
|
{
|
||||||
|
ctx := context.WithValue(context.Background(), fsctx.FileSizeCtx, uint64(10))
|
||||||
|
res, err := handler.Token(ctx, 10, "key")
|
||||||
|
asserts.Error(err)
|
||||||
|
asserts.Equal(serializer.UploadCredential{}, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 无法获取文件大小
|
||||||
|
{
|
||||||
|
ctx := context.WithValue(context.Background(), fsctx.SavePathCtx, "/123")
|
||||||
|
res, err := handler.Token(ctx, 10, "key")
|
||||||
|
asserts.Error(err)
|
||||||
|
asserts.Equal(serializer.UploadCredential{}, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 小文件成功
|
||||||
|
{
|
||||||
|
ctx := context.WithValue(context.Background(), fsctx.SavePathCtx, "/123")
|
||||||
|
ctx = context.WithValue(ctx, fsctx.FileSizeCtx, uint64(10))
|
||||||
|
res, err := handler.Token(ctx, 10, "key")
|
||||||
|
asserts.NoError(err)
|
||||||
|
asserts.Equal(serializer.UploadCredential{}, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 分片上传 失败
|
||||||
|
{
|
||||||
|
cache.Set("setting_siteURL", "http://test.cloudreve.org", 0)
|
||||||
|
handler.Client, _ = NewClient(&model.Policy{})
|
||||||
|
handler.Client.Credential.ExpiresIn = time.Now().Add(time.Duration(100) * time.Hour).Unix()
|
||||||
|
clientMock := ClientMock{}
|
||||||
|
clientMock.On(
|
||||||
|
"Request",
|
||||||
|
"POST",
|
||||||
|
testMock.Anything,
|
||||||
|
testMock.Anything,
|
||||||
|
testMock.Anything,
|
||||||
|
).Return(&request.Response{
|
||||||
|
Err: nil,
|
||||||
|
Response: &http.Response{
|
||||||
|
StatusCode: 400,
|
||||||
|
Body: ioutil.NopCloser(strings.NewReader(`{"uploadUrl":"123321"}`)),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
handler.Client.Request = clientMock
|
||||||
|
ctx := context.WithValue(context.Background(), fsctx.SavePathCtx, "/123")
|
||||||
|
ctx = context.WithValue(ctx, fsctx.FileSizeCtx, uint64(20*1024*1024))
|
||||||
|
res, err := handler.Token(ctx, 10, "key")
|
||||||
|
asserts.Error(err)
|
||||||
|
asserts.Equal(serializer.UploadCredential{}, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 分片上传 成功
|
||||||
|
{
|
||||||
|
cache.Set("setting_siteURL", "http://test.cloudreve.org", 0)
|
||||||
|
cache.Set("setting_onedrive_monitor_timeout", "600", 0)
|
||||||
|
cache.Set("setting_onedrive_callback_check", "20", 0)
|
||||||
|
handler.Client, _ = NewClient(&model.Policy{})
|
||||||
|
handler.Client.Credential.ExpiresIn = time.Now().Add(time.Duration(100) * time.Hour).Unix()
|
||||||
|
handler.Client.Credential.AccessToken = "1"
|
||||||
|
clientMock := ClientMock{}
|
||||||
|
clientMock.On(
|
||||||
|
"Request",
|
||||||
|
"POST",
|
||||||
|
testMock.Anything,
|
||||||
|
testMock.Anything,
|
||||||
|
testMock.Anything,
|
||||||
|
).Return(&request.Response{
|
||||||
|
Err: nil,
|
||||||
|
Response: &http.Response{
|
||||||
|
StatusCode: 200,
|
||||||
|
Body: ioutil.NopCloser(strings.NewReader(`{"uploadUrl":"123321"}`)),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
handler.Client.Request = clientMock
|
||||||
|
ctx := context.WithValue(context.Background(), fsctx.SavePathCtx, "/123")
|
||||||
|
ctx = context.WithValue(ctx, fsctx.FileSizeCtx, uint64(20*1024*1024))
|
||||||
|
go func() {
|
||||||
|
time.Sleep(time.Duration(1) * time.Second)
|
||||||
|
FinishCallback("key")
|
||||||
|
}()
|
||||||
|
res, err := handler.Token(ctx, 10, "key")
|
||||||
|
asserts.NoError(err)
|
||||||
|
asserts.Equal("123321", res.Policy)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDriver_Source(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.ExpiresIn = time.Now().Add(time.Duration(100) * time.Hour).Unix()
|
||||||
|
|
||||||
|
// 失败
|
||||||
|
{
|
||||||
|
res, err := handler.Source(context.Background(), "123.jpg", url.URL{}, 0, true, 0)
|
||||||
|
asserts.Error(err)
|
||||||
|
asserts.Empty(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 成功
|
||||||
|
{
|
||||||
|
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(`{"@microsoft.graph.downloadUrl":"123321"}`)),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
handler.Client.Request = clientMock
|
||||||
|
handler.Client.Credential.AccessToken = "1"
|
||||||
|
res, err := handler.Source(context.Background(), "123.jpg", url.URL{}, 0, true, 0)
|
||||||
|
asserts.NoError(err)
|
||||||
|
asserts.Equal("123321", res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDriver_Thumb(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.ExpiresIn = time.Now().Add(time.Duration(100) * time.Hour).Unix()
|
||||||
|
|
||||||
|
// 失败
|
||||||
|
{
|
||||||
|
ctx := context.WithValue(context.Background(), fsctx.ThumbSizeCtx, [2]uint{10, 20})
|
||||||
|
ctx = context.WithValue(ctx, fsctx.FileModelCtx, model.File{})
|
||||||
|
mock.ExpectBegin()
|
||||||
|
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||||
|
mock.ExpectCommit()
|
||||||
|
res, err := handler.Thumb(ctx, "123.jpg")
|
||||||
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
|
asserts.Error(err)
|
||||||
|
asserts.Empty(res.URL)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 上下文错误
|
||||||
|
{
|
||||||
|
_, err := handler.Thumb(context.Background(), "123.jpg")
|
||||||
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
|
asserts.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDriver_Delete(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.ExpiresIn = time.Now().Add(time.Duration(100) * time.Hour).Unix()
|
||||||
|
|
||||||
|
// 失败
|
||||||
|
{
|
||||||
|
_, err := handler.Delete(context.Background(), []string{"1"})
|
||||||
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
|
asserts.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDriver_Put(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.ExpiresIn = time.Now().Add(time.Duration(100) * time.Hour).Unix()
|
||||||
|
|
||||||
|
// 失败
|
||||||
|
{
|
||||||
|
err := handler.Put(context.Background(), ioutil.NopCloser(strings.NewReader("")), "dst", 0)
|
||||||
|
asserts.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDriver_Get(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.ExpiresIn = time.Now().Add(time.Duration(100) * time.Hour).Unix()
|
||||||
|
|
||||||
|
// 无法获取source
|
||||||
|
{
|
||||||
|
res, err := handler.Get(context.Background(), "123.txt")
|
||||||
|
asserts.Error(err)
|
||||||
|
asserts.Nil(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 成功
|
||||||
|
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(`{"@microsoft.graph.downloadUrl":"123321"}`)),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
handler.Client.Request = clientMock
|
||||||
|
handler.Client.Credential.AccessToken = "1"
|
||||||
|
|
||||||
|
driverClientMock := ClientMock{}
|
||||||
|
driverClientMock.On(
|
||||||
|
"Request",
|
||||||
|
"GET",
|
||||||
|
testMock.Anything,
|
||||||
|
testMock.Anything,
|
||||||
|
testMock.Anything,
|
||||||
|
).Return(&request.Response{
|
||||||
|
Err: nil,
|
||||||
|
Response: &http.Response{
|
||||||
|
StatusCode: 200,
|
||||||
|
Body: ioutil.NopCloser(strings.NewReader(`123`)),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
handler.HTTPClient = driverClientMock
|
||||||
|
res, err := handler.Get(context.Background(), "123.txt")
|
||||||
|
clientMock.AssertExpectations(t)
|
||||||
|
asserts.NoError(err)
|
||||||
|
_, err = res.Seek(0, io.SeekEnd)
|
||||||
|
asserts.NoError(err)
|
||||||
|
content, err := ioutil.ReadAll(res)
|
||||||
|
asserts.NoError(err)
|
||||||
|
asserts.Equal("123", string(content))
|
||||||
|
}
|
|
@ -26,7 +26,7 @@ func (handler Driver) Get(ctx context.Context, path string) (response.RSCloser,
|
||||||
ctx,
|
ctx,
|
||||||
path,
|
path,
|
||||||
url.URL{},
|
url.URL{},
|
||||||
int64(model.GetIntSetting("preview_timeout", 60)),
|
60,
|
||||||
false,
|
false,
|
||||||
0,
|
0,
|
||||||
)
|
)
|
||||||
|
|
|
@ -2,7 +2,6 @@ package onedrive
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/gob"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/HFO4/cloudreve/pkg/cache"
|
"github.com/HFO4/cloudreve/pkg/cache"
|
||||||
"github.com/HFO4/cloudreve/pkg/request"
|
"github.com/HFO4/cloudreve/pkg/request"
|
||||||
|
@ -14,33 +13,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// oauthEndpoint OAuth接口地址
|
|
||||||
type oauthEndpoint struct {
|
|
||||||
token url.URL
|
|
||||||
authorize url.URL
|
|
||||||
}
|
|
||||||
|
|
||||||
// Credential 获取token时返回的凭证
|
|
||||||
type Credential struct {
|
|
||||||
TokenType string `json:"token_type"`
|
|
||||||
ExpiresIn int64 `json:"expires_in"`
|
|
||||||
Scope string `json:"scope"`
|
|
||||||
AccessToken string `json:"access_token"`
|
|
||||||
RefreshToken string `json:"refresh_token"`
|
|
||||||
UserID string `json:"user_id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// OAuthError OAuth相关接口的错误响应
|
|
||||||
type OAuthError struct {
|
|
||||||
ErrorType string `json:"error"`
|
|
||||||
ErrorDescription string `json:"error_description"`
|
|
||||||
CorrelationID string `json:"correlation_id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
gob.Register(Credential{})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Error 实现error接口
|
// Error 实现error接口
|
||||||
func (err OAuthError) Error() string {
|
func (err OAuthError) Error() string {
|
||||||
return err.ErrorDescription
|
return err.ErrorDescription
|
||||||
|
|
|
@ -38,13 +38,6 @@ func WithConflictBehavior(t string) Option {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithExpires 设置过期时间
|
|
||||||
func WithExpires(t time.Time) Option {
|
|
||||||
return optionFunc(func(o *options) {
|
|
||||||
o.expires = t
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f optionFunc) apply(o *options) {
|
func (f optionFunc) apply(o *options) {
|
||||||
f(o)
|
f(o)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package onedrive
|
package onedrive
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/gob"
|
||||||
"io"
|
"io"
|
||||||
|
"net/url"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -91,6 +93,33 @@ type Chunk struct {
|
||||||
Reader io.Reader
|
Reader io.Reader
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// oauthEndpoint OAuth接口地址
|
||||||
|
type oauthEndpoint struct {
|
||||||
|
token url.URL
|
||||||
|
authorize url.URL
|
||||||
|
}
|
||||||
|
|
||||||
|
// Credential 获取token时返回的凭证
|
||||||
|
type Credential struct {
|
||||||
|
TokenType string `json:"token_type"`
|
||||||
|
ExpiresIn int64 `json:"expires_in"`
|
||||||
|
Scope string `json:"scope"`
|
||||||
|
AccessToken string `json:"access_token"`
|
||||||
|
RefreshToken string `json:"refresh_token"`
|
||||||
|
UserID string `json:"user_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// OAuthError OAuth相关接口的错误响应
|
||||||
|
type OAuthError struct {
|
||||||
|
ErrorType string `json:"error"`
|
||||||
|
ErrorDescription string `json:"error_description"`
|
||||||
|
CorrelationID string `json:"correlation_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
gob.Register(Credential{})
|
||||||
|
}
|
||||||
|
|
||||||
// IsLast 返回是否为最后一个分片
|
// IsLast 返回是否为最后一个分片
|
||||||
func (chunk *Chunk) IsLast() bool {
|
func (chunk *Chunk) IsLast() bool {
|
||||||
return chunk.Total-chunk.Offset == chunk.ChunkSize
|
return chunk.Total-chunk.Offset == chunk.ChunkSize
|
||||||
|
|
Loading…
Reference in New Issue