Fix: test error / missing AfterFind hook in user model
This commit is contained in:
parent
16067c3ac8
commit
b3ef833afd
5 changed files with 30 additions and 10 deletions
|
@ -21,6 +21,19 @@ type Group struct {
|
|||
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获取用户组
|
||||
func GetGroupByID(ID interface{}) (Group, error) {
|
||||
var group Group
|
||||
|
|
|
@ -149,13 +149,6 @@ func (user *User) AfterCreate(tx *gorm.DB) (err error) {
|
|||
return err
|
||||
}
|
||||
|
||||
//SerializeOptions 将序列后的Option写入到数据库字段
|
||||
func (user *User) SerializeOptions() (err error) {
|
||||
optionsValue, err := json.Marshal(&user.OptionsSerialized)
|
||||
user.Options = string(optionsValue)
|
||||
return err
|
||||
}
|
||||
|
||||
// AfterFind 找到用户后的钩子
|
||||
func (user *User) AfterFind() (err error) {
|
||||
// 解析用户设置到OptionsSerialized
|
||||
|
@ -166,6 +159,13 @@ func (user *User) AfterFind() (err error) {
|
|||
return err
|
||||
}
|
||||
|
||||
//SerializeOptions 将序列后的Option写入到数据库字段
|
||||
func (user *User) SerializeOptions() (err error) {
|
||||
optionsValue, err := json.Marshal(&user.OptionsSerialized)
|
||||
user.Options = string(optionsValue)
|
||||
return err
|
||||
}
|
||||
|
||||
// CheckPassword 根据明文校验密码
|
||||
func (user *User) CheckPassword(password string) (bool, error) {
|
||||
|
||||
|
|
|
@ -47,6 +47,9 @@ func TestGetUserByID(t *testing.T) {
|
|||
Model: gorm.Model{
|
||||
ID: 1,
|
||||
},
|
||||
OptionsSerialized: PolicyOption{
|
||||
FileType: []string{},
|
||||
},
|
||||
Name: "默认上传策略",
|
||||
},
|
||||
}, user)
|
||||
|
|
|
@ -41,7 +41,7 @@ type Group struct {
|
|||
|
||||
// BuildUser 序列化用户
|
||||
func BuildUser(user model.User) User {
|
||||
fmt.Println(user)
|
||||
aria2Option := user.Group.GetAria2Option()
|
||||
return User{
|
||||
ID: user.ID,
|
||||
Email: user.Email,
|
||||
|
@ -58,8 +58,8 @@ func BuildUser(user model.User) User {
|
|||
},
|
||||
Group: Group{
|
||||
AllowShare: user.Group.ShareEnabled,
|
||||
AllowRemoteDownload: user.Group.Aria2Option[0] == '1',
|
||||
AllowTorrentDownload: user.Group.Aria2Option[2] == '1',
|
||||
AllowRemoteDownload: aria2Option[0],
|
||||
AllowTorrentDownload: aria2Option[2],
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -82,6 +82,10 @@ func TestUserSession(t *testing.T) {
|
|||
expected: serializer.BuildUserResponse(model.User{
|
||||
Email: "admin@cloudreve.org",
|
||||
Nick: "admin",
|
||||
Policy: model.Policy{
|
||||
Type: "local",
|
||||
OptionsSerialized: model.PolicyOption{FileType: []string{}},
|
||||
},
|
||||
}),
|
||||
},
|
||||
// 登录信息正确,需要验证码,验证码错误
|
||||
|
|
Loading…
Add table
Reference in a new issue