mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Test: Share model
This commit is contained in:
@@ -167,7 +167,7 @@ func TestPolicy_GetUploadURL(t *testing.T) {
|
||||
{
|
||||
cache.Set("setting_siteURL", "http://127.0.0.1", 0)
|
||||
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 创建分享
|
||||
// TODO 测试
|
||||
func (share *Share) Create() (uint, error) {
|
||||
if err := DB.Create(share).Error; err != nil {
|
||||
util.Log().Warning("无法插入数据库记录, %s", err)
|
||||
@@ -43,7 +42,6 @@ func (share *Share) Create() (uint, error) {
|
||||
}
|
||||
|
||||
// GetShareByHashID 根据HashID查找分享
|
||||
// TODO 测试
|
||||
func GetShareByHashID(hashID string) *Share {
|
||||
id, err := hashid.DecodeHashID(hashID, hashid.ShareID)
|
||||
if err != nil {
|
||||
@@ -59,7 +57,6 @@ func GetShareByHashID(hashID string) *Share {
|
||||
}
|
||||
|
||||
// IsAvailable 返回此分享是否可用(是否过期)
|
||||
// TODO 测试
|
||||
func (share *Share) IsAvailable() bool {
|
||||
if share.RemainDownloads == 0 {
|
||||
return false
|
||||
@@ -71,37 +68,38 @@ func (share *Share) IsAvailable() bool {
|
||||
// 检查源对象是否存在
|
||||
var sourceID uint
|
||||
if share.IsDir {
|
||||
folder := share.GetSourceFolder()
|
||||
folder := share.SourceFolder()
|
||||
sourceID = folder.ID
|
||||
} else {
|
||||
file := share.GetSourceFile()
|
||||
file := share.SourceFile()
|
||||
sourceID = file.ID
|
||||
}
|
||||
if sourceID == 0 {
|
||||
// TODO 是否要在这里删除这个无效分享?
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// GetCreator 获取分享的创建者
|
||||
func (share *Share) GetCreator() *User {
|
||||
// Creator 获取分享的创建者
|
||||
func (share *Share) Creator() *User {
|
||||
if share.User.ID == 0 {
|
||||
share.User, _ = GetUserByID(share.UserID)
|
||||
}
|
||||
return &share.User
|
||||
}
|
||||
|
||||
// GetSource 返回源对象
|
||||
func (share *Share) GetSource() interface{} {
|
||||
// Source 返回源对象
|
||||
func (share *Share) Source() interface{} {
|
||||
if share.IsDir {
|
||||
return share.GetSourceFolder()
|
||||
return share.SourceFolder()
|
||||
}
|
||||
return share.GetSourceFile()
|
||||
return share.SourceFile()
|
||||
}
|
||||
|
||||
// GetSourceFolder 获取源目录
|
||||
func (share *Share) GetSourceFolder() *Folder {
|
||||
// SourceFolder 获取源目录
|
||||
func (share *Share) SourceFolder() *Folder {
|
||||
if share.Folder.ID == 0 {
|
||||
folders, _ := GetFoldersByIDs([]uint{share.SourceID}, share.UserID)
|
||||
if len(folders) > 0 {
|
||||
@@ -111,8 +109,8 @@ func (share *Share) GetSourceFolder() *Folder {
|
||||
return &share.Folder
|
||||
}
|
||||
|
||||
// GetSourceFile 获取源文件
|
||||
func (share *Share) GetSourceFile() *File {
|
||||
// SourceFile 获取源文件
|
||||
func (share *Share) SourceFile() *File {
|
||||
if share.File.ID == 0 {
|
||||
files, _ := GetFilesByIDs([]uint{share.SourceID}, share.UserID)
|
||||
if len(files) > 0 {
|
||||
@@ -182,7 +180,7 @@ func (share *Share) Purchase(user *User) error {
|
||||
|
||||
scoreRate := GetIntSetting("share_score_rate", 100)
|
||||
gainedScore := int(math.Ceil(float64(share.Score*scoreRate) / 100))
|
||||
share.GetCreator().AddScore(gainedScore)
|
||||
share.Creator().AddScore(gainedScore)
|
||||
|
||||
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 扣除积分,返回是否成功
|
||||
// todo 测试
|
||||
func (user *User) PayScore(score int) bool {
|
||||
if score == 0 {
|
||||
return true
|
||||
@@ -109,7 +108,6 @@ func (user *User) PayScore(score int) bool {
|
||||
}
|
||||
|
||||
// AddScore 增加积分
|
||||
// todo 测试
|
||||
func (user *User) AddScore(score int) {
|
||||
user.Score += score
|
||||
DB.Model(user).UpdateColumn("score", gorm.Expr("score + ?", score))
|
||||
@@ -257,7 +255,6 @@ func (user *User) SetPassword(password string) error {
|
||||
}
|
||||
|
||||
// NewAnonymousUser 返回一个匿名用户
|
||||
// TODO 测试
|
||||
func NewAnonymousUser() *User {
|
||||
user := User{}
|
||||
user.Policy.Type = "anonymous"
|
||||
@@ -266,7 +263,6 @@ func NewAnonymousUser() *User {
|
||||
}
|
||||
|
||||
// IsAnonymous 返回是否为未登录用户
|
||||
// TODO 测试
|
||||
func (user *User) IsAnonymous() bool {
|
||||
return user.ID == 0
|
||||
}
|
||||
|
||||
@@ -340,3 +340,48 @@ func TestUser_Root(t *testing.T) {
|
||||
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())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user