Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
hexbee
Cloudreve
提交
8672042d
C
Cloudreve
项目概览
hexbee
/
Cloudreve
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
Cloudreve
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
8672042d
编写于
1月 22, 2020
作者:
H
HFO4
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Test: onedrive related
上级
0d2e3cc7
变更
8
展开全部
隐藏空白更改
内联
并排
Showing
8 changed file
with
1372 addition
and
39 deletion
+1372
-39
pkg/filesystem/driver/onedrive/api.go
pkg/filesystem/driver/onedrive/api.go
+2
-2
pkg/filesystem/driver/onedrive/api_test.go
pkg/filesystem/driver/onedrive/api_test.go
+1012
-0
pkg/filesystem/driver/onedrive/client_test.go
pkg/filesystem/driver/onedrive/client_test.go
+25
-1
pkg/filesystem/driver/onedrive/handler_test.go
pkg/filesystem/driver/onedrive/handler_test.go
+303
-0
pkg/filesystem/driver/onedrive/handller.go
pkg/filesystem/driver/onedrive/handller.go
+1
-1
pkg/filesystem/driver/onedrive/oauth.go
pkg/filesystem/driver/onedrive/oauth.go
+0
-28
pkg/filesystem/driver/onedrive/options.go
pkg/filesystem/driver/onedrive/options.go
+0
-7
pkg/filesystem/driver/onedrive/types.go
pkg/filesystem/driver/onedrive/types.go
+29
-0
未找到文件。
pkg/filesystem/driver/onedrive/api.go
浏览文件 @
8672042d
...
...
@@ -393,7 +393,7 @@ func (client *Client) MonitorUpload(uploadURL, callbackKey, path string, size ui
cache
.
Deletes
([]
string
{
callbackKey
},
"callback_"
)
_
,
err
=
client
.
Delete
(
context
.
Background
(),
[]
string
{
path
})
if
err
!=
nil
{
util
.
Log
()
.
Warning
(
"无法删除未回
掉
的文件,%s"
,
err
)
util
.
Log
()
.
Warning
(
"无法删除未回
调
的文件,%s"
,
err
)
}
}
return
...
...
@@ -486,7 +486,7 @@ func (client *Client) request(ctx context.Context, method string, url string, bo
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
)
if
decodeErr
!=
nil
{
return
""
,
sysError
(
decodeErr
)
...
...
pkg/filesystem/driver/onedrive/api_test.go
0 → 100644
浏览文件 @
8672042d
此差异已折叠。
点击以展开。
pkg/filesystem/driver/onedrive/client_test.go
浏览文件 @
8672042d
package
onedrive
import
"testing"
import
(
model
"github.com/HFO4/cloudreve/models"
"github.com/stretchr/testify/assert"
"testing"
)
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
)
}
}
pkg/filesystem/driver/onedrive/handler_test.go
0 → 100644
浏览文件 @
8672042d
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
))
}
pkg/filesystem/driver/onedrive/handller.go
浏览文件 @
8672042d
...
...
@@ -26,7 +26,7 @@ func (handler Driver) Get(ctx context.Context, path string) (response.RSCloser,
ctx
,
path
,
url
.
URL
{},
int64
(
model
.
GetIntSetting
(
"preview_timeout"
,
60
))
,
60
,
false
,
0
,
)
...
...
pkg/filesystem/driver/onedrive/oauth.go
浏览文件 @
8672042d
...
...
@@ -2,7 +2,6 @@ package onedrive
import
(
"context"
"encoding/gob"
"encoding/json"
"github.com/HFO4/cloudreve/pkg/cache"
"github.com/HFO4/cloudreve/pkg/request"
...
...
@@ -14,33 +13,6 @@ import (
"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接口
func
(
err
OAuthError
)
Error
()
string
{
return
err
.
ErrorDescription
...
...
pkg/filesystem/driver/onedrive/options.go
浏览文件 @
8672042d
...
...
@@ -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
)
{
f
(
o
)
}
...
...
pkg/filesystem/driver/onedrive/types.go
浏览文件 @
8672042d
package
onedrive
import
(
"encoding/gob"
"io"
"net/url"
"sync"
)
...
...
@@ -91,6 +93,33 @@ type Chunk struct {
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 返回是否为最后一个分片
func
(
chunk
*
Chunk
)
IsLast
()
bool
{
return
chunk
.
Total
-
chunk
.
Offset
==
chunk
.
ChunkSize
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录