mirror of https://github.com/cloudreve/Cloudreve
Fix: test error / missing AfterFind hook in user model
parent
16067c3ac8
commit
b3ef833afd
|
@ -21,6 +21,19 @@ type Group struct {
|
||||||
PolicyList []uint `gorm:"-"`
|
PolicyList []uint `gorm:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAria2Option 获取用户离线下载设备
|
||||||
|
// TODO:测试
|
||||||
|
func (group *Group) GetAria2Option() [3]bool {
|
||||||
|
if len(group.Aria2Option) != 3 {
|
||||||
|
return [3]bool{false, false, false}
|
||||||
|
}
|
||||||
|
return [3]bool{
|
||||||
|
group.Aria2Option[0] == '1',
|
||||||
|
group.Aria2Option[1] == '1',
|
||||||
|
group.Aria2Option[2] == '1',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// GetGroupByID 用ID获取用户组
|
// GetGroupByID 用ID获取用户组
|
||||||
func GetGroupByID(ID interface{}) (Group, error) {
|
func GetGroupByID(ID interface{}) (Group, error) {
|
||||||
var group Group
|
var group Group
|
||||||
|
|
|
@ -149,13 +149,6 @@ func (user *User) AfterCreate(tx *gorm.DB) (err error) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
//SerializeOptions 将序列后的Option写入到数据库字段
|
|
||||||
func (user *User) SerializeOptions() (err error) {
|
|
||||||
optionsValue, err := json.Marshal(&user.OptionsSerialized)
|
|
||||||
user.Options = string(optionsValue)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// AfterFind 找到用户后的钩子
|
// AfterFind 找到用户后的钩子
|
||||||
func (user *User) AfterFind() (err error) {
|
func (user *User) AfterFind() (err error) {
|
||||||
// 解析用户设置到OptionsSerialized
|
// 解析用户设置到OptionsSerialized
|
||||||
|
@ -166,6 +159,13 @@ func (user *User) AfterFind() (err error) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//SerializeOptions 将序列后的Option写入到数据库字段
|
||||||
|
func (user *User) SerializeOptions() (err error) {
|
||||||
|
optionsValue, err := json.Marshal(&user.OptionsSerialized)
|
||||||
|
user.Options = string(optionsValue)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// CheckPassword 根据明文校验密码
|
// CheckPassword 根据明文校验密码
|
||||||
func (user *User) CheckPassword(password string) (bool, error) {
|
func (user *User) CheckPassword(password string) (bool, error) {
|
||||||
|
|
||||||
|
|
|
@ -47,6 +47,9 @@ func TestGetUserByID(t *testing.T) {
|
||||||
Model: gorm.Model{
|
Model: gorm.Model{
|
||||||
ID: 1,
|
ID: 1,
|
||||||
},
|
},
|
||||||
|
OptionsSerialized: PolicyOption{
|
||||||
|
FileType: []string{},
|
||||||
|
},
|
||||||
Name: "默认上传策略",
|
Name: "默认上传策略",
|
||||||
},
|
},
|
||||||
}, user)
|
}, user)
|
||||||
|
|
|
@ -41,7 +41,7 @@ type Group struct {
|
||||||
|
|
||||||
// BuildUser 序列化用户
|
// BuildUser 序列化用户
|
||||||
func BuildUser(user model.User) User {
|
func BuildUser(user model.User) User {
|
||||||
fmt.Println(user)
|
aria2Option := user.Group.GetAria2Option()
|
||||||
return User{
|
return User{
|
||||||
ID: user.ID,
|
ID: user.ID,
|
||||||
Email: user.Email,
|
Email: user.Email,
|
||||||
|
@ -58,8 +58,8 @@ func BuildUser(user model.User) User {
|
||||||
},
|
},
|
||||||
Group: Group{
|
Group: Group{
|
||||||
AllowShare: user.Group.ShareEnabled,
|
AllowShare: user.Group.ShareEnabled,
|
||||||
AllowRemoteDownload: user.Group.Aria2Option[0] == '1',
|
AllowRemoteDownload: aria2Option[0],
|
||||||
AllowTorrentDownload: user.Group.Aria2Option[2] == '1',
|
AllowTorrentDownload: aria2Option[2],
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,6 +82,10 @@ func TestUserSession(t *testing.T) {
|
||||||
expected: serializer.BuildUserResponse(model.User{
|
expected: serializer.BuildUserResponse(model.User{
|
||||||
Email: "admin@cloudreve.org",
|
Email: "admin@cloudreve.org",
|
||||||
Nick: "admin",
|
Nick: "admin",
|
||||||
|
Policy: model.Policy{
|
||||||
|
Type: "local",
|
||||||
|
OptionsSerialized: model.PolicyOption{FileType: []string{}},
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
// 登录信息正确,需要验证码,验证码错误
|
// 登录信息正确,需要验证码,验证码错误
|
||||||
|
|
Loading…
Reference in New Issue