fix(db): aggregate store pkgs under db dir
This commit is contained in:
parent
8e7dd50efd
commit
4d8a8999a5
15 changed files with 13 additions and 13 deletions
421
src/db/userstore/user_store.go
Normal file
421
src/db/userstore/user_store.go
Normal file
|
@ -0,0 +1,421 @@
|
|||
package userstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ihexxa/quickshare/src/kvstore"
|
||||
)
|
||||
|
||||
const (
|
||||
AdminRole = "admin"
|
||||
UserRole = "user"
|
||||
VisitorRole = "visitor"
|
||||
InitNs = "usersInit"
|
||||
IDsNs = "ids"
|
||||
UsersNs = "users"
|
||||
RoleListNs = "roleList"
|
||||
InitTimeKey = "initTime"
|
||||
VisitorID = uint64(1)
|
||||
VisitorName = "visitor"
|
||||
|
||||
defaultSpaceLimit = 1024 * 1024 * 1024 // 1GB
|
||||
defaultUploadSpeedLimit = 50 * 1024 * 1024 // 50MB
|
||||
defaultDownloadSpeedLimit = 50 * 1024 * 1024 // 50MB
|
||||
visitorUploadSpeedLimit = 10 * 1024 * 1024 // 10MB
|
||||
visitorDownloadSpeedLimit = 10 * 1024 * 1024 // 10MB
|
||||
)
|
||||
|
||||
var (
|
||||
ErrReachedLimit = errors.New("reached space limit")
|
||||
)
|
||||
|
||||
func IsReachedLimitErr(err error) bool {
|
||||
return err == ErrReachedLimit
|
||||
}
|
||||
|
||||
type Quota struct {
|
||||
SpaceLimit int64 `json:"spaceLimit,string"`
|
||||
UploadSpeedLimit int `json:"uploadSpeedLimit"`
|
||||
DownloadSpeedLimit int `json:"downloadSpeedLimit"`
|
||||
}
|
||||
|
||||
type UserCfg struct {
|
||||
Name string `json:"name"`
|
||||
Role string `json:"role"`
|
||||
Pwd string `json:"pwd"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID uint64 `json:"id,string"`
|
||||
Name string `json:"name"`
|
||||
Pwd string `json:"pwd"`
|
||||
Role string `json:"role"`
|
||||
UsedSpace int64 `json:"usedSpace,string"`
|
||||
Quota *Quota `json:"quota"`
|
||||
}
|
||||
|
||||
type IUserStore interface {
|
||||
Init(rootName, rootPwd string) error
|
||||
IsInited() bool
|
||||
AddUser(user *User) error
|
||||
DelUser(id uint64) error
|
||||
GetUser(id uint64) (*User, error)
|
||||
GetUserByName(name string) (*User, error)
|
||||
SetInfo(id uint64, user *User) error
|
||||
CanIncrUsed(id uint64, capacity int64) (bool, error)
|
||||
SetUsed(id uint64, incr bool, capacity int64) error
|
||||
SetPwd(id uint64, pwd string) error
|
||||
ListUsers() ([]*User, error)
|
||||
AddRole(role string) error
|
||||
DelRole(role string) error
|
||||
ListRoles() (map[string]bool, error)
|
||||
}
|
||||
|
||||
type KVUserStore struct {
|
||||
store kvstore.IKVStore
|
||||
mtx *sync.RWMutex
|
||||
}
|
||||
|
||||
func NewKVUserStore(store kvstore.IKVStore) (*KVUserStore, error) {
|
||||
_, ok := store.GetStringIn(InitNs, InitTimeKey)
|
||||
if !ok {
|
||||
var err error
|
||||
for _, nsName := range []string{
|
||||
IDsNs,
|
||||
UsersNs,
|
||||
InitNs,
|
||||
RoleListNs,
|
||||
} {
|
||||
if err = store.AddNamespace(nsName); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &KVUserStore{
|
||||
store: store,
|
||||
mtx: &sync.RWMutex{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (us *KVUserStore) Init(rootName, rootPwd string) error {
|
||||
var err error
|
||||
admin := &User{
|
||||
ID: 0,
|
||||
Name: rootName,
|
||||
Pwd: rootPwd,
|
||||
Role: AdminRole,
|
||||
Quota: &Quota{
|
||||
SpaceLimit: defaultSpaceLimit,
|
||||
UploadSpeedLimit: defaultUploadSpeedLimit,
|
||||
DownloadSpeedLimit: defaultDownloadSpeedLimit,
|
||||
},
|
||||
}
|
||||
visitor := &User{
|
||||
ID: VisitorID,
|
||||
Name: VisitorName,
|
||||
Pwd: rootPwd,
|
||||
Role: VisitorRole,
|
||||
Quota: &Quota{
|
||||
SpaceLimit: 0,
|
||||
UploadSpeedLimit: visitorUploadSpeedLimit,
|
||||
DownloadSpeedLimit: visitorDownloadSpeedLimit,
|
||||
},
|
||||
}
|
||||
|
||||
for _, user := range []*User{admin, visitor} {
|
||||
err = us.AddUser(user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, role := range []string{AdminRole, UserRole, VisitorRole} {
|
||||
err = us.AddRole(role)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return us.store.SetStringIn(InitNs, InitTimeKey, fmt.Sprintf("%d", time.Now().Unix()))
|
||||
}
|
||||
|
||||
func (us *KVUserStore) IsInited() bool {
|
||||
_, ok := us.store.GetStringIn(InitNs, InitTimeKey)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (us *KVUserStore) AddUser(user *User) error {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
|
||||
userID := fmt.Sprint(user.ID)
|
||||
_, ok := us.store.GetStringIn(UsersNs, userID)
|
||||
if ok {
|
||||
return fmt.Errorf("userID (%d) exists", user.ID)
|
||||
}
|
||||
if user.Name == "" || user.Pwd == "" {
|
||||
return errors.New("user name or password can not be empty")
|
||||
}
|
||||
_, ok = us.store.GetStringIn(IDsNs, user.Name)
|
||||
if ok {
|
||||
return fmt.Errorf("user name (%s) exists", user.Name)
|
||||
}
|
||||
|
||||
err := us.store.SetStringIn(IDsNs, user.Name, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
userBytes, err := json.Marshal(user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return us.store.SetStringIn(UsersNs, userID, string(userBytes))
|
||||
}
|
||||
|
||||
func (us *KVUserStore) DelUser(id uint64) error {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
|
||||
userID := fmt.Sprint(id)
|
||||
name, ok := us.store.GetStringIn(UsersNs, userID)
|
||||
if !ok {
|
||||
return fmt.Errorf("userID (%s) does not exist", userID)
|
||||
}
|
||||
|
||||
// TODO: add complement operations if part of the actions fails
|
||||
err1 := us.store.DelStringIn(IDsNs, name)
|
||||
err2 := us.store.DelStringIn(UsersNs, userID)
|
||||
if err1 != nil || err2 != nil {
|
||||
return fmt.Errorf("get id(%s) user(%s)", err1, err2)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (us *KVUserStore) GetUser(id uint64) (*User, error) {
|
||||
us.mtx.RLock()
|
||||
defer us.mtx.RUnlock()
|
||||
|
||||
userID := fmt.Sprint(id)
|
||||
|
||||
infoStr, ok := us.store.GetStringIn(UsersNs, userID)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("user (%s) not found", userID)
|
||||
}
|
||||
user := &User{}
|
||||
err := json.Unmarshal([]byte(infoStr), user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
gotID, ok := us.store.GetStringIn(IDsNs, user.Name)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("user id (%s) not found", user.Name)
|
||||
} else if gotID != userID {
|
||||
return nil, fmt.Errorf("user id (%s) not match: got(%s) expected(%s)", user.Name, gotID, userID)
|
||||
}
|
||||
|
||||
// TODO: use sync.Pool instead
|
||||
return user, nil
|
||||
|
||||
}
|
||||
|
||||
func (us *KVUserStore) GetUserByName(name string) (*User, error) {
|
||||
us.mtx.RLock()
|
||||
defer us.mtx.RUnlock()
|
||||
|
||||
userID, ok := us.store.GetStringIn(IDsNs, name)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("user id (%s) not found", name)
|
||||
}
|
||||
infoStr, ok := us.store.GetStringIn(UsersNs, userID)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("user name (%s) not found", userID)
|
||||
}
|
||||
|
||||
user := &User{}
|
||||
err := json.Unmarshal([]byte(infoStr), user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if user.Name != name {
|
||||
return nil, fmt.Errorf("user id (%s) not match: got(%s) expected(%s)", userID, user.Name, name)
|
||||
}
|
||||
|
||||
// TODO: use sync.Pool instead
|
||||
return user, nil
|
||||
|
||||
}
|
||||
|
||||
func (us *KVUserStore) SetPwd(id uint64, pwd string) error {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
|
||||
userID := fmt.Sprint(id)
|
||||
infoStr, ok := us.store.GetStringIn(UsersNs, userID)
|
||||
if !ok {
|
||||
return fmt.Errorf("user (%d) does not exist", id)
|
||||
}
|
||||
gotUser := &User{}
|
||||
err := json.Unmarshal([]byte(infoStr), gotUser)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if gotUser.ID != id {
|
||||
return fmt.Errorf("user id key(%d) info(%d) does match", id, gotUser.ID)
|
||||
}
|
||||
|
||||
gotUser.Pwd = pwd
|
||||
infoBytes, err := json.Marshal(gotUser)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return us.store.SetStringIn(UsersNs, userID, string(infoBytes))
|
||||
}
|
||||
|
||||
func (us *KVUserStore) CanIncrUsed(id uint64, capacity int64) (bool, error) {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
userID := fmt.Sprint(id)
|
||||
infoStr, ok := us.store.GetStringIn(UsersNs, userID)
|
||||
if !ok {
|
||||
return false, fmt.Errorf("user (%d) does not exist", id)
|
||||
}
|
||||
|
||||
gotUser := &User{}
|
||||
err := json.Unmarshal([]byte(infoStr), gotUser)
|
||||
if err != nil {
|
||||
return false, err
|
||||
} else if gotUser.ID != id {
|
||||
return false, fmt.Errorf("user id key(%d) info(%d) does match", id, gotUser.ID)
|
||||
}
|
||||
|
||||
return gotUser.UsedSpace+capacity <= int64(gotUser.Quota.SpaceLimit), nil
|
||||
}
|
||||
|
||||
func (us *KVUserStore) SetUsed(id uint64, incr bool, capacity int64) error {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
|
||||
userID := fmt.Sprint(id)
|
||||
infoStr, ok := us.store.GetStringIn(UsersNs, userID)
|
||||
if !ok {
|
||||
return fmt.Errorf("user (%d) does not exist", id)
|
||||
}
|
||||
|
||||
gotUser := &User{}
|
||||
err := json.Unmarshal([]byte(infoStr), gotUser)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if gotUser.ID != id {
|
||||
return fmt.Errorf("user id key(%d) info(%d) does match", id, gotUser.ID)
|
||||
}
|
||||
|
||||
if incr && gotUser.UsedSpace+capacity > int64(gotUser.Quota.SpaceLimit) {
|
||||
return ErrReachedLimit
|
||||
}
|
||||
|
||||
if incr {
|
||||
gotUser.UsedSpace = gotUser.UsedSpace + capacity
|
||||
} else {
|
||||
gotUser.UsedSpace = gotUser.UsedSpace - capacity
|
||||
}
|
||||
infoBytes, err := json.Marshal(gotUser)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return us.store.SetStringIn(UsersNs, userID, string(infoBytes))
|
||||
}
|
||||
|
||||
func (us *KVUserStore) SetInfo(id uint64, user *User) error {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
|
||||
userID := fmt.Sprint(id)
|
||||
infoStr, ok := us.store.GetStringIn(UsersNs, userID)
|
||||
if !ok {
|
||||
return fmt.Errorf("user (%d) does not exist", id)
|
||||
}
|
||||
gotUser := &User{}
|
||||
err := json.Unmarshal([]byte(infoStr), gotUser)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if gotUser.ID != id {
|
||||
return fmt.Errorf("user id key(%d) info(%d) does match", id, gotUser.ID)
|
||||
}
|
||||
|
||||
// name and password can not be updated here
|
||||
if user.Role != "" {
|
||||
gotUser.Role = user.Role
|
||||
}
|
||||
if user.Quota != nil {
|
||||
gotUser.Quota = user.Quota
|
||||
}
|
||||
if user.UsedSpace > 0 {
|
||||
gotUser.UsedSpace = user.UsedSpace
|
||||
}
|
||||
|
||||
infoBytes, err := json.Marshal(gotUser)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return us.store.SetStringIn(UsersNs, userID, string(infoBytes))
|
||||
}
|
||||
|
||||
func (us *KVUserStore) ListUsers() ([]*User, error) {
|
||||
us.mtx.RLock()
|
||||
defer us.mtx.RUnlock()
|
||||
|
||||
idToInfo, err := us.store.ListStringsIn(UsersNs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
users := []*User{}
|
||||
for _, infoStr := range idToInfo {
|
||||
user := &User{}
|
||||
err = json.Unmarshal([]byte(infoStr), user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
user.Pwd = ""
|
||||
|
||||
users = append(users, user)
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (us *KVUserStore) AddRole(role string) error {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
|
||||
_, ok := us.store.GetBoolIn(RoleListNs, role)
|
||||
if ok {
|
||||
return fmt.Errorf("role (%s) exists", role)
|
||||
}
|
||||
|
||||
return us.store.SetBoolIn(RoleListNs, role, true)
|
||||
}
|
||||
|
||||
func (us *KVUserStore) DelRole(role string) error {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
|
||||
if role == AdminRole || role == UserRole || role == VisitorRole {
|
||||
return errors.New("predefined roles can not be deleted")
|
||||
}
|
||||
|
||||
return us.store.DelBoolIn(RoleListNs, role)
|
||||
}
|
||||
|
||||
func (us *KVUserStore) ListRoles() (map[string]bool, error) {
|
||||
us.mtx.Lock()
|
||||
defer us.mtx.Unlock()
|
||||
|
||||
return us.store.ListBoolsIn(RoleListNs)
|
||||
}
|
286
src/db/userstore/user_store_test.go
Normal file
286
src/db/userstore/user_store_test.go
Normal file
|
@ -0,0 +1,286 @@
|
|||
package userstore
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/ihexxa/quickshare/src/kvstore/boltdbpvd"
|
||||
)
|
||||
|
||||
func TestUserStores(t *testing.T) {
|
||||
rootName, rootPwd := "root", "rootPwd"
|
||||
|
||||
testUserMethods := func(t *testing.T, store IUserStore) {
|
||||
root, err := store.GetUser(0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if root.Name != rootName {
|
||||
t.Fatal("root user not found")
|
||||
}
|
||||
if root.Pwd != rootPwd {
|
||||
t.Fatalf("passwords not match %s", err)
|
||||
}
|
||||
if root.Role != AdminRole {
|
||||
t.Fatalf("incorrect root role")
|
||||
}
|
||||
if root.Quota.SpaceLimit != defaultSpaceLimit {
|
||||
t.Fatalf("incorrect root SpaceLimit")
|
||||
}
|
||||
if root.Quota.UploadSpeedLimit != defaultUploadSpeedLimit {
|
||||
t.Fatalf("incorrect root UploadSpeedLimit")
|
||||
}
|
||||
if root.Quota.DownloadSpeedLimit != defaultDownloadSpeedLimit {
|
||||
t.Fatalf("incorrect root DownloadSpeedLimit")
|
||||
}
|
||||
|
||||
visitor, err := store.GetUser(1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if visitor.Name != VisitorName {
|
||||
t.Fatal("visitor not found")
|
||||
}
|
||||
if visitor.Pwd != rootPwd {
|
||||
t.Fatalf("passwords not match %s", err)
|
||||
}
|
||||
if visitor.Role != VisitorRole {
|
||||
t.Fatalf("incorrect visitor role")
|
||||
}
|
||||
if visitor.Quota.SpaceLimit != 0 {
|
||||
t.Fatalf("incorrect visitor SpaceLimit")
|
||||
}
|
||||
if visitor.Quota.UploadSpeedLimit != visitorUploadSpeedLimit {
|
||||
t.Fatalf("incorrect visitor UploadSpeedLimit")
|
||||
}
|
||||
if visitor.Quota.DownloadSpeedLimit != visitorDownloadSpeedLimit {
|
||||
t.Fatalf("incorrect visitor DownloadSpeedLimit")
|
||||
}
|
||||
|
||||
id, name1 := uint64(2), "test_user1"
|
||||
pwd1, pwd2 := "666", "888"
|
||||
role1, role2 := UserRole, AdminRole
|
||||
spaceLimit1, upLimit1, downLimit1 := int64(17), 5, 7
|
||||
spaceLimit2, upLimit2, downLimit2 := int64(19), 13, 17
|
||||
|
||||
err = store.AddUser(&User{
|
||||
ID: id,
|
||||
Name: name1,
|
||||
Pwd: pwd1,
|
||||
Role: role1,
|
||||
Quota: &Quota{
|
||||
SpaceLimit: spaceLimit1,
|
||||
UploadSpeedLimit: upLimit1,
|
||||
DownloadSpeedLimit: downLimit1,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal("there should be no error")
|
||||
}
|
||||
|
||||
user, err := store.GetUser(id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if user.Name != name1 {
|
||||
t.Fatalf("names not matched %s %s", name1, user.Name)
|
||||
}
|
||||
if user.Pwd != pwd1 {
|
||||
t.Fatalf("passwords not match %s", err)
|
||||
}
|
||||
if user.Role != role1 {
|
||||
t.Fatalf("roles not matched %s %s", role1, user.Role)
|
||||
}
|
||||
if user.Quota.SpaceLimit != spaceLimit1 {
|
||||
t.Fatalf("space limit not matched %d %d", spaceLimit1, user.Quota.SpaceLimit)
|
||||
}
|
||||
if user.Quota.UploadSpeedLimit != upLimit1 {
|
||||
t.Fatalf("up limit not matched %d %d", upLimit1, user.Quota.UploadSpeedLimit)
|
||||
}
|
||||
if user.Quota.DownloadSpeedLimit != downLimit1 {
|
||||
t.Fatalf("down limit not matched %d %d", downLimit1, user.Quota.DownloadSpeedLimit)
|
||||
}
|
||||
|
||||
users, err := store.ListUsers()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(users) != 3 {
|
||||
t.Fatalf("users size should be 3 (%d)", len(users))
|
||||
}
|
||||
for _, user := range users {
|
||||
if user.ID == 0 {
|
||||
if user.Name != rootName || user.Role != AdminRole {
|
||||
t.Fatalf("incorrect root info %v", user)
|
||||
}
|
||||
}
|
||||
if user.ID == id {
|
||||
if user.Name != name1 || user.Role != role1 {
|
||||
t.Fatalf("incorrect user info %v", user)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err = store.SetPwd(id, pwd2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store.SetInfo(id, &User{
|
||||
ID: id,
|
||||
Role: role2,
|
||||
Quota: &Quota{
|
||||
SpaceLimit: spaceLimit2,
|
||||
UploadSpeedLimit: upLimit2,
|
||||
DownloadSpeedLimit: downLimit2,
|
||||
},
|
||||
})
|
||||
|
||||
usedIncr, usedDecr := int64(spaceLimit2), int64(7)
|
||||
err = store.SetUsed(id, true, usedIncr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = store.SetUsed(id, false, usedDecr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = store.SetUsed(id, true, int64(spaceLimit2)-(usedIncr-usedDecr)+1)
|
||||
if err == nil || !strings.Contains(err.Error(), "reached space limit") {
|
||||
t.Fatal("should reject big file")
|
||||
} else {
|
||||
err = nil
|
||||
}
|
||||
|
||||
user, err = store.GetUser(id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if user.Pwd != pwd2 {
|
||||
t.Fatalf("passwords not match %s", err)
|
||||
}
|
||||
if user.Role != role2 {
|
||||
t.Fatalf("roles not matched %s %s", role2, user.Role)
|
||||
}
|
||||
if user.Quota.SpaceLimit != spaceLimit2 {
|
||||
t.Fatalf("space limit not matched %d %d", spaceLimit2, user.Quota.SpaceLimit)
|
||||
}
|
||||
if user.Quota.UploadSpeedLimit != upLimit2 {
|
||||
t.Fatalf("up limit not matched %d %d", upLimit2, user.Quota.UploadSpeedLimit)
|
||||
}
|
||||
if user.Quota.DownloadSpeedLimit != downLimit2 {
|
||||
t.Fatalf("down limit not matched %d %d", downLimit2, user.Quota.DownloadSpeedLimit)
|
||||
}
|
||||
if user.UsedSpace != usedIncr-usedDecr {
|
||||
t.Fatalf("used space not matched %d %d", user.UsedSpace, usedIncr-usedDecr)
|
||||
}
|
||||
|
||||
user, err = store.GetUserByName(name1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if user.ID != id {
|
||||
t.Fatalf("ids not matched %d %d", id, user.ID)
|
||||
}
|
||||
if user.Pwd != pwd2 {
|
||||
t.Fatalf("passwords not match %s", err)
|
||||
}
|
||||
if user.Role != role2 {
|
||||
t.Fatalf("roles not matched %s %s", role2, user.Role)
|
||||
}
|
||||
if user.Quota.SpaceLimit != spaceLimit2 {
|
||||
t.Fatalf("space limit not matched %d %d", spaceLimit2, user.Quota.SpaceLimit)
|
||||
}
|
||||
if user.Quota.UploadSpeedLimit != upLimit2 {
|
||||
t.Fatalf("up limit not matched %d %d", upLimit2, user.Quota.UploadSpeedLimit)
|
||||
}
|
||||
if user.Quota.DownloadSpeedLimit != downLimit2 {
|
||||
t.Fatalf("down limit not matched %d %d", downLimit2, user.Quota.DownloadSpeedLimit)
|
||||
}
|
||||
|
||||
err = store.DelUser(id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
users, err = store.ListUsers()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(users) != 2 {
|
||||
t.Fatalf("users size should be 2 (%d)", len(users))
|
||||
}
|
||||
for _, user := range users {
|
||||
if user.ID == 0 && user.Name != rootName && user.Role != AdminRole {
|
||||
t.Fatalf("incorrect root info %v", user)
|
||||
}
|
||||
if user.ID == VisitorID && user.Name != VisitorName && user.Role != VisitorRole {
|
||||
t.Fatalf("incorrect visitor info %v", user)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
testRoleMethods := func(t *testing.T, store IUserStore) {
|
||||
roles := []string{"role1", "role2"}
|
||||
var err error
|
||||
for _, role := range roles {
|
||||
err = store.AddRole(role)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
roleMap, err := store.ListRoles()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, role := range append(roles, []string{
|
||||
AdminRole, UserRole, VisitorRole,
|
||||
}...) {
|
||||
if !roleMap[role] {
|
||||
t.Fatalf("role(%s) not found", role)
|
||||
}
|
||||
}
|
||||
|
||||
for _, role := range roles {
|
||||
err = store.DelRole(role)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
roleMap, err = store.ListRoles()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, role := range roles {
|
||||
if roleMap[role] {
|
||||
t.Fatalf("role(%s) should not exist", role)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("testing KVUserStore", func(t *testing.T) {
|
||||
rootPath, err := ioutil.TempDir("./", "quickshare_userstore_test_")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(rootPath)
|
||||
|
||||
kvstore := boltdbpvd.New(rootPath, 1024)
|
||||
defer kvstore.Close()
|
||||
|
||||
store, err := NewKVUserStore(kvstore)
|
||||
if err != nil {
|
||||
t.Fatal("fail to new kvstore", err)
|
||||
}
|
||||
if err = store.Init(rootName, rootPwd); err != nil {
|
||||
t.Fatal("fail to init kvstore", err)
|
||||
}
|
||||
|
||||
testUserMethods(t, store)
|
||||
testRoleMethods(t, store)
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue