fix(user_store): add checking and move some common vars

This commit is contained in:
hexxa 2022-03-24 14:46:22 +08:00 committed by Hexxa
parent c3a3b2dc5b
commit b7609e6c06
17 changed files with 567 additions and 560 deletions

View file

@ -4,8 +4,6 @@ import (
"encoding/json"
"github.com/ihexxa/quickshare/src/db"
"github.com/ihexxa/quickshare/src/db/sitestore"
"github.com/ihexxa/quickshare/src/db/userstore"
)
type DbConfig struct {
@ -19,23 +17,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"`
PredefinedUsers []*userstore.UserCfg `json:"predefinedUsers" yaml:"predefinedUsers"`
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 []*db.UserCfg `json:"predefinedUsers" yaml:"predefinedUsers"`
}
type Secrets struct {
@ -59,13 +57,13 @@ type WorkerPoolCfg struct {
}
type Config struct {
Fs *FSConfig `json:"fs" yaml:"fs"`
Secrets *Secrets `json:"secrets" yaml:"secrets"`
Server *ServerCfg `json:"server" yaml:"server"`
Users *UsersCfg `json:"users" yaml:"users"`
Workers *WorkerPoolCfg `json:"workers" yaml:"workers"`
Site *sitestore.SiteConfig `json:"site" yaml:"site"`
Db *DbConfig `json:"db" yaml:"db"`
Fs *FSConfig `json:"fs" yaml:"fs"`
Secrets *Secrets `json:"secrets" yaml:"secrets"`
Server *ServerCfg `json:"server" yaml:"server"`
Users *UsersCfg `json:"users" yaml:"users"`
Workers *WorkerPoolCfg `json:"workers" yaml:"workers"`
Site *db.SiteConfig `json:"site" yaml:"site"`
Db *DbConfig `json:"db" yaml:"db"`
}
func NewConfig() *Config {
@ -101,7 +99,7 @@ func DefaultConfigStruct() *Config {
SpaceLimit: 1024 * 1024 * 100, // 100MB
LimiterCapacity: 1000,
LimiterCyc: 1000, // 1s
PredefinedUsers: []*userstore.UserCfg{},
PredefinedUsers: []*db.UserCfg{},
},
Secrets: &Secrets{
TokenSecret: "",
@ -120,15 +118,15 @@ func DefaultConfigStruct() *Config {
SleepCyc: 1,
WorkerCount: 2,
},
Site: &sitestore.SiteConfig{
Site: &db.SiteConfig{
ClientCfg: &db.ClientConfig{
SiteName: "Quickshare",
SiteDesc: "quick and simple file sharing",
Bg: &db.BgConfig{
Url: "/static/img/textured_paper.png",
Repeat: "repeat",
Position: "fixed",
Align: "center",
Position: "center",
Align: "fixed",
BgColor: "#ccc",
},
},

View file

@ -8,8 +8,6 @@ import (
"github.com/ihexxa/gocfg"
"github.com/ihexxa/quickshare/src/db"
"github.com/ihexxa/quickshare/src/db/sitestore"
"github.com/ihexxa/quickshare/src/db/userstore"
)
func TestLoadCfg(t *testing.T) {
@ -86,8 +84,8 @@ func TestLoadCfg(t *testing.T) {
SpaceLimit: 1,
LimiterCapacity: 1,
LimiterCyc: 1,
PredefinedUsers: []*userstore.UserCfg{
&userstore.UserCfg{
PredefinedUsers: []*db.UserCfg{
&db.UserCfg{
Name: "1",
Pwd: "1",
Role: "1",
@ -111,7 +109,7 @@ func TestLoadCfg(t *testing.T) {
SleepCyc: 1,
WorkerCount: 1,
},
Site: &sitestore.SiteConfig{
Site: &db.SiteConfig{
ClientCfg: &db.ClientConfig{
SiteName: "1",
SiteDesc: "1",
@ -152,8 +150,8 @@ func TestLoadCfg(t *testing.T) {
SpaceLimit: 4,
LimiterCapacity: 4,
LimiterCyc: 4,
PredefinedUsers: []*userstore.UserCfg{
&userstore.UserCfg{
PredefinedUsers: []*db.UserCfg{
&db.UserCfg{
Name: "4",
Pwd: "4",
Role: "4",
@ -177,7 +175,7 @@ func TestLoadCfg(t *testing.T) {
SleepCyc: 4,
WorkerCount: 4,
},
Site: &sitestore.SiteConfig{
Site: &db.SiteConfig{
ClientCfg: &db.ClientConfig{
SiteName: "4",
SiteDesc: "4",
@ -218,8 +216,8 @@ func TestLoadCfg(t *testing.T) {
SpaceLimit: 5,
LimiterCapacity: 5,
LimiterCyc: 5,
PredefinedUsers: []*userstore.UserCfg{
&userstore.UserCfg{
PredefinedUsers: []*db.UserCfg{
&db.UserCfg{
Name: "5",
Pwd: "5",
Role: "5",
@ -243,7 +241,7 @@ func TestLoadCfg(t *testing.T) {
SleepCyc: 4,
WorkerCount: 4,
},
Site: &sitestore.SiteConfig{
Site: &db.SiteConfig{
ClientCfg: &db.ClientConfig{
SiteName: "4",
SiteDesc: "4",
@ -284,8 +282,8 @@ func TestLoadCfg(t *testing.T) {
SpaceLimit: 5,
LimiterCapacity: 5,
LimiterCyc: 5,
PredefinedUsers: []*userstore.UserCfg{
&userstore.UserCfg{
PredefinedUsers: []*db.UserCfg{
&db.UserCfg{
Name: "5",
Pwd: "5",
Role: "5",
@ -309,7 +307,7 @@ func TestLoadCfg(t *testing.T) {
SleepCyc: 4,
WorkerCount: 4,
},
Site: &sitestore.SiteConfig{
Site: &db.SiteConfig{
ClientCfg: &db.ClientConfig{
SiteName: "4",
SiteDesc: "4",

View file

@ -162,15 +162,15 @@ func initDeps(cfg gocfg.ICfg) *depidx.Deps {
panic(fmt.Sprintf("fail to init bolt store: %s", err))
}
err = siteStore.Init(&sitestore.SiteConfig{
err = siteStore.Init(&db.SiteConfig{
ClientCfg: &db.ClientConfig{
SiteName: cfg.StringOr("Site.ClientCfg.SiteName", "Quickshare"),
SiteDesc: cfg.StringOr("Site.ClientCfg.SiteDesc", "quick and simple file sharing"),
Bg: &db.BgConfig{
Url: cfg.StringOr("Site.ClientCfg.Bg.Url", "/static/img/textured_paper.png"),
Repeat: cfg.StringOr("Site.ClientCfg.Bg.Repeat", "repeat"),
Position: cfg.StringOr("Site.ClientCfg.Bg.Position", "fixed"),
Align: cfg.StringOr("Site.ClientCfg.Bg.Align", "center"),
Position: cfg.StringOr("Site.ClientCfg.Bg.Position", "center"),
Align: cfg.StringOr("Site.ClientCfg.Bg.Align", "fixed"),
BgColor: cfg.StringOr("Site.ClientCfg.Bg.BgColor", "#ccc"),
},
},

View file

@ -11,7 +11,6 @@ import (
"github.com/ihexxa/quickshare/src/client"
"github.com/ihexxa/quickshare/src/db"
"github.com/ihexxa/quickshare/src/db/userstore"
q "github.com/ihexxa/quickshare/src/handlers"
"github.com/ihexxa/quickshare/src/handlers/settings"
)
@ -105,7 +104,7 @@ func TestPermissions(t *testing.T) {
testUsersAPIs := func(desc, user string, pwd string, requireAuth bool, expectedCodes map[string]int) {
newPwd := "12345"
newRole := userstore.AdminRole
newRole := db.AdminRole
newQuota := &db.Quota{
SpaceLimit: int64(2046),
UploadSpeedLimit: int(8 * 1024 * 1024),
@ -146,7 +145,7 @@ func TestPermissions(t *testing.T) {
// user management
resp, addUserResp, errs := cl.AddUser(tmpUser, tmpPwd, tmpRole, token)
assertResp(t, resp, errs, expectedCodes["AddUser"], fmt.Sprintf("%s-%s", desc, "AddUser"))
resp, addAdminResp, errs := cl.AddUser(tmpAdmin, tmpAdminPwd, userstore.AdminRole, token)
resp, addAdminResp, errs := cl.AddUser(tmpAdmin, tmpAdminPwd, db.AdminRole, token)
assertResp(t, resp, errs, expectedCodes["AddUser"], fmt.Sprintf("%s-%s", desc, "AddUser"))
resp, _, errs = cl.ListUsers(token)
@ -186,9 +185,9 @@ func TestPermissions(t *testing.T) {
resp, _, errs = cl.SetUser(userID, newRole, newQuota, token)
assertResp(t, resp, errs, expectedCodes["SetUserSelf"], fmt.Sprintf("%s-%s", desc, "SetUserSelf"))
// update other users
resp, _, errs = cl.SetUser(tmpUserID, userstore.AdminRole, newQuota, token)
resp, _, errs = cl.SetUser(tmpUserID, db.AdminRole, newQuota, token)
assertResp(t, resp, errs, expectedCodes["SetUserOthers"], fmt.Sprintf("%s-%s", desc, "SetUserOthers"))
resp, _, errs = cl.SetUser(0, userstore.UserRole, newQuota, token)
resp, _, errs = cl.SetUser(0, db.UserRole, newQuota, token)
assertResp(t, resp, errs, expectedCodes["SetUserOthersAdmin"], fmt.Sprintf("%s-%s", desc, "SetUserOthersAdmin"))
resp, _, errs = cl.DelUser(addUserResp.ID, token)

View file

@ -9,7 +9,7 @@ import (
"time"
"github.com/ihexxa/quickshare/src/client"
"github.com/ihexxa/quickshare/src/db/userstore"
"github.com/ihexxa/quickshare/src/db"
q "github.com/ihexxa/quickshare/src/handlers"
)
@ -82,7 +82,7 @@ func TestSpaceLimit(t *testing.T) {
for i := 0; i < userCount; i++ {
userName := getUserName(i)
resp, adResp, errs := usersCl.AddUser(userName, userPwd, userstore.UserRole, token)
resp, adResp, errs := usersCl.AddUser(userName, userPwd, db.UserRole, token)
if len(errs) > 0 {
t.Fatal(errs)
} else if resp.StatusCode != 200 {

View file

@ -89,7 +89,7 @@ func TestUsersHandlers(t *testing.T) {
for _, user := range lsResp.Users {
if user.Name == adminName {
if user.ID != 0 ||
user.Role != userstore.AdminRole ||
user.Role != db.AdminRole ||
user.UsedSpace != 0 ||
user.Quota.SpaceLimit != 1024*1024*1024 || // TODO: export these
user.Quota.UploadSpeedLimit != 50*1024*1024 ||
@ -99,7 +99,7 @@ func TestUsersHandlers(t *testing.T) {
}
}
if user.Name == "visitor" {
if user.Role != userstore.VisitorRole ||
if user.Role != db.VisitorRole ||
user.Quota.SpaceLimit != 0 || // TODO: export these
user.Quota.UploadSpeedLimit != 10*1024*1024 ||
user.Quota.DownloadSpeedLimit != 10*1024*1024 ||
@ -108,7 +108,7 @@ func TestUsersHandlers(t *testing.T) {
}
}
if user.Name == "demo" {
if user.Role != userstore.UserRole ||
if user.Role != db.UserRole ||
user.Quota.SpaceLimit != 1024 ||
user.Quota.UploadSpeedLimit != 409600 ||
user.Quota.DownloadSpeedLimit != 409600 ||
@ -125,7 +125,7 @@ func TestUsersHandlers(t *testing.T) {
ID: 0,
Name: adminName,
Pwd: adminPwd,
Role: userstore.AdminRole,
Role: db.AdminRole,
UsedSpace: 0,
Quota: &db.Quota{
UploadSpeedLimit: 50 * 1024 * 1024,
@ -137,7 +137,7 @@ func TestUsersHandlers(t *testing.T) {
ID: 0,
Name: "demo",
Pwd: "Quicksh@re",
Role: userstore.UserRole,
Role: db.UserRole,
UsedSpace: 0,
Quota: &db.Quota{
UploadSpeedLimit: 409600,
@ -171,7 +171,7 @@ func TestUsersHandlers(t *testing.T) {
// TODO: expose default values from userstore
t.Fatalf("user infos don't match %v", selfResp)
}
if selfResp.Role == userstore.AdminRole {
if selfResp.Role == db.AdminRole {
if selfResp.ID != "0" {
t.Fatalf("user id don't match %v", selfResp)
}
@ -210,7 +210,7 @@ func TestUsersHandlers(t *testing.T) {
token := client.GetCookie(resp.Cookies(), su.TokenCookie)
userName, userPwd := "user_login", "1234"
resp, auResp, errs := usersCl.AddUser(userName, userPwd, userstore.UserRole, token)
resp, auResp, errs := usersCl.AddUser(userName, userPwd, db.UserRole, token)
if len(errs) > 0 {
t.Fatal(errs)
} else if resp.StatusCode != 200 {
@ -270,7 +270,7 @@ func TestUsersHandlers(t *testing.T) {
token := client.GetCookie(resp.Cookies(), su.TokenCookie)
userName, userPwd, userRole := "new_user", "1234", userstore.UserRole
userName, userPwd, userRole := "new_user", "1234", db.UserRole
resp, auResp, errs := usersCl.AddUser(userName, userPwd, userRole, token)
if len(errs) > 0 {
t.Fatal(errs)
@ -297,7 +297,7 @@ func TestUsersHandlers(t *testing.T) {
for _, user := range lsResp.Users {
if user.ID == 0 {
if user.Name != adminName ||
user.Role != userstore.AdminRole {
user.Role != db.AdminRole {
t.Fatal(fmt.Errorf("incorrect root info (%v)", user))
}
}
@ -309,7 +309,7 @@ func TestUsersHandlers(t *testing.T) {
}
}
newRole, newQuota := userstore.AdminRole, &db.Quota{
newRole, newQuota := db.AdminRole, &db.Quota{
SpaceLimit: 3,
UploadSpeedLimit: 3,
DownloadSpeedLimit: 3,
@ -396,9 +396,9 @@ func TestUsersHandlers(t *testing.T) {
t.Fatal(resp.StatusCode)
}
for _, role := range append(roles, []string{
userstore.AdminRole,
userstore.UserRole,
userstore.VisitorRole,
db.AdminRole,
db.UserRole,
db.VisitorRole,
}...) {
if !lsResp.Roles[role] {
t.Fatalf("role(%s) not found", role)

View file

@ -14,7 +14,7 @@ import (
"github.com/ihexxa/gocfg"
"github.com/ihexxa/quickshare/src/client"
"github.com/ihexxa/quickshare/src/db/userstore"
"github.com/ihexxa/quickshare/src/db"
fspkg "github.com/ihexxa/quickshare/src/fs"
)
@ -62,7 +62,7 @@ func addUsers(t *testing.T, addr, userPwd string, userCount int, adminToken *htt
for i := range make([]int, userCount) {
userName := getUserName(i)
resp, adResp, errs := usersCl.AddUser(userName, userPwd, userstore.UserRole, adminToken)
resp, adResp, errs := usersCl.AddUser(userName, userPwd, db.UserRole, adminToken)
if len(errs) > 0 {
t.Fatal(errs)
} else if resp.StatusCode != 200 {

Binary file not shown.