Test: Share model
This commit is contained in:
parent
437bdf46d4
commit
b49202e7e1
9 changed files with 399 additions and 38 deletions
|
@ -167,7 +167,7 @@ func TestPolicy_GetUploadURL(t *testing.T) {
|
||||||
{
|
{
|
||||||
cache.Set("setting_siteURL", "http://127.0.0.1", 0)
|
cache.Set("setting_siteURL", "http://127.0.0.1", 0)
|
||||||
policy := Policy{Type: "local", Server: "http://127.0.0.1"}
|
policy := Policy{Type: "local", Server: "http://127.0.0.1"}
|
||||||
asserts.Equal("http://127.0.0.1/api/v3/file/upload", policy.GetUploadURL())
|
asserts.Equal("/api/v3/file/upload", policy.GetUploadURL())
|
||||||
}
|
}
|
||||||
|
|
||||||
// 远程
|
// 远程
|
||||||
|
|
|
@ -33,7 +33,6 @@ type Share struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create 创建分享
|
// Create 创建分享
|
||||||
// TODO 测试
|
|
||||||
func (share *Share) Create() (uint, error) {
|
func (share *Share) Create() (uint, error) {
|
||||||
if err := DB.Create(share).Error; err != nil {
|
if err := DB.Create(share).Error; err != nil {
|
||||||
util.Log().Warning("无法插入数据库记录, %s", err)
|
util.Log().Warning("无法插入数据库记录, %s", err)
|
||||||
|
@ -43,7 +42,6 @@ func (share *Share) Create() (uint, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetShareByHashID 根据HashID查找分享
|
// GetShareByHashID 根据HashID查找分享
|
||||||
// TODO 测试
|
|
||||||
func GetShareByHashID(hashID string) *Share {
|
func GetShareByHashID(hashID string) *Share {
|
||||||
id, err := hashid.DecodeHashID(hashID, hashid.ShareID)
|
id, err := hashid.DecodeHashID(hashID, hashid.ShareID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -59,7 +57,6 @@ func GetShareByHashID(hashID string) *Share {
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsAvailable 返回此分享是否可用(是否过期)
|
// IsAvailable 返回此分享是否可用(是否过期)
|
||||||
// TODO 测试
|
|
||||||
func (share *Share) IsAvailable() bool {
|
func (share *Share) IsAvailable() bool {
|
||||||
if share.RemainDownloads == 0 {
|
if share.RemainDownloads == 0 {
|
||||||
return false
|
return false
|
||||||
|
@ -71,37 +68,38 @@ func (share *Share) IsAvailable() bool {
|
||||||
// 检查源对象是否存在
|
// 检查源对象是否存在
|
||||||
var sourceID uint
|
var sourceID uint
|
||||||
if share.IsDir {
|
if share.IsDir {
|
||||||
folder := share.GetSourceFolder()
|
folder := share.SourceFolder()
|
||||||
sourceID = folder.ID
|
sourceID = folder.ID
|
||||||
} else {
|
} else {
|
||||||
file := share.GetSourceFile()
|
file := share.SourceFile()
|
||||||
sourceID = file.ID
|
sourceID = file.ID
|
||||||
}
|
}
|
||||||
if sourceID == 0 {
|
if sourceID == 0 {
|
||||||
|
// TODO 是否要在这里删除这个无效分享?
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetCreator 获取分享的创建者
|
// Creator 获取分享的创建者
|
||||||
func (share *Share) GetCreator() *User {
|
func (share *Share) Creator() *User {
|
||||||
if share.User.ID == 0 {
|
if share.User.ID == 0 {
|
||||||
share.User, _ = GetUserByID(share.UserID)
|
share.User, _ = GetUserByID(share.UserID)
|
||||||
}
|
}
|
||||||
return &share.User
|
return &share.User
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSource 返回源对象
|
// Source 返回源对象
|
||||||
func (share *Share) GetSource() interface{} {
|
func (share *Share) Source() interface{} {
|
||||||
if share.IsDir {
|
if share.IsDir {
|
||||||
return share.GetSourceFolder()
|
return share.SourceFolder()
|
||||||
}
|
}
|
||||||
return share.GetSourceFile()
|
return share.SourceFile()
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSourceFolder 获取源目录
|
// SourceFolder 获取源目录
|
||||||
func (share *Share) GetSourceFolder() *Folder {
|
func (share *Share) SourceFolder() *Folder {
|
||||||
if share.Folder.ID == 0 {
|
if share.Folder.ID == 0 {
|
||||||
folders, _ := GetFoldersByIDs([]uint{share.SourceID}, share.UserID)
|
folders, _ := GetFoldersByIDs([]uint{share.SourceID}, share.UserID)
|
||||||
if len(folders) > 0 {
|
if len(folders) > 0 {
|
||||||
|
@ -111,8 +109,8 @@ func (share *Share) GetSourceFolder() *Folder {
|
||||||
return &share.Folder
|
return &share.Folder
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSourceFile 获取源文件
|
// SourceFile 获取源文件
|
||||||
func (share *Share) GetSourceFile() *File {
|
func (share *Share) SourceFile() *File {
|
||||||
if share.File.ID == 0 {
|
if share.File.ID == 0 {
|
||||||
files, _ := GetFilesByIDs([]uint{share.SourceID}, share.UserID)
|
files, _ := GetFilesByIDs([]uint{share.SourceID}, share.UserID)
|
||||||
if len(files) > 0 {
|
if len(files) > 0 {
|
||||||
|
@ -182,7 +180,7 @@ func (share *Share) Purchase(user *User) error {
|
||||||
|
|
||||||
scoreRate := GetIntSetting("share_score_rate", 100)
|
scoreRate := GetIntSetting("share_score_rate", 100)
|
||||||
gainedScore := int(math.Ceil(float64(share.Score*scoreRate) / 100))
|
gainedScore := int(math.Ceil(float64(share.Score*scoreRate) / 100))
|
||||||
share.GetCreator().AddScore(gainedScore)
|
share.Creator().AddScore(gainedScore)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
322
models/share_test.go
Normal file
322
models/share_test.go
Normal file
|
@ -0,0 +1,322 @@
|
||||||
|
package model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"github.com/DATA-DOG/go-sqlmock"
|
||||||
|
"github.com/HFO4/cloudreve/pkg/cache"
|
||||||
|
"github.com/HFO4/cloudreve/pkg/conf"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestShare_Create(t *testing.T) {
|
||||||
|
asserts := assert.New(t)
|
||||||
|
share := Share{UserID: 1}
|
||||||
|
|
||||||
|
// 成功
|
||||||
|
{
|
||||||
|
mock.ExpectBegin()
|
||||||
|
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(2, 1))
|
||||||
|
mock.ExpectCommit()
|
||||||
|
id, err := share.Create()
|
||||||
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
|
asserts.NoError(err)
|
||||||
|
asserts.EqualValues(2, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 失败
|
||||||
|
{
|
||||||
|
mock.ExpectBegin()
|
||||||
|
mock.ExpectExec("INSERT(.+)").WillReturnError(errors.New("error"))
|
||||||
|
mock.ExpectRollback()
|
||||||
|
id, err := share.Create()
|
||||||
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
|
asserts.Error(err)
|
||||||
|
asserts.EqualValues(0, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetShareByHashID(t *testing.T) {
|
||||||
|
asserts := assert.New(t)
|
||||||
|
conf.SystemConfig.HashIDSalt = ""
|
||||||
|
|
||||||
|
// 成功
|
||||||
|
{
|
||||||
|
mock.ExpectQuery("SELECT(.+)").
|
||||||
|
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||||
|
res := GetShareByHashID("x9T4")
|
||||||
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
|
asserts.NotNil(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查询失败
|
||||||
|
{
|
||||||
|
mock.ExpectQuery("SELECT(.+)").
|
||||||
|
WillReturnError(errors.New("error"))
|
||||||
|
res := GetShareByHashID("x9T4")
|
||||||
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
|
asserts.Nil(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ID解码失败
|
||||||
|
{
|
||||||
|
res := GetShareByHashID("empty")
|
||||||
|
asserts.Nil(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestShare_IsAvailable(t *testing.T) {
|
||||||
|
asserts := assert.New(t)
|
||||||
|
|
||||||
|
// 下载剩余次数为0
|
||||||
|
{
|
||||||
|
share := Share{}
|
||||||
|
asserts.False(share.IsAvailable())
|
||||||
|
}
|
||||||
|
|
||||||
|
// 时效过期
|
||||||
|
{
|
||||||
|
expires := time.Unix(10, 10)
|
||||||
|
share := Share{
|
||||||
|
RemainDownloads: -1,
|
||||||
|
Expires: &expires,
|
||||||
|
}
|
||||||
|
asserts.False(share.IsAvailable())
|
||||||
|
}
|
||||||
|
|
||||||
|
// 源对象为目录,但不存在
|
||||||
|
{
|
||||||
|
share := Share{
|
||||||
|
RemainDownloads: -1,
|
||||||
|
SourceID: 2,
|
||||||
|
IsDir: true,
|
||||||
|
}
|
||||||
|
mock.ExpectQuery("SELECT(.+)").
|
||||||
|
WillReturnRows(sqlmock.NewRows([]string{"id"}))
|
||||||
|
asserts.False(share.IsAvailable())
|
||||||
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
|
}
|
||||||
|
|
||||||
|
// 源对象为目录,存在
|
||||||
|
{
|
||||||
|
share := Share{
|
||||||
|
RemainDownloads: -1,
|
||||||
|
SourceID: 2,
|
||||||
|
IsDir: false,
|
||||||
|
}
|
||||||
|
mock.ExpectQuery("SELECT(.+)files(.+)").
|
||||||
|
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(13))
|
||||||
|
asserts.True(share.IsAvailable())
|
||||||
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestShare_GetCreator(t *testing.T) {
|
||||||
|
asserts := assert.New(t)
|
||||||
|
|
||||||
|
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||||
|
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||||
|
share := Share{UserID: 1}
|
||||||
|
res := share.Creator()
|
||||||
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
|
asserts.EqualValues(1, res.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestShare_Source(t *testing.T) {
|
||||||
|
asserts := assert.New(t)
|
||||||
|
|
||||||
|
// 目录
|
||||||
|
{
|
||||||
|
share := Share{IsDir: true, SourceID: 3}
|
||||||
|
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(3))
|
||||||
|
asserts.EqualValues(3, share.Source().(*Folder).ID)
|
||||||
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
|
}
|
||||||
|
|
||||||
|
// 文件
|
||||||
|
{
|
||||||
|
share := Share{IsDir: false, SourceID: 3}
|
||||||
|
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(3))
|
||||||
|
asserts.EqualValues(3, share.Source().(*File).ID)
|
||||||
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestShare_CanBeDownloadBy(t *testing.T) {
|
||||||
|
asserts := assert.New(t)
|
||||||
|
share := Share{}
|
||||||
|
|
||||||
|
// 未登录,无权
|
||||||
|
{
|
||||||
|
user := &User{
|
||||||
|
Group: Group{
|
||||||
|
OptionsSerialized: GroupOption{
|
||||||
|
ShareDownloadEnabled: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
asserts.Error(share.CanBeDownloadBy(user))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 已登录,无权
|
||||||
|
{
|
||||||
|
user := &User{
|
||||||
|
Model: gorm.Model{ID: 1},
|
||||||
|
Group: Group{
|
||||||
|
OptionsSerialized: GroupOption{
|
||||||
|
ShareDownloadEnabled: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
asserts.Error(share.CanBeDownloadBy(user))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 未登录,需要积分
|
||||||
|
{
|
||||||
|
user := &User{
|
||||||
|
Group: Group{
|
||||||
|
OptionsSerialized: GroupOption{
|
||||||
|
ShareDownloadEnabled: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
share.Score = 1
|
||||||
|
asserts.Error(share.CanBeDownloadBy(user))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 成功
|
||||||
|
{
|
||||||
|
user := &User{
|
||||||
|
Model: gorm.Model{ID: 1},
|
||||||
|
Group: Group{
|
||||||
|
OptionsSerialized: GroupOption{
|
||||||
|
ShareDownloadEnabled: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
share.Score = 1
|
||||||
|
asserts.NoError(share.CanBeDownloadBy(user))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestShare_WasDownloadedBy(t *testing.T) {
|
||||||
|
asserts := assert.New(t)
|
||||||
|
share := Share{
|
||||||
|
Model: gorm.Model{ID: 1},
|
||||||
|
}
|
||||||
|
|
||||||
|
// 已登录,已下载
|
||||||
|
{
|
||||||
|
user := User{
|
||||||
|
Model: gorm.Model{
|
||||||
|
ID: 1,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
r := httptest.NewRecorder()
|
||||||
|
c, _ := gin.CreateTestContext(r)
|
||||||
|
cache.Set("share_1_1", true, 0)
|
||||||
|
asserts.True(share.WasDownloadedBy(&user, c))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestShare_DownloadBy(t *testing.T) {
|
||||||
|
asserts := assert.New(t)
|
||||||
|
share := Share{
|
||||||
|
Model: gorm.Model{ID: 1},
|
||||||
|
}
|
||||||
|
user := User{
|
||||||
|
Model: gorm.Model{
|
||||||
|
ID: 1,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
cache.Deletes([]string{"1_1"}, "share_")
|
||||||
|
r := httptest.NewRecorder()
|
||||||
|
c, _ := gin.CreateTestContext(r)
|
||||||
|
|
||||||
|
mock.ExpectBegin()
|
||||||
|
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||||
|
mock.ExpectCommit()
|
||||||
|
|
||||||
|
err := share.DownloadBy(&user, c)
|
||||||
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
|
asserts.NoError(err)
|
||||||
|
_, ok := cache.Get("share_1_1")
|
||||||
|
asserts.True(ok)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestShare_Purchase(t *testing.T) {
|
||||||
|
asserts := assert.New(t)
|
||||||
|
|
||||||
|
// 不需要购买
|
||||||
|
{
|
||||||
|
share := Share{}
|
||||||
|
user := User{}
|
||||||
|
asserts.NoError(share.Purchase(&user))
|
||||||
|
|
||||||
|
share.Score = 1
|
||||||
|
user.Group.OptionsSerialized.ShareFreeEnabled = true
|
||||||
|
asserts.NoError(share.Purchase(&user))
|
||||||
|
|
||||||
|
user.Group.OptionsSerialized.ShareFreeEnabled = false
|
||||||
|
share.UserID = 1
|
||||||
|
user.ID = 1
|
||||||
|
asserts.NoError(share.Purchase(&user))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 积分不足
|
||||||
|
{
|
||||||
|
share := Share{
|
||||||
|
Score: 1,
|
||||||
|
UserID: 2,
|
||||||
|
}
|
||||||
|
user := User{}
|
||||||
|
asserts.Error(share.Purchase(&user))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 成功
|
||||||
|
{
|
||||||
|
cache.Set("setting_share_score_rate", "80", 0)
|
||||||
|
share := Share{
|
||||||
|
Score: 10,
|
||||||
|
UserID: 2,
|
||||||
|
User: User{
|
||||||
|
Model: gorm.Model{
|
||||||
|
ID: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
user := User{
|
||||||
|
Score: 10,
|
||||||
|
}
|
||||||
|
|
||||||
|
mock.ExpectBegin()
|
||||||
|
mock.ExpectExec("UPDATE(.+)").
|
||||||
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||||
|
mock.ExpectCommit()
|
||||||
|
mock.ExpectBegin()
|
||||||
|
mock.ExpectExec("UPDATE(.+)").
|
||||||
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||||
|
mock.ExpectCommit()
|
||||||
|
asserts.NoError(share.Purchase(&user))
|
||||||
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
|
asserts.EqualValues(0, user.Score)
|
||||||
|
asserts.EqualValues(8, share.User.Score)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestShare_Viewed(t *testing.T) {
|
||||||
|
asserts := assert.New(t)
|
||||||
|
share := Share{}
|
||||||
|
mock.ExpectBegin()
|
||||||
|
mock.ExpectExec("UPDATE(.+)").
|
||||||
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||||
|
mock.ExpectCommit()
|
||||||
|
share.Viewed()
|
||||||
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
|
asserts.EqualValues(1, share.Views)
|
||||||
|
}
|
|
@ -95,7 +95,6 @@ func (user *User) IncreaseStorage(size uint64) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// PayScore 扣除积分,返回是否成功
|
// PayScore 扣除积分,返回是否成功
|
||||||
// todo 测试
|
|
||||||
func (user *User) PayScore(score int) bool {
|
func (user *User) PayScore(score int) bool {
|
||||||
if score == 0 {
|
if score == 0 {
|
||||||
return true
|
return true
|
||||||
|
@ -109,7 +108,6 @@ func (user *User) PayScore(score int) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddScore 增加积分
|
// AddScore 增加积分
|
||||||
// todo 测试
|
|
||||||
func (user *User) AddScore(score int) {
|
func (user *User) AddScore(score int) {
|
||||||
user.Score += score
|
user.Score += score
|
||||||
DB.Model(user).UpdateColumn("score", gorm.Expr("score + ?", score))
|
DB.Model(user).UpdateColumn("score", gorm.Expr("score + ?", score))
|
||||||
|
@ -257,7 +255,6 @@ func (user *User) SetPassword(password string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewAnonymousUser 返回一个匿名用户
|
// NewAnonymousUser 返回一个匿名用户
|
||||||
// TODO 测试
|
|
||||||
func NewAnonymousUser() *User {
|
func NewAnonymousUser() *User {
|
||||||
user := User{}
|
user := User{}
|
||||||
user.Policy.Type = "anonymous"
|
user.Policy.Type = "anonymous"
|
||||||
|
@ -266,7 +263,6 @@ func NewAnonymousUser() *User {
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsAnonymous 返回是否为未登录用户
|
// IsAnonymous 返回是否为未登录用户
|
||||||
// TODO 测试
|
|
||||||
func (user *User) IsAnonymous() bool {
|
func (user *User) IsAnonymous() bool {
|
||||||
return user.ID == 0
|
return user.ID == 0
|
||||||
}
|
}
|
||||||
|
|
|
@ -340,3 +340,48 @@ func TestUser_Root(t *testing.T) {
|
||||||
asserts.Error(err)
|
asserts.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUser_PayScore(t *testing.T) {
|
||||||
|
asserts := assert.New(t)
|
||||||
|
user := User{Score: 5}
|
||||||
|
|
||||||
|
asserts.True(user.PayScore(0))
|
||||||
|
asserts.False(user.PayScore(10))
|
||||||
|
|
||||||
|
mock.ExpectBegin()
|
||||||
|
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||||
|
mock.ExpectCommit()
|
||||||
|
asserts.True(user.PayScore(5))
|
||||||
|
asserts.EqualValues(0, user.Score)
|
||||||
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUser_AddScore(t *testing.T) {
|
||||||
|
asserts := assert.New(t)
|
||||||
|
user := User{Score: 5}
|
||||||
|
|
||||||
|
mock.ExpectBegin()
|
||||||
|
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||||
|
mock.ExpectCommit()
|
||||||
|
user.AddScore(5)
|
||||||
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
|
asserts.EqualValues(10, user.Score)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewAnonymousUser(t *testing.T) {
|
||||||
|
asserts := assert.New(t)
|
||||||
|
|
||||||
|
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(3))
|
||||||
|
user := NewAnonymousUser()
|
||||||
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
|
asserts.NotNil(user)
|
||||||
|
asserts.EqualValues(3, user.Group.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUser_IsAnonymous(t *testing.T) {
|
||||||
|
asserts := assert.New(t)
|
||||||
|
user := User{}
|
||||||
|
asserts.True(user.IsAnonymous())
|
||||||
|
user.ID = 1
|
||||||
|
asserts.False(user.IsAnonymous())
|
||||||
|
}
|
||||||
|
|
|
@ -240,7 +240,7 @@ func (fs *FileSystem) GetDownloadURL(ctx context.Context, path string, timeout s
|
||||||
return source, nil
|
return source, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSource 获取可直接访问文件的外链地址
|
// Source 获取可直接访问文件的外链地址
|
||||||
func (fs *FileSystem) GetSource(ctx context.Context, fileID uint) (string, error) {
|
func (fs *FileSystem) GetSource(ctx context.Context, fileID uint) (string, error) {
|
||||||
// 查找文件记录
|
// 查找文件记录
|
||||||
err := fs.resetFileIDIfNotExist(ctx, fileID)
|
err := fs.resetFileIDIfNotExist(ctx, fileID)
|
||||||
|
|
|
@ -34,7 +34,7 @@ type shareSource struct {
|
||||||
|
|
||||||
// BuildShareResponse 构建获取分享信息响应
|
// BuildShareResponse 构建获取分享信息响应
|
||||||
func BuildShareResponse(share *model.Share, unlocked bool) Share {
|
func BuildShareResponse(share *model.Share, unlocked bool) Share {
|
||||||
creator := share.GetCreator()
|
creator := share.Creator()
|
||||||
resp := Share{
|
resp := Share{
|
||||||
Key: hashid.HashID(share.ID, hashid.ShareID),
|
Key: hashid.HashID(share.ID, hashid.ShareID),
|
||||||
Locked: !unlocked,
|
Locked: !unlocked,
|
||||||
|
@ -62,13 +62,13 @@ func BuildShareResponse(share *model.Share, unlocked bool) Share {
|
||||||
}
|
}
|
||||||
|
|
||||||
if share.IsDir {
|
if share.IsDir {
|
||||||
source := share.GetSourceFolder()
|
source := share.SourceFolder()
|
||||||
resp.Source = &shareSource{
|
resp.Source = &shareSource{
|
||||||
Name: source.Name,
|
Name: source.Name,
|
||||||
Size: 0,
|
Size: 0,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
source := share.GetSourceFile()
|
source := share.SourceFile()
|
||||||
resp.Source = &shareSource{
|
resp.Source = &shareSource{
|
||||||
Name: source.Name,
|
Name: source.Name,
|
||||||
Size: source.Size,
|
Size: source.Size,
|
||||||
|
|
|
@ -64,7 +64,7 @@ func AnonymousGetContent(c *gin.Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSource 获取文件的外链地址
|
// Source 获取文件的外链地址
|
||||||
func GetSource(c *gin.Context) {
|
func GetSource(c *gin.Context) {
|
||||||
// 创建上下文
|
// 创建上下文
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
|
@ -85,7 +85,7 @@ func (service *Service) CreateDownloadSession(c *gin.Context) serializer.Respons
|
||||||
defer fs.Recycle()
|
defer fs.Recycle()
|
||||||
|
|
||||||
// 重设文件系统处理目标为源文件
|
// 重设文件系统处理目标为源文件
|
||||||
err = fs.SetTargetByInterface(share.GetSource())
|
err = fs.SetTargetByInterface(share.Source())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return serializer.Err(serializer.CodePolicyNotAllowed, "源文件不存在", err)
|
return serializer.Err(serializer.CodePolicyNotAllowed, "源文件不存在", err)
|
||||||
}
|
}
|
||||||
|
@ -115,9 +115,9 @@ func (service *Service) PreviewContent(ctx context.Context, c *gin.Context, isTe
|
||||||
|
|
||||||
// 用于调下层service
|
// 用于调下层service
|
||||||
if share.IsDir {
|
if share.IsDir {
|
||||||
ctx = context.WithValue(ctx, fsctx.FolderModelCtx, share.GetSource())
|
ctx = context.WithValue(ctx, fsctx.FolderModelCtx, share.Source())
|
||||||
} else {
|
} else {
|
||||||
ctx = context.WithValue(ctx, fsctx.FileModelCtx, share.GetSource())
|
ctx = context.WithValue(ctx, fsctx.FileModelCtx, share.Source())
|
||||||
}
|
}
|
||||||
subService := explorer.SingleFileService{
|
subService := explorer.SingleFileService{
|
||||||
Path: service.Path,
|
Path: service.Path,
|
||||||
|
@ -134,9 +134,9 @@ func (service *Service) CreateDocPreviewSession(c *gin.Context) serializer.Respo
|
||||||
// 用于调下层service
|
// 用于调下层service
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
if share.IsDir {
|
if share.IsDir {
|
||||||
ctx = context.WithValue(ctx, fsctx.FolderModelCtx, share.GetSource())
|
ctx = context.WithValue(ctx, fsctx.FolderModelCtx, share.Source())
|
||||||
} else {
|
} else {
|
||||||
ctx = context.WithValue(ctx, fsctx.FileModelCtx, share.GetSource())
|
ctx = context.WithValue(ctx, fsctx.FileModelCtx, share.Source())
|
||||||
}
|
}
|
||||||
subService := explorer.SingleFileService{
|
subService := explorer.SingleFileService{
|
||||||
Path: service.Path,
|
Path: service.Path,
|
||||||
|
@ -165,7 +165,7 @@ func (service *Service) SaveToMyFile(c *gin.Context) serializer.Response {
|
||||||
defer fs.Recycle()
|
defer fs.Recycle()
|
||||||
|
|
||||||
// 重设文件系统处理目标为源文件
|
// 重设文件系统处理目标为源文件
|
||||||
err = fs.SetTargetByInterface(share.GetSource())
|
err = fs.SetTargetByInterface(share.Source())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return serializer.Err(serializer.CodePolicyNotAllowed, "源文件不存在", err)
|
return serializer.Err(serializer.CodePolicyNotAllowed, "源文件不存在", err)
|
||||||
}
|
}
|
||||||
|
@ -192,7 +192,7 @@ func (service *Service) List(c *gin.Context) serializer.Response {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建文件系统
|
// 创建文件系统
|
||||||
fs, err := filesystem.NewFileSystem(share.GetCreator())
|
fs, err := filesystem.NewFileSystem(share.Creator())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
|
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
|
||||||
}
|
}
|
||||||
|
@ -203,7 +203,7 @@ func (service *Service) List(c *gin.Context) serializer.Response {
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
// 重设根目录
|
// 重设根目录
|
||||||
fs.Root = share.GetSource().(*model.Folder)
|
fs.Root = share.Source().(*model.Folder)
|
||||||
fs.Root.Name = "/"
|
fs.Root.Name = "/"
|
||||||
|
|
||||||
// 分享Key上下文
|
// 分享Key上下文
|
||||||
|
@ -231,14 +231,14 @@ func (service *Service) Thumb(c *gin.Context) serializer.Response {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建文件系统
|
// 创建文件系统
|
||||||
fs, err := filesystem.NewFileSystem(share.GetCreator())
|
fs, err := filesystem.NewFileSystem(share.Creator())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
|
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
|
||||||
}
|
}
|
||||||
defer fs.Recycle()
|
defer fs.Recycle()
|
||||||
|
|
||||||
// 重设根目录
|
// 重设根目录
|
||||||
fs.Root = share.GetSource().(*model.Folder)
|
fs.Root = share.Source().(*model.Folder)
|
||||||
|
|
||||||
// 找到缩略图的父目录
|
// 找到缩略图的父目录
|
||||||
exist, parent := fs.IsPathExist(service.Path)
|
exist, parent := fs.IsPathExist(service.Path)
|
||||||
|
@ -297,7 +297,7 @@ func (service *ArchiveService) Archive(c *gin.Context) serializer.Response {
|
||||||
defer fs.Recycle()
|
defer fs.Recycle()
|
||||||
|
|
||||||
// 重设根目录
|
// 重设根目录
|
||||||
fs.Root = share.GetSource().(*model.Folder)
|
fs.Root = share.Source().(*model.Folder)
|
||||||
|
|
||||||
// 找到要打包文件的父目录
|
// 找到要打包文件的父目录
|
||||||
exist, parent := fs.IsPathExist(service.Path)
|
exist, parent := fs.IsPathExist(service.Path)
|
||||||
|
@ -309,7 +309,7 @@ func (service *ArchiveService) Archive(c *gin.Context) serializer.Response {
|
||||||
ctx := context.WithValue(context.Background(), fsctx.LimitParentCtx, parent)
|
ctx := context.WithValue(context.Background(), fsctx.LimitParentCtx, parent)
|
||||||
|
|
||||||
// 用于调下层service
|
// 用于调下层service
|
||||||
tempUser := share.GetCreator()
|
tempUser := share.Creator()
|
||||||
tempUser.Group.OptionsSerialized.ArchiveDownloadEnabled = true
|
tempUser.Group.OptionsSerialized.ArchiveDownloadEnabled = true
|
||||||
c.Set("user", tempUser)
|
c.Set("user", tempUser)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue