feat(users): support predefined users
This commit is contained in:
parent
80342a7333
commit
52c8610271
8 changed files with 173 additions and 60 deletions
|
@ -1,6 +1,10 @@
|
|||
package server
|
||||
|
||||
import "encoding/json"
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/ihexxa/quickshare/src/userstore"
|
||||
)
|
||||
|
||||
type FSConfig struct {
|
||||
Root string `json:"root"`
|
||||
|
@ -9,22 +13,23 @@ type FSConfig struct {
|
|||
}
|
||||
|
||||
type UsersCfg struct {
|
||||
EnableAuth bool `json:"enableAuth" yaml:"enableAuth"`
|
||||
DefaultAdmin string `json:"defaultAdmin" yaml:"defaultAdmin" cfg:"env"`
|
||||
DefaultAdminPwd string `json:"defaultAdminPwd" yaml:"defaultAdminPwd" cfg:"env"`
|
||||
CookieTTL int `json:"cookieTTL" yaml:"cookieTTL"`
|
||||
CookieSecure bool `json:"cookieSecure" yaml:"cookieSecure"`
|
||||
CookieHttpOnly bool `json:"cookieHttpOnly" yaml:"cookieHttpOnly"`
|
||||
MinUserNameLen int `json:"minUserNameLen" yaml:"minUserNameLen"`
|
||||
MinPwdLen int `json:"minPwdLen" yaml:"minPwdLen"`
|
||||
CaptchaWidth int `json:"captchaWidth" yaml:"captchaWidth"`
|
||||
CaptchaHeight int `json:"captchaHeight" yaml:"captchaHeight"`
|
||||
CaptchaEnabled bool `json:"captchaEnabled" yaml:"captchaEnabled"`
|
||||
UploadSpeedLimit int `json:"uploadSpeedLimit" yaml:"uploadSpeedLimit"`
|
||||
DownloadSpeedLimit int `json:"downloadSpeedLimit" yaml:"downloadSpeedLimit"`
|
||||
SpaceLimit int `json:"spaceLimit" yaml:"spaceLimit"`
|
||||
LimiterCapacity int `json:"limiterCapacity" yaml:"limiterCapacity"`
|
||||
LimiterCyc int `json:"limiterCyc" yaml:"limiterCyc"`
|
||||
EnableAuth bool `json:"enableAuth" yaml:"enableAuth"`
|
||||
DefaultAdmin string `json:"defaultAdmin" yaml:"defaultAdmin" cfg:"env"`
|
||||
DefaultAdminPwd string `json:"defaultAdminPwd" yaml:"defaultAdminPwd" cfg:"env"`
|
||||
CookieTTL int `json:"cookieTTL" yaml:"cookieTTL"`
|
||||
CookieSecure bool `json:"cookieSecure" yaml:"cookieSecure"`
|
||||
CookieHttpOnly bool `json:"cookieHttpOnly" yaml:"cookieHttpOnly"`
|
||||
MinUserNameLen int `json:"minUserNameLen" yaml:"minUserNameLen"`
|
||||
MinPwdLen int `json:"minPwdLen" yaml:"minPwdLen"`
|
||||
CaptchaWidth int `json:"captchaWidth" yaml:"captchaWidth"`
|
||||
CaptchaHeight int `json:"captchaHeight" yaml:"captchaHeight"`
|
||||
CaptchaEnabled bool `json:"captchaEnabled" yaml:"captchaEnabled"`
|
||||
UploadSpeedLimit int `json:"uploadSpeedLimit" yaml:"uploadSpeedLimit"`
|
||||
DownloadSpeedLimit int `json:"downloadSpeedLimit" yaml:"downloadSpeedLimit"`
|
||||
SpaceLimit int `json:"spaceLimit" yaml:"spaceLimit"`
|
||||
LimiterCapacity int `json:"limiterCapacity" yaml:"limiterCapacity"`
|
||||
LimiterCyc int `json:"limiterCyc" yaml:"limiterCyc"`
|
||||
PredefinedUsers []*userstore.UserCfg `json:"predefinedUsers" yaml:"predefinedUsers"`
|
||||
}
|
||||
|
||||
type Secrets struct {
|
||||
|
|
|
@ -182,7 +182,7 @@ func initHandlers(router *gin.Engine, cfg gocfg.ICfg, deps *depidx.Deps) (*gin.E
|
|||
return nil, fmt.Errorf("init admin error: %w", err)
|
||||
}
|
||||
|
||||
deps.Log().Infof("user (%s) is created\n", adminName)
|
||||
deps.Log().Infof("admin(%s) is created", adminName)
|
||||
}
|
||||
|
||||
fileHdrs, err := fileshdr.NewFileHandlers(cfg, deps)
|
||||
|
|
|
@ -25,7 +25,14 @@ func TestUsersHandlers(t *testing.T) {
|
|||
"downloadSpeedLimit": 409600,
|
||||
"spaceLimit": 1024,
|
||||
"limiterCapacity": 1000,
|
||||
"limiterCyc": 1000
|
||||
"limiterCyc": 1000,
|
||||
"predefinedUsers": [
|
||||
{
|
||||
"name": "demo",
|
||||
"pwd": "Quicksh@re",
|
||||
"role": "user"
|
||||
}
|
||||
]
|
||||
},
|
||||
"server": {
|
||||
"debug": true,
|
||||
|
@ -59,50 +66,85 @@ func TestUsersHandlers(t *testing.T) {
|
|||
}
|
||||
|
||||
t.Run("test users APIs: Login-Self-SetPwd-Logout-Login", func(t *testing.T) {
|
||||
resp, _, errs := usersCl.Login(adminName, adminPwd)
|
||||
if len(errs) > 0 {
|
||||
t.Fatal(errs)
|
||||
} else if resp.StatusCode != 200 {
|
||||
t.Fatal(resp.StatusCode)
|
||||
users := []*userstore.User{
|
||||
{
|
||||
ID: 0,
|
||||
Name: adminName,
|
||||
Pwd: adminPwd,
|
||||
Role: userstore.AdminRole,
|
||||
UsedSpace: 0,
|
||||
Quota: &userstore.Quota{
|
||||
UploadSpeedLimit: 50 * 1024 * 1024,
|
||||
DownloadSpeedLimit: 50 * 1024 * 1024,
|
||||
SpaceLimit: 1024 * 1024 * 1024,
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: 0,
|
||||
Name: "demo",
|
||||
Pwd: "Quicksh@re",
|
||||
Role: userstore.UserRole,
|
||||
UsedSpace: 0,
|
||||
Quota: &userstore.Quota{
|
||||
UploadSpeedLimit: 409600,
|
||||
DownloadSpeedLimit: 409600,
|
||||
SpaceLimit: 1024,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
token := client.GetCookie(resp.Cookies(), su.TokenCookie)
|
||||
for _, user := range users {
|
||||
usersCl := client.NewSingleUserClient(addr)
|
||||
|
||||
resp, selfResp, errs := usersCl.Self(token)
|
||||
if len(errs) > 0 {
|
||||
t.Fatal(errs)
|
||||
} else if resp.StatusCode != 200 {
|
||||
t.Fatal(resp.StatusCode)
|
||||
} else if selfResp.ID != "0" ||
|
||||
selfResp.Name != adminName ||
|
||||
selfResp.Role != userstore.AdminRole ||
|
||||
selfResp.UsedSpace != 0 ||
|
||||
selfResp.Quota.SpaceLimit != 1024*1024*1024 ||
|
||||
selfResp.Quota.UploadSpeedLimit != 50*1024*1024 ||
|
||||
selfResp.Quota.DownloadSpeedLimit != 50*1024*1024 {
|
||||
// TODO: expose default values from userstore
|
||||
t.Fatalf("user infos don't match %v", selfResp)
|
||||
}
|
||||
resp, _, errs := usersCl.Login(user.Name, user.Pwd)
|
||||
if len(errs) > 0 {
|
||||
t.Fatal(errs)
|
||||
} else if resp.StatusCode != 200 {
|
||||
t.Fatal(resp.StatusCode)
|
||||
}
|
||||
|
||||
resp, _, errs = usersCl.SetPwd(adminPwd, adminNewPwd, token)
|
||||
if len(errs) > 0 {
|
||||
t.Fatal(errs)
|
||||
} else if resp.StatusCode != 200 {
|
||||
t.Fatal(resp.StatusCode)
|
||||
}
|
||||
token := client.GetCookie(resp.Cookies(), su.TokenCookie)
|
||||
|
||||
resp, _, errs = usersCl.Logout(token)
|
||||
if len(errs) > 0 {
|
||||
t.Fatal(errs)
|
||||
} else if resp.StatusCode != 200 {
|
||||
t.Fatal(resp.StatusCode)
|
||||
}
|
||||
resp, selfResp, errs := usersCl.Self(token)
|
||||
if len(errs) > 0 {
|
||||
t.Fatal(errs)
|
||||
} else if resp.StatusCode != 200 {
|
||||
t.Fatal(resp.StatusCode)
|
||||
} else if selfResp.Name != user.Name ||
|
||||
selfResp.Role != user.Role ||
|
||||
selfResp.UsedSpace != 0 ||
|
||||
selfResp.Quota.UploadSpeedLimit != user.Quota.UploadSpeedLimit ||
|
||||
selfResp.Quota.DownloadSpeedLimit != user.Quota.DownloadSpeedLimit ||
|
||||
selfResp.Quota.SpaceLimit != user.Quota.SpaceLimit {
|
||||
// TODO: expose default values from userstore
|
||||
t.Fatalf("user infos don't match %v", selfResp)
|
||||
}
|
||||
if selfResp.Role == userstore.AdminRole {
|
||||
if selfResp.ID != "0" {
|
||||
t.Fatalf("user id don't match %v", selfResp)
|
||||
}
|
||||
}
|
||||
|
||||
resp, _, errs = usersCl.Login(adminName, adminNewPwd)
|
||||
if len(errs) > 0 {
|
||||
t.Fatal(errs)
|
||||
} else if resp.StatusCode != 200 {
|
||||
t.Fatal(resp.StatusCode)
|
||||
resp, _, errs = usersCl.SetPwd(user.Pwd, adminNewPwd, token)
|
||||
if len(errs) > 0 {
|
||||
t.Fatal(errs)
|
||||
} else if resp.StatusCode != 200 {
|
||||
t.Fatal(resp.StatusCode)
|
||||
}
|
||||
|
||||
resp, _, errs = usersCl.Logout(token)
|
||||
if len(errs) > 0 {
|
||||
t.Fatal(errs)
|
||||
} else if resp.StatusCode != 200 {
|
||||
t.Fatal(resp.StatusCode)
|
||||
}
|
||||
|
||||
resp, _, errs = usersCl.Login(user.Name, adminNewPwd)
|
||||
if len(errs) > 0 {
|
||||
t.Fatal(errs)
|
||||
} else if resp.StatusCode != 200 {
|
||||
t.Fatal(resp.StatusCode)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -198,7 +240,7 @@ func TestUsersHandlers(t *testing.T) {
|
|||
t.Fatal(resp.StatusCode)
|
||||
}
|
||||
|
||||
if len(lsResp.Users) != 2 {
|
||||
if len(lsResp.Users) != 3 {
|
||||
t.Fatal(fmt.Errorf("incorrect users size (%d)", len(lsResp.Users)))
|
||||
}
|
||||
for _, user := range lsResp.Users {
|
||||
|
@ -264,7 +306,7 @@ func TestUsersHandlers(t *testing.T) {
|
|||
} else if resp.StatusCode != 200 {
|
||||
t.Fatal(resp.StatusCode)
|
||||
}
|
||||
if len(lsResp.Users) != 1 {
|
||||
if len(lsResp.Users) != 2 {
|
||||
t.Fatal(fmt.Errorf("incorrect users size (%d)", len(lsResp.Users)))
|
||||
} else if lsResp.Users[0].ID != 0 ||
|
||||
lsResp.Users[0].Name != adminName ||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue